diff --git a/toolchain/check/decl_name_stack.cpp b/toolchain/check/decl_name_stack.cpp index 5ed6aee3ebd9..8dcfe8055114 100644 --- a/toolchain/check/decl_name_stack.cpp +++ b/toolchain/check/decl_name_stack.cpp @@ -213,6 +213,15 @@ static auto PushNameQualifierScope(Context& context, context.scope_stack().Push(scope_inst_id, scope_id, instance_id, has_error); + // An interface also introduces its 'Self' parameter into scope, despite it + // not being redeclared as part of the qualifier. + if (auto interface_decl = + context.insts().TryGetAs(scope_inst_id)) { + auto& interface = context.interfaces().Get(interface_decl->interface_id); + context.scope_stack().AddCompileTimeBinding(); + context.scope_stack().PushCompileTimeBinding(interface.self_param_id); + } + // Enter a parameter scope in case the qualified name itself has parameters. context.scope_stack().Push(); } diff --git a/toolchain/check/testdata/interface/fail_todo_define_default_fn_out_of_line.carbon b/toolchain/check/testdata/interface/fail_todo_define_default_fn_out_of_line.carbon index 191084cae188..45fa1976a8d6 100644 --- a/toolchain/check/testdata/interface/fail_todo_define_default_fn_out_of_line.carbon +++ b/toolchain/check/testdata/interface/fail_todo_define_default_fn_out_of_line.carbon @@ -8,6 +8,8 @@ // TIP: To dump output, run: // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/fail_todo_define_default_fn_out_of_line.carbon +// --- fail_todo_define_default_fn_out_of_line.carbon + interface Interface { // CHECK:STDERR: fail_todo_define_default_fn_out_of_line.carbon:[[@LINE+4]]:3: ERROR: Semantics TODO: `interface modifier`. // CHECK:STDERR: default fn F(); @@ -39,6 +41,28 @@ fn Interface.F() {} // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ fn Interface.G(a: i32, b: i32) -> i32 = "int.sadd"; +// --- dependent_return_type.carbon + +library "dependent_return_type.carbon"; + +// Ensure that a dependent return type matches between an interface definition +// and an out-of-line member. This requires `Self` to be properly reintroduced +// into the list of generic parameters. + +interface Interface { + // TODO: This should be + // default fn F[self: Self](U:! type, u: U) -> U; + // rather than a class member, but we don't currently accept that due to the + // TODOs above. + class C { + fn F[self: Self](U:! type, u: U) -> U; + } +} + +// TODO: This should be +// fn Interface.F[self: Self](U:! type, u: U) -> U { return u; } +fn Interface.C.F[self: Self](U:! type, u: U) -> U { return u; } + // CHECK:STDOUT: --- fail_todo_define_default_fn_out_of_line.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { @@ -82,21 +106,21 @@ fn Interface.G(a: i32, b: i32) -> i32 = "int.sadd"; // CHECK:STDOUT: } // CHECK:STDOUT: %Core.import = import Core // CHECK:STDOUT: %Interface.decl: type = interface_decl @Interface [template = constants.%.1] {} -// CHECK:STDOUT: %.decl.loc32: %.type.1 = fn_decl @.1 [template = constants.%.7] {} -// CHECK:STDOUT: %.decl.loc40: %.type.2 = fn_decl @.2 [template = constants.%.8] { -// CHECK:STDOUT: %int.make_type_32.loc40_19: init type = call constants.%Int32() [template = i32] -// CHECK:STDOUT: %.loc40_19.1: type = value_of_initializer %int.make_type_32.loc40_19 [template = i32] -// CHECK:STDOUT: %.loc40_19.2: type = converted %int.make_type_32.loc40_19, %.loc40_19.1 [template = i32] -// CHECK:STDOUT: %a.loc40_16.1: i32 = param a -// CHECK:STDOUT: @.2.%a: i32 = bind_name a, %a.loc40_16.1 -// CHECK:STDOUT: %int.make_type_32.loc40_27: init type = call constants.%Int32() [template = i32] -// CHECK:STDOUT: %.loc40_27.1: type = value_of_initializer %int.make_type_32.loc40_27 [template = i32] -// CHECK:STDOUT: %.loc40_27.2: type = converted %int.make_type_32.loc40_27, %.loc40_27.1 [template = i32] -// CHECK:STDOUT: %b.loc40_24.1: i32 = param b -// CHECK:STDOUT: @.2.%b: i32 = bind_name b, %b.loc40_24.1 -// CHECK:STDOUT: %int.make_type_32.loc40_35: init type = call constants.%Int32() [template = i32] -// CHECK:STDOUT: %.loc40_35.1: type = value_of_initializer %int.make_type_32.loc40_35 [template = i32] -// CHECK:STDOUT: %.loc40_35.2: type = converted %int.make_type_32.loc40_35, %.loc40_35.1 [template = i32] +// CHECK:STDOUT: %.decl.loc23: %.type.1 = fn_decl @.1 [template = constants.%.7] {} +// CHECK:STDOUT: %.decl.loc31: %.type.2 = fn_decl @.2 [template = constants.%.8] { +// CHECK:STDOUT: %int.make_type_32.loc31_19: init type = call constants.%Int32() [template = i32] +// CHECK:STDOUT: %.loc31_19.1: type = value_of_initializer %int.make_type_32.loc31_19 [template = i32] +// CHECK:STDOUT: %.loc31_19.2: type = converted %int.make_type_32.loc31_19, %.loc31_19.1 [template = i32] +// CHECK:STDOUT: %a.loc31_16.1: i32 = param a +// CHECK:STDOUT: @.2.%a: i32 = bind_name a, %a.loc31_16.1 +// CHECK:STDOUT: %int.make_type_32.loc31_27: init type = call constants.%Int32() [template = i32] +// CHECK:STDOUT: %.loc31_27.1: type = value_of_initializer %int.make_type_32.loc31_27 [template = i32] +// CHECK:STDOUT: %.loc31_27.2: type = converted %int.make_type_32.loc31_27, %.loc31_27.1 [template = i32] +// CHECK:STDOUT: %b.loc31_24.1: i32 = param b +// CHECK:STDOUT: @.2.%b: i32 = bind_name b, %b.loc31_24.1 +// CHECK:STDOUT: %int.make_type_32.loc31_35: init type = call constants.%Int32() [template = i32] +// CHECK:STDOUT: %.loc31_35.1: type = value_of_initializer %int.make_type_32.loc31_35 [template = i32] +// CHECK:STDOUT: %.loc31_35.2: type = converted %int.make_type_32.loc31_35, %.loc31_35.1 [template = i32] // CHECK:STDOUT: @.2.%return: ref i32 = var // CHECK:STDOUT: } // CHECK:STDOUT: } @@ -104,29 +128,29 @@ fn Interface.G(a: i32, b: i32) -> i32 = "int.sadd"; // CHECK:STDOUT: interface @Interface { // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self] // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} -// CHECK:STDOUT: %.loc16: %.3 = assoc_entity element0, %F.decl [template = constants.%.4] +// CHECK:STDOUT: %.loc7: %.3 = assoc_entity element0, %F.decl [template = constants.%.4] // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] { -// CHECK:STDOUT: %int.make_type_32.loc22_19: init type = call constants.%Int32() [template = i32] -// CHECK:STDOUT: %.loc22_19.1: type = value_of_initializer %int.make_type_32.loc22_19 [template = i32] -// CHECK:STDOUT: %.loc22_19.2: type = converted %int.make_type_32.loc22_19, %.loc22_19.1 [template = i32] -// CHECK:STDOUT: %a.loc22_16.1: i32 = param a -// CHECK:STDOUT: %a.loc22_16.2: i32 = bind_name a, %a.loc22_16.1 -// CHECK:STDOUT: %int.make_type_32.loc22_27: init type = call constants.%Int32() [template = i32] -// CHECK:STDOUT: %.loc22_27.1: type = value_of_initializer %int.make_type_32.loc22_27 [template = i32] -// CHECK:STDOUT: %.loc22_27.2: type = converted %int.make_type_32.loc22_27, %.loc22_27.1 [template = i32] -// CHECK:STDOUT: %b.loc22_24.1: i32 = param b -// CHECK:STDOUT: %b.loc22_24.2: i32 = bind_name b, %b.loc22_24.1 -// CHECK:STDOUT: %int.make_type_32.loc22_35: init type = call constants.%Int32() [template = i32] -// CHECK:STDOUT: %.loc22_35.1: type = value_of_initializer %int.make_type_32.loc22_35 [template = i32] -// CHECK:STDOUT: %.loc22_35.2: type = converted %int.make_type_32.loc22_35, %.loc22_35.1 [template = i32] +// CHECK:STDOUT: %int.make_type_32.loc13_19: init type = call constants.%Int32() [template = i32] +// CHECK:STDOUT: %.loc13_19.1: type = value_of_initializer %int.make_type_32.loc13_19 [template = i32] +// CHECK:STDOUT: %.loc13_19.2: type = converted %int.make_type_32.loc13_19, %.loc13_19.1 [template = i32] +// CHECK:STDOUT: %a.loc13_16.1: i32 = param a +// CHECK:STDOUT: %a.loc13_16.2: i32 = bind_name a, %a.loc13_16.1 +// CHECK:STDOUT: %int.make_type_32.loc13_27: init type = call constants.%Int32() [template = i32] +// CHECK:STDOUT: %.loc13_27.1: type = value_of_initializer %int.make_type_32.loc13_27 [template = i32] +// CHECK:STDOUT: %.loc13_27.2: type = converted %int.make_type_32.loc13_27, %.loc13_27.1 [template = i32] +// CHECK:STDOUT: %b.loc13_24.1: i32 = param b +// CHECK:STDOUT: %b.loc13_24.2: i32 = bind_name b, %b.loc13_24.1 +// CHECK:STDOUT: %int.make_type_32.loc13_35: init type = call constants.%Int32() [template = i32] +// CHECK:STDOUT: %.loc13_35.1: type = value_of_initializer %int.make_type_32.loc13_35 [template = i32] +// CHECK:STDOUT: %.loc13_35.2: type = converted %int.make_type_32.loc13_35, %.loc13_35.1 [template = i32] // CHECK:STDOUT: %return.var: ref i32 = var // CHECK:STDOUT: } -// CHECK:STDOUT: %.loc22_38: %.5 = assoc_entity element1, %G.decl [template = constants.%.6] +// CHECK:STDOUT: %.loc13_38: %.5 = assoc_entity element1, %G.decl [template = constants.%.6] // CHECK:STDOUT: // CHECK:STDOUT: !members: // CHECK:STDOUT: .Self = %Self -// CHECK:STDOUT: .F = %.loc16 -// CHECK:STDOUT: .G = %.loc22_38 +// CHECK:STDOUT: .F = %.loc7 +// CHECK:STDOUT: .G = %.loc13_38 // CHECK:STDOUT: witness = (%F.decl, %G.decl) // CHECK:STDOUT: } // CHECK:STDOUT: @@ -139,17 +163,137 @@ fn Interface.G(a: i32, b: i32) -> i32 = "int.sadd"; // CHECK:STDOUT: // CHECK:STDOUT: generic fn @G(@Interface.%Self: %.1) { // CHECK:STDOUT: -// CHECK:STDOUT: fn(@Interface.%a.loc22_16.2: i32, @Interface.%b.loc22_24.2: i32) -> i32; +// CHECK:STDOUT: fn(@Interface.%a.loc13_16.2: i32, @Interface.%b.loc13_24.2: i32) -> i32; // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: fn @.1() { -// CHECK:STDOUT: !entry: -// CHECK:STDOUT: return +// CHECK:STDOUT: generic fn @.1(@Interface.%Self: %.1) { +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: fn() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: return +// CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: fn @.2(%a: i32, %b: i32) -> i32 = "int.sadd"; +// CHECK:STDOUT: generic fn @.2(@Interface.%Self: %.1) { +// CHECK:STDOUT: +// CHECK:STDOUT: fn(%a: i32, %b: i32) -> i32 = "int.sadd"; +// CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: specific @F(constants.%Self) {} // CHECK:STDOUT: // CHECK:STDOUT: specific @G(constants.%Self) {} // CHECK:STDOUT: +// CHECK:STDOUT: specific @.1(constants.%Self) {} +// CHECK:STDOUT: +// CHECK:STDOUT: specific @.2(constants.%Self) {} +// CHECK:STDOUT: +// CHECK:STDOUT: --- dependent_return_type.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %.1: type = interface_type @Interface [template] +// CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic] +// CHECK:STDOUT: %C: type = class_type @C [template] +// CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic] +// CHECK:STDOUT: %F.type: type = fn_type @F, @C(%Self) [symbolic] +// CHECK:STDOUT: %.2: type = tuple_type () [template] +// CHECK:STDOUT: %F.1: %F.type = struct_value () [symbolic] +// CHECK:STDOUT: %.3: type = struct_type {} [template] +// CHECK:STDOUT: %F.2: %F.type = struct_value () [symbolic] +// CHECK:STDOUT: %.4: type = ptr_type %.3 [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Core: = namespace file.%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: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = imports.%Core +// CHECK:STDOUT: .Interface = %Interface.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core.import = import Core +// CHECK:STDOUT: %Interface.decl: type = interface_decl @Interface [template = constants.%.1] {} +// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [symbolic = constants.%F.1] { +// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C] +// CHECK:STDOUT: %self.loc20_18.1: %C = param self +// CHECK:STDOUT: @F.%self: %C = bind_name self, %self.loc20_18.1 +// CHECK:STDOUT: %U.loc20_30.1: type = param U +// CHECK:STDOUT: @F.%U.loc20: type = bind_symbolic_name U 1, %U.loc20_30.1 [symbolic = constants.%U] +// CHECK:STDOUT: %U.ref.loc20_43: type = name_ref U, @F.%U.loc20 [symbolic = constants.%U] +// CHECK:STDOUT: %u.loc20_40.1: %U = param u +// CHECK:STDOUT: @F.%u: %U = bind_name u, %u.loc20_40.1 +// CHECK:STDOUT: %U.ref.loc20_49: type = name_ref U, @F.%U.loc20 [symbolic = constants.%U] +// CHECK:STDOUT: @F.%return: ref %U = var +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: interface @Interface { +// CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self] +// CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .C = %C.decl +// CHECK:STDOUT: witness = () +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic class @C(@Interface.%Self: %.1) { +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self)] +// CHECK:STDOUT: %F.type: type = fn_type @F, @C(%Self) [symbolic = %F.type (constants.%F.type)] +// CHECK:STDOUT: %F: @C.%F.type (%F.type) = struct_value () [symbolic = %F (constants.%F.1)] +// CHECK:STDOUT: +// CHECK:STDOUT: class { +// CHECK:STDOUT: %F.decl: @C.%F.type (%F.type) = fn_decl @F [symbolic = %F (constants.%F.1)] { +// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C] +// CHECK:STDOUT: %self.loc14_10.1: %C = param self +// CHECK:STDOUT: %self.loc14_10.2: %C = bind_name self, %self.loc14_10.1 +// CHECK:STDOUT: %U.loc14_22.1: type = param U +// CHECK:STDOUT: %U.loc14_22.2: type = bind_symbolic_name U 1, %U.loc14_22.1 [symbolic = @F.%U.1 (constants.%U)] +// CHECK:STDOUT: %U.ref.loc14_35: type = name_ref U, %U.loc14_22.2 [symbolic = @F.%U.1 (constants.%U)] +// CHECK:STDOUT: %u.loc14_32.1: @F.%U.1 (%U) = param u +// CHECK:STDOUT: %u.loc14_32.2: @F.%U.1 (%U) = bind_name u, %u.loc14_32.1 +// CHECK:STDOUT: %U.ref.loc14_41: type = name_ref U, %U.loc14_22.2 [symbolic = @F.%U.1 (constants.%U)] +// CHECK:STDOUT: %return.var: ref @F.%U.1 (%U) = var +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = constants.%C +// CHECK:STDOUT: .F = %F.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic fn @F(@Interface.%Self: %.1, @C.%U.loc14_22.2: type) { +// CHECK:STDOUT: %U.1: type = bind_symbolic_name U 1 [symbolic = %U.1 (constants.%U)] +// CHECK:STDOUT: +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: fn[%self: %C](%U.loc20: type, %u: %U) -> %U { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %u.ref: @F.%U.1 (%U) = name_ref u, %u +// CHECK:STDOUT: return %u.ref +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @C(constants.%Self) { +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: %Self => constants.%Self +// CHECK:STDOUT: %F.type => constants.%F.type +// CHECK:STDOUT: %F => constants.%F.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @F(constants.%Self, constants.%U) { +// CHECK:STDOUT: %U.1 => constants.%U +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @C(@C.%Self) {} +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/interface/no_prelude/fail_add_member_outside_definition.carbon b/toolchain/check/testdata/interface/no_prelude/fail_add_member_outside_definition.carbon index 88af674bf7ab..e88b08f03173 100644 --- a/toolchain/check/testdata/interface/no_prelude/fail_add_member_outside_definition.carbon +++ b/toolchain/check/testdata/interface/no_prelude/fail_add_member_outside_definition.carbon @@ -98,9 +98,13 @@ interface Outer { // CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: fn @F.1() { -// CHECK:STDOUT: !entry: -// CHECK:STDOUT: return +// CHECK:STDOUT: generic fn @F.1(@Interface.%Self: %.1) { +// CHECK:STDOUT: !definition: +// CHECK:STDOUT: +// CHECK:STDOUT: fn() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: return +// CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: generic fn @.1(@Outer.%Self: %.3, @Inner.%Self.1: %.4) { @@ -108,11 +112,13 @@ interface Outer { // CHECK:STDOUT: fn(); // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: generic fn @F.2(@Outer.%Self: %.3) { +// CHECK:STDOUT: generic fn @F.2(@Outer.%Self: %.3, @Inner.%Self.1: %.4) { // CHECK:STDOUT: // CHECK:STDOUT: fn(); // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: specific @F.1(constants.%Self.1) {} +// CHECK:STDOUT: // CHECK:STDOUT: specific @Inner(constants.%Self.2) { // CHECK:STDOUT: !definition: // CHECK:STDOUT: %Self.2 => constants.%Self.3 @@ -125,5 +131,5 @@ interface Outer { // CHECK:STDOUT: // CHECK:STDOUT: specific @Inner(@Inner.%Self.3) {} // CHECK:STDOUT: -// CHECK:STDOUT: specific @F.2(constants.%Self.2) {} +// CHECK:STDOUT: specific @F.2(constants.%Self.2, constants.%Self.3) {} // CHECK:STDOUT: diff --git a/toolchain/check/testdata/interface/no_prelude/fail_todo_generic_default_fn.carbon b/toolchain/check/testdata/interface/no_prelude/fail_todo_generic_default_fn.carbon index 1356aaefc10d..0f5fa7aed376 100644 --- a/toolchain/check/testdata/interface/no_prelude/fail_todo_generic_default_fn.carbon +++ b/toolchain/check/testdata/interface/no_prelude/fail_todo_generic_default_fn.carbon @@ -107,7 +107,7 @@ fn I(T:! type).F[self: Self]() -> Self { return self; } // CHECK:STDOUT: fn[@I.%self.loc13_8.2: @F.%Self (%Self)]() -> @F.%Self (%Self); // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: generic fn @.1(file.%T.loc22_6.2: type) { +// CHECK:STDOUT: generic fn @.1(file.%T.loc22_6.2: type, @I.%Self.1: @I.%.1 (%.2)) { // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)] // CHECK:STDOUT: %.1: type = interface_type @I, @I(%T) [symbolic = %.1 (constants.%.2)] // CHECK:STDOUT: %Self: %.2 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self)] @@ -151,7 +151,7 @@ fn I(T:! type).F[self: Self]() -> Self { return self; } // CHECK:STDOUT: %T => constants.%T // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: specific @.1(constants.%T) { +// CHECK:STDOUT: specific @.1(constants.%T, constants.%Self) { // CHECK:STDOUT: %T => constants.%T // CHECK:STDOUT: %.1 => constants.%.2 // CHECK:STDOUT: %Self => constants.%Self