Add Self to the name scope for a class. (#3736)

Rather than just adding `Self` to the lexical scope, add it to the
class's name scope so that it is visible in later lexical scopes for the
same class -- in particular, for out-of-line definitions of members.
Also switch some tests in `check/testdata/class` over to making
idiomatic use of `Self` both inside a class and out-of-line, now that it
works more consistently.

Note that this does not permit using `Class.Self`, but only because we
don't yet support keyword names after `.` at all. If that changed, one
could use `Class.Self` to redundantly refer to `Class`. Whether we allow
that is left to a future decision.
This commit is contained in:
Richard Smith
2024-02-29 21:33:47 +00:00
committed by GitHub
parent a6f4752a7b
commit 67d5446b03
81 changed files with 535 additions and 193 deletions
+47 -47
View File
@@ -17,15 +17,14 @@ class Outer {
}
fn F(a: Outer*) {
// TODO: Simplify this once `Outer.Inner` works.
// let b: Outer.Inner* = (*a).pi;
let b: Outer.Inner* = (*a).pi;
(*a).po = a;
(*a).qo = a;
(*a).pi = (*a).pi;
(*(*a).pi).po = a;
(*(*a).pi).pi = (*a).pi;
(*(*a).pi).qi = (*a).pi;
(*b).po = a;
(*b).pi = (*a).pi;
(*b).qi = (*a).pi;
}
// CHECK:STDOUT: --- nested.carbon
@@ -72,6 +71,7 @@ fn F(a: Outer*) {
// CHECK:STDOUT: %.loc16_9: <unbound element of class Outer> = field_decl pi, element2 [template]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%Outer
// CHECK:STDOUT: .Inner = %Inner.decl
// CHECK:STDOUT: .po = %.loc14_9
// CHECK:STDOUT: .qo = %.loc15_9
@@ -90,6 +90,7 @@ fn F(a: Outer*) {
// CHECK:STDOUT: %.loc11_11: <unbound element of class Inner> = field_decl qi, element2 [template]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%Inner
// CHECK:STDOUT: .pi = %.loc9_11
// CHECK:STDOUT: .po = %.loc10_11
// CHECK:STDOUT: .qi = %.loc11_11
@@ -97,54 +98,53 @@ fn F(a: Outer*) {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%a: Outer*) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %Outer.ref: type = name_ref Outer, file.%Outer.decl [template = constants.%Outer]
// CHECK:STDOUT: %Inner.ref: type = name_ref Inner, @Outer.%Inner.decl [template = constants.%Inner]
// CHECK:STDOUT: %.loc20_21: type = ptr_type Inner [template = constants.%.1]
// CHECK:STDOUT: %a.ref.loc20: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc20_26: ref Outer = deref %a.ref.loc20
// CHECK:STDOUT: %.loc20_29.1: ref Inner* = class_element_access %.loc20_26, element2
// CHECK:STDOUT: %.loc20_29.2: Inner* = bind_value %.loc20_29.1
// CHECK:STDOUT: %b: Inner* = bind_name b, %.loc20_29.2
// CHECK:STDOUT: %a.ref.loc22_5: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc22_4: ref Outer = deref %a.ref.loc22_5
// CHECK:STDOUT: %.loc22_7: ref Outer* = class_element_access %.loc22_4, element0
// CHECK:STDOUT: %a.ref.loc22_13: Outer* = name_ref a, %a
// CHECK:STDOUT: assign %.loc22_7, %a.ref.loc22_13
// CHECK:STDOUT: %a.ref.loc23_5: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc23_4: ref Outer = deref %a.ref.loc23_5
// CHECK:STDOUT: %.loc23_7: ref Outer* = class_element_access %.loc23_4, element0
// CHECK:STDOUT: %.loc23_7: ref Outer* = class_element_access %.loc23_4, element1
// CHECK:STDOUT: %a.ref.loc23_13: Outer* = name_ref a, %a
// CHECK:STDOUT: assign %.loc23_7, %a.ref.loc23_13
// CHECK:STDOUT: %a.ref.loc24_5: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc24_4: ref Outer = deref %a.ref.loc24_5
// CHECK:STDOUT: %.loc24_7: ref Outer* = class_element_access %.loc24_4, element1
// CHECK:STDOUT: %a.ref.loc24_13: Outer* = name_ref a, %a
// CHECK:STDOUT: assign %.loc24_7, %a.ref.loc24_13
// CHECK:STDOUT: %a.ref.loc25_5: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc25_4: ref Outer = deref %a.ref.loc25_5
// CHECK:STDOUT: %.loc25_7: ref Inner* = class_element_access %.loc25_4, element2
// CHECK:STDOUT: %a.ref.loc25_15: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc25_14: ref Outer = deref %a.ref.loc25_15
// CHECK:STDOUT: %.loc25_17.1: ref Inner* = class_element_access %.loc25_14, element2
// CHECK:STDOUT: %.loc25_17.2: Inner* = bind_value %.loc25_17.1
// CHECK:STDOUT: assign %.loc25_7, %.loc25_17.2
// CHECK:STDOUT: %a.ref.loc26_7: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc26_6: ref Outer = deref %a.ref.loc26_7
// CHECK:STDOUT: %.loc26_9.1: ref Inner* = class_element_access %.loc26_6, element2
// CHECK:STDOUT: %.loc26_9.2: Inner* = bind_value %.loc26_9.1
// CHECK:STDOUT: %.loc26_4: ref Inner = deref %.loc26_9.2
// CHECK:STDOUT: %.loc26_13: ref Outer* = class_element_access %.loc26_4, element1
// CHECK:STDOUT: %a.ref.loc26_19: Outer* = name_ref a, %a
// CHECK:STDOUT: assign %.loc26_13, %a.ref.loc26_19
// CHECK:STDOUT: %a.ref.loc27_7: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc27_6: ref Outer = deref %a.ref.loc27_7
// CHECK:STDOUT: %.loc27_9.1: ref Inner* = class_element_access %.loc27_6, element2
// CHECK:STDOUT: %.loc27_9.2: Inner* = bind_value %.loc27_9.1
// CHECK:STDOUT: %.loc27_4: ref Inner = deref %.loc27_9.2
// CHECK:STDOUT: %.loc27_13: ref Inner* = class_element_access %.loc27_4, element0
// CHECK:STDOUT: %a.ref.loc27_21: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc27_20: ref Outer = deref %a.ref.loc27_21
// CHECK:STDOUT: %.loc27_23.1: ref Inner* = class_element_access %.loc27_20, element2
// CHECK:STDOUT: %.loc27_23.2: Inner* = bind_value %.loc27_23.1
// CHECK:STDOUT: assign %.loc27_13, %.loc27_23.2
// CHECK:STDOUT: %a.ref.loc28_7: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc28_6: ref Outer = deref %a.ref.loc28_7
// CHECK:STDOUT: %.loc28_9.1: ref Inner* = class_element_access %.loc28_6, element2
// CHECK:STDOUT: %.loc28_9.2: Inner* = bind_value %.loc28_9.1
// CHECK:STDOUT: %.loc28_4: ref Inner = deref %.loc28_9.2
// CHECK:STDOUT: %.loc28_13: ref Inner* = class_element_access %.loc28_4, element2
// CHECK:STDOUT: %a.ref.loc28_21: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc28_20: ref Outer = deref %a.ref.loc28_21
// CHECK:STDOUT: %.loc28_23.1: ref Inner* = class_element_access %.loc28_20, element2
// CHECK:STDOUT: %.loc28_23.2: Inner* = bind_value %.loc28_23.1
// CHECK:STDOUT: assign %.loc28_13, %.loc28_23.2
// CHECK:STDOUT: %.loc24_7: ref Inner* = class_element_access %.loc24_4, element2
// CHECK:STDOUT: %a.ref.loc24_15: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc24_14: ref Outer = deref %a.ref.loc24_15
// CHECK:STDOUT: %.loc24_17.1: ref Inner* = class_element_access %.loc24_14, element2
// CHECK:STDOUT: %.loc24_17.2: Inner* = bind_value %.loc24_17.1
// CHECK:STDOUT: assign %.loc24_7, %.loc24_17.2
// CHECK:STDOUT: %b.ref.loc25: Inner* = name_ref b, %b
// CHECK:STDOUT: %.loc25_4: ref Inner = deref %b.ref.loc25
// CHECK:STDOUT: %.loc25_7: ref Outer* = class_element_access %.loc25_4, element1
// CHECK:STDOUT: %a.ref.loc25: Outer* = name_ref a, %a
// CHECK:STDOUT: assign %.loc25_7, %a.ref.loc25
// CHECK:STDOUT: %b.ref.loc26: Inner* = name_ref b, %b
// CHECK:STDOUT: %.loc26_4: ref Inner = deref %b.ref.loc26
// CHECK:STDOUT: %.loc26_7: ref Inner* = class_element_access %.loc26_4, element0
// CHECK:STDOUT: %a.ref.loc26: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc26_14: ref Outer = deref %a.ref.loc26
// CHECK:STDOUT: %.loc26_17.1: ref Inner* = class_element_access %.loc26_14, element2
// CHECK:STDOUT: %.loc26_17.2: Inner* = bind_value %.loc26_17.1
// CHECK:STDOUT: assign %.loc26_7, %.loc26_17.2
// CHECK:STDOUT: %b.ref.loc27: Inner* = name_ref b, %b
// CHECK:STDOUT: %.loc27_4: ref Inner = deref %b.ref.loc27
// CHECK:STDOUT: %.loc27_7: ref Inner* = class_element_access %.loc27_4, element2
// CHECK:STDOUT: %a.ref.loc27: Outer* = name_ref a, %a
// CHECK:STDOUT: %.loc27_14: ref Outer = deref %a.ref.loc27
// CHECK:STDOUT: %.loc27_17.1: ref Inner* = class_element_access %.loc27_14, element2
// CHECK:STDOUT: %.loc27_17.2: Inner* = bind_value %.loc27_17.1
// CHECK:STDOUT: assign %.loc27_7, %.loc27_17.2
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: