Skip to content

Commit

Permalink
Allow simple uses of Self in impls. (#3824)
Browse files Browse the repository at this point in the history
Per #3714, some of the details here are not yet settled. In particular,
we might want `Self` to come into scope at the start of the definition,
not at the `as` keyword. However, this change allows us to accept the
uncontroversial examples.
  • Loading branch information
zygoloid authored Mar 27, 2024
1 parent a790271 commit e8cc089
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 68 deletions.
11 changes: 9 additions & 2 deletions toolchain/check/handle_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ auto HandleTypeImplAs(Context& context, Parse::TypeImplAsId node_id) -> bool {
auto [self_node, self_id] = context.node_stack().PopExprWithNodeId();
auto self_type_id = ExprAsType(context, self_node, self_id);
context.node_stack().Push(node_id, self_type_id);
// TODO: `Self` should come into scope here, at least if it's not already in
// scope. Check the design for the latter case.

// Introduce `Self`. Note that we add this name lexically rather than adding
// to the `NameScopeId` of the `impl`, because this happens before we enter
// the `impl` scope or even identify which `impl` we're declaring.
// TODO: Revisit this once #3714 is resolved.
context.AddNameToLookup(SemIR::NameId::SelfType,
context.types().GetInstId(self_type_id));
return true;
}

Expand Down Expand Up @@ -85,6 +90,8 @@ auto HandleDefaultSelfImplAs(Context& context,
self_type_id = SemIR::TypeId::Error;
}

// There's no need to push `Self` into scope here, because we can find it in
// the enclosing class scope.
context.node_stack().Push(node_id, self_type_id);
return true;
}
Expand Down
1 change: 0 additions & 1 deletion toolchain/check/handle_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ static auto BuildInterfaceDecl(Context& context,
// there was an error in the qualifier, we will have lost track of the
// interface name here. We should keep track of it even if the name is
// invalid.
// TODO: should have a `Self` type id member
interface_decl.interface_id = context.interfaces().Add(
{.name_id = name_context.name_id_for_new_inst(),
.enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(),
Expand Down
7 changes: 3 additions & 4 deletions toolchain/check/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ static auto CheckAssociatedFunctionImplementation(
return SemIR::InstId::BuiltinError;
}

// TODO: Substitute the `Self` from the `impl` into the type in the interface
// before checking. Also, this should be a semantic check rather than a
// syntactic one. The functions should be allowed to have different signatures
// as long as we can synthesize a suitable thunk.
// TODO: This should be a semantic check rather than a syntactic one. The
// functions should be allowed to have different signatures as long as we can
// synthesize a suitable thunk.
if (!CheckFunctionTypeMatches(context, impl_function_decl->function_id,
interface_function_id, substitutions)) {
return SemIR::InstId::BuiltinError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Core;
// TODO: This should be in `Core`, but currently impl lookup only looks in the
// current file.
impl i32 as Core.Add {
fn Op[self: i32](other: i32) -> i32 = "int.add";
fn Op[self: Self](other: Self) -> Self = "int.add";
}

var arr: [i32; 1 + 2] = (3, 4, 3 + 4);
Expand Down Expand Up @@ -125,10 +125,13 @@ var arr: [i32; 1 + 2] = (3, 4, 3 + 4);
// CHECK:STDOUT:
// CHECK:STDOUT: impl @impl: i32 as Add {
// CHECK:STDOUT: %Op: <function> = fn_decl @Op.1 [template] {
// CHECK:STDOUT: %Self.ref.loc7_15: type = name_ref Self, i32 [template = i32]
// CHECK:STDOUT: %self.loc7_9.1: i32 = param self
// CHECK:STDOUT: %self.loc7_9.2: i32 = bind_name self, %self.loc7_9.1
// CHECK:STDOUT: %other.loc7_20.1: i32 = param other
// CHECK:STDOUT: %other.loc7_20.2: i32 = bind_name other, %other.loc7_20.1
// CHECK:STDOUT: %Self.ref.loc7_28: type = name_ref Self, i32 [template = i32]
// CHECK:STDOUT: %other.loc7_21.1: i32 = param other
// CHECK:STDOUT: %other.loc7_21.2: i32 = bind_name other, %other.loc7_21.1
// CHECK:STDOUT: %Self.ref.loc7_37: type = name_ref Self, i32 [template = i32]
// CHECK:STDOUT: %return.var: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.1: <witness> = interface_witness (%Op) [template = constants.%.2]
Expand All @@ -138,7 +141,7 @@ var arr: [i32; 1 + 2] = (3, 4, 3 + 4);
// CHECK:STDOUT: witness = %.1
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Op.1[@impl.%self.loc7_9.2: i32](@impl.%other.loc7_20.2: i32) -> i32 = "int.add";
// CHECK:STDOUT: fn @Op.1[@impl.%self.loc7_9.2: i32](@impl.%other.loc7_21.2: i32) -> i32 = "int.add";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Op.2[%self: Self](%other: Self) -> Self;
// CHECK:STDOUT:
Expand Down
84 changes: 84 additions & 0 deletions toolchain/check/testdata/impl/self_in_class.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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
//
// AUTOUPDATE

interface DefaultConstructible {
fn Make() -> Self;
}

class A {
impl i32 as DefaultConstructible {
// `Self` here refers to `i32`, not `A`.
// TODO: Revisit this once #3714 is resolved.
fn Make() -> Self { return 0; }
}
}

// CHECK:STDOUT: --- self_in_class.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.1: type = interface_type @DefaultConstructible [template]
// CHECK:STDOUT: %.2: type = assoc_entity_type @DefaultConstructible, <function> [template]
// CHECK:STDOUT: %.3: <associated <function> in DefaultConstructible> = assoc_entity element0, @DefaultConstructible.%Make [template]
// CHECK:STDOUT: %A: type = class_type @A [template]
// CHECK:STDOUT: %.4: i32 = int_literal 0 [template]
// CHECK:STDOUT: %.5: <witness> = interface_witness (@impl.%Make) [template]
// CHECK:STDOUT: %.6: type = struct_type {} [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .DefaultConstructible = %DefaultConstructible.decl
// CHECK:STDOUT: .A = %A.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %DefaultConstructible.decl: type = interface_decl @DefaultConstructible [template = constants.%.1] {}
// CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: interface @DefaultConstructible {
// CHECK:STDOUT: %Self: DefaultConstructible = bind_symbolic_name Self [symbolic]
// CHECK:STDOUT: %Make: <function> = fn_decl @Make.1 [template] {
// CHECK:STDOUT: %Self.ref: DefaultConstructible = name_ref Self, %Self [symbolic = %Self]
// CHECK:STDOUT: %.loc8_16.1: type = facet_type_access %Self.ref [symbolic = %Self]
// CHECK:STDOUT: %.loc8_16.2: type = converted %Self.ref, %.loc8_16.1 [symbolic = %Self]
// CHECK:STDOUT: %return.var: ref Self = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.loc8_20: <associated <function> in DefaultConstructible> = assoc_entity element0, %Make [template = constants.%.3]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = %Self
// CHECK:STDOUT: .Make = %.loc8_20
// CHECK:STDOUT: witness = (%Make)
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: impl @impl: i32 as DefaultConstructible {
// CHECK:STDOUT: %Make: <function> = fn_decl @Make.2 [template] {
// CHECK:STDOUT: %Self.ref: type = name_ref Self, i32 [template = i32]
// CHECK:STDOUT: %return.var: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %.1: <witness> = interface_witness (%Make) [template = constants.%.5]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Make = %Make
// CHECK:STDOUT: witness = %.1
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A {
// CHECK:STDOUT: impl_decl @impl {
// CHECK:STDOUT: %DefaultConstructible.ref: type = name_ref DefaultConstructible, file.%DefaultConstructible.decl [template = constants.%.1]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%A
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Make.1() -> Self;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Make.2() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc15: i32 = int_literal 0 [template = constants.%.4]
// CHECK:STDOUT: return %.loc15
// CHECK:STDOUT: }
// CHECK:STDOUT:
Loading

0 comments on commit e8cc089

Please sign in to comment.