mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 19:10:14 +01:00
Implement the toolchain side of proposal #7254, removing the `:!` binding syntax for generic and template parameters in favor of the keywords `generic`, `template`, and `runtime` plus contextual defaults for phase. For valid programs this is semantics-preserving: each binding resolves to the same phase, and produces the same SemIR, as it did under `:!`/`:`. The parser derives a binding's phase from its syntactic context plus any explicit phase keyword; new diagnostics and error recovery for misused keywords are described below. Implementation details for each component: - Lexer: remove the `:!` (`ColonExclaim`) token, move its virtual parse-node budget onto `:`, and add the `generic` and `runtime` keywords. - Parser: thread a `BindingContext` (`ExplicitParam`, `DeducedParam`, or `CompileTimeEntityParam`) from declaration introducers down through parameter lists to each binding pattern, using a one-token lookahead to distinguish a name-qualifier parameter list from a declaration's own final list. Parameters of a compile-time entity (`class`, `interface`, `constraint`, `choice`, `alias`, `export`, `namespace`) and deduced `[]` parameters default to checked generic; explicit function parameters and local bindings default to runtime. `HandleBindingPattern` resolves the phase from that context plus the keyword: a `generic` keyword needs no node of its own (the phase is carried by the binding's node kind), while a `runtime` keyword is preserved as a `RuntimeBindingName` node so `check` can name it in a diagnostic. A phase keyword that is merely redundant with the contextual default is diagnosed here, without invalidating the parse tree. - Check: a phase keyword that is invalid for its context (for example `runtime` on a checked-generic parameter) is diagnosed here, and recovers by building an error binding that still introduces the name so that later uses of it do not produce cascading errors. The removed `:!` syntax is now rejected as an ordinary parse error. The `form`/`:?`/`->?` ("extended types") portion of proposal #7254 is left for a separate change. Assisted-by: Claude Code
207 lines
13 KiB
Plaintext
207 lines
13 KiB
Plaintext
// 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/class/generic/member_lookup.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/member_lookup.carbon
|
|
|
|
// --- member_access.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
base class Base(T: type) {
|
|
var b: T;
|
|
}
|
|
|
|
class Derived(T: type) {
|
|
extend base: Base(T);
|
|
var d: T;
|
|
}
|
|
|
|
fn AccessDerived[T: Core.Copy](x: Derived(T)) -> T {
|
|
//@dump-sem-ir-begin
|
|
return x.d;
|
|
//@dump-sem-ir-end
|
|
}
|
|
|
|
fn AccessBase[T: Core.Copy](x: Derived(T)) -> T {
|
|
//@dump-sem-ir-begin
|
|
return x.b;
|
|
//@dump-sem-ir-end
|
|
}
|
|
|
|
fn AccessConcrete(x: Derived(i32)) -> i32 {
|
|
return x.b;
|
|
}
|
|
|
|
// --- fail_no_member.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
base class Base(T: type) {
|
|
var b: T;
|
|
}
|
|
|
|
class Derived(T: type) {
|
|
extend base: Base(T);
|
|
var d: T;
|
|
}
|
|
|
|
fn AccessMissingBase[T: type](x: Base(T)) -> T {
|
|
// CHECK:STDERR: fail_no_member.carbon:[[@LINE+4]]:10: error: member name `nonesuch` not found in `Base(T)` [MemberNameNotFoundInSpecificScope]
|
|
// CHECK:STDERR: return x.nonesuch;
|
|
// CHECK:STDERR: ^~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
return x.nonesuch;
|
|
}
|
|
|
|
fn AccessMissingDerived[T: type](x: Derived(T)) -> T {
|
|
// CHECK:STDERR: fail_no_member.carbon:[[@LINE+4]]:10: error: member name `nonesuch` not found in `Derived(T)` [MemberNameNotFoundInSpecificScope]
|
|
// CHECK:STDERR: return x.nonesuch;
|
|
// CHECK:STDERR: ^~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
return x.nonesuch;
|
|
}
|
|
|
|
fn AccessMissingConcrete(x: Derived(i32)) -> i32 {
|
|
// CHECK:STDERR: fail_no_member.carbon:[[@LINE+4]]:10: error: member name `nonesuch` not found in `Derived(i32)` [MemberNameNotFoundInSpecificScope]
|
|
// CHECK:STDERR: return x.nonesuch;
|
|
// CHECK:STDERR: ^~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
return x.nonesuch;
|
|
}
|
|
|
|
|
|
// CHECK:STDOUT: --- member_access.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
|
|
// CHECK:STDOUT: %pattern_type.e81: type = pattern_type %Copy.type [concrete]
|
|
// CHECK:STDOUT: %T.patt.fe6: %pattern_type.e81 = symbolic_binding_pattern T, 0 [symbolic]
|
|
// CHECK:STDOUT: %T.f84: %Copy.type = symbolic_binding T, 0 [symbolic]
|
|
// CHECK:STDOUT: %T.as_type: type = facet_access_type %T.f84 [symbolic]
|
|
// CHECK:STDOUT: %Derived.e87: type = class_type @Derived, @Derived(%T.as_type) [symbolic]
|
|
// CHECK:STDOUT: %pattern_type.ebc: type = pattern_type %Derived.e87 [symbolic]
|
|
// CHECK:STDOUT: %x.param_patt.203: %pattern_type.ebc = value_param_pattern [symbolic]
|
|
// CHECK:STDOUT: %x.patt.c37: %pattern_type.ebc = at_binding_pattern x, %x.param_patt.203 [symbolic]
|
|
// CHECK:STDOUT: %.1ce700.2: Core.Form = init_form %T.as_type [symbolic]
|
|
// CHECK:STDOUT: %pattern_type.2c66b4.2: type = pattern_type %T.as_type [symbolic]
|
|
// CHECK:STDOUT: %return.param_patt.bf4475.2: %pattern_type.2c66b4.2 = out_param_pattern [symbolic]
|
|
// CHECK:STDOUT: %return.patt.f44: %pattern_type.2c66b4.2 = return_slot_pattern %return.param_patt.bf4475.2, %T.as_type [symbolic]
|
|
// CHECK:STDOUT: %Base.146: type = class_type @Base, @Base(%T.as_type) [symbolic]
|
|
// CHECK:STDOUT: %require_complete.0fb: <witness> = require_complete_type %Base.146 [symbolic]
|
|
// CHECK:STDOUT: %require_complete.d83: <witness> = require_complete_type %T.as_type [symbolic]
|
|
// CHECK:STDOUT: %Derived.elem.eb3: type = unbound_element_type %Derived.e87, %T.as_type [symbolic]
|
|
// CHECK:STDOUT: %Base.elem.209: type = unbound_element_type %Base.146, %T.as_type [symbolic]
|
|
// CHECK:STDOUT: %Copy.lookup_impl_witness.d9c: <witness> = lookup_impl_witness %T.f84, @Copy [symbolic]
|
|
// CHECK:STDOUT: %Copy.WithSelf.Op.type.c85f21.2: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%T.f84) [symbolic]
|
|
// CHECK:STDOUT: %.c29: type = fn_type_with_self_type %Copy.WithSelf.Op.type.c85f21.2, %T.f84 [symbolic]
|
|
// CHECK:STDOUT: %impl.elem0.bd2: %.c29 = impl_witness_access %Copy.lookup_impl_witness.d9c, element0 [symbolic]
|
|
// CHECK:STDOUT: %specific_impl_fn.e2a: <specific function> = specific_impl_function %impl.elem0.bd2, @Copy.WithSelf.Op(%T.f84) [symbolic]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: generic fn @AccessDerived(%T.loc13_19.2: %Copy.type) {
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: %Derived.elem: type = unbound_element_type %Derived.loc13_44.1, %T.as_type.loc13_44.1 [symbolic = %Derived.elem (constants.%Derived.elem.eb3)]
|
|
// CHECK:STDOUT: %require_complete.loc15: <witness> = require_complete_type %T.as_type.loc13_44.1 [symbolic = %require_complete.loc15 (constants.%require_complete.d83)]
|
|
// CHECK:STDOUT: %Copy.WithSelf.Op.type: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%T.loc13_19.1) [symbolic = %Copy.WithSelf.Op.type (constants.%Copy.WithSelf.Op.type.c85f21.2)]
|
|
// CHECK:STDOUT: %.loc15_11.5: type = fn_type_with_self_type %Copy.WithSelf.Op.type, %T.loc13_19.1 [symbolic = %.loc15_11.5 (constants.%.c29)]
|
|
// CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_19.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.d9c)]
|
|
// CHECK:STDOUT: %impl.elem0.loc15_11.2: @AccessDerived.%.loc15_11.5 (%.c29) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc15_11.2 (constants.%impl.elem0.bd2)]
|
|
// CHECK:STDOUT: %specific_impl_fn.loc15_11.2: <specific function> = specific_impl_function %impl.elem0.loc15_11.2, @Copy.WithSelf.Op(%T.loc13_19.1) [symbolic = %specific_impl_fn.loc15_11.2 (constants.%specific_impl_fn.e2a)]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn(%x.param: @AccessDerived.%Derived.loc13_44.1 (%Derived.e87)) -> out %return.param: @AccessDerived.%T.as_type.loc13_44.1 (%T.as_type) {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: %x.ref: @AccessDerived.%Derived.loc13_44.1 (%Derived.e87) = name_ref x, %x
|
|
// CHECK:STDOUT: %d.ref: @AccessDerived.%Derived.elem (%Derived.elem.eb3) = name_ref d, @Derived.%.loc10 [concrete = @Derived.%.loc10]
|
|
// CHECK:STDOUT: %.loc15_11.1: ref @AccessDerived.%T.as_type.loc13_44.1 (%T.as_type) = class_element_access %x.ref, element1
|
|
// CHECK:STDOUT: %.loc15_11.2: @AccessDerived.%T.as_type.loc13_44.1 (%T.as_type) = acquire_value %.loc15_11.1
|
|
// CHECK:STDOUT: %impl.elem0.loc15_11.1: @AccessDerived.%.loc15_11.5 (%.c29) = impl_witness_access constants.%Copy.lookup_impl_witness.d9c, element0 [symbolic = %impl.elem0.loc15_11.2 (constants.%impl.elem0.bd2)]
|
|
// CHECK:STDOUT: %bound_method.loc15_11.1: <bound method> = bound_method %.loc15_11.2, %impl.elem0.loc15_11.1
|
|
// CHECK:STDOUT: %.loc15_11.3: %Copy.type = converted constants.%T.as_type, constants.%T.f84 [symbolic = %T.loc13_19.1 (constants.%T.f84)]
|
|
// CHECK:STDOUT: %.loc15_11.4: %Copy.type = converted constants.%T.as_type, constants.%T.f84 [symbolic = %T.loc13_19.1 (constants.%T.f84)]
|
|
// CHECK:STDOUT: %specific_impl_fn.loc15_11.1: <specific function> = specific_impl_function %impl.elem0.loc15_11.1, @Copy.WithSelf.Op(constants.%T.f84) [symbolic = %specific_impl_fn.loc15_11.2 (constants.%specific_impl_fn.e2a)]
|
|
// CHECK:STDOUT: %bound_method.loc15_11.2: <bound method> = bound_method %.loc15_11.2, %specific_impl_fn.loc15_11.1
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: %Copy.WithSelf.Op.call: init @AccessDerived.%T.as_type.loc13_44.1 (%T.as_type) to %.loc13_50.1 = call %bound_method.loc15_11.2(%.loc15_11.2)
|
|
// CHECK:STDOUT: return %Copy.WithSelf.Op.call to %return.param
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !observes:
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: generic fn @AccessBase(%T.loc19_16.2: %Copy.type) {
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !definition:
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: %Base: type = class_type @Base, @Base(%T.as_type.loc19_41.1) [symbolic = %Base (constants.%Base.146)]
|
|
// CHECK:STDOUT: %Base.elem: type = unbound_element_type %Base, %T.as_type.loc19_41.1 [symbolic = %Base.elem (constants.%Base.elem.209)]
|
|
// CHECK:STDOUT: %require_complete.loc21_11: <witness> = require_complete_type %Base [symbolic = %require_complete.loc21_11 (constants.%require_complete.0fb)]
|
|
// CHECK:STDOUT: %require_complete.loc21_13: <witness> = require_complete_type %T.as_type.loc19_41.1 [symbolic = %require_complete.loc21_13 (constants.%require_complete.d83)]
|
|
// CHECK:STDOUT: %Copy.WithSelf.Op.type: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%T.loc19_16.1) [symbolic = %Copy.WithSelf.Op.type (constants.%Copy.WithSelf.Op.type.c85f21.2)]
|
|
// CHECK:STDOUT: %.loc21_11.8: type = fn_type_with_self_type %Copy.WithSelf.Op.type, %T.loc19_16.1 [symbolic = %.loc21_11.8 (constants.%.c29)]
|
|
// CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc19_16.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.d9c)]
|
|
// CHECK:STDOUT: %impl.elem0.loc21_11.2: @AccessBase.%.loc21_11.8 (%.c29) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc21_11.2 (constants.%impl.elem0.bd2)]
|
|
// CHECK:STDOUT: %specific_impl_fn.loc21_11.2: <specific function> = specific_impl_function %impl.elem0.loc21_11.2, @Copy.WithSelf.Op(%T.loc19_16.1) [symbolic = %specific_impl_fn.loc21_11.2 (constants.%specific_impl_fn.e2a)]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn(%x.param: @AccessBase.%Derived.loc19_41.1 (%Derived.e87)) -> out %return.param: @AccessBase.%T.as_type.loc19_41.1 (%T.as_type) {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: %x.ref: @AccessBase.%Derived.loc19_41.1 (%Derived.e87) = name_ref x, %x
|
|
// CHECK:STDOUT: %b.ref: @AccessBase.%Base.elem (%Base.elem.209) = name_ref b, @Base.%.loc5 [concrete = @Base.%.loc5]
|
|
// CHECK:STDOUT: %.loc21_11.1: @AccessBase.%Base (%Base.146) = as_compatible %x.ref
|
|
// CHECK:STDOUT: %.loc21_11.2: ref @AccessBase.%Base (%Base.146) = class_element_access %.loc21_11.1, element0
|
|
// CHECK:STDOUT: %.loc21_11.3: ref @AccessBase.%Base (%Base.146) = converted %x.ref, %.loc21_11.2
|
|
// CHECK:STDOUT: %.loc21_11.4: ref @AccessBase.%T.as_type.loc19_41.1 (%T.as_type) = class_element_access %.loc21_11.3, element0
|
|
// CHECK:STDOUT: %.loc21_11.5: @AccessBase.%T.as_type.loc19_41.1 (%T.as_type) = acquire_value %.loc21_11.4
|
|
// CHECK:STDOUT: %impl.elem0.loc21_11.1: @AccessBase.%.loc21_11.8 (%.c29) = impl_witness_access constants.%Copy.lookup_impl_witness.d9c, element0 [symbolic = %impl.elem0.loc21_11.2 (constants.%impl.elem0.bd2)]
|
|
// CHECK:STDOUT: %bound_method.loc21_11.1: <bound method> = bound_method %.loc21_11.5, %impl.elem0.loc21_11.1
|
|
// CHECK:STDOUT: %.loc21_11.6: %Copy.type = converted constants.%T.as_type, constants.%T.f84 [symbolic = %T.loc19_16.1 (constants.%T.f84)]
|
|
// CHECK:STDOUT: %.loc21_11.7: %Copy.type = converted constants.%T.as_type, constants.%T.f84 [symbolic = %T.loc19_16.1 (constants.%T.f84)]
|
|
// CHECK:STDOUT: %specific_impl_fn.loc21_11.1: <specific function> = specific_impl_function %impl.elem0.loc21_11.1, @Copy.WithSelf.Op(constants.%T.f84) [symbolic = %specific_impl_fn.loc21_11.2 (constants.%specific_impl_fn.e2a)]
|
|
// CHECK:STDOUT: %bound_method.loc21_11.2: <bound method> = bound_method %.loc21_11.5, %specific_impl_fn.loc21_11.1
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: %Copy.WithSelf.Op.call: init @AccessBase.%T.as_type.loc19_41.1 (%T.as_type) to %.loc19_47.1 = call %bound_method.loc21_11.2(%.loc21_11.5)
|
|
// CHECK:STDOUT: return %Copy.WithSelf.Op.call to %return.param
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !observes:
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @AccessDerived(constants.%T.f84) {
|
|
// CHECK:STDOUT: %T.patt.loc13_19.2 => constants.%T.patt.fe6
|
|
// CHECK:STDOUT: %T.loc13_19.1 => constants.%T.f84
|
|
// CHECK:STDOUT: %T.as_type.loc13_44.1 => constants.%T.as_type
|
|
// CHECK:STDOUT: %Derived.loc13_44.1 => constants.%Derived.e87
|
|
// CHECK:STDOUT: %pattern_type.loc13_33 => constants.%pattern_type.ebc
|
|
// CHECK:STDOUT: %x.param_patt.loc13_33.2 => constants.%x.param_patt.203
|
|
// CHECK:STDOUT: %x.patt.loc13_33.2 => constants.%x.patt.c37
|
|
// CHECK:STDOUT: %.loc13_50.2 => constants.%.1ce700.2
|
|
// CHECK:STDOUT: %pattern_type.loc13_50 => constants.%pattern_type.2c66b4.2
|
|
// CHECK:STDOUT: %return.param_patt.loc13_50.2 => constants.%return.param_patt.bf4475.2
|
|
// CHECK:STDOUT: %return.patt.loc13_47.2 => constants.%return.patt.f44
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: specific @AccessBase(constants.%T.f84) {
|
|
// CHECK:STDOUT: %T.patt.loc19_16.2 => constants.%T.patt.fe6
|
|
// CHECK:STDOUT: %T.loc19_16.1 => constants.%T.f84
|
|
// CHECK:STDOUT: %T.as_type.loc19_41.1 => constants.%T.as_type
|
|
// CHECK:STDOUT: %Derived.loc19_41.1 => constants.%Derived.e87
|
|
// CHECK:STDOUT: %pattern_type.loc19_30 => constants.%pattern_type.ebc
|
|
// CHECK:STDOUT: %x.param_patt.loc19_30.2 => constants.%x.param_patt.203
|
|
// CHECK:STDOUT: %x.patt.loc19_30.2 => constants.%x.patt.c37
|
|
// CHECK:STDOUT: %.loc19_47.2 => constants.%.1ce700.2
|
|
// CHECK:STDOUT: %pattern_type.loc19_47 => constants.%pattern_type.2c66b4.2
|
|
// CHECK:STDOUT: %return.param_patt.loc19_47.2 => constants.%return.param_patt.bf4475.2
|
|
// CHECK:STDOUT: %return.patt.loc19_44.2 => constants.%return.patt.f44
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|