diff --git a/toolchain/check/eval.cpp b/toolchain/check/eval.cpp index e31cd7197f33..c6537f880f87 100644 --- a/toolchain/check/eval.cpp +++ b/toolchain/check/eval.cpp @@ -1335,13 +1335,9 @@ auto TryEvalInstInContext(EvalContext& eval_context, SemIR::InstId inst_id, const auto& specific = eval_context.generic_instances().Get(eval_context.specific_id); auto args = eval_context.inst_blocks().Get(specific.args_id); - if (static_cast(bind_name.bind_index.index) >= args.size()) { - // TODO: For now we don't provide a mapping for the `Self` type in an - // interface, and fall back to the canonical constant type. - CARBON_CHECK(bind_name.name_id == SemIR::NameId::SelfType) - << "Use of binding " << bind_name.bind_index - << " with no corresponding value."; - } else { + // Bindings past the ones with known arguments can appear as local + // bindings of entities declared within this generic. + if (static_cast(bind_name.bind_index.index) < args.size()) { return eval_context.context.constant_values().Get( args[bind_name.bind_index.index]); } diff --git a/toolchain/check/testdata/class/generic/call.carbon b/toolchain/check/testdata/class/generic/call.carbon index b82071240cc0..71adafceaab0 100644 --- a/toolchain/check/testdata/class/generic/call.carbon +++ b/toolchain/check/testdata/class/generic/call.carbon @@ -63,6 +63,25 @@ class Class(T:! type, N:! i32) {} // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var a: Class(5, i32*); +// --- call_in_nested_return_type.carbon + +class Outer(T:! type) { + class Inner(U:! type) { + fn A() -> Outer(T) { + return {}; + } + fn B() -> Outer(U) { + return {}; + } + fn C() -> Inner(T) { + return {}; + } + fn D() -> Inner(U) { + return {}; + } + } +} + // CHECK:STDOUT: --- call.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { @@ -403,3 +422,248 @@ var a: Class(5, i32*); // CHECK:STDOUT: %N => constants.%N // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: --- call_in_nested_return_type.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic] +// CHECK:STDOUT: %Outer.type: type = generic_class_type @Outer [template] +// CHECK:STDOUT: %.1: type = tuple_type () [template] +// CHECK:STDOUT: %Outer.1: %Outer.type = struct_value () [template] +// CHECK:STDOUT: %Outer.2: type = class_type @Outer, @Outer(%T) [symbolic] +// CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic] +// CHECK:STDOUT: %Inner.type: type = generic_class_type @Inner [template] +// CHECK:STDOUT: %Inner.1: %Inner.type = struct_value () [template] +// CHECK:STDOUT: %Inner.2: type = class_type @Inner, @Inner(%T, %U) [symbolic] +// CHECK:STDOUT: %A.type: type = fn_type @A [template] +// CHECK:STDOUT: %A: %A.type = struct_value () [template] +// CHECK:STDOUT: %Outer.3: type = class_type @Outer, @Outer(%U) [symbolic] +// CHECK:STDOUT: %B.type: type = fn_type @B [template] +// CHECK:STDOUT: %B: %B.type = struct_value () [template] +// CHECK:STDOUT: %Inner.3: type = class_type @Inner, @Inner(%T) [symbolic] +// CHECK:STDOUT: %C.type: type = fn_type @C [template] +// CHECK:STDOUT: %C: %C.type = struct_value () [template] +// CHECK:STDOUT: %Inner.4: type = class_type @Inner, @Inner(%U) [symbolic] +// CHECK:STDOUT: %D.type: type = fn_type @D [template] +// CHECK:STDOUT: %D: %D.type = struct_value () [template] +// CHECK:STDOUT: %.2: type = struct_type {} [template] +// CHECK:STDOUT: %.3: type = ptr_type %.2 [template] +// CHECK:STDOUT: %struct.1: %Outer.2 = struct_value () [symbolic] +// CHECK:STDOUT: %struct.2: %Outer.3 = struct_value () [symbolic] +// CHECK:STDOUT: %struct.3: %Inner.3 = struct_value () [symbolic] +// CHECK:STDOUT: %struct.4: %Inner.4 = struct_value () [symbolic] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .Outer = %Outer.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core.import = import Core +// CHECK:STDOUT: %Core: = namespace %Core.import, [template] { +// CHECK:STDOUT: import Core//prelude +// CHECK:STDOUT: import Core//prelude/operators +// CHECK:STDOUT: import Core//prelude/types +// CHECK:STDOUT: import Core//prelude/operators/arithmetic +// CHECK:STDOUT: import Core//prelude/operators/bitwise +// CHECK:STDOUT: import Core//prelude/operators/comparison +// CHECK:STDOUT: import Core//prelude/types/bool +// CHECK:STDOUT: } +// CHECK:STDOUT: %Outer.decl: %Outer.type = class_decl @Outer [template = constants.%Outer.1] { +// CHECK:STDOUT: %T.loc2_13.1: type = param T +// CHECK:STDOUT: %T.loc2_13.2: type = bind_symbolic_name T 0, %T.loc2_13.1 [symbolic = @Outer.%T (constants.%T)] +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic class @Outer(file.%T.loc2_13.2: type) { +// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: class { +// CHECK:STDOUT: %Inner.decl: %Inner.type = class_decl @Inner [template = constants.%Inner.1] { +// CHECK:STDOUT: %U.loc3_15.1: type = param U +// CHECK:STDOUT: %U.loc3_15.2: type = bind_symbolic_name U 1, %U.loc3_15.1 [symbolic = @Inner.%U (constants.%U)] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%Outer.2 +// CHECK:STDOUT: .Inner = %Inner.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic class @Inner(file.%T.loc2_13.2: type, @Outer.%U.loc3_15.2: type) { +// CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic = %U (constants.%U)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: class { +// CHECK:STDOUT: %A.decl: %A.type = fn_decl @A [template = constants.%A] { +// CHECK:STDOUT: %Outer.ref.loc4: %Outer.type = name_ref Outer, file.%Outer.decl [template = constants.%Outer.1] +// CHECK:STDOUT: %T.ref.loc4: type = name_ref T, file.%T.loc2_13.2 [symbolic = @A.%T (constants.%T)] +// CHECK:STDOUT: %.loc4_20: init type = call %Outer.ref.loc4(%T.ref.loc4) [symbolic = @A.%Outer (constants.%Outer.2)] +// CHECK:STDOUT: %.loc4_22.1: type = value_of_initializer %.loc4_20 [symbolic = @A.%Outer (constants.%Outer.2)] +// CHECK:STDOUT: %.loc4_22.2: type = converted %.loc4_20, %.loc4_22.1 [symbolic = @A.%Outer (constants.%Outer.2)] +// CHECK:STDOUT: %return.var.loc4: ref %Outer.2 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [template = constants.%B] { +// CHECK:STDOUT: %Outer.ref.loc7: %Outer.type = name_ref Outer, file.%Outer.decl [template = constants.%Outer.1] +// CHECK:STDOUT: %U.ref.loc7: type = name_ref U, @Outer.%U.loc3_15.2 [symbolic = @B.%U (constants.%U)] +// CHECK:STDOUT: %.loc7_20: init type = call %Outer.ref.loc7(%U.ref.loc7) [symbolic = @B.%Outer (constants.%Outer.3)] +// CHECK:STDOUT: %.loc7_22.1: type = value_of_initializer %.loc7_20 [symbolic = @B.%Outer (constants.%Outer.3)] +// CHECK:STDOUT: %.loc7_22.2: type = converted %.loc7_20, %.loc7_22.1 [symbolic = @B.%Outer (constants.%Outer.3)] +// CHECK:STDOUT: %return.var.loc7: ref %Outer.3 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %C.decl: %C.type = fn_decl @C [template = constants.%C] { +// CHECK:STDOUT: %Inner.ref.loc10: %Inner.type = name_ref Inner, @Outer.%Inner.decl [template = constants.%Inner.1] +// CHECK:STDOUT: %T.ref.loc10: type = name_ref T, file.%T.loc2_13.2 [symbolic = @C.%T (constants.%T)] +// CHECK:STDOUT: %.loc10_20: init type = call %Inner.ref.loc10(%T.ref.loc10) [symbolic = @C.%Inner (constants.%Inner.3)] +// CHECK:STDOUT: %.loc10_22.1: type = value_of_initializer %.loc10_20 [symbolic = @C.%Inner (constants.%Inner.3)] +// CHECK:STDOUT: %.loc10_22.2: type = converted %.loc10_20, %.loc10_22.1 [symbolic = @C.%Inner (constants.%Inner.3)] +// CHECK:STDOUT: %return.var.loc10: ref %Inner.3 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: %D.decl: %D.type = fn_decl @D [template = constants.%D] { +// CHECK:STDOUT: %Inner.ref.loc13: %Inner.type = name_ref Inner, @Outer.%Inner.decl [template = constants.%Inner.1] +// CHECK:STDOUT: %U.ref.loc13: type = name_ref U, @Outer.%U.loc3_15.2 [symbolic = @D.%U (constants.%U)] +// CHECK:STDOUT: %.loc13_20: init type = call %Inner.ref.loc13(%U.ref.loc13) [symbolic = @D.%Inner (constants.%Inner.4)] +// CHECK:STDOUT: %.loc13_22.1: type = value_of_initializer %.loc13_20 [symbolic = @D.%Inner (constants.%Inner.4)] +// CHECK:STDOUT: %.loc13_22.2: type = converted %.loc13_20, %.loc13_22.1 [symbolic = @D.%Inner (constants.%Inner.4)] +// CHECK:STDOUT: %return.var.loc13: ref %Inner.4 = var +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%Inner.2 +// CHECK:STDOUT: .A = %A.decl +// CHECK:STDOUT: .B = %B.decl +// CHECK:STDOUT: .C = %C.decl +// CHECK:STDOUT: .D = %D.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic fn @A(file.%T.loc2_13.2: type, @Outer.%U.loc3_15.2: type) { +// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)] +// CHECK:STDOUT: %Outer: type = class_type @Outer, @Outer(%T) [symbolic = %Outer (constants.%Outer.2)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: %struct: @A.%Outer (%Outer.2) = struct_value () [symbolic = %struct (constants.%struct.1)] +// CHECK:STDOUT: +// CHECK:STDOUT: fn() -> @Inner.%return.var.loc4: %Outer.2 { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %.loc5_15.1: %.2 = struct_literal () +// CHECK:STDOUT: %.loc5_15.2: init @A.%Outer (%Outer.2) = class_init (), @Inner.%return.var.loc4 [symbolic = %struct (constants.%struct.1)] +// CHECK:STDOUT: %.loc5_16: init @A.%Outer (%Outer.2) = converted %.loc5_15.1, %.loc5_15.2 [symbolic = %struct (constants.%struct.1)] +// CHECK:STDOUT: return %.loc5_16 to @Inner.%return.var.loc4 +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic fn @B(file.%T.loc2_13.2: type, @Outer.%U.loc3_15.2: type) { +// CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic = %U (constants.%U)] +// CHECK:STDOUT: %Outer: type = class_type @Outer, @Outer(%U) [symbolic = %Outer (constants.%Outer.3)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: %struct: @B.%Outer (%Outer.3) = struct_value () [symbolic = %struct (constants.%struct.2)] +// CHECK:STDOUT: +// CHECK:STDOUT: fn() -> @Inner.%return.var.loc7: %Outer.3 { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %.loc8_15.1: %.2 = struct_literal () +// CHECK:STDOUT: %.loc8_15.2: init @B.%Outer (%Outer.3) = class_init (), @Inner.%return.var.loc7 [symbolic = %struct (constants.%struct.2)] +// CHECK:STDOUT: %.loc8_16: init @B.%Outer (%Outer.3) = converted %.loc8_15.1, %.loc8_15.2 [symbolic = %struct (constants.%struct.2)] +// CHECK:STDOUT: return %.loc8_16 to @Inner.%return.var.loc7 +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic fn @C(file.%T.loc2_13.2: type, @Outer.%U.loc3_15.2: type) { +// CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)] +// CHECK:STDOUT: %Inner: type = class_type @Inner, @Inner(%T) [symbolic = %Inner (constants.%Inner.3)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: %struct: @C.%Inner (%Inner.3) = struct_value () [symbolic = %struct (constants.%struct.3)] +// CHECK:STDOUT: +// CHECK:STDOUT: fn() -> @Inner.%return.var.loc10: %Inner.3 { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %.loc11_15.1: %.2 = struct_literal () +// CHECK:STDOUT: %.loc11_15.2: init @C.%Inner (%Inner.3) = class_init (), @Inner.%return.var.loc10 [symbolic = %struct (constants.%struct.3)] +// CHECK:STDOUT: %.loc11_16: init @C.%Inner (%Inner.3) = converted %.loc11_15.1, %.loc11_15.2 [symbolic = %struct (constants.%struct.3)] +// CHECK:STDOUT: return %.loc11_16 to @Inner.%return.var.loc10 +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic fn @D(file.%T.loc2_13.2: type, @Outer.%U.loc3_15.2: type) { +// CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic = %U (constants.%U)] +// CHECK:STDOUT: %Inner: type = class_type @Inner, @Inner(%U) [symbolic = %Inner (constants.%Inner.4)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: %struct: @D.%Inner (%Inner.4) = struct_value () [symbolic = %struct (constants.%struct.4)] +// CHECK:STDOUT: +// CHECK:STDOUT: fn() -> @Inner.%return.var.loc13: %Inner.4 { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %.loc14_15.1: %.2 = struct_literal () +// CHECK:STDOUT: %.loc14_15.2: init @D.%Inner (%Inner.4) = class_init (), @Inner.%return.var.loc13 [symbolic = %struct (constants.%struct.4)] +// CHECK:STDOUT: %.loc14_16: init @D.%Inner (%Inner.4) = converted %.loc14_15.1, %.loc14_15.2 [symbolic = %struct (constants.%struct.4)] +// CHECK:STDOUT: return %.loc14_16 to @Inner.%return.var.loc13 +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Outer(constants.%T) { +// CHECK:STDOUT: %T => constants.%T +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Inner(constants.%T, constants.%U) { +// CHECK:STDOUT: %U => constants.%U +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Outer(@A.%T) { +// CHECK:STDOUT: %T => constants.%T +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @A(constants.%T, constants.%U) { +// CHECK:STDOUT: %T => constants.%T +// CHECK:STDOUT: %Outer => constants.%Outer.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Outer(constants.%U) { +// CHECK:STDOUT: %T => constants.%U +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Outer(@B.%U) { +// CHECK:STDOUT: %T => constants.%U +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @B(constants.%T, constants.%U) { +// CHECK:STDOUT: %U => constants.%U +// CHECK:STDOUT: %Outer => constants.%Outer.3 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Inner(constants.%T) { +// CHECK:STDOUT: %U => constants.%U +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Inner(@C.%T) { +// CHECK:STDOUT: %U => constants.%U +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @C(constants.%T, constants.%U) { +// CHECK:STDOUT: %T => constants.%T +// CHECK:STDOUT: %Inner => constants.%Inner.3 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Inner(constants.%U) { +// CHECK:STDOUT: %U => constants.%U +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @Inner(@D.%U) { +// CHECK:STDOUT: %U => constants.%U +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @D(constants.%T, constants.%U) { +// CHECK:STDOUT: %U => constants.%U +// CHECK:STDOUT: %Inner => constants.%Inner.4 +// CHECK:STDOUT: } +// CHECK:STDOUT: