feat(pbs): add parametric arrays and concrete generic contracts
Support explicit contract type parameters, canonical nominal applications, recursive substitution, concrete implementations and instantiated editor views. Complete parametric Array syntax and contextual construction integration. Preserve type origins across imports and reexports, fix trait argument stack order, and relocate global slots before lifecycle assembly. Publish specs, installed examples and consolidated discussion lessons. Validate frontend, common pipeline, architecture, LSP and installed knowledge gates, plus the full production VM conformance corpus with 14 generic fixtures.
This commit is contained in:
@@ -180,3 +180,12 @@ This document is healthy when:
|
||||
## Typed arrays and optional execution extension
|
||||
|
||||
The [typed arrays and optional execution specification](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines array types/construction/indexing, contextual literals, optional bindings and runtime storage. Its rules extend this specification, including source typing, complete groups, Struct/contract execution and diagnostics.
|
||||
|
||||
Array syntax uses `array<T>` with compositional nesting. Explicit `new array<T>(n)` supplies its type; `new array<>(n)` and `new array(n)` use the expected type. The [Array contract](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines contextual closers, spans, invariant validation and lowering; obsolete Array spellings are rejected.
|
||||
|
||||
|
||||
## Parameterized contracts
|
||||
|
||||
Contract declarations and barrel items preserve ordered type-parameter names and individual spans. Applied type references preserve ordered child arguments and spans. Implements and as carry a complete target type reference. Semantic imported bindings retain the original declaration and importing lexical scope rather than inventing nominal origins. Existing visitors must traverse type arguments as well as inner/group/Fn/tuple children.
|
||||
|
||||
See [PBS generic contracts](../../../compiler/pbs/specs/generic-contracts.md).
|
||||
|
||||
@@ -304,3 +304,12 @@ The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-a
|
||||
## Typed arrays and optional execution extension
|
||||
|
||||
The [typed arrays and optional execution specification](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines array types/construction/indexing, contextual literals, optional bindings and runtime storage. Its rules extend this specification, including source typing, complete groups, Struct/contract execution and diagnostics.
|
||||
|
||||
Array syntax uses `array<T>` with compositional nesting. Explicit `new array<T>(n)` supplies its type; `new array<>(n)` and `new array(n)` use the expected type. The [Array contract](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines contextual closers, spans, invariant validation and lowering; obsolete Array spellings are rejected.
|
||||
|
||||
|
||||
## Parameterized contracts
|
||||
|
||||
Parameterized contract diagnostics distinguish exact arity (E_SEM_CONTRACT_ARITY), undeclared/duplicate parameters (E_SEM_CONTRACT_PARAMETER), invalid applications (E_SEM_CONTRACT_ARGUMENT), incompatible or incomplete implementations (E_SEM_CONTRACT_SIGNATURE), duplicate pairs (E_SEM_CONTRACT_DUPLICATE_IMPLEMENTATION), invalid owners/modules (E_SEM_CONTRACT_OWNER / E_SEM_CONTRACT_OWNER_MODULE), invalid coercions (E_SEM_CONTRACT_COERCION), and bounded expansion/resource exhaustion (E_SEM_CONTRACT_EXPANSION). Barrel parameter disagreement uses E_LINK_CONTRACT_PARAMETERS. Existing invalid Array/Fn/optional/Self diagnostics also apply after substitution.
|
||||
|
||||
See [PBS generic contracts](../../../compiler/pbs/specs/generic-contracts.md).
|
||||
|
||||
@@ -246,3 +246,12 @@ The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-a
|
||||
## Typed arrays and optional execution extension
|
||||
|
||||
The [typed arrays and optional execution specification](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines array types/construction/indexing, contextual literals, optional bindings and runtime storage. Its rules extend this specification, including source typing, complete groups, Struct/contract execution and diagnostics.
|
||||
|
||||
Array syntax uses `array<T>` with compositional nesting. Explicit `new array<T>(n)` supplies its type; `new array<>(n)` and `new array(n)` use the expected type. The [Array contract](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines contextual closers, spans, invariant validation and lowering; obsolete Array spellings are rejected.
|
||||
|
||||
|
||||
## Parameterized contracts
|
||||
|
||||
The frontend substitutes each closed application and emits a concrete nominal Trait descriptor shared by equivalent uses. Distinct logical applications remain distinct despite shared physical carriers. Concrete implementation functions take the receiver first. CALL_TRAIT evaluates and roots the receiver before arguments, evaluates arguments left to right, then places the complete receiver/ImplementationId view after the argument groups as required by the runtime ABI. Preserve exact signatures, absence, ImplementationId zero, ownership, GC and quotas; generic calls do not add a Dynamic ABI or adaptation wrapper.
|
||||
|
||||
See [PBS generic contracts](../../../compiler/pbs/specs/generic-contracts.md).
|
||||
|
||||
@@ -167,7 +167,7 @@ BarrelVisibility ::= 'mod' | 'pub'
|
||||
|
||||
BarrelFnItem ::= BarrelVisibility 'fn' Identifier ParamList ReturnAnn? ';'
|
||||
BarrelStructItem ::= BarrelVisibility 'struct' Identifier ';'
|
||||
BarrelContractItem ::= BarrelVisibility 'contract' Identifier ';'
|
||||
BarrelContractItem ::= BarrelVisibility 'contract' Identifier TypeParameters? ';'
|
||||
BarrelHostItem ::= BarrelVisibility 'host' Identifier ';'
|
||||
BarrelErrorItem ::= BarrelVisibility 'error' Identifier ';'
|
||||
BarrelEnumItem ::= BarrelVisibility 'enum' Identifier ';'
|
||||
@@ -282,7 +282,9 @@ Top-level `let` forms and top-level executable statements are forbidden.
|
||||
TypeDecl ::= StructDecl | ServiceDecl | ContractDecl | HostDecl | BuiltinTypeDecl | ErrorDecl | EnumDecl
|
||||
StructDecl ::= 'declare' 'struct' Identifier '(' StructFieldList? ')' (StructMethodBody | ';')
|
||||
ServiceDecl ::= 'declare' 'service' Identifier ServiceBody
|
||||
ContractDecl ::= 'declare' 'contract' Identifier ContractBody
|
||||
ContractDecl ::= 'declare' 'contract' Identifier TypeParameters? ContractBody
|
||||
TypeParameters ::= '<' Identifier (',' Identifier)* '>'
|
||||
TypeArguments ::= '<' TypeRef (',' TypeRef)* '>'
|
||||
HostDecl ::= 'declare' 'host' Identifier HostBody
|
||||
BuiltinTypeDecl ::= AttrList? 'declare' 'builtin' 'type' Identifier '(' BuiltinFieldList? ')' BuiltinTypeBody
|
||||
ErrorDecl ::= 'declare' 'error' Identifier ErrorBody
|
||||
@@ -452,7 +454,7 @@ Rules:
|
||||
TypeRef ::= OptionalType | TypePrimary
|
||||
TypePrimary ::= FnType | SelfType | SimpleType | NamedTupleType | GroupType | UnitType
|
||||
SelfType ::= 'Self'
|
||||
SimpleType ::= Identifier
|
||||
SimpleType ::= Identifier TypeArguments?
|
||||
NamedTupleType ::= '(' NamedTupleField (',' NamedTupleField){0,5} ')'
|
||||
NamedTupleField ::= Identifier ':' TypeRef
|
||||
GroupType ::= '(' TypeRef ')'
|
||||
@@ -465,7 +467,7 @@ Rules:
|
||||
- Named tuple arity is 1..6 in v1 core syntax.
|
||||
- Tuple types in PBS core are always named.
|
||||
- Single-slot tuple types are allowed only in type/signature surface, not as tuple literals.
|
||||
- General user-defined generics are not part of PBS core v1.
|
||||
- User-defined parameterized declarations are supported for contracts. General generic structs and functions remain outside PBS core v1.
|
||||
- `SimpleType` may denote either a visible nominal type identifier or one of the predeclared builtin simple types.
|
||||
- The predeclared builtin simple types in v1 core are `int`, `float`, `bool`, and `str`.
|
||||
- Builtin simple types are always available in type position and do not require import.
|
||||
@@ -610,7 +612,7 @@ Rules:
|
||||
- `new Type.ctorName(args...)` is the canonical named-constructor surface.
|
||||
- Struct construction arguments are positional and follow the declared struct field order.
|
||||
- `as` is reserved in expressions for explicit contract-view conversion only; it does not denote a general cast surface in v1 core.
|
||||
- The right operand of expression-form `as` is a simple contract identifier already visible in scope, never a qualified module path.
|
||||
- A contract-view `as` target is a visible contract type, including explicit arguments such as `Identity<int>`, never a qualified module path. Existing numeric conversions retain their value-alignment rules.
|
||||
- `this` is an implicit receiver name available only inside struct method, service method, and `ctor` bodies.
|
||||
- `Self` is valid only inside struct method declarations, service method declarations, and `ctor` declarations.
|
||||
- `apply` is right-associative: `f1 apply f2 apply x` parses as `f1 apply (f2 apply x)`.
|
||||
@@ -1200,3 +1202,12 @@ fn test(e: Enum) -> str {
|
||||
The [typed arrays and optional execution specification](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines array types/construction/indexing, contextual literals, optional bindings and runtime storage. Its rules extend this specification, including source typing, complete groups, Struct/contract execution and diagnostics.
|
||||
|
||||
Retired `declare callback`, callback exports and `bind(...)` are rejected. They do not provide alternative spellings for Fn or lambdas.
|
||||
|
||||
Array syntax uses `array<T>` with compositional nesting. Explicit `new array<T>(n)` supplies its type; `new array<>(n)` and `new array(n)` use the expected type. The [Array contract](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines contextual closers, spans, invariant validation and lowering; obsolete Array spellings are rejected.
|
||||
|
||||
|
||||
## Parameterized contracts
|
||||
|
||||
Parameterized contracts use explicit angular arguments in annotations, nested types, implements and as. Barrels export the ordered parameter list. Raw generic uses, empty argument lists and default arguments are invalid; existing contextual closers and Array construction sugars remain unchanged.
|
||||
|
||||
See [PBS generic contracts](../../../compiler/pbs/specs/generic-contracts.md).
|
||||
|
||||
@@ -388,7 +388,7 @@ Rules:
|
||||
- Services do not have fields.
|
||||
- Services do not declare named constructors and are never targets of `new`.
|
||||
- Contract implementations do not inject their methods into the direct method namespace of the concrete struct or service.
|
||||
- `implements Contract for Owner using s { ... }` is unique per `(Contract, Owner)` pair, where `Owner` is a declared struct or service.
|
||||
- `implements Contract for Owner using s { ... }` is unique per `(instantiated Contract, Owner)` pair, where `Owner` is a declared struct or service.
|
||||
- A contract for a struct or service may be implemented only in the module where that owner itself is declared.
|
||||
- Contracts may be declared in other modules and implemented for the local struct or service in its owner module.
|
||||
- An `implements` block has no independent visibility; its observability follows the visibility of the owner declaration, and the contract must also be nameable at the use site.
|
||||
@@ -396,7 +396,7 @@ Rules:
|
||||
- A struct value or service singleton value may be coerced to a contract value when exactly one visible `implements Contract for Owner ...` applies.
|
||||
- Contract coercion may happen by explicit `expr as ContractType` or by contextual typing in assignments, parameter passing, and returns.
|
||||
- `as` in expression position is reserved for explicit contract coercion only; no other cast semantics exist in v1 core.
|
||||
- The contract name in `expr as ContractName` must be a simple visible identifier resolved through ordinary scope and import rules.
|
||||
- The contract in `expr as ContractType` is resolved through ordinary scope/import rules and may carry explicit type arguments. Aliases preserve declaration identity.
|
||||
- A contract value carries the underlying struct reference or service singleton value together with the selected contract implementation for runtime dispatch.
|
||||
- Calling `contractValue.method(...)` uses dynamic dispatch through the contract value.
|
||||
- Calling `structValue.contractMethod(...)` is not valid unless that method is also declared directly on the struct.
|
||||
@@ -821,3 +821,12 @@ The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-a
|
||||
## Typed arrays and optional execution extension
|
||||
|
||||
The [typed arrays and optional execution specification](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines array types/construction/indexing, contextual literals, optional bindings and runtime storage. Its rules extend this specification, including source typing, complete groups, Struct/contract execution and diagnostics.
|
||||
|
||||
Array syntax uses `array<T>` with compositional nesting. Explicit `new array<T>(n)` supplies its type; `new array<>(n)` and `new array(n)` use the expected type. The [Array contract](../../../compiler/pbs/specs/typed-arrays-and-optional-execution.md) defines contextual closers, spans, invariant validation and lowering; obsolete Array spellings are rejected.
|
||||
|
||||
|
||||
## Parameterized contracts
|
||||
|
||||
Application identity combines the defining nominal declaration with ordered canonical logical arguments after alias resolution. Applications are invariant. Substitution preserves existing type-position restrictions, names, labels and complete groups across Array, Fn, optional, tuples and nested contracts. A struct/service may implement several applications, with complete unique implementations in its owner module. Finite instance recursion is accepted within resource limits; unbounded expansion receives a bounded diagnostic.
|
||||
|
||||
See [PBS generic contracts](../../../compiler/pbs/specs/generic-contracts.md).
|
||||
|
||||
+7
@@ -556,3 +556,10 @@ dot and signature help during incomplete arguments. An unrelated parse error MUS
|
||||
NOT invalidate a recoverable receiver. Unknown or ambiguous receivers MUST NOT
|
||||
invent a type or overload. Indexing/operator-produced receivers are outside the
|
||||
new coverage; existing behavior MUST NOT regress.
|
||||
|
||||
|
||||
## Parameterized contracts
|
||||
|
||||
A barrel exports a parameterized contract declaration with the same ordered parameters as its source declaration. Imported bindings retain the original declaration and lexical source scope, including aliases in signatures of dependencies. A local alias never creates another nominal type. Implementations remain in the declaring module of the concrete struct/service owner.
|
||||
|
||||
See [PBS generic contracts](../../../compiler/pbs/specs/generic-contracts.md).
|
||||
|
||||
@@ -139,3 +139,10 @@ That means:
|
||||
The `@sdk:random` module exports `Random` and `random`. Use an explicit long seed;
|
||||
copying a generator shares its state. Read `pbs:spec:runtime-random-integration.md`
|
||||
and `pbs:example:game:1:src/random.pbs` for the exact contract and a small example.
|
||||
|
||||
|
||||
## Parameterized contracts
|
||||
|
||||
Parameterized nominal contracts, concrete frontend specialization, implementation/coercion rules, composition, recursion and production conformance are defined by the owner specification.
|
||||
|
||||
See [PBS generic contracts](../../../compiler/pbs/specs/generic-contracts.md).
|
||||
|
||||
Reference in New Issue
Block a user