From 0851d657c8b6d4e9ba2cd0454ac9b9649a9ad27d Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Thu, 2 Apr 2026 12:13:06 -0700 Subject: [PATCH] Add comment and test for special case in `NameRef` lowering (#7008) --- toolchain/lower/handle.cpp | 5 ++ toolchain/lower/testdata/let/global.carbon | 60 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 toolchain/lower/testdata/let/global.carbon diff --git a/toolchain/lower/handle.cpp b/toolchain/lower/handle.cpp index 421573f90e10..a2edb0c49f87 100644 --- a/toolchain/lower/handle.cpp +++ b/toolchain/lower/handle.cpp @@ -226,6 +226,11 @@ auto HandleInst(FunctionContext& context, SemIR::InstId inst_id, auto inner_inst_id = inst.value_id; + // `GetValue` will fail on package-scope value bindings because they aren't + // constants, and they aren't global variables, so as a workaround we + // peek through bindings here to directly access the bound value. + // TODO: Find a way of dealing with this that still works if the bound + // value isn't a global variable or constant either. if (auto bind_name = context.sem_ir().insts().TryGetAs(inner_inst_id)) { inner_inst_id = bind_name->value_id; diff --git a/toolchain/lower/testdata/let/global.carbon b/toolchain/lower/testdata/let/global.carbon new file mode 100644 index 000000000000..18e53daeb93d --- /dev/null +++ b/toolchain/lower/testdata/let/global.carbon @@ -0,0 +1,60 @@ +// 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 +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/let/global.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/let/global.carbon + +class C {} + +let c: C = {}; + +fn Sink(_: C); + +fn F() { + Sink(c); +} + +// CHECK:STDOUT: ; ModuleID = 'global.carbon' +// CHECK:STDOUT: source_filename = "global.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: @C.val = internal constant {} zeroinitializer +// CHECK:STDOUT: @C.val.loc15_13.5 = internal constant {} zeroinitializer +// CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }] +// CHECK:STDOUT: +// CHECK:STDOUT: declare void @_CSink.Main(ptr) +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define void @_CF.Main() #0 !dbg !4 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: call void @_CSink.Main(ptr @C.val.loc15_13.5), !dbg !7 +// CHECK:STDOUT: ret void, !dbg !8 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define internal void @_C__global_init.Main() #0 !dbg !9 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: ret void, !dbg !10 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: attributes #0 = { nounwind } +// CHECK:STDOUT: +// CHECK:STDOUT: !llvm.module.flags = !{!0, !1} +// CHECK:STDOUT: !llvm.dbg.cu = !{!2} +// CHECK:STDOUT: +// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5} +// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3} +// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug) +// CHECK:STDOUT: !3 = !DIFile(filename: "global.carbon", directory: "") +// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main", scope: null, file: !3, line: 19, type: !5, spFlags: DISPFlagDefinition, unit: !2) +// CHECK:STDOUT: !5 = !DISubroutineType(types: !6) +// CHECK:STDOUT: !6 = !{null} +// CHECK:STDOUT: !7 = !DILocation(line: 20, column: 3, scope: !4) +// CHECK:STDOUT: !8 = !DILocation(line: 19, column: 1, scope: !4) +// CHECK:STDOUT: !9 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2) +// CHECK:STDOUT: !10 = !DILocation(line: 0, scope: !9)