From 30b8a93fde603bb7768172f4ae8d6b53bd2764b2 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 20 Aug 2025 16:27:03 -0700 Subject: [PATCH] Support conversion from T* to const T*. (#5971) Also support conversion from Derived* to const Base*. --------- Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com> --- toolchain/check/convert.cpp | 80 ++- .../testdata/class/derived_to_base.carbon | 486 +++++++++--------- toolchain/check/testdata/const/import.carbon | 32 +- .../function/arithmetic_types_bridged.carbon | 19 +- .../function/arithmetic_types_direct.carbon | 19 +- .../pointer/convert_qualifiers.carbon | 214 ++++++++ toolchain/check/type.cpp | 10 + toolchain/check/type.h | 4 + toolchain/sem_ir/type.cpp | 8 +- toolchain/sem_ir/type.h | 23 +- 10 files changed, 580 insertions(+), 315 deletions(-) create mode 100644 toolchain/check/testdata/pointer/convert_qualifiers.carbon diff --git a/toolchain/check/convert.cpp b/toolchain/check/convert.cpp index e37b4442e0bf..4cf17a657b29 100644 --- a/toolchain/check/convert.cpp +++ b/toolchain/check/convert.cpp @@ -673,13 +673,20 @@ static auto ConvertDerivedToBase(Context& context, SemIR::LocId loc_id, // Materialize a temporary if necessary. value_id = ConvertToValueOrRefExpr(context, value_id); + // Preserve type qualifiers. + auto quals = context.types() + .GetUnqualifiedTypeAndQualifiers( + context.insts().Get(value_id).type_id()) + .second; + // Add a series of `.base` accesses. for (auto [base_id, base_type_id] : path) { auto base_decl = context.insts().GetAs(base_id); - value_id = AddInst(context, loc_id, - {.type_id = base_type_id, - .base_id = value_id, - .index = base_decl.index}); + value_id = AddInst( + context, loc_id, + {.type_id = GetQualifiedType(context, base_type_id, quals), + .base_id = value_id, + .index = base_decl.index}); } return value_id; } @@ -689,13 +696,13 @@ static auto ConvertDerivedPointerToBasePointer( Context& context, SemIR::LocId loc_id, SemIR::PointerType src_ptr_type, SemIR::TypeId dest_ptr_type_id, SemIR::InstId ptr_id, const InheritancePath& path) -> SemIR::InstId { + auto pointee_type_id = + context.types().GetTypeIdForTypeInstId(src_ptr_type.pointee_id); + // Form `*p`. ptr_id = ConvertToValueExpr(context, ptr_id); auto ref_id = AddInst( - context, loc_id, - {.type_id = - context.types().GetTypeIdForTypeInstId(src_ptr_type.pointee_id), - .pointer_id = ptr_id}); + context, loc_id, {.type_id = pointee_type_id, .pointer_id = ptr_id}); // Convert as a reference expression. ref_id = ConvertDerivedToBase(context, loc_id, ref_id, path); @@ -950,6 +957,12 @@ static auto PerformBuiltinConversion( } } + // No other conversions apply when the source and destination types are the + // same. + if (value_type_id == target.type_id) { + return value_id; + } + // A tuple (T1, T2, ..., Tn) converts to array(T, n) if each Ti converts to T. if (auto target_array_type = target_type_inst.TryAs()) { if (auto src_tuple_type = @@ -983,21 +996,50 @@ static auto PerformBuiltinConversion( } } - // A pointer T* converts to U* if T is a class derived from U. + // A pointer T* converts to [const] U* if T is the same as U, or is a class + // derived from U. if (auto target_pointer_type = target_type_inst.TryAs()) { if (auto src_pointer_type = sem_ir.types().TryGetAs(value_type_id)) { - if (auto path = - ComputeInheritancePath(context, loc_id, - context.types().GetTypeIdForTypeInstId( - src_pointer_type->pointee_id), - context.types().GetTypeIdForTypeInstId( - target_pointer_type->pointee_id)); - path && !path->empty()) { - return ConvertDerivedPointerToBasePointer( - context, loc_id, *src_pointer_type, target.type_id, value_id, - *path); + auto [unqual_target_pointee_type_id, target_quals] = + sem_ir.types().GetUnqualifiedTypeAndQualifiers( + context.types().GetTypeIdForTypeInstId( + target_pointer_type->pointee_id)); + auto [unqual_src_pointee_type_id, src_quals] = + sem_ir.types().GetUnqualifiedTypeAndQualifiers( + context.types().GetTypeIdForTypeInstId( + src_pointer_type->pointee_id)); + + // If the qualifiers are incompatible, we can't perform a conversion. + if ((src_quals & ~target_quals) != SemIR::TypeQualifiers::None) { + // TODO: Consider producing a custom diagnostic here for a cast that + // discards constness. We should allow this with `unsafe as`. + return value_id; } + + if (unqual_target_pointee_type_id != unqual_src_pointee_type_id) { + // If there's an inheritance path from target to source, this is a + // derived to base conversion. + if (auto path = ComputeInheritancePath(context, loc_id, + unqual_src_pointee_type_id, + unqual_target_pointee_type_id); + path && !path->empty()) { + value_id = ConvertDerivedPointerToBasePointer( + context, loc_id, *src_pointer_type, target.type_id, value_id, + *path); + } else { + // No conversion was possible. + return value_id; + } + } + + // Perform a compatible conversion to add any new qualifiers. + if (src_quals != target_quals) { + return AddInst( + context, loc_id, + {.type_id = target.type_id, .source_id = value_id}); + } + return value_id; } } diff --git a/toolchain/check/testdata/class/derived_to_base.carbon b/toolchain/check/testdata/class/derived_to_base.carbon index 9c6b1fee4464..99aff9899d7b 100644 --- a/toolchain/check/testdata/class/derived_to_base.carbon +++ b/toolchain/check/testdata/class/derived_to_base.carbon @@ -3,8 +3,6 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.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: @@ -12,6 +10,10 @@ // TIP: To dump output, run: // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/derived_to_base.carbon +// --- basic.carbon + +library "[[@TEST_NAME]]"; + base class A { var a: i32; } @@ -26,6 +28,7 @@ class C { var c: i32; } +//@dump-sem-ir-begin fn ConvertCToB(p: C*) -> B* { return p; } fn ConvertBToA(p: B*) -> A* { return p; } fn ConvertCToA(p: C*) -> A* { return p; } @@ -41,46 +44,95 @@ fn ConvertRef(c: C*) -> A* { fn ConvertInit() { let a: A = {.base = {.base = {.a = 1}, .b = 2}, .c = 3} as C; } +//@dump-sem-ir-end -// CHECK:STDOUT: --- derived_to_base.carbon +// --- qualified.carbon + +library "[[@TEST_NAME]]"; + +base class A { +} + +class B { + extend base: A; +} + +fn TakeConstAPtr(p: const A*); + +fn PassNonConstBPtr(p: B*) { + //@dump-sem-ir-begin + TakeConstAPtr(p); + //@dump-sem-ir-end +} + +fn PassConstBPtr(p: const B*) { + //@dump-sem-ir-begin + TakeConstAPtr(p); + //@dump-sem-ir-end +} + +// --- fail_todo_qualified_non_ptr.carbon + +library "[[@TEST_NAME]]"; + +base class A { +} + +class B { + extend base: A; +} + +fn TakeConstA(p: const A); + +fn PassNonConstB(p: B) { + //@dump-sem-ir-begin + // CHECK:STDERR: fail_todo_qualified_non_ptr.carbon:[[@LINE+10]]:14: error: cannot implicitly convert expression of type `B` to `const A` [ConversionFailure] + // CHECK:STDERR: TakeConstA(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_qualified_non_ptr.carbon:[[@LINE+7]]:14: note: type `B` does not implement interface `Core.ImplicitAs(const A)` [MissingImplInMemberAccessNote] + // CHECK:STDERR: TakeConstA(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_qualified_non_ptr.carbon:[[@LINE-10]]:15: note: initializing function parameter [InCallToFunctionParam] + // CHECK:STDERR: fn TakeConstA(p: const A); + // CHECK:STDERR: ^~~~~~~~~~ + // CHECK:STDERR: + TakeConstA(p); + //@dump-sem-ir-end +} + +fn PassConstB(p: const B) { + //@dump-sem-ir-begin + // CHECK:STDERR: fail_todo_qualified_non_ptr.carbon:[[@LINE+10]]:14: error: cannot implicitly convert expression of type `const B` to `const A` [ConversionFailure] + // CHECK:STDERR: TakeConstA(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_qualified_non_ptr.carbon:[[@LINE+7]]:14: note: type `const B` does not implement interface `Core.ImplicitAs(const A)` [MissingImplInMemberAccessNote] + // CHECK:STDERR: TakeConstA(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_qualified_non_ptr.carbon:[[@LINE-26]]:15: note: initializing function parameter [InCallToFunctionParam] + // CHECK:STDERR: fn TakeConstA(p: const A); + // CHECK:STDERR: ^~~~~~~~~~ + // CHECK:STDERR: + TakeConstA(p); + //@dump-sem-ir-end +} + +// CHECK:STDOUT: --- basic.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %A: type = class_type @A [concrete] // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete] -// CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete] // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] -// CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete] // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete] -// CHECK:STDOUT: %A.elem: type = unbound_element_type %A, %i32 [concrete] -// CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete] -// CHECK:STDOUT: %pattern_type.f6d: type = pattern_type auto [concrete] -// CHECK:STDOUT: %Destroy.impl_witness.b44: = impl_witness @A.%Destroy.impl_witness_table [concrete] // CHECK:STDOUT: %ptr.6db: type = ptr_type %A [concrete] // CHECK:STDOUT: %pattern_type.5f8: type = pattern_type %ptr.6db [concrete] -// CHECK:STDOUT: %A.as.Destroy.impl.Op.type: type = fn_type @A.as.Destroy.impl.Op [concrete] -// CHECK:STDOUT: %A.as.Destroy.impl.Op: %A.as.Destroy.impl.Op.type = struct_value () [concrete] -// CHECK:STDOUT: %struct_type.a.ba9: type = struct_type {.a: %i32} [concrete] -// CHECK:STDOUT: %complete_type.fd7: = complete_type_witness %struct_type.a.ba9 [concrete] // CHECK:STDOUT: %B: type = class_type @B [concrete] -// CHECK:STDOUT: %B.elem.e38: type = unbound_element_type %B, %A [concrete] -// CHECK:STDOUT: %B.elem.5c3: type = unbound_element_type %B, %i32 [concrete] -// CHECK:STDOUT: %Destroy.impl_witness.40d: = impl_witness @B.%Destroy.impl_witness_table [concrete] // CHECK:STDOUT: %ptr.e79: type = ptr_type %B [concrete] // CHECK:STDOUT: %pattern_type.960: type = pattern_type %ptr.e79 [concrete] -// CHECK:STDOUT: %B.as.Destroy.impl.Op.type: type = fn_type @B.as.Destroy.impl.Op [concrete] -// CHECK:STDOUT: %B.as.Destroy.impl.Op: %B.as.Destroy.impl.Op.type = struct_value () [concrete] -// CHECK:STDOUT: %struct_type.base.b.b44: type = struct_type {.base: %A, .b: %i32} [concrete] -// CHECK:STDOUT: %complete_type.725: = complete_type_witness %struct_type.base.b.b44 [concrete] // CHECK:STDOUT: %C: type = class_type @C [concrete] -// CHECK:STDOUT: %C.elem.f0c: type = unbound_element_type %C, %B [concrete] -// CHECK:STDOUT: %C.elem.646: type = unbound_element_type %C, %i32 [concrete] -// CHECK:STDOUT: %Destroy.impl_witness.edd: = impl_witness @C.%Destroy.impl_witness_table [concrete] // CHECK:STDOUT: %ptr.019: type = ptr_type %C [concrete] // CHECK:STDOUT: %pattern_type.44a: type = pattern_type %ptr.019 [concrete] // CHECK:STDOUT: %C.as.Destroy.impl.Op.type: type = fn_type @C.as.Destroy.impl.Op [concrete] // CHECK:STDOUT: %C.as.Destroy.impl.Op: %C.as.Destroy.impl.Op.type = struct_value () [concrete] -// CHECK:STDOUT: %struct_type.base.c.8e2: type = struct_type {.base: %B, .c: %i32} [concrete] -// CHECK:STDOUT: %complete_type.58a: = complete_type_witness %struct_type.base.c.8e2 [concrete] // CHECK:STDOUT: %ConvertCToB.type: type = fn_type @ConvertCToB [concrete] // CHECK:STDOUT: %ConvertCToB: %ConvertCToB.type = struct_value () [concrete] // CHECK:STDOUT: %ConvertBToA.type: type = fn_type @ConvertBToA [concrete] @@ -101,8 +153,6 @@ fn ConvertInit() { // CHECK:STDOUT: %struct_type.base.b.bf0: type = struct_type {.base: %struct_type.a.a6c, .b: Core.IntLiteral} [concrete] // CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete] // CHECK:STDOUT: %struct_type.base.c.136: type = struct_type {.base: %struct_type.base.b.bf0, .c: Core.IntLiteral} [concrete] -// CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete] -// CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete] // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete] // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete] // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic] @@ -129,37 +179,11 @@ fn ConvertInit() { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { -// CHECK:STDOUT: %Core: = namespace file.%Core.import, [concrete] { -// CHECK:STDOUT: .Int = %Core.Int -// CHECK:STDOUT: .Destroy = %Core.Destroy -// CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs -// 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.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type] -// CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic] // CHECK:STDOUT: %Core.import_ref.a5b: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.0f9) = import_ref Core//prelude/parts/int, loc16_39, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.f06)] // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (%Core.import_ref.a5b), @Core.IntLiteral.as.ImplicitAs.impl [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: file { -// CHECK:STDOUT: package: = namespace [concrete] { -// CHECK:STDOUT: .Core = imports.%Core -// CHECK:STDOUT: .A = %A.decl -// CHECK:STDOUT: .B = %B.decl -// CHECK:STDOUT: .C = %C.decl -// CHECK:STDOUT: .ConvertCToB = %ConvertCToB.decl -// CHECK:STDOUT: .ConvertBToA = %ConvertBToA.decl -// CHECK:STDOUT: .ConvertCToA = %ConvertCToA.decl -// CHECK:STDOUT: .ConvertValue = %ConvertValue.decl -// CHECK:STDOUT: .ConvertRef = %ConvertRef.decl -// CHECK:STDOUT: .ConvertInit = %ConvertInit.decl -// CHECK:STDOUT: } -// CHECK:STDOUT: %Core.import = import Core -// CHECK:STDOUT: %A.decl: type = class_decl @A [concrete = constants.%A] {} {} -// CHECK:STDOUT: %B.decl: type = class_decl @B [concrete = constants.%B] {} {} -// CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {} // CHECK:STDOUT: %ConvertCToB.decl: %ConvertCToB.type = fn_decl @ConvertCToB [concrete = constants.%ConvertCToB] { // CHECK:STDOUT: %p.patt: %pattern_type.44a = binding_pattern p [concrete] // CHECK:STDOUT: %p.param_patt: %pattern_type.44a = value_param_pattern %p.patt, call_param0 [concrete] @@ -167,11 +191,11 @@ fn ConvertInit() { // CHECK:STDOUT: %return.param_patt: %pattern_type.960 = out_param_pattern %return.patt, call_param1 [concrete] // CHECK:STDOUT: } { // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [concrete = constants.%B] -// CHECK:STDOUT: %ptr.loc29_27: type = ptr_type %B.ref [concrete = constants.%ptr.e79] +// CHECK:STDOUT: %ptr.loc19_27: type = ptr_type %B.ref [concrete = constants.%ptr.e79] // CHECK:STDOUT: %p.param: %ptr.019 = value_param call_param0 -// CHECK:STDOUT: %.loc29_20: type = splice_block %ptr.loc29_20 [concrete = constants.%ptr.019] { +// CHECK:STDOUT: %.loc19_20: type = splice_block %ptr.loc19_20 [concrete = constants.%ptr.019] { // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C] -// CHECK:STDOUT: %ptr.loc29_20: type = ptr_type %C.ref [concrete = constants.%ptr.019] +// CHECK:STDOUT: %ptr.loc19_20: type = ptr_type %C.ref [concrete = constants.%ptr.019] // CHECK:STDOUT: } // CHECK:STDOUT: %p: %ptr.019 = bind_name p, %p.param // CHECK:STDOUT: %return.param: ref %ptr.e79 = out_param call_param1 @@ -184,11 +208,11 @@ fn ConvertInit() { // CHECK:STDOUT: %return.param_patt: %pattern_type.5f8 = out_param_pattern %return.patt, call_param1 [concrete] // CHECK:STDOUT: } { // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [concrete = constants.%A] -// CHECK:STDOUT: %ptr.loc30_27: type = ptr_type %A.ref [concrete = constants.%ptr.6db] +// CHECK:STDOUT: %ptr.loc20_27: type = ptr_type %A.ref [concrete = constants.%ptr.6db] // CHECK:STDOUT: %p.param: %ptr.e79 = value_param call_param0 -// CHECK:STDOUT: %.loc30_20: type = splice_block %ptr.loc30_20 [concrete = constants.%ptr.e79] { +// CHECK:STDOUT: %.loc20_20: type = splice_block %ptr.loc20_20 [concrete = constants.%ptr.e79] { // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [concrete = constants.%B] -// CHECK:STDOUT: %ptr.loc30_20: type = ptr_type %B.ref [concrete = constants.%ptr.e79] +// CHECK:STDOUT: %ptr.loc20_20: type = ptr_type %B.ref [concrete = constants.%ptr.e79] // CHECK:STDOUT: } // CHECK:STDOUT: %p: %ptr.e79 = bind_name p, %p.param // CHECK:STDOUT: %return.param: ref %ptr.6db = out_param call_param1 @@ -201,11 +225,11 @@ fn ConvertInit() { // CHECK:STDOUT: %return.param_patt: %pattern_type.5f8 = out_param_pattern %return.patt, call_param1 [concrete] // CHECK:STDOUT: } { // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [concrete = constants.%A] -// CHECK:STDOUT: %ptr.loc31_27: type = ptr_type %A.ref [concrete = constants.%ptr.6db] +// CHECK:STDOUT: %ptr.loc21_27: type = ptr_type %A.ref [concrete = constants.%ptr.6db] // CHECK:STDOUT: %p.param: %ptr.019 = value_param call_param0 -// CHECK:STDOUT: %.loc31_20: type = splice_block %ptr.loc31_20 [concrete = constants.%ptr.019] { +// CHECK:STDOUT: %.loc21_20: type = splice_block %ptr.loc21_20 [concrete = constants.%ptr.019] { // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C] -// CHECK:STDOUT: %ptr.loc31_20: type = ptr_type %C.ref [concrete = constants.%ptr.019] +// CHECK:STDOUT: %ptr.loc21_20: type = ptr_type %C.ref [concrete = constants.%ptr.019] // CHECK:STDOUT: } // CHECK:STDOUT: %p: %ptr.019 = bind_name p, %p.param // CHECK:STDOUT: %return.param: ref %ptr.6db = out_param call_param1 @@ -225,12 +249,12 @@ fn ConvertInit() { // CHECK:STDOUT: %return.patt: %pattern_type.5f8 = return_slot_pattern [concrete] // CHECK:STDOUT: %return.param_patt: %pattern_type.5f8 = out_param_pattern %return.patt, call_param1 [concrete] // CHECK:STDOUT: } { -// CHECK:STDOUT: %A.ref.loc37: type = name_ref A, file.%A.decl [concrete = constants.%A] -// CHECK:STDOUT: %ptr.loc37_26: type = ptr_type %A.ref.loc37 [concrete = constants.%ptr.6db] +// CHECK:STDOUT: %A.ref.loc27: type = name_ref A, file.%A.decl [concrete = constants.%A] +// CHECK:STDOUT: %ptr.loc27_26: type = ptr_type %A.ref.loc27 [concrete = constants.%ptr.6db] // CHECK:STDOUT: %c.param: %ptr.019 = value_param call_param0 -// CHECK:STDOUT: %.loc37: type = splice_block %ptr.loc37_19 [concrete = constants.%ptr.019] { +// CHECK:STDOUT: %.loc27: type = splice_block %ptr.loc27_19 [concrete = constants.%ptr.019] { // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C] -// CHECK:STDOUT: %ptr.loc37_19: type = ptr_type %C.ref [concrete = constants.%ptr.019] +// CHECK:STDOUT: %ptr.loc27_19: type = ptr_type %C.ref [concrete = constants.%ptr.019] // CHECK:STDOUT: } // CHECK:STDOUT: %c: %ptr.019 = bind_name c, %c.param // CHECK:STDOUT: %return.param: ref %ptr.6db = out_param call_param1 @@ -239,147 +263,35 @@ fn ConvertInit() { // CHECK:STDOUT: %ConvertInit.decl: %ConvertInit.type = fn_decl @ConvertInit [concrete = constants.%ConvertInit] {} {} // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: impl @A.as.Destroy.impl: @A.%Self.ref as constants.%Destroy.type { -// CHECK:STDOUT: %A.as.Destroy.impl.Op.decl: %A.as.Destroy.impl.Op.type = fn_decl @A.as.Destroy.impl.Op [concrete = constants.%A.as.Destroy.impl.Op] { -// CHECK:STDOUT: %self.patt: %pattern_type.5f8 = binding_pattern self [concrete] -// CHECK:STDOUT: %self.param_patt: %pattern_type.5f8 = value_param_pattern %self.patt, call_param0 [concrete] -// CHECK:STDOUT: %.loc15: %pattern_type.f6d = addr_pattern %self.param_patt [concrete] -// CHECK:STDOUT: } { -// CHECK:STDOUT: %self.param: %ptr.6db = value_param call_param0 -// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%A [concrete = constants.%A] -// CHECK:STDOUT: %self: %ptr.6db = bind_name self, %self.param -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Op = %A.as.Destroy.impl.Op.decl -// CHECK:STDOUT: witness = @A.%Destroy.impl_witness -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: impl @B.as.Destroy.impl: @B.%Self.ref as constants.%Destroy.type { -// CHECK:STDOUT: %B.as.Destroy.impl.Op.decl: %B.as.Destroy.impl.Op.type = fn_decl @B.as.Destroy.impl.Op [concrete = constants.%B.as.Destroy.impl.Op] { -// CHECK:STDOUT: %self.patt: %pattern_type.960 = binding_pattern self [concrete] -// CHECK:STDOUT: %self.param_patt: %pattern_type.960 = value_param_pattern %self.patt, call_param0 [concrete] -// CHECK:STDOUT: %.loc19: %pattern_type.f6d = addr_pattern %self.param_patt [concrete] -// CHECK:STDOUT: } { -// CHECK:STDOUT: %self.param: %ptr.e79 = value_param call_param0 -// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%B [concrete = constants.%B] -// CHECK:STDOUT: %self: %ptr.e79 = bind_name self, %self.param -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Op = %B.as.Destroy.impl.Op.decl -// CHECK:STDOUT: witness = @B.%Destroy.impl_witness -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: impl @C.as.Destroy.impl: @C.%Self.ref as constants.%Destroy.type { -// CHECK:STDOUT: %C.as.Destroy.impl.Op.decl: %C.as.Destroy.impl.Op.type = fn_decl @C.as.Destroy.impl.Op [concrete = constants.%C.as.Destroy.impl.Op] { -// CHECK:STDOUT: %self.patt: %pattern_type.44a = binding_pattern self [concrete] -// CHECK:STDOUT: %self.param_patt: %pattern_type.44a = value_param_pattern %self.patt, call_param0 [concrete] -// CHECK:STDOUT: %.loc24: %pattern_type.f6d = addr_pattern %self.param_patt [concrete] -// CHECK:STDOUT: } { -// CHECK:STDOUT: %self.param: %ptr.019 = value_param call_param0 -// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C] -// CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Op = %C.as.Destroy.impl.Op.decl -// CHECK:STDOUT: witness = @C.%Destroy.impl_witness -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: class @A { -// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32] -// CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32] -// CHECK:STDOUT: %.loc16: %A.elem = field_decl a, element0 [concrete] -// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%A [concrete = constants.%A] -// CHECK:STDOUT: impl_decl @A.as.Destroy.impl [concrete] {} {} -// CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (@A.as.Destroy.impl.%A.as.Destroy.impl.Op.decl), @A.as.Destroy.impl [concrete] -// CHECK:STDOUT: %Destroy.impl_witness: = impl_witness %Destroy.impl_witness_table [concrete = constants.%Destroy.impl_witness.b44] -// CHECK:STDOUT: %complete_type: = complete_type_witness constants.%struct_type.a.ba9 [concrete = constants.%complete_type.fd7] -// CHECK:STDOUT: complete_type_witness = %complete_type -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = constants.%A -// CHECK:STDOUT: .a = %.loc16 -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: class @B { -// CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [concrete = constants.%A] -// CHECK:STDOUT: %.loc20: %B.elem.e38 = base_decl %A.ref, element0 [concrete] -// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32] -// CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32] -// CHECK:STDOUT: %.loc21: %B.elem.5c3 = field_decl b, element1 [concrete] -// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%B [concrete = constants.%B] -// CHECK:STDOUT: impl_decl @B.as.Destroy.impl [concrete] {} {} -// CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (@B.as.Destroy.impl.%B.as.Destroy.impl.Op.decl), @B.as.Destroy.impl [concrete] -// CHECK:STDOUT: %Destroy.impl_witness: = impl_witness %Destroy.impl_witness_table [concrete = constants.%Destroy.impl_witness.40d] -// CHECK:STDOUT: %complete_type: = complete_type_witness constants.%struct_type.base.b.b44 [concrete = constants.%complete_type.725] -// CHECK:STDOUT: complete_type_witness = %complete_type -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = constants.%B -// CHECK:STDOUT: .A = -// CHECK:STDOUT: .base = %.loc20 -// CHECK:STDOUT: .b = %.loc21 -// CHECK:STDOUT: extend %A.ref -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: class @C { -// CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [concrete = constants.%B] -// CHECK:STDOUT: %.loc25: %C.elem.f0c = base_decl %B.ref, element0 [concrete] -// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32] -// CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32] -// CHECK:STDOUT: %.loc26: %C.elem.646 = field_decl c, element1 [concrete] -// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C] -// CHECK:STDOUT: impl_decl @C.as.Destroy.impl [concrete] {} {} -// CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (@C.as.Destroy.impl.%C.as.Destroy.impl.Op.decl), @C.as.Destroy.impl [concrete] -// CHECK:STDOUT: %Destroy.impl_witness: = impl_witness %Destroy.impl_witness_table [concrete = constants.%Destroy.impl_witness.edd] -// CHECK:STDOUT: %complete_type: = complete_type_witness constants.%struct_type.base.c.8e2 [concrete = constants.%complete_type.58a] -// CHECK:STDOUT: complete_type_witness = %complete_type -// CHECK:STDOUT: -// CHECK:STDOUT: !members: -// CHECK:STDOUT: .Self = constants.%C -// CHECK:STDOUT: .B = -// CHECK:STDOUT: .base = %.loc25 -// CHECK:STDOUT: .c = %.loc26 -// CHECK:STDOUT: extend %B.ref -// CHECK:STDOUT: } -// CHECK:STDOUT: -// CHECK:STDOUT: fn @A.as.Destroy.impl.Op(%self.param: %ptr.6db) = "no_op"; -// CHECK:STDOUT: -// CHECK:STDOUT: fn @B.as.Destroy.impl.Op(%self.param: %ptr.e79) = "no_op"; -// CHECK:STDOUT: -// CHECK:STDOUT: fn @C.as.Destroy.impl.Op(%self.param: %ptr.019) = "no_op"; -// CHECK:STDOUT: // CHECK:STDOUT: fn @ConvertCToB(%p.param: %ptr.019) -> %ptr.e79 { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %p.ref: %ptr.019 = name_ref p, %p -// CHECK:STDOUT: %.loc29_39.1: ref %C = deref %p.ref -// CHECK:STDOUT: %.loc29_39.2: ref %B = class_element_access %.loc29_39.1, element0 -// CHECK:STDOUT: %addr: %ptr.e79 = addr_of %.loc29_39.2 -// CHECK:STDOUT: %.loc29_39.3: %ptr.e79 = converted %p.ref, %addr -// CHECK:STDOUT: return %.loc29_39.3 +// CHECK:STDOUT: %.loc19_39.1: ref %C = deref %p.ref +// CHECK:STDOUT: %.loc19_39.2: ref %B = class_element_access %.loc19_39.1, element0 +// CHECK:STDOUT: %addr: %ptr.e79 = addr_of %.loc19_39.2 +// CHECK:STDOUT: %.loc19_39.3: %ptr.e79 = converted %p.ref, %addr +// CHECK:STDOUT: return %.loc19_39.3 // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: fn @ConvertBToA(%p.param: %ptr.e79) -> %ptr.6db { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %p.ref: %ptr.e79 = name_ref p, %p -// CHECK:STDOUT: %.loc30_39.1: ref %B = deref %p.ref -// CHECK:STDOUT: %.loc30_39.2: ref %A = class_element_access %.loc30_39.1, element0 -// CHECK:STDOUT: %addr: %ptr.6db = addr_of %.loc30_39.2 -// CHECK:STDOUT: %.loc30_39.3: %ptr.6db = converted %p.ref, %addr -// CHECK:STDOUT: return %.loc30_39.3 +// CHECK:STDOUT: %.loc20_39.1: ref %B = deref %p.ref +// CHECK:STDOUT: %.loc20_39.2: ref %A = class_element_access %.loc20_39.1, element0 +// CHECK:STDOUT: %addr: %ptr.6db = addr_of %.loc20_39.2 +// CHECK:STDOUT: %.loc20_39.3: %ptr.6db = converted %p.ref, %addr +// CHECK:STDOUT: return %.loc20_39.3 // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: fn @ConvertCToA(%p.param: %ptr.019) -> %ptr.6db { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %p.ref: %ptr.019 = name_ref p, %p -// CHECK:STDOUT: %.loc31_39.1: ref %C = deref %p.ref -// CHECK:STDOUT: %.loc31_39.2: ref %B = class_element_access %.loc31_39.1, element0 -// CHECK:STDOUT: %.loc31_39.3: ref %A = class_element_access %.loc31_39.2, element0 -// CHECK:STDOUT: %addr: %ptr.6db = addr_of %.loc31_39.3 -// CHECK:STDOUT: %.loc31_39.4: %ptr.6db = converted %p.ref, %addr -// CHECK:STDOUT: return %.loc31_39.4 +// CHECK:STDOUT: %.loc21_39.1: ref %C = deref %p.ref +// CHECK:STDOUT: %.loc21_39.2: ref %B = class_element_access %.loc21_39.1, element0 +// CHECK:STDOUT: %.loc21_39.3: ref %A = class_element_access %.loc21_39.2, element0 +// CHECK:STDOUT: %addr: %ptr.6db = addr_of %.loc21_39.3 +// CHECK:STDOUT: %.loc21_39.4: %ptr.6db = converted %p.ref, %addr +// CHECK:STDOUT: return %.loc21_39.4 // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: fn @ConvertValue(%c.param: %C) { @@ -389,23 +301,23 @@ fn ConvertInit() { // CHECK:STDOUT: } // CHECK:STDOUT: %c.ref: %C = name_ref c, %c // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [concrete = constants.%A] -// CHECK:STDOUT: %.loc34_14.1: ref %B = class_element_access %c.ref, element0 -// CHECK:STDOUT: %.loc34_14.2: ref %A = class_element_access %.loc34_14.1, element0 -// CHECK:STDOUT: %.loc34_14.3: ref %A = converted %c.ref, %.loc34_14.2 -// CHECK:STDOUT: %.loc34_14.4: %A = bind_value %.loc34_14.3 -// CHECK:STDOUT: %a: %A = bind_name a, %.loc34_14.4 +// CHECK:STDOUT: %.loc24_14.1: ref %B = class_element_access %c.ref, element0 +// CHECK:STDOUT: %.loc24_14.2: ref %A = class_element_access %.loc24_14.1, element0 +// CHECK:STDOUT: %.loc24_14.3: ref %A = converted %c.ref, %.loc24_14.2 +// CHECK:STDOUT: %.loc24_14.4: %A = bind_value %.loc24_14.3 +// CHECK:STDOUT: %a: %A = bind_name a, %.loc24_14.4 // CHECK:STDOUT: return // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: fn @ConvertRef(%c.param: %ptr.019) -> %ptr.6db { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %c.ref: %ptr.019 = name_ref c, %c -// CHECK:STDOUT: %.loc38_12: ref %C = deref %c.ref -// CHECK:STDOUT: %A.ref.loc38: type = name_ref A, file.%A.decl [concrete = constants.%A] -// CHECK:STDOUT: %.loc38_15.1: ref %B = class_element_access %.loc38_12, element0 -// CHECK:STDOUT: %.loc38_15.2: ref %A = class_element_access %.loc38_15.1, element0 -// CHECK:STDOUT: %.loc38_15.3: ref %A = converted %.loc38_12, %.loc38_15.2 -// CHECK:STDOUT: %addr: %ptr.6db = addr_of %.loc38_15.3 +// CHECK:STDOUT: %.loc28_12: ref %C = deref %c.ref +// CHECK:STDOUT: %A.ref.loc28: type = name_ref A, file.%A.decl [concrete = constants.%A] +// CHECK:STDOUT: %.loc28_15.1: ref %B = class_element_access %.loc28_12, element0 +// CHECK:STDOUT: %.loc28_15.2: ref %A = class_element_access %.loc28_15.1, element0 +// CHECK:STDOUT: %.loc28_15.3: ref %A = converted %.loc28_12, %.loc28_15.2 +// CHECK:STDOUT: %addr: %ptr.6db = addr_of %.loc28_15.3 // CHECK:STDOUT: return %addr // CHECK:STDOUT: } // CHECK:STDOUT: @@ -415,55 +327,131 @@ fn ConvertInit() { // CHECK:STDOUT: %a.patt: %pattern_type.c10 = binding_pattern a [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8] -// CHECK:STDOUT: %.loc42_39.1: %struct_type.a.a6c = struct_literal (%int_1) +// CHECK:STDOUT: %.loc32_39.1: %struct_type.a.a6c = struct_literal (%int_1) // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc] -// CHECK:STDOUT: %.loc42_48.1: %struct_type.base.b.bf0 = struct_literal (%.loc42_39.1, %int_2) +// CHECK:STDOUT: %.loc32_48.1: %struct_type.base.b.bf0 = struct_literal (%.loc32_39.1, %int_2) // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [concrete = constants.%int_3.1ba] -// CHECK:STDOUT: %.loc42_57.1: %struct_type.base.c.136 = struct_literal (%.loc42_48.1, %int_3) +// CHECK:STDOUT: %.loc32_57.1: %struct_type.base.c.136 = struct_literal (%.loc32_48.1, %int_3) // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C] -// CHECK:STDOUT: %impl.elem0.loc42_39: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956] -// CHECK:STDOUT: %bound_method.loc42_39.1: = bound_method %int_1, %impl.elem0.loc42_39 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.ab5] -// CHECK:STDOUT: %specific_fn.loc42_39: = specific_function %impl.elem0.loc42_39, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] -// CHECK:STDOUT: %bound_method.loc42_39.2: = bound_method %int_1, %specific_fn.loc42_39 [concrete = constants.%bound_method.9a1] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc42_39: init %i32 = call %bound_method.loc42_39.2(%int_1) [concrete = constants.%int_1.5d2] -// CHECK:STDOUT: %.loc42_39.2: init %i32 = converted %int_1, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc42_39 [concrete = constants.%int_1.5d2] -// CHECK:STDOUT: %.loc42_57.2: ref %C = temporary_storage -// CHECK:STDOUT: %.loc42_57.3: ref %B = class_element_access %.loc42_57.2, element0 -// CHECK:STDOUT: %.loc42_48.2: ref %A = class_element_access %.loc42_57.3, element0 -// CHECK:STDOUT: %.loc42_39.3: ref %i32 = class_element_access %.loc42_48.2, element0 -// CHECK:STDOUT: %.loc42_39.4: init %i32 = initialize_from %.loc42_39.2 to %.loc42_39.3 [concrete = constants.%int_1.5d2] -// CHECK:STDOUT: %.loc42_39.5: init %A = class_init (%.loc42_39.4), %.loc42_48.2 [concrete = constants.%A.val] -// CHECK:STDOUT: %.loc42_48.3: init %A = converted %.loc42_39.1, %.loc42_39.5 [concrete = constants.%A.val] -// CHECK:STDOUT: %impl.elem0.loc42_48: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956] -// CHECK:STDOUT: %bound_method.loc42_48.1: = bound_method %int_2, %impl.elem0.loc42_48 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.ef9] -// CHECK:STDOUT: %specific_fn.loc42_48: = specific_function %impl.elem0.loc42_48, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] -// CHECK:STDOUT: %bound_method.loc42_48.2: = bound_method %int_2, %specific_fn.loc42_48 [concrete = constants.%bound_method.b92] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc42_48: init %i32 = call %bound_method.loc42_48.2(%int_2) [concrete = constants.%int_2.ef8] -// CHECK:STDOUT: %.loc42_48.4: init %i32 = converted %int_2, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc42_48 [concrete = constants.%int_2.ef8] -// CHECK:STDOUT: %.loc42_48.5: ref %i32 = class_element_access %.loc42_57.3, element1 -// CHECK:STDOUT: %.loc42_48.6: init %i32 = initialize_from %.loc42_48.4 to %.loc42_48.5 [concrete = constants.%int_2.ef8] -// CHECK:STDOUT: %.loc42_48.7: init %B = class_init (%.loc42_48.3, %.loc42_48.6), %.loc42_57.3 [concrete = constants.%B.val] -// CHECK:STDOUT: %.loc42_57.4: init %B = converted %.loc42_48.1, %.loc42_48.7 [concrete = constants.%B.val] -// CHECK:STDOUT: %impl.elem0.loc42_57: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956] -// CHECK:STDOUT: %bound_method.loc42_57.1: = bound_method %int_3, %impl.elem0.loc42_57 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.b30] -// CHECK:STDOUT: %specific_fn.loc42_57: = specific_function %impl.elem0.loc42_57, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] -// CHECK:STDOUT: %bound_method.loc42_57.2: = bound_method %int_3, %specific_fn.loc42_57 [concrete = constants.%bound_method.047] -// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc42_57: init %i32 = call %bound_method.loc42_57.2(%int_3) [concrete = constants.%int_3.822] -// CHECK:STDOUT: %.loc42_57.5: init %i32 = converted %int_3, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc42_57 [concrete = constants.%int_3.822] -// CHECK:STDOUT: %.loc42_57.6: ref %i32 = class_element_access %.loc42_57.2, element1 -// CHECK:STDOUT: %.loc42_57.7: init %i32 = initialize_from %.loc42_57.5 to %.loc42_57.6 [concrete = constants.%int_3.822] -// CHECK:STDOUT: %.loc42_57.8: init %C = class_init (%.loc42_57.4, %.loc42_57.7), %.loc42_57.2 [concrete = constants.%C.val] -// CHECK:STDOUT: %.loc42_57.9: ref %C = temporary %.loc42_57.2, %.loc42_57.8 -// CHECK:STDOUT: %.loc42_59.1: ref %C = converted %.loc42_57.1, %.loc42_57.9 +// CHECK:STDOUT: %impl.elem0.loc32_39: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956] +// CHECK:STDOUT: %bound_method.loc32_39.1: = bound_method %int_1, %impl.elem0.loc32_39 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.ab5] +// CHECK:STDOUT: %specific_fn.loc32_39: = specific_function %impl.elem0.loc32_39, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] +// CHECK:STDOUT: %bound_method.loc32_39.2: = bound_method %int_1, %specific_fn.loc32_39 [concrete = constants.%bound_method.9a1] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc32_39: init %i32 = call %bound_method.loc32_39.2(%int_1) [concrete = constants.%int_1.5d2] +// CHECK:STDOUT: %.loc32_39.2: init %i32 = converted %int_1, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc32_39 [concrete = constants.%int_1.5d2] +// CHECK:STDOUT: %.loc32_57.2: ref %C = temporary_storage +// CHECK:STDOUT: %.loc32_57.3: ref %B = class_element_access %.loc32_57.2, element0 +// CHECK:STDOUT: %.loc32_48.2: ref %A = class_element_access %.loc32_57.3, element0 +// CHECK:STDOUT: %.loc32_39.3: ref %i32 = class_element_access %.loc32_48.2, element0 +// CHECK:STDOUT: %.loc32_39.4: init %i32 = initialize_from %.loc32_39.2 to %.loc32_39.3 [concrete = constants.%int_1.5d2] +// CHECK:STDOUT: %.loc32_39.5: init %A = class_init (%.loc32_39.4), %.loc32_48.2 [concrete = constants.%A.val] +// CHECK:STDOUT: %.loc32_48.3: init %A = converted %.loc32_39.1, %.loc32_39.5 [concrete = constants.%A.val] +// CHECK:STDOUT: %impl.elem0.loc32_48: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956] +// CHECK:STDOUT: %bound_method.loc32_48.1: = bound_method %int_2, %impl.elem0.loc32_48 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.ef9] +// CHECK:STDOUT: %specific_fn.loc32_48: = specific_function %impl.elem0.loc32_48, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] +// CHECK:STDOUT: %bound_method.loc32_48.2: = bound_method %int_2, %specific_fn.loc32_48 [concrete = constants.%bound_method.b92] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc32_48: init %i32 = call %bound_method.loc32_48.2(%int_2) [concrete = constants.%int_2.ef8] +// CHECK:STDOUT: %.loc32_48.4: init %i32 = converted %int_2, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc32_48 [concrete = constants.%int_2.ef8] +// CHECK:STDOUT: %.loc32_48.5: ref %i32 = class_element_access %.loc32_57.3, element1 +// CHECK:STDOUT: %.loc32_48.6: init %i32 = initialize_from %.loc32_48.4 to %.loc32_48.5 [concrete = constants.%int_2.ef8] +// CHECK:STDOUT: %.loc32_48.7: init %B = class_init (%.loc32_48.3, %.loc32_48.6), %.loc32_57.3 [concrete = constants.%B.val] +// CHECK:STDOUT: %.loc32_57.4: init %B = converted %.loc32_48.1, %.loc32_48.7 [concrete = constants.%B.val] +// CHECK:STDOUT: %impl.elem0.loc32_57: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956] +// CHECK:STDOUT: %bound_method.loc32_57.1: = bound_method %int_3, %impl.elem0.loc32_57 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.b30] +// CHECK:STDOUT: %specific_fn.loc32_57: = specific_function %impl.elem0.loc32_57, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] +// CHECK:STDOUT: %bound_method.loc32_57.2: = bound_method %int_3, %specific_fn.loc32_57 [concrete = constants.%bound_method.047] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc32_57: init %i32 = call %bound_method.loc32_57.2(%int_3) [concrete = constants.%int_3.822] +// CHECK:STDOUT: %.loc32_57.5: init %i32 = converted %int_3, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc32_57 [concrete = constants.%int_3.822] +// CHECK:STDOUT: %.loc32_57.6: ref %i32 = class_element_access %.loc32_57.2, element1 +// CHECK:STDOUT: %.loc32_57.7: init %i32 = initialize_from %.loc32_57.5 to %.loc32_57.6 [concrete = constants.%int_3.822] +// CHECK:STDOUT: %.loc32_57.8: init %C = class_init (%.loc32_57.4, %.loc32_57.7), %.loc32_57.2 [concrete = constants.%C.val] +// CHECK:STDOUT: %.loc32_57.9: ref %C = temporary %.loc32_57.2, %.loc32_57.8 +// CHECK:STDOUT: %.loc32_59.1: ref %C = converted %.loc32_57.1, %.loc32_57.9 // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [concrete = constants.%A] -// CHECK:STDOUT: %.loc42_59.2: ref %B = class_element_access %.loc42_59.1, element0 -// CHECK:STDOUT: %.loc42_59.3: ref %A = class_element_access %.loc42_59.2, element0 -// CHECK:STDOUT: %.loc42_59.4: ref %A = converted %.loc42_59.1, %.loc42_59.3 -// CHECK:STDOUT: %.loc42_59.5: %A = bind_value %.loc42_59.4 -// CHECK:STDOUT: %a: %A = bind_name a, %.loc42_59.5 -// CHECK:STDOUT: %C.as.Destroy.impl.Op.bound: = bound_method %.loc42_57.2, constants.%C.as.Destroy.impl.Op -// CHECK:STDOUT: %addr: %ptr.019 = addr_of %.loc42_57.2 +// CHECK:STDOUT: %.loc32_59.2: ref %B = class_element_access %.loc32_59.1, element0 +// CHECK:STDOUT: %.loc32_59.3: ref %A = class_element_access %.loc32_59.2, element0 +// CHECK:STDOUT: %.loc32_59.4: ref %A = converted %.loc32_59.1, %.loc32_59.3 +// CHECK:STDOUT: %.loc32_59.5: %A = bind_value %.loc32_59.4 +// CHECK:STDOUT: %a: %A = bind_name a, %.loc32_59.5 +// CHECK:STDOUT: %C.as.Destroy.impl.Op.bound: = bound_method %.loc32_57.2, constants.%C.as.Destroy.impl.Op +// CHECK:STDOUT: %addr: %ptr.019 = addr_of %.loc32_57.2 // CHECK:STDOUT: %C.as.Destroy.impl.Op.call: init %empty_tuple.type = call %C.as.Destroy.impl.Op.bound(%addr) // CHECK:STDOUT: return // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: --- qualified.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %A: type = class_type @A [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %B: type = class_type @B [concrete] +// CHECK:STDOUT: %ptr.e79: type = ptr_type %B [concrete] +// CHECK:STDOUT: %const.d98: type = const_type %A [concrete] +// CHECK:STDOUT: %ptr.985: type = ptr_type %const.d98 [concrete] +// CHECK:STDOUT: %TakeConstAPtr.type: type = fn_type @TakeConstAPtr [concrete] +// CHECK:STDOUT: %TakeConstAPtr: %TakeConstAPtr.type = struct_value () [concrete] +// CHECK:STDOUT: %const.44f: type = const_type %B [concrete] +// CHECK:STDOUT: %ptr.eb4: type = ptr_type %const.44f [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @PassNonConstBPtr(%p.param: %ptr.e79) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConstAPtr.ref: %TakeConstAPtr.type = name_ref TakeConstAPtr, file.%TakeConstAPtr.decl [concrete = constants.%TakeConstAPtr] +// CHECK:STDOUT: %p.ref: %ptr.e79 = name_ref p, %p +// CHECK:STDOUT: %.loc15_17.1: ref %B = deref %p.ref +// CHECK:STDOUT: %.loc15_17.2: ref %A = class_element_access %.loc15_17.1, element0 +// CHECK:STDOUT: %addr: %ptr.985 = addr_of %.loc15_17.2 +// CHECK:STDOUT: %.loc15_17.3: %ptr.985 = as_compatible %addr +// CHECK:STDOUT: %.loc15_17.4: %ptr.985 = converted %p.ref, %.loc15_17.3 +// CHECK:STDOUT: %TakeConstAPtr.call: init %empty_tuple.type = call %TakeConstAPtr.ref(%.loc15_17.4) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @PassConstBPtr(%p.param: %ptr.eb4) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConstAPtr.ref: %TakeConstAPtr.type = name_ref TakeConstAPtr, file.%TakeConstAPtr.decl [concrete = constants.%TakeConstAPtr] +// CHECK:STDOUT: %p.ref: %ptr.eb4 = name_ref p, %p +// CHECK:STDOUT: %.loc21_17.1: ref %const.44f = deref %p.ref +// CHECK:STDOUT: %.loc21_17.2: ref %const.d98 = class_element_access %.loc21_17.1, element0 +// CHECK:STDOUT: %addr: %ptr.985 = addr_of %.loc21_17.2 +// CHECK:STDOUT: %.loc21_17.3: %ptr.985 = converted %p.ref, %addr +// CHECK:STDOUT: %TakeConstAPtr.call: init %empty_tuple.type = call %TakeConstAPtr.ref(%.loc21_17.3) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_todo_qualified_non_ptr.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %A: type = class_type @A [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %B: type = class_type @B [concrete] +// CHECK:STDOUT: %const.d98: type = const_type %A [concrete] +// CHECK:STDOUT: %TakeConstA.type: type = fn_type @TakeConstA [concrete] +// CHECK:STDOUT: %TakeConstA: %TakeConstA.type = struct_value () [concrete] +// CHECK:STDOUT: %const.44f: type = const_type %B [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @PassNonConstB(%p.param: %B) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConstA.ref: %TakeConstA.type = name_ref TakeConstA, file.%TakeConstA.decl [concrete = constants.%TakeConstA] +// CHECK:STDOUT: %p.ref: %B = name_ref p, %p +// CHECK:STDOUT: %.loc25: %const.d98 = converted %p.ref, [concrete = ] +// CHECK:STDOUT: %TakeConstA.call: init %empty_tuple.type = call %TakeConstA.ref() +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @PassConstB(%p.param: %const.44f) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConstA.ref: %TakeConstA.type = name_ref TakeConstA, file.%TakeConstA.decl [concrete = constants.%TakeConstA] +// CHECK:STDOUT: %p.ref: %const.44f = name_ref p, %p +// CHECK:STDOUT: %.loc41: %const.d98 = converted %p.ref, [concrete = ] +// CHECK:STDOUT: %TakeConstA.call: init %empty_tuple.type = call %TakeConstA.ref() +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/const/import.carbon b/toolchain/check/testdata/const/import.carbon index 537d6a4e3dd7..fe8a9041b067 100644 --- a/toolchain/check/testdata/const/import.carbon +++ b/toolchain/check/testdata/const/import.carbon @@ -35,22 +35,22 @@ var a_ptr: const C* = a_ptr_ref; // CHECK:STDOUT: constants { // CHECK:STDOUT: %C: type = class_type @C [concrete] // CHECK:STDOUT: %const: type = const_type %C [concrete] -// CHECK:STDOUT: %ptr.801: type = ptr_type %const [concrete] -// CHECK:STDOUT: %pattern_type.c0d: type = pattern_type %ptr.801 [concrete] +// CHECK:STDOUT: %ptr: type = ptr_type %const [concrete] +// CHECK:STDOUT: %pattern_type.c0d: type = pattern_type %ptr [concrete] // CHECK:STDOUT: %pattern_type.6af: type = pattern_type %const [concrete] -// CHECK:STDOUT: %addr: %ptr.801 = addr_of imports.%a_ref.var [concrete] +// CHECK:STDOUT: %addr: %ptr = addr_of imports.%a_ref.var [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { // CHECK:STDOUT: %Main.C: type = import_ref Main//implicit, C, loaded [concrete = constants.%C] // CHECK:STDOUT: %Main.a_ref: ref %const = import_ref Main//implicit, a_ref, loaded [concrete = %a_ref.var] -// CHECK:STDOUT: %Main.a_ptr_ref: ref %ptr.801 = import_ref Main//implicit, a_ptr_ref, loaded [concrete = %a_ptr_ref.var] +// CHECK:STDOUT: %Main.a_ptr_ref: ref %ptr = import_ref Main//implicit, a_ptr_ref, loaded [concrete = %a_ptr_ref.var] // CHECK:STDOUT: %a_ref.patt: %pattern_type.6af = binding_pattern a_ref [concrete] // CHECK:STDOUT: %a_ref.var_patt: %pattern_type.6af = var_pattern %a_ref.patt [concrete] // CHECK:STDOUT: %a_ref.var: ref %const = var %a_ref.var_patt [concrete] // CHECK:STDOUT: %a_ptr_ref.patt: %pattern_type.c0d = binding_pattern a_ptr_ref [concrete] // CHECK:STDOUT: %a_ptr_ref.var_patt: %pattern_type.c0d = var_pattern %a_ptr_ref.patt [concrete] -// CHECK:STDOUT: %a_ptr_ref.var: ref %ptr.801 = var %a_ptr_ref.var_patt [concrete] +// CHECK:STDOUT: %a_ptr_ref.var: ref %ptr = var %a_ptr_ref.var_patt [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: file { @@ -58,33 +58,33 @@ var a_ptr: const C* = a_ptr_ref; // CHECK:STDOUT: %a.patt: %pattern_type.c0d = binding_pattern a [concrete] // CHECK:STDOUT: %a.var_patt: %pattern_type.c0d = var_pattern %a.patt [concrete] // CHECK:STDOUT: } -// CHECK:STDOUT: %a.var: ref %ptr.801 = var %a.var_patt [concrete] -// CHECK:STDOUT: %.loc6: type = splice_block %ptr.loc6 [concrete = constants.%ptr.801] { +// CHECK:STDOUT: %a.var: ref %ptr = var %a.var_patt [concrete] +// CHECK:STDOUT: %.loc6: type = splice_block %ptr.loc6 [concrete = constants.%ptr] { // CHECK:STDOUT: %C.ref.loc6: type = name_ref C, imports.%Main.C [concrete = constants.%C] // CHECK:STDOUT: %const.loc6: type = const_type %C.ref.loc6 [concrete = constants.%const] -// CHECK:STDOUT: %ptr.loc6: type = ptr_type %const.loc6 [concrete = constants.%ptr.801] +// CHECK:STDOUT: %ptr.loc6: type = ptr_type %const.loc6 [concrete = constants.%ptr] // CHECK:STDOUT: } -// CHECK:STDOUT: %a: ref %ptr.801 = bind_name a, %a.var [concrete = %a.var] +// CHECK:STDOUT: %a: ref %ptr = bind_name a, %a.var [concrete = %a.var] // CHECK:STDOUT: name_binding_decl { // CHECK:STDOUT: %a_ptr.patt: %pattern_type.c0d = binding_pattern a_ptr [concrete] // CHECK:STDOUT: %a_ptr.var_patt: %pattern_type.c0d = var_pattern %a_ptr.patt [concrete] // CHECK:STDOUT: } -// CHECK:STDOUT: %a_ptr.var: ref %ptr.801 = var %a_ptr.var_patt [concrete] -// CHECK:STDOUT: %.loc7: type = splice_block %ptr.loc7 [concrete = constants.%ptr.801] { +// CHECK:STDOUT: %a_ptr.var: ref %ptr = var %a_ptr.var_patt [concrete] +// CHECK:STDOUT: %.loc7: type = splice_block %ptr.loc7 [concrete = constants.%ptr] { // CHECK:STDOUT: %C.ref.loc7: type = name_ref C, imports.%Main.C [concrete = constants.%C] // CHECK:STDOUT: %const.loc7: type = const_type %C.ref.loc7 [concrete = constants.%const] -// CHECK:STDOUT: %ptr.loc7: type = ptr_type %const.loc7 [concrete = constants.%ptr.801] +// CHECK:STDOUT: %ptr.loc7: type = ptr_type %const.loc7 [concrete = constants.%ptr] // CHECK:STDOUT: } -// CHECK:STDOUT: %a_ptr: ref %ptr.801 = bind_name a_ptr, %a_ptr.var [concrete = %a_ptr.var] +// CHECK:STDOUT: %a_ptr: ref %ptr = bind_name a_ptr, %a_ptr.var [concrete = %a_ptr.var] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: fn @__global_init() { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %a_ref.ref: ref %const = name_ref a_ref, imports.%Main.a_ref [concrete = imports.%a_ref.var] -// CHECK:STDOUT: %addr: %ptr.801 = addr_of %a_ref.ref [concrete = constants.%addr] +// CHECK:STDOUT: %addr: %ptr = addr_of %a_ref.ref [concrete = constants.%addr] // CHECK:STDOUT: assign file.%a.var, %addr -// CHECK:STDOUT: %a_ptr_ref.ref: ref %ptr.801 = name_ref a_ptr_ref, imports.%Main.a_ptr_ref [concrete = imports.%a_ptr_ref.var] -// CHECK:STDOUT: %.loc7: %ptr.801 = bind_value %a_ptr_ref.ref +// CHECK:STDOUT: %a_ptr_ref.ref: ref %ptr = name_ref a_ptr_ref, imports.%Main.a_ptr_ref [concrete = imports.%a_ptr_ref.var] +// CHECK:STDOUT: %.loc7: %ptr = bind_value %a_ptr_ref.ref // CHECK:STDOUT: assign file.%a_ptr.var, %.loc7 // CHECK:STDOUT: // CHECK:STDOUT: } diff --git a/toolchain/check/testdata/interop/cpp/function/arithmetic_types_bridged.carbon b/toolchain/check/testdata/interop/cpp/function/arithmetic_types_bridged.carbon index 392c6cfbc1b1..fbad1fb163e7 100644 --- a/toolchain/check/testdata/interop/cpp/function/arithmetic_types_bridged.carbon +++ b/toolchain/check/testdata/interop/cpp/function/arithmetic_types_bridged.carbon @@ -337,7 +337,7 @@ fn F() { auto foo(const short* _Nonnull a) -> void; -// --- fail_todo_import_const_short_pointer_param.carbon +// --- import_const_short_pointer_param.carbon library "[[@TEST_NAME]]"; @@ -346,14 +346,6 @@ import Cpp library "const_short_pointer_param.h"; fn F() { var a: i16 = 1; //@dump-sem-ir-begin - // CHECK:STDERR: fail_todo_import_const_short_pointer_param.carbon:[[@LINE+8]]:11: error: cannot implicitly convert expression of type `i16*` to `const i16*` [ConversionFailure] - // CHECK:STDERR: Cpp.foo(&a); - // CHECK:STDERR: ^~ - // CHECK:STDERR: fail_todo_import_const_short_pointer_param.carbon:[[@LINE+5]]:11: note: type `i16*` does not implement interface `Core.ImplicitAs(const i16*)` [MissingImplInMemberAccessNote] - // CHECK:STDERR: Cpp.foo(&a); - // CHECK:STDERR: ^~ - // CHECK:STDERR: fail_todo_import_const_short_pointer_param.carbon: note: initializing function parameter [InCallToFunctionParam] - // CHECK:STDERR: Cpp.foo(&a); //@dump-sem-ir-end } @@ -1355,7 +1347,7 @@ fn F() { // CHECK:STDOUT: // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_todo_import_const_short_pointer_param.carbon +// CHECK:STDOUT: --- import_const_short_pointer_param.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] @@ -1386,9 +1378,10 @@ fn F() { // CHECK:STDOUT: %Cpp.ref: = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp] // CHECK:STDOUT: %foo.ref: %foo.type = name_ref foo, imports.%foo.decl [concrete = constants.%foo] // CHECK:STDOUT: %a.ref: ref %i16 = name_ref a, %a -// CHECK:STDOUT: %addr.loc17: %ptr.251 = addr_of %a.ref -// CHECK:STDOUT: %.loc17: %ptr.758 = converted %addr.loc17, [concrete = ] -// CHECK:STDOUT: %foo.call: init %empty_tuple.type = call %foo.ref() +// CHECK:STDOUT: %addr.loc9: %ptr.251 = addr_of %a.ref +// CHECK:STDOUT: %.loc9_11.1: %ptr.758 = as_compatible %addr.loc9 +// CHECK:STDOUT: %.loc9_11.2: %ptr.758 = converted %addr.loc9, %.loc9_11.1 +// CHECK:STDOUT: %foo.call: init %empty_tuple.type = call %foo.ref(%.loc9_11.2) // CHECK:STDOUT: // CHECK:STDOUT: } // CHECK:STDOUT: diff --git a/toolchain/check/testdata/interop/cpp/function/arithmetic_types_direct.carbon b/toolchain/check/testdata/interop/cpp/function/arithmetic_types_direct.carbon index 9b5ed0964fce..cfd1570a3997 100644 --- a/toolchain/check/testdata/interop/cpp/function/arithmetic_types_direct.carbon +++ b/toolchain/check/testdata/interop/cpp/function/arithmetic_types_direct.carbon @@ -364,7 +364,7 @@ fn F() { auto foo(const int* _Nonnull a) -> int; -// --- fail_todo_import_const_int_pointer_param.carbon +// --- import_const_int_pointer_param.carbon library "[[@TEST_NAME]]"; @@ -373,14 +373,6 @@ import Cpp library "const_int_pointer_param.h"; fn F() { var a : i32 = 1; //@dump-sem-ir-begin - // CHECK:STDERR: fail_todo_import_const_int_pointer_param.carbon:[[@LINE+8]]:11: error: cannot implicitly convert expression of type `i32*` to `const i32*` [ConversionFailure] - // CHECK:STDERR: Cpp.foo(&a); - // CHECK:STDERR: ^~ - // CHECK:STDERR: fail_todo_import_const_int_pointer_param.carbon:[[@LINE+5]]:11: note: type `i32*` does not implement interface `Core.ImplicitAs(const i32*)` [MissingImplInMemberAccessNote] - // CHECK:STDERR: Cpp.foo(&a); - // CHECK:STDERR: ^~ - // CHECK:STDERR: fail_todo_import_const_int_pointer_param.carbon: note: initializing function parameter [InCallToFunctionParam] - // CHECK:STDERR: Cpp.foo(&a); //@dump-sem-ir-end } @@ -1138,7 +1130,7 @@ fn F() { // CHECK:STDOUT: // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: --- fail_todo_import_const_int_pointer_param.carbon +// CHECK:STDOUT: --- import_const_int_pointer_param.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete] @@ -1168,9 +1160,10 @@ fn F() { // CHECK:STDOUT: %Cpp.ref: = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp] // CHECK:STDOUT: %foo.ref: %foo.type = name_ref foo, imports.%foo.decl [concrete = constants.%foo] // CHECK:STDOUT: %a.ref: ref %i32 = name_ref a, %a -// CHECK:STDOUT: %addr.loc17: %ptr.235 = addr_of %a.ref -// CHECK:STDOUT: %.loc17: %ptr.36b = converted %addr.loc17, [concrete = ] -// CHECK:STDOUT: %foo.call: init %i32 = call %foo.ref() +// CHECK:STDOUT: %addr.loc9: %ptr.235 = addr_of %a.ref +// CHECK:STDOUT: %.loc9_11.1: %ptr.36b = as_compatible %addr.loc9 +// CHECK:STDOUT: %.loc9_11.2: %ptr.36b = converted %addr.loc9, %.loc9_11.1 +// CHECK:STDOUT: %foo.call: init %i32 = call %foo.ref(%.loc9_11.2) // CHECK:STDOUT: // CHECK:STDOUT: } // CHECK:STDOUT: diff --git a/toolchain/check/testdata/pointer/convert_qualifiers.carbon b/toolchain/check/testdata/pointer/convert_qualifiers.carbon new file mode 100644 index 000000000000..fa50fff4b9f3 --- /dev/null +++ b/toolchain/check/testdata/pointer/convert_qualifiers.carbon @@ -0,0 +1,214 @@ +// 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/int.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/pointer/convert_qualifiers.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/pointer/convert_qualifiers.carbon + +// --- same_or_added_qualifiers.carbon + +library "[[@TEST_NAME]]"; + +class X {} + +fn TakeConst(p: const X*); +fn TakeNonConst(p: X*); + +fn NonConst(p: X*) { + //@dump-sem-ir-begin + TakeNonConst(p); + TakeConst(p); + //@dump-sem-ir-end +} +fn Const(p: const X*) { + //@dump-sem-ir-begin + TakeConst(p); + //@dump-sem-ir-end +} + +// Same thing, but the pointee type happens to be a pointer. +fn TakeConstConst(p: const (const X*)*); +fn NonConstConst(p: const X**) { + //@dump-sem-ir-begin + TakeConstConst(p); + //@dump-sem-ir-end +} + +// --- fail_removed_qualifiers.carbon + +library "[[@TEST_NAME]]"; + +class X {} + +fn TakeNonConst(p: X*); + +// CHECK:STDERR: fail_removed_qualifiers.carbon:[[@LINE+10]]:38: error: cannot implicitly convert expression of type `const X*` to `X*` [ConversionFailure] +// CHECK:STDERR: fn Const(p: const X*) { TakeNonConst(p); } +// CHECK:STDERR: ^ +// CHECK:STDERR: fail_removed_qualifiers.carbon:[[@LINE+7]]:38: note: type `const X*` does not implement interface `Core.ImplicitAs(X*)` [MissingImplInMemberAccessNote] +// CHECK:STDERR: fn Const(p: const X*) { TakeNonConst(p); } +// CHECK:STDERR: ^ +// CHECK:STDERR: fail_removed_qualifiers.carbon:[[@LINE-8]]:17: note: initializing function parameter [InCallToFunctionParam] +// CHECK:STDERR: fn TakeNonConst(p: X*); +// CHECK:STDERR: ^~~~~ +// CHECK:STDERR: +fn Const(p: const X*) { TakeNonConst(p); } + +// --- fail_todo_nested_qualifiers.carbon + +library "[[@TEST_NAME]]"; + +class X {} + +fn TakeConstConst(p: const (const X*)*); + +// TODO: We should allow these conversions. + +fn NonConstNonConst(p: X**) { + //@dump-sem-ir-begin + // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+10]]:18: error: cannot implicitly convert expression of type `X**` to `const (const X*)*` [ConversionFailure] + // CHECK:STDERR: TakeConstConst(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+7]]:18: note: type `X**` does not implement interface `Core.ImplicitAs(const (const X*)*)` [MissingImplInMemberAccessNote] + // CHECK:STDERR: TakeConstConst(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE-12]]:19: note: initializing function parameter [InCallToFunctionParam] + // CHECK:STDERR: fn TakeConstConst(p: const (const X*)*); + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: + TakeConstConst(p); + //@dump-sem-ir-end +} + +fn ConstNonConst(p: const (X*)*) { + //@dump-sem-ir-begin + // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+10]]:18: error: cannot implicitly convert expression of type `const (X*)*` to `const (const X*)*` [ConversionFailure] + // CHECK:STDERR: TakeConstConst(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+7]]:18: note: type `const (X*)*` does not implement interface `Core.ImplicitAs(const (const X*)*)` [MissingImplInMemberAccessNote] + // CHECK:STDERR: TakeConstConst(p); + // CHECK:STDERR: ^ + // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE-28]]:19: note: initializing function parameter [InCallToFunctionParam] + // CHECK:STDERR: fn TakeConstConst(p: const (const X*)*); + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: + TakeConstConst(p); + //@dump-sem-ir-end +} + +// --- fail_nested_added_qualifiers.carbon + +library "[[@TEST_NAME]]"; + +class X {} + +fn TakeNonConstConst(p: (const X*)*); + +// CHECK:STDERR: fail_nested_added_qualifiers.carbon:[[@LINE+10]]:49: error: cannot implicitly convert expression of type `X**` to `const X**` [ConversionFailure] +// CHECK:STDERR: fn NonConstNonConst(p: X**) { TakeNonConstConst(p); } +// CHECK:STDERR: ^ +// CHECK:STDERR: fail_nested_added_qualifiers.carbon:[[@LINE+7]]:49: note: type `X**` does not implement interface `Core.ImplicitAs(const X**)` [MissingImplInMemberAccessNote] +// CHECK:STDERR: fn NonConstNonConst(p: X**) { TakeNonConstConst(p); } +// CHECK:STDERR: ^ +// CHECK:STDERR: fail_nested_added_qualifiers.carbon:[[@LINE-8]]:22: note: initializing function parameter [InCallToFunctionParam] +// CHECK:STDERR: fn TakeNonConstConst(p: (const X*)*); +// CHECK:STDERR: ^~~~~~~~~~~~~~ +// CHECK:STDERR: +fn NonConstNonConst(p: X**) { TakeNonConstConst(p); } + +// CHECK:STDOUT: --- same_or_added_qualifiers.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %X: type = class_type @X [concrete] +// CHECK:STDOUT: %ptr.d17: type = ptr_type %X [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %const.dde: type = const_type %X [concrete] +// CHECK:STDOUT: %ptr.cbd: type = ptr_type %const.dde [concrete] +// CHECK:STDOUT: %TakeConst.type: type = fn_type @TakeConst [concrete] +// CHECK:STDOUT: %TakeConst: %TakeConst.type = struct_value () [concrete] +// CHECK:STDOUT: %TakeNonConst.type: type = fn_type @TakeNonConst [concrete] +// CHECK:STDOUT: %TakeNonConst: %TakeNonConst.type = struct_value () [concrete] +// CHECK:STDOUT: %const.5df: type = const_type %ptr.cbd [concrete] +// CHECK:STDOUT: %ptr.20a: type = ptr_type %const.5df [concrete] +// CHECK:STDOUT: %TakeConstConst.type: type = fn_type @TakeConstConst [concrete] +// CHECK:STDOUT: %TakeConstConst: %TakeConstConst.type = struct_value () [concrete] +// CHECK:STDOUT: %ptr.dd0: type = ptr_type %ptr.cbd [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @NonConst(%p.param: %ptr.d17) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeNonConst.ref: %TakeNonConst.type = name_ref TakeNonConst, file.%TakeNonConst.decl [concrete = constants.%TakeNonConst] +// CHECK:STDOUT: %p.ref.loc11: %ptr.d17 = name_ref p, %p +// CHECK:STDOUT: %TakeNonConst.call: init %empty_tuple.type = call %TakeNonConst.ref(%p.ref.loc11) +// CHECK:STDOUT: %TakeConst.ref: %TakeConst.type = name_ref TakeConst, file.%TakeConst.decl [concrete = constants.%TakeConst] +// CHECK:STDOUT: %p.ref.loc12: %ptr.d17 = name_ref p, %p +// CHECK:STDOUT: %.loc12_13.1: %ptr.cbd = as_compatible %p.ref.loc12 +// CHECK:STDOUT: %.loc12_13.2: %ptr.cbd = converted %p.ref.loc12, %.loc12_13.1 +// CHECK:STDOUT: %TakeConst.call: init %empty_tuple.type = call %TakeConst.ref(%.loc12_13.2) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Const(%p.param: %ptr.cbd) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConst.ref: %TakeConst.type = name_ref TakeConst, file.%TakeConst.decl [concrete = constants.%TakeConst] +// CHECK:STDOUT: %p.ref: %ptr.cbd = name_ref p, %p +// CHECK:STDOUT: %TakeConst.call: init %empty_tuple.type = call %TakeConst.ref(%p.ref) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @NonConstConst(%p.param: %ptr.dd0) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConstConst.ref: %TakeConstConst.type = name_ref TakeConstConst, file.%TakeConstConst.decl [concrete = constants.%TakeConstConst] +// CHECK:STDOUT: %p.ref: %ptr.dd0 = name_ref p, %p +// CHECK:STDOUT: %.loc25_18.1: %ptr.20a = as_compatible %p.ref +// CHECK:STDOUT: %.loc25_18.2: %ptr.20a = converted %p.ref, %.loc25_18.1 +// CHECK:STDOUT: %TakeConstConst.call: init %empty_tuple.type = call %TakeConstConst.ref(%.loc25_18.2) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_todo_nested_qualifiers.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %X: type = class_type @X [concrete] +// CHECK:STDOUT: %ptr.d17: type = ptr_type %X [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %const.dde: type = const_type %X [concrete] +// CHECK:STDOUT: %ptr.cbd: type = ptr_type %const.dde [concrete] +// CHECK:STDOUT: %const.5df: type = const_type %ptr.cbd [concrete] +// CHECK:STDOUT: %ptr.20a: type = ptr_type %const.5df [concrete] +// CHECK:STDOUT: %TakeConstConst.type: type = fn_type @TakeConstConst [concrete] +// CHECK:STDOUT: %TakeConstConst: %TakeConstConst.type = struct_value () [concrete] +// CHECK:STDOUT: %ptr.b1b: type = ptr_type %ptr.d17 [concrete] +// CHECK:STDOUT: %const.2bc: type = const_type %ptr.d17 [concrete] +// CHECK:STDOUT: %ptr.297: type = ptr_type %const.2bc [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @NonConstNonConst(%p.param: %ptr.b1b) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConstConst.ref: %TakeConstConst.type = name_ref TakeConstConst, file.%TakeConstConst.decl [concrete = constants.%TakeConstConst] +// CHECK:STDOUT: %p.ref: %ptr.b1b = name_ref p, %p +// CHECK:STDOUT: %.loc22: %ptr.20a = converted %p.ref, [concrete = ] +// CHECK:STDOUT: %TakeConstConst.call: init %empty_tuple.type = call %TakeConstConst.ref() +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @ConstNonConst(%p.param: %ptr.297) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %TakeConstConst.ref: %TakeConstConst.type = name_ref TakeConstConst, file.%TakeConstConst.decl [concrete = constants.%TakeConstConst] +// CHECK:STDOUT: %p.ref: %ptr.297 = name_ref p, %p +// CHECK:STDOUT: %.loc38: %ptr.20a = converted %p.ref, [concrete = ] +// CHECK:STDOUT: %TakeConstConst.call: init %empty_tuple.type = call %TakeConstConst.ref() +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/type.cpp b/toolchain/check/type.cpp index 1e29844080d6..759f0e1b744b 100644 --- a/toolchain/check/type.cpp +++ b/toolchain/check/type.cpp @@ -136,6 +136,16 @@ auto GetConstType(Context& context, SemIR::TypeInstId inner_type_id) return GetTypeImpl(context, inner_type_id); } +auto GetQualifiedType(Context& context, SemIR::TypeId type_id, + SemIR::TypeQualifiers quals) -> SemIR::TypeId { + if ((quals & SemIR::TypeQualifiers::Const) != SemIR::TypeQualifiers::None) { + type_id = GetConstType(context, context.types().GetInstId(type_id)); + quals &= ~SemIR::TypeQualifiers::Const; + } + CARBON_CHECK(quals == SemIR::TypeQualifiers::None); + return type_id; +} + auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id) -> SemIR::TypeId { CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id)); diff --git a/toolchain/check/type.h b/toolchain/check/type.h index 496cb2d07065..705d5d23350f 100644 --- a/toolchain/check/type.h +++ b/toolchain/check/type.h @@ -39,6 +39,10 @@ auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id) auto GetConstType(Context& context, SemIR::TypeInstId inner_type_id) -> SemIR::TypeId; +// Gets a qualified version of a type. +auto GetQualifiedType(Context& context, SemIR::TypeId type_id, + SemIR::TypeQualifiers quals) -> SemIR::TypeId; + // Gets a class type. auto GetClassType(Context& context, SemIR::ClassId class_id, SemIR::SpecificId specific_id) -> SemIR::TypeId; diff --git a/toolchain/sem_ir/type.cpp b/toolchain/sem_ir/type.cpp index 8b903a2ab3d6..fed4dccf256f 100644 --- a/toolchain/sem_ir/type.cpp +++ b/toolchain/sem_ir/type.cpp @@ -93,12 +93,14 @@ auto TypeStore::GetTransitiveAdaptedType(TypeId type_id) const -> TypeId { return type_id; } -auto TypeStore::GetUnqualifiedType(TypeId type_id) const -> TypeId { +auto TypeStore::GetUnqualifiedTypeAndQualifiers(TypeId type_id) const + -> std::pair { if (auto const_type = TryGetAs(type_id)) { - return file_->types().GetTypeIdForTypeInstId(const_type->inner_id); + return {file_->types().GetTypeIdForTypeInstId(const_type->inner_id), + TypeQualifiers::Const}; } // TODO: Look through PartialType when this is reachable/testable - return type_id; + return {type_id, TypeQualifiers::None}; } auto TypeStore::TryGetIntTypeInfo(TypeId int_type_id) const diff --git a/toolchain/sem_ir/type.h b/toolchain/sem_ir/type.h index 2a15769b3df6..a05477dc0682 100644 --- a/toolchain/sem_ir/type.h +++ b/toolchain/sem_ir/type.h @@ -5,6 +5,7 @@ #ifndef CARBON_TOOLCHAIN_SEM_IR_TYPE_H_ #define CARBON_TOOLCHAIN_SEM_IR_TYPE_H_ +#include "llvm/ADT/BitmaskEnum.h" #include "llvm/ADT/STLExtras.h" #include "toolchain/base/shared_value_stores.h" #include "toolchain/sem_ir/constant.h" @@ -14,6 +15,17 @@ namespace Carbon::SemIR { +LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); + +// A bitmask of type qualifiers. +enum class TypeQualifiers { + None = 0, + Const = 1 << 0, + // TODO: Partial + + LLVM_MARK_AS_BITMASK_ENUM(Const) +}; + // Provides a ValueStore wrapper with an API specific to types. class TypeStore : public Yaml::Printable { public: @@ -160,8 +172,15 @@ class TypeStore : public Yaml::Printable { return complete_type_info_.Contains(type_id); } - // Removes any top-level `const` qualifiers from a type. - auto GetUnqualifiedType(TypeId type_id) const -> TypeId; + // Removes any top-level qualifiers from a type. + auto GetUnqualifiedType(TypeId type_id) const -> TypeId { + return GetUnqualifiedTypeAndQualifiers(type_id).first; + } + + // Removes any top-level qualifiers from a type and returns the unqualified + // type and qualifiers. + auto GetUnqualifiedTypeAndQualifiers(TypeId type_id) const + -> std::pair; // Determines whether the given type is a signed integer type. This includes // the case where the type is `Core.IntLiteral` or a class type whose object