ソースを参照

Fix lowering of a call to an imported function. (#3863)

Richard Smith 2 年 前
コミット
1792c8f522

+ 5 - 5
toolchain/lower/file_context.cpp

@@ -58,17 +58,17 @@ auto FileContext::Run() -> std::unique_ptr<llvm::Module> {
 }
 
 auto FileContext::GetGlobal(SemIR::InstId inst_id) -> llvm::Value* {
+  auto const_id = sem_ir().constant_values().Get(inst_id);
+  if (const_id.is_constant()) {
+    inst_id = const_id.inst_id();
+  }
+
   // All builtins are types, with the same empty lowered value.
   if (inst_id.is_builtin()) {
     return GetTypeAsValue();
   }
 
   auto target = sem_ir().insts().Get(inst_id);
-  while (auto alias = target.TryAs<SemIR::BindAlias>()) {
-    inst_id = alias->value_id;
-    target = sem_ir().insts().Get(inst_id);
-  }
-
   if (auto function_decl = target.TryAs<SemIR::FunctionDecl>()) {
     return GetFunction(function_decl->function_id);
   }

+ 33 - 0
toolchain/lower/testdata/packages/cross_package_call.carbon

@@ -0,0 +1,33 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+// --- a.carbon
+
+package A api;
+
+fn F() {}
+
+// --- b.carbon
+
+import A;
+
+fn G() { A.F(); }
+
+// CHECK:STDOUT: ; ModuleID = 'a.carbon'
+// CHECK:STDOUT: source_filename = "a.carbon"
+// CHECK:STDOUT:
+// CHECK:STDOUT: define void @F() {
+// CHECK:STDOUT:   ret void
+// CHECK:STDOUT: }
+// CHECK:STDOUT: ; ModuleID = 'b.carbon'
+// CHECK:STDOUT: source_filename = "b.carbon"
+// CHECK:STDOUT:
+// CHECK:STDOUT: define void @G() {
+// CHECK:STDOUT:   call void @F()
+// CHECK:STDOUT:   ret void
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: declare void @F()