@@ -892,19 +892,34 @@ extern "C" void LLVMRustWriteValueToString(LLVMValueRef Value, RustStringRef str
892892extern " C" bool
893893LLVMRustLinkInExternalBitcode (LLVMModuleRef dst, char *bc, size_t len) {
894894 Module *Dst = unwrap (dst);
895+
895896 std::unique_ptr<MemoryBuffer> buf = MemoryBuffer::getMemBufferCopy (StringRef (bc, len));
897+
898+ #if LLVM_VERSION_GE(4, 0)
899+ Expected<std::unique_ptr<Module>> SrcOrError =
900+ llvm::getLazyBitcodeModule (buf->getMemBufferRef (), Dst->getContext ());
901+ if (!SrcOrError) {
902+ LLVMRustSetLastError (toString (SrcOrError.takeError ()).c_str ());
903+ return false ;
904+ }
905+
906+ auto Src = std::move (*SrcOrError);
907+ #else
896908 ErrorOr<std::unique_ptr<Module>> Src =
897909 llvm::getLazyBitcodeModule (std::move (buf), Dst->getContext ());
898910 if (!Src) {
899911 LLVMRustSetLastError (Src.getError ().message ().c_str ());
900912 return false ;
901913 }
914+ #endif
902915
903916 std::string Err;
904917
905918 raw_string_ostream Stream (Err);
906919 DiagnosticPrinterRawOStream DP (Stream);
907- #if LLVM_VERSION_GE(3, 8)
920+ #if LLVM_VERSION_GE(4, 0)
921+ if (Linker::linkModules (*Dst, std::move (Src))) {
922+ #elif LLVM_VERSION_GE(3, 8)
908923 if (Linker::linkModules (*Dst, std::move (Src.get ()))) {
909924#else
910925 if (Linker::LinkModules (Dst, Src->get (), [&](const DiagnosticInfo &DI) { DI.print (DP); })) {
0 commit comments