Files
carbon-lang/toolchain/check/testdata/struct/reorder_fields.carbon
T
Richard Smith 05cc09daca Fix cross-package signature mismatches. (#7232)
Fixes link failures when referencing a symbol involving a fingerprint
from a different package.

Previously we included the `Namespace`'s `import_id` as part of its
fingerprint, which caused local and imported namespaces to get different
fingerprints. We now store the `import_id` on the `NameScope` instead of
on the `Namespace` inst to avoid this problem.

Also, when we reach a package-level `NameScopeId`, consistently
fingerprint it as a (package name, library name) pair. Previously the
fingerprinting depended on whether it was imported or not, as an
imported `NameScopeId` had a parent scope (the current package). We need
to include the library name here so that private entities with the same
name in different libraries have different fingerprints.
2026-05-21 00:02:29 +00:00

190 lines
14 KiB
Plaintext

// 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/primitives.carbon
// TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
// EXTRA-ARGS: --dump-sem-ir-ranges=if-present
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/struct/reorder_fields.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/struct/reorder_fields.carbon
fn MakeI32() -> i32;
fn MakeF64() -> f64;
fn F() -> {.a: i32, .b: f64} {
let x: {.a: i32, .b: f64} = {.b = MakeF64(), .a = MakeI32()};
let y: {.b: f64, .a: i32} = x;
return y;
}
// CHECK:STDOUT: --- reorder_fields.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
// CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
// CHECK:STDOUT: %N: Core.IntLiteral = symbolic_binding N, 0 [symbolic]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %.70c: Core.Form = init_form %i32 [concrete]
// CHECK:STDOUT: %pattern_type.c6b: type = pattern_type %i32 [concrete]
// CHECK:STDOUT: %MakeI32.type: type = fn_type @MakeI32 [concrete]
// CHECK:STDOUT: %MakeI32: %MakeI32.type = struct_value () [concrete]
// CHECK:STDOUT: %int_64: Core.IntLiteral = int_value 64 [concrete]
// CHECK:STDOUT: %Float.type: type = generic_class_type @Float [concrete]
// CHECK:STDOUT: %Float.generic: %Float.type = struct_value () [concrete]
// CHECK:STDOUT: %f64.9a0: type = class_type @Float, @Float(%int_64) [concrete]
// CHECK:STDOUT: %.48c: Core.Form = init_form %f64.9a0 [concrete]
// CHECK:STDOUT: %pattern_type.f4f: type = pattern_type %f64.9a0 [concrete]
// CHECK:STDOUT: %MakeF64.type: type = fn_type @MakeF64 [concrete]
// CHECK:STDOUT: %MakeF64: %MakeF64.type = struct_value () [concrete]
// CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %f64.9a0} [concrete]
// CHECK:STDOUT: %.fb5: Core.Form = init_form %struct_type.a.b [concrete]
// CHECK:STDOUT: %pattern_type.e70: type = pattern_type %struct_type.a.b [concrete]
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
// CHECK:STDOUT: %struct_type.b.a: type = struct_type {.b: %f64.9a0, .a: %i32} [concrete]
// CHECK:STDOUT: %pattern_type.b37: type = pattern_type %struct_type.b.a [concrete]
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
// CHECK:STDOUT: %Int.as.Copy.impl.Op.type.b5d: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%N) [symbolic]
// CHECK:STDOUT: %Int.as.Copy.impl.Op.c85: %Int.as.Copy.impl.Op.type.b5d = struct_value () [symbolic]
// CHECK:STDOUT: %Copy.impl_witness.32d: <witness> = impl_witness imports.%Copy.impl_witness_table.07a, @Int.as.Copy.impl(%int_32) [concrete]
// CHECK:STDOUT: %Int.as.Copy.impl.Op.type.4a0: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%int_32) [concrete]
// CHECK:STDOUT: %Int.as.Copy.impl.Op.c4a: %Int.as.Copy.impl.Op.type.4a0 = struct_value () [concrete]
// CHECK:STDOUT: %Copy.facet.66a: %Copy.type = facet_value %i32, (%Copy.impl_witness.32d) [concrete]
// CHECK:STDOUT: %Copy.WithSelf.Op.type.afe: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet.66a) [concrete]
// CHECK:STDOUT: %.a5f: type = fn_type_with_self_type %Copy.WithSelf.Op.type.afe, %Copy.facet.66a [concrete]
// CHECK:STDOUT: %Int.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %Int.as.Copy.impl.Op.c4a, @Int.as.Copy.impl.Op(%int_32) [concrete]
// CHECK:STDOUT: %Float.as.Copy.impl.Op.type.44e: type = fn_type @Float.as.Copy.impl.Op, @Float.as.Copy.impl(%N) [symbolic]
// CHECK:STDOUT: %Float.as.Copy.impl.Op.30e: %Float.as.Copy.impl.Op.type.44e = struct_value () [symbolic]
// CHECK:STDOUT: %Copy.impl_witness.0b5: <witness> = impl_witness imports.%Copy.impl_witness_table.637, @Float.as.Copy.impl(%int_64) [concrete]
// CHECK:STDOUT: %Float.as.Copy.impl.Op.type.d57: type = fn_type @Float.as.Copy.impl.Op, @Float.as.Copy.impl(%int_64) [concrete]
// CHECK:STDOUT: %Float.as.Copy.impl.Op.447: %Float.as.Copy.impl.Op.type.d57 = struct_value () [concrete]
// CHECK:STDOUT: %Copy.facet.8dd: %Copy.type = facet_value %f64.9a0, (%Copy.impl_witness.0b5) [concrete]
// CHECK:STDOUT: %Copy.WithSelf.Op.type.363: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet.8dd) [concrete]
// CHECK:STDOUT: %.a39: type = fn_type_with_self_type %Copy.WithSelf.Op.type.363, %Copy.facet.8dd [concrete]
// CHECK:STDOUT: %Float.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %Float.as.Copy.impl.Op.447, @Float.as.Copy.impl.Op(%int_64) [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: .Float = %Core.Float
// CHECK:STDOUT: .Copy = %Core.Copy
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: %Core.Float: %Float.type = import_ref Core//prelude/parts/float, Float, loaded [concrete = constants.%Float.generic]
// CHECK:STDOUT: %Core.Copy: type = import_ref Core//prelude/parts/copy, Copy, loaded [concrete = constants.%Copy.type]
// CHECK:STDOUT: %Core.import_ref.809: @Int.as.Copy.impl.%Int.as.Copy.impl.Op.type (%Int.as.Copy.impl.Op.type.b5d) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Int.as.Copy.impl.%Int.as.Copy.impl.Op (constants.%Int.as.Copy.impl.Op.c85)]
// CHECK:STDOUT: %Copy.impl_witness_table.07a = impl_witness_table (%Core.import_ref.809), @Int.as.Copy.impl [concrete]
// CHECK:STDOUT: %Core.import_ref.729: @Float.as.Copy.impl.%Float.as.Copy.impl.Op.type (%Float.as.Copy.impl.Op.type.44e) = import_ref Core//prelude/parts/float, loc{{\d+_\d+}}, loaded [symbolic = @Float.as.Copy.impl.%Float.as.Copy.impl.Op (constants.%Float.as.Copy.impl.Op.30e)]
// CHECK:STDOUT: %Copy.impl_witness_table.637 = impl_witness_table (%Core.import_ref.729), @Float.as.Copy.impl [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .MakeI32 = %MakeI32.decl
// CHECK:STDOUT: .MakeF64 = %MakeF64.decl
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %MakeI32.decl: %MakeI32.type = fn_decl @MakeI32 [concrete = constants.%MakeI32] {
// CHECK:STDOUT: %return.param_patt: %pattern_type.c6b = out_param_pattern [concrete]
// CHECK:STDOUT: %return.patt: %pattern_type.c6b = return_slot_pattern %return.param_patt, %i32 [concrete]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %i32: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: %.loc15: Core.Form = init_form %i32 [concrete = constants.%.70c]
// CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
// CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %MakeF64.decl: %MakeF64.type = fn_decl @MakeF64 [concrete = constants.%MakeF64] {
// CHECK:STDOUT: %return.param_patt: %pattern_type.f4f = out_param_pattern [concrete]
// CHECK:STDOUT: %return.patt: %pattern_type.f4f = return_slot_pattern %return.param_patt, %f64 [concrete]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %f64: type = type_literal constants.%f64.9a0 [concrete = constants.%f64.9a0]
// CHECK:STDOUT: %.loc16: Core.Form = init_form %f64 [concrete = constants.%.48c]
// CHECK:STDOUT: %return.param: ref %f64.9a0 = out_param call_param0
// CHECK:STDOUT: %return: ref %f64.9a0 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
// CHECK:STDOUT: %return.param_patt: %pattern_type.e70 = out_param_pattern [concrete]
// CHECK:STDOUT: %return.patt: %pattern_type.e70 = return_slot_pattern %return.param_patt, %struct_type.a.b.loc18 [concrete]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %i32.loc18: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: %f64.loc18: type = type_literal constants.%f64.9a0 [concrete = constants.%f64.9a0]
// CHECK:STDOUT: %struct_type.a.b.loc18: type = struct_type {.a: %i32, .b: %f64.9a0} [concrete = constants.%struct_type.a.b]
// CHECK:STDOUT: %.loc18: Core.Form = init_form %struct_type.a.b.loc18 [concrete = constants.%.fb5]
// CHECK:STDOUT: %return.param: ref %struct_type.a.b = out_param call_param0
// CHECK:STDOUT: %return: ref %struct_type.a.b = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @MakeI32() -> out %return.param: %i32;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @MakeF64() -> out %return.param: %f64.9a0;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F() -> out %return.param: %struct_type.a.b {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %x.patt: %pattern_type.e70 = value_binding_pattern x [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %MakeF64.ref: %MakeF64.type = name_ref MakeF64, file.%MakeF64.decl [concrete = constants.%MakeF64]
// CHECK:STDOUT: %MakeF64.call: init %f64.9a0 = call %MakeF64.ref()
// CHECK:STDOUT: %MakeI32.ref: %MakeI32.type = name_ref MakeI32, file.%MakeI32.decl [concrete = constants.%MakeI32]
// CHECK:STDOUT: %MakeI32.call: init %i32 = call %MakeI32.ref()
// CHECK:STDOUT: %.loc19_62.1: %struct_type.b.a = struct_literal (%MakeF64.call, %MakeI32.call)
// CHECK:STDOUT: %.loc19_27: type = splice_block %struct_type.a.b.loc19 [concrete = constants.%struct_type.a.b] {
// CHECK:STDOUT: %i32.loc19: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: %f64.loc19: type = type_literal constants.%f64.9a0 [concrete = constants.%f64.9a0]
// CHECK:STDOUT: %struct_type.a.b.loc19: type = struct_type {.a: %i32, .b: %f64.9a0} [concrete = constants.%struct_type.a.b]
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc19_62.2: %i32 = value_of_initializer %MakeI32.call
// CHECK:STDOUT: %.loc19_62.3: %i32 = converted %MakeI32.call, %.loc19_62.2
// CHECK:STDOUT: %.loc19_62.4: %f64.9a0 = value_of_initializer %MakeF64.call
// CHECK:STDOUT: %.loc19_62.5: %f64.9a0 = converted %MakeF64.call, %.loc19_62.4
// CHECK:STDOUT: %struct.loc19: %struct_type.a.b = struct_value (%.loc19_62.3, %.loc19_62.5)
// CHECK:STDOUT: %.loc19_62.6: %struct_type.a.b = converted %.loc19_62.1, %struct.loc19
// CHECK:STDOUT: %x: %struct_type.a.b = value_binding x, %.loc19_62.6
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %y.patt: %pattern_type.b37 = value_binding_pattern y [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %x.ref: %struct_type.a.b = name_ref x, %x
// CHECK:STDOUT: %.loc20_27: type = splice_block %struct_type.b.a [concrete = constants.%struct_type.b.a] {
// CHECK:STDOUT: %f64.loc20: type = type_literal constants.%f64.9a0 [concrete = constants.%f64.9a0]
// CHECK:STDOUT: %i32.loc20: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: %struct_type.b.a: type = struct_type {.b: %f64.9a0, .a: %i32} [concrete = constants.%struct_type.b.a]
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc20_31.1: %f64.9a0 = struct_access %x.ref, element1
// CHECK:STDOUT: %.loc20_31.2: %i32 = struct_access %x.ref, element0
// CHECK:STDOUT: %struct.loc20: %struct_type.b.a = struct_value (%.loc20_31.1, %.loc20_31.2)
// CHECK:STDOUT: %.loc20_31.3: %struct_type.b.a = converted %x.ref, %struct.loc20
// CHECK:STDOUT: %y: %struct_type.b.a = value_binding y, %.loc20_31.3
// CHECK:STDOUT: %y.ref: %struct_type.b.a = name_ref y, %y
// CHECK:STDOUT: %.loc21_10.1: %i32 = struct_access %y.ref, element1
// CHECK:STDOUT: %impl.elem0.loc21_10.1: %.a5f = impl_witness_access constants.%Copy.impl_witness.32d, element0 [concrete = constants.%Int.as.Copy.impl.Op.c4a]
// CHECK:STDOUT: %bound_method.loc21_10.1: <bound method> = bound_method %.loc21_10.1, %impl.elem0.loc21_10.1
// CHECK:STDOUT: %specific_fn.loc21_10.1: <specific function> = specific_function %impl.elem0.loc21_10.1, @Int.as.Copy.impl.Op(constants.%int_32) [concrete = constants.%Int.as.Copy.impl.Op.specific_fn]
// CHECK:STDOUT: %bound_method.loc21_10.2: <bound method> = bound_method %.loc21_10.1, %specific_fn.loc21_10.1
// CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc21_10.2(%.loc21_10.1)
// CHECK:STDOUT: %.loc21_10.2: ref %i32 = struct_access %return.param, element1
// CHECK:STDOUT: %.loc21_10.3: init %i32 to %.loc21_10.2 = in_place_init %Int.as.Copy.impl.Op.call
// CHECK:STDOUT: %.loc21_10.4: %f64.9a0 = struct_access %y.ref, element0
// CHECK:STDOUT: %impl.elem0.loc21_10.2: %.a39 = impl_witness_access constants.%Copy.impl_witness.0b5, element0 [concrete = constants.%Float.as.Copy.impl.Op.447]
// CHECK:STDOUT: %bound_method.loc21_10.3: <bound method> = bound_method %.loc21_10.4, %impl.elem0.loc21_10.2
// CHECK:STDOUT: %specific_fn.loc21_10.2: <specific function> = specific_function %impl.elem0.loc21_10.2, @Float.as.Copy.impl.Op(constants.%int_64) [concrete = constants.%Float.as.Copy.impl.Op.specific_fn]
// CHECK:STDOUT: %bound_method.loc21_10.4: <bound method> = bound_method %.loc21_10.4, %specific_fn.loc21_10.2
// CHECK:STDOUT: %Float.as.Copy.impl.Op.call: init %f64.9a0 = call %bound_method.loc21_10.4(%.loc21_10.4)
// CHECK:STDOUT: %.loc21_10.5: ref %f64.9a0 = struct_access %return.param, element0
// CHECK:STDOUT: %.loc21_10.6: init %f64.9a0 to %.loc21_10.5 = in_place_init %Float.as.Copy.impl.Op.call
// CHECK:STDOUT: %.loc21_10.7: init %struct_type.a.b to %return.param = struct_init (%.loc21_10.3, %.loc21_10.6)
// CHECK:STDOUT: %.loc21_11: init %struct_type.a.b = converted %y.ref, %.loc21_10.7
// CHECK:STDOUT: return %.loc21_11 to %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: