mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:11:12 +01:00
Replace :! binding syntax with phase keywords and contextual defaults (#7479)
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
This commit is contained in:
+4
-4
@@ -14,21 +14,21 @@ export import library "prelude/parts/default";
|
||||
export import library "prelude/parts/destroy";
|
||||
export import library "prelude/parts/form";
|
||||
|
||||
interface UnsafeAs(Dest:! type) {
|
||||
interface UnsafeAs(Dest: type) {
|
||||
fn Convert(self) -> Dest;
|
||||
}
|
||||
|
||||
interface As(Dest:! type) {
|
||||
interface As(Dest: type) {
|
||||
// TODO: extend UnsafeAs(Dest);
|
||||
fn Convert(self) -> Dest;
|
||||
}
|
||||
|
||||
interface ImplicitAs(Dest:! type) {
|
||||
interface ImplicitAs(Dest: type) {
|
||||
// TODO: extend As(Dest);
|
||||
fn Convert(self) -> Dest;
|
||||
}
|
||||
|
||||
interface BitAndWith(Other:! type) {
|
||||
interface BitAndWith(Other: type) {
|
||||
fn Op(self, other: Other) -> Self;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ private alias FloatLiteral = MakeFloatLiteral();
|
||||
private fn MakeIntLiteral() -> type = "int_literal.make_type";
|
||||
private alias IntLiteral = MakeIntLiteral();
|
||||
|
||||
impl forall [T:! Copy] const T as Copy {
|
||||
impl forall [T: Copy] const T as Copy {
|
||||
fn Op(self) -> Self { return (self as T).(Copy.Op)() as const T; }
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ impl IntLiteral as Copy {
|
||||
fn Op(self) -> Self = "primitive_copy";
|
||||
}
|
||||
|
||||
impl forall [T:! type] T* as Copy {
|
||||
impl forall [T: type] T* as Copy {
|
||||
fn Op(self) -> Self = "primitive_copy";
|
||||
}
|
||||
|
||||
@@ -53,13 +53,13 @@ impl () as Copy {
|
||||
fn Op(self) -> Self = "no_op";
|
||||
}
|
||||
|
||||
impl forall [T:! Copy, U:! Copy] (T, U) as Copy {
|
||||
impl forall [T: Copy, U: Copy] (T, U) as Copy {
|
||||
fn Op(self) -> Self {
|
||||
return (self.0.Op(), self.1.Op());
|
||||
}
|
||||
}
|
||||
|
||||
impl forall [T:! Copy, U:! Copy, V:! Copy] (T, U, V) as Copy {
|
||||
impl forall [T: Copy, U: Copy, V: Copy] (T, U, V) as Copy {
|
||||
fn Op(self) -> Self {
|
||||
return (self.0.Op(), self.1.Op(), self.2.Op());
|
||||
}
|
||||
|
||||
@@ -10,12 +10,12 @@ interface DefaultOrUnformed { fn Op() -> Self; }
|
||||
|
||||
interface Default { fn Op() -> Self; }
|
||||
|
||||
final impl forall [T:! Default] T as DefaultOrUnformed {
|
||||
final impl forall [T: Default] T as DefaultOrUnformed {
|
||||
fn Op() -> Self { return T.(Default.Op)(); }
|
||||
}
|
||||
|
||||
// In tests, just allow any type to be default-initialized as a no-op.
|
||||
// TODO: This is a hack to allow existing tests to contiue working.
|
||||
impl forall [T:! type] T as DefaultOrUnformed {
|
||||
impl forall [T: type] T as DefaultOrUnformed {
|
||||
fn Op() -> Self = "make_uninitialized";
|
||||
}
|
||||
|
||||
@@ -18,19 +18,19 @@ export import library "prelude/parts/int_literal";
|
||||
|
||||
private fn MakeFloat(size: IntLiteral) -> type = "float.make_type";
|
||||
|
||||
class Float(N:! IntLiteral) {
|
||||
class Float(N: IntLiteral) {
|
||||
adapt MakeFloat(N);
|
||||
}
|
||||
|
||||
impl forall [N:! IntLiteral] Float(N) as Copy {
|
||||
impl forall [N: IntLiteral] Float(N) as Copy {
|
||||
fn Op(self) -> Self = "primitive_copy";
|
||||
}
|
||||
|
||||
impl forall [To:! IntLiteral] FloatLiteral as ImplicitAs(Float(To)) {
|
||||
impl forall [To: IntLiteral] FloatLiteral as ImplicitAs(Float(To)) {
|
||||
fn Convert(self) -> Float(To) = "float.convert_checked";
|
||||
}
|
||||
|
||||
// TODO: Remove this once ImplicitAs extends As.
|
||||
impl forall [To:! IntLiteral] FloatLiteral as As(Float(To)) {
|
||||
impl forall [To: IntLiteral] FloatLiteral as As(Float(To)) {
|
||||
fn Convert(self) -> Float(To) = "float.convert_checked";
|
||||
}
|
||||
|
||||
+7
-7
@@ -16,38 +16,38 @@ export import library "prelude/parts/int_literal";
|
||||
|
||||
private fn MakeInt(size: IntLiteral) -> type = "int.make_type_signed";
|
||||
|
||||
class Int(N:! IntLiteral) {
|
||||
class Int(N: IntLiteral) {
|
||||
adapt MakeInt(N);
|
||||
}
|
||||
|
||||
// Copy.
|
||||
|
||||
impl forall [N:! IntLiteral] Int(N) as Copy {
|
||||
impl forall [N: IntLiteral] Int(N) as Copy {
|
||||
fn Op(self) -> Self = "primitive_copy";
|
||||
}
|
||||
|
||||
// Conversions.
|
||||
|
||||
impl forall [To:! IntLiteral] IntLiteral as ImplicitAs(Int(To)) {
|
||||
impl forall [To: IntLiteral] IntLiteral as ImplicitAs(Int(To)) {
|
||||
fn Convert(self) -> Int(To) = "int.convert_checked";
|
||||
}
|
||||
|
||||
final impl forall [From:! IntLiteral] Int(From) as ImplicitAs(IntLiteral) {
|
||||
final impl forall [From: IntLiteral] Int(From) as ImplicitAs(IntLiteral) {
|
||||
fn Convert(self) -> IntLiteral = "int.convert_checked";
|
||||
}
|
||||
|
||||
// TODO: Remove these once ImplicitAs extends As.
|
||||
impl forall [To:! IntLiteral] IntLiteral as As(Int(To)) {
|
||||
impl forall [To: IntLiteral] IntLiteral as As(Int(To)) {
|
||||
fn Convert(self) -> Int(To) = "int.convert_checked";
|
||||
}
|
||||
|
||||
final impl forall [From:! IntLiteral] Int(From) as As(IntLiteral) {
|
||||
final impl forall [From: IntLiteral] Int(From) as As(IntLiteral) {
|
||||
fn Convert(self) -> IntLiteral = "int.convert_checked";
|
||||
}
|
||||
|
||||
// Negate.
|
||||
|
||||
final impl forall [N:! IntLiteral]
|
||||
final impl forall [N: IntLiteral]
|
||||
Int(N) as Negate where .Result = Self {
|
||||
fn Op(self) -> Self = "int.snegate";
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ alias IntLiteral = MakeIntLiteral();
|
||||
// Negate.
|
||||
|
||||
interface Negate {
|
||||
let Result:! type;
|
||||
let Result: type;
|
||||
fn Op(self) -> Result;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ import library "prelude/parts/copy";
|
||||
import library "prelude/parts/optional";
|
||||
|
||||
interface Iterate {
|
||||
let ElementType:! Copy;
|
||||
let CursorType:! type;
|
||||
let ElementType: Copy;
|
||||
let CursorType: type;
|
||||
fn NewCursor(self) -> CursorType;
|
||||
fn Next(self, cursor: CursorType*) -> Optional(ElementType);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,6 @@ export import library "prelude/parts/destroy";
|
||||
|
||||
private fn Make(t: type) -> type = "maybe_unformed.make_type";
|
||||
|
||||
class MaybeUnformed(T:! type) {
|
||||
class MaybeUnformed(T: type) {
|
||||
adapt Make(T);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import library "prelude/parts/copy";
|
||||
import library "prelude/parts/default";
|
||||
import library "prelude/parts/destroy";
|
||||
|
||||
class Optional(T:! Copy) {
|
||||
class Optional(T: Copy) {
|
||||
fn None() -> Self {
|
||||
returned var me: Self;
|
||||
me.has_value = false;
|
||||
|
||||
@@ -16,38 +16,38 @@ export import library "prelude/parts/int_literal";
|
||||
|
||||
private fn MakeUInt(size: IntLiteral) -> type = "int.make_type_unsigned";
|
||||
|
||||
class UInt(N:! IntLiteral) {
|
||||
class UInt(N: IntLiteral) {
|
||||
adapt MakeUInt(N);
|
||||
}
|
||||
|
||||
// Copy.
|
||||
|
||||
impl forall [N:! IntLiteral] UInt(N) as Copy {
|
||||
impl forall [N: IntLiteral] UInt(N) as Copy {
|
||||
fn Op(self) -> Self = "primitive_copy";
|
||||
}
|
||||
|
||||
// Conversions.
|
||||
|
||||
impl forall [To:! IntLiteral] IntLiteral as ImplicitAs(UInt(To)) {
|
||||
impl forall [To: IntLiteral] IntLiteral as ImplicitAs(UInt(To)) {
|
||||
fn Convert(self) -> UInt(To) = "int.convert_checked";
|
||||
}
|
||||
|
||||
final impl forall [From:! IntLiteral] UInt(From) as ImplicitAs(IntLiteral) {
|
||||
final impl forall [From: IntLiteral] UInt(From) as ImplicitAs(IntLiteral) {
|
||||
fn Convert(self) -> IntLiteral = "int.convert_checked";
|
||||
}
|
||||
|
||||
// TODO: Remove these once ImplicitAs extends As.
|
||||
impl forall [To:! IntLiteral] IntLiteral as As(UInt(To)) {
|
||||
impl forall [To: IntLiteral] IntLiteral as As(UInt(To)) {
|
||||
fn Convert(self) -> UInt(To) = "int.convert_checked";
|
||||
}
|
||||
|
||||
final impl forall [From:! IntLiteral] UInt(From) as As(IntLiteral) {
|
||||
final impl forall [From: IntLiteral] UInt(From) as As(IntLiteral) {
|
||||
fn Convert(self) -> IntLiteral = "int.convert_checked";
|
||||
}
|
||||
|
||||
// Negate.
|
||||
|
||||
final impl forall [N:! IntLiteral]
|
||||
final impl forall [N: IntLiteral]
|
||||
UInt(N) as Negate where .Result = Self {
|
||||
fn Op(self) -> Self = "int.unegate";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user