compiler 0.0.2-preview.1
The Zena self-hosted compiler
compiler#
The Zena compiler, written in Zena. It parses, type-checks, and compiles Zena source code to WebAssembly (Wasm GC) and WebAssembly Text (WAT).
Usage#
Parsing a Single File#
Use compiler/parser to parse Zena source text into an AST (Module):
import { parse } from 'compiler/parser';
let ast = parse('export let answer = 42;', 'example.zena');
Loading and Checking a Program#
Use compiler (createCompiler and checkCompilation) with a CompilerHost to resolve imports across a module graph and type-check a program:
import {
createCompiler, checkCompilation, buildStdlibConfig
} from 'compiler';
import { FsCompilerHost } from 'compiler/fs-host';
let stdlibRoot = '/stdlib';
let host = new FsCompilerHost(stdlibRoot);
let packages = buildStdlibConfig(stdlibRoot, 'host');
let compiler = createCompiler(host, {
stdlibRoot,
packages,
});
let result = await compiler.compile({
entry: '/workspace/main.zena',
target: 'host',
});
let checked = checkCompilation(result);
Compiling a File to WebAssembly#
Use compiler/compile-file to compile an entry file on disk to a .wasm or .wat output file:
import { compileFile } from 'compiler/compile-file';
let ok = await compileFile({
entry: 'src/main.zena',
outPath: 'out/main.wasm',
stdlibRoot: '/stdlib',
});
Libraries#
compiler/compiler/compiler— compiler entry point (Compiler,createCompiler,checkCompilation,ProgramCheckResult)compiler/compile-file— end-to-end file compilation (compileFile,CompileFileOptions)compiler/tokenizer— lexer and token definitions (tokenize,Token,TokenType)compiler/parser— recursive-descent parser (parse,Parser)compiler/ast— AST node definitions (Node,Module,Statement,Expression,TypeAnnotation)compiler/visitor— AST traversal helpers (walk,Visitor)compiler/scope— lexical scope and symbol resolution (ScopeBuilder,ScopeResult,Symbol)compiler/checker— static type checker (checkModule,CheckResult)compiler/semantic-model— semantic queries over a checked module (SemanticModel)compiler/types— compiler type representations (Type,ClassType,FunctionType,typeToString)compiler/diagnostics— compiler diagnostics and source spans (Diagnostic,DiagnosticCode,DiagnosticSeverity,SourceSpan)compiler/package-manifest—zena-package.jsonmanifest and workspace loader (loadWorkspace,loadPackageMap)compiler/module-resolver— package and relative module specifier resolution (ModuleResolver,PackageConfig)compiler/library-loader— module graph loader (LibraryLoader,CompilerHost,SourceFile)compiler/codegen/binary-generator— WebAssembly GC binary emitter (BinaryGenerator)compiler/codegen/wat-generator— WebAssembly Text (WAT) emitter (WatGenerator)
License#
MIT
40.4 KiB
Raw
import { HashMap, HashSet, Set } from 'zena:collections';
import { growable } from 'zena:core';
import {
AccessorDeclaration, BindingPattern, BooleanLiteral, FieldDefinition,
FunctionExpression, FunctionTypeAnnotation, InlineTypeAnnotation,
LiteralTypeAnnotation, MethodDefinition, NamedTypeAnnotation, Node,
NullLiteral, NullableTypeAnnotation, NumberLiteral, RecordTypeAnnotation,
SourceLocation, StringLiteral, ThisTypeAnnotation, TuplePattern,
TupleTypeAnnotation, TypeAnnotation, TypeParameter, UnionTypeAnnotation
} from '../ast.zena';
import {
ArrayType, BoolLiteralType, BooleanType, ClassType, ErrorType,
FunctionType, InlineParamType, InlineRecordType, InlineTupleType as InlineTupleT,
InterfaceType, MixinType, NullType, NumLiteralType, RecordType as RecordT,
StringLiteralType, ThisType, TupleType as TupleT, Type, TypeAliasType,
TypeParameterType, UnionType as UnionT, applyTypeIntrinsic, baseType,
instantiateClassType, instantiateInterfaceType, instantiateMixinType,
instantiateTypeAliasType, isBigIntLiteralRaw, isNumericType, isOwnershipWrapper,
recursiveAliasKey, typeToString, unwrapAliases
} from '../types.zena';
import { DiagnosticCode } from '../diagnostics.zena';
import { CheckerContext } from './context.zena';
import { applyAndCheckTypeArguments, substituteTypeParams } from './generics.zena';
import {
rejectAffineStructuralMember, rejectConcreteSecondClassStorage,
rejectSecondClassStorage
} from './ownership.zena';
import { isAssignableTo } from './relations.zena';
import { createUnionType } from './unions.zena';
/**
* Maximum alias hops `isInlineType` and `inlineFormOf` will follow. Aliases
* nest only a few deep in practice; the bound exists because a recursive
* alias (`type T = Array<T>`) otherwise cycles forever between the
* structural and alias steps — `resolving` guards only the declaration,
* not later uses. A chain this long is already an error elsewhere, and an
* endlessly expanding alias cannot name a concrete inline tuple, so giving
* up is safe.
*/
let maxAliasHops = 32;
/**
* Whether a resolved type is an inline value: an inline tuple, a union of
* them, or an alias for either.
*
* This looks at the top only. A type argument, record field, function
* parameter or plain tuple element is resolved as a position of its own,
* which reports an inline type there, so a deeper walk would report it
* twice.
*/
export function isInlineType(t: Type, aliasHops: i32 = 0): boolean {
return match (t) {
case InlineTupleT {}: true
// It may be, so it follows the same placement rules.
case InlineParamType {}: true
case InlineRecordType {}: true
// Aliases are transparent here — `type Pair = inline (i32, i32)` is
// legal to declare, so the restriction has to be enforced through the
// name at each use. `resolving` means we are inside this alias's own
// target resolution, where reading `target` would not terminate.
case TypeAliasType as alias: {
if (alias.resolving || aliasHops >= maxAliasHops) {
false
} else {
alias.resolving = true;
let res = isInlineType(alias.target, aliasHops + 1);
alias.resolving = false;
res
}
}
case UnionT {types}: {
var found = false;
for (let member in types) {
if (isInlineType(member, aliasHops)) {
found = true;
break;
}
}
found
}
case _: false
};
}
/**
* The type a tuple pattern's element binds at an element of type `t`. A
* nested inline tuple is taken apart by a tuple pattern in place; any
* other pattern holds it as a value, which is its boxed form.
*/
export function tupleElementPatternType(element: Node, t: Type): Type {
var inner = element;
if (let BindingPattern {pattern} = inner) {
inner = pattern as Node;
}
if (inner is TuplePattern) {
return t;
}
return if (isInlineType(t)) boxedFormOf(t) else t;
}
/**
* The boxed form of an inline type: what a binding or an inferred type
* argument holds when an inline value is stored. A type that is not
* inline comes back unchanged.
*/
export function boxedFormOf(t: Type): Type {
// `unwrapAliases` reads through an inline record to its record, and an
// inline field of it is boxed too: a stored value holds no inline part.
if (let InlineRecordType {record} = t) {
return boxedRecordOf(record);
}
return match (unwrapAliases(t)) {
case InlineTupleT {elementTypes}: {
// A nested inline tuple is boxed too: a stored value holds no
// inline part.
let boxed = new Array<Type>();
for (let el in elementTypes) {
boxed.push(boxedFormOf(el));
}
new TupleT(boxed, null, 0) as Type
}
case InlineParamType {param}: param as Type
case UnionT {types}: {
if (!isInlineType(t)) {
t
} else {
let members = new Array<Type>();
for (let member in types) {
members.push(boxedFormOf(member));
}
createUnionType(members)
}
}
case _: t
};
}
/**
* Resolve the operand of `inline` to the unboxed form of its type.
*
* A tuple written in place has its elements resolved here, where they may
* be `inline` themselves: an inline value is the one place `inline` nests.
* A union distributes over its members, so `inline A | B` needs no
* modifier on `B`. Anything else (a name, above all) is resolved as
* written and then converted by `inlineFormOf`, which is where the
* operands that cannot be unboxed are reported.
*/
function resolveInlineOperand(ctx: CheckerContext, ann: TypeAnnotation): Type {
return match (ann) {
case TupleTypeAnnotation {elementTypes, spreadIndices}: {
if (spreadIndices != null) {
// A spread splices in a whole tuple type, so let the tuple arm
// resolve it; its elements then follow the storage rule.
inlineFormOf(ctx, resolveTypeAnnotation(ctx, ann), ann.loc)
} else {
let elTypes = new Array<Type>();
for (let element in elementTypes) {
let elType = resolveTypeAnnotation(ctx, element, true);
// An inline tuple nests: its slots become the outer one's. A
// nested inline union is distributed over the outer tuple's
// members (#811).
// An inline record nests the same way, its fields in order; a
// field's presence word has no position to go in yet.
let nestedRecord = inlineRecordOf(elType);
if (nestedRecord != null) {
let {optionalProperties} = nestedRecord;
if (optionalProperties != null && optionalProperties.size > 0) {
ctx.error(
"An inline record with optional fields inside an inline tuple is not supported yet.",
DiagnosticCode.TypeMismatch,
element.loc,
);
}
}
elTypes.push(elType);
}
new InlineTupleT(elTypes) as Type
}
}
case UnionTypeAnnotation {types}: {
let members = new Array<Type>();
for (let member in types) {
members.push(resolveInlineOperand(ctx, member));
}
unionOfInlineMembers(ctx, members, ann.loc)
}
// A field of an inline record may be inline itself, its values the
// outer record's (the record arm of `resolveTypeAnnotationInner`).
case RecordTypeAnnotation {}: {
ctx.inlineRecordFields = true;
let resolved = resolveTypeAnnotation(ctx, ann, true);
ctx.inlineRecordFields = false;
inlineFormOf(ctx, resolved, ann.loc)
}
case _: inlineFormOf(ctx, resolveTypeAnnotation(ctx, ann, true), ann.loc)
};
}
/**
* Reports what an inline field of an inline record cannot be yet: an
* optional field, which an inline value cannot be absent from; a union,
* whose slots do not fit one list of positions; and a record with
* optional fields, whose presence words have no position.
*/
function checkInlineRecordField(ctx: CheckerContext, t: Type, optional: boolean,
loc: SourceLocation | null) {
if (optional) {
ctx.error("An inline field cannot be optional: an inline value has no absent form.",
DiagnosticCode.TypeMismatch, loc);
return;
}
let nestedRecord = inlineRecordOf(t);
if (nestedRecord != null) {
let {optionalProperties} = nestedRecord;
if (optionalProperties != null && optionalProperties.size > 0) {
ctx.error("An inline record with optional fields inside an inline record is not supported yet.",
DiagnosticCode.TypeMismatch, loc);
}
return;
}
let unwrapped = unwrapAliases(t);
if (!(unwrapped is InlineTupleT) && !(unwrapped is InlineParamType)) {
ctx.error("An inline union inside an inline record is not supported yet.",
DiagnosticCode.TypeMismatch, loc);
}
}
/**
* `inline? T`: the argument for the type parameter `T` unboxed when it can
* be (a tuple, today), and as it is otherwise, decided per substitution
* (`InlineParamType`). On a type that is already known there is nothing
* to decide, so it is an error.
*/
function resolveInlineIfPossible(ctx: CheckerContext, ann: TypeAnnotation): Type {
let t = resolveTypeAnnotation(ctx, ann, true);
return match (t) {
case TypeParameterType as tp: new InlineParamType(tp) as Type
case ErrorType {}: t
case _: {
ctx.error(
"'inline?' applies to a type parameter: write 'inline' to unbox a known type.",
DiagnosticCode.TypeMismatch,
ann.loc,
);
new ErrorType() as Type
}
};
}
/**
* The union of converted members, or an error type when a member could not
* be converted: half an inline union would only produce follow-on errors.
*/
function unionOfInlineMembers(
ctx: CheckerContext,
members: Array<Type>,
loc: SourceLocation | null,
): Type {
for (let member in members) {
if (member is ErrorType) {
return member;
}
if (let InlineParamType {param} = member) {
// A member has to start with a literal, which an argument for the
// parameter may not.
ctx.error(
`Type parameter '${param.name}' cannot be a member of an inline union.`,
DiagnosticCode.TypeMismatch,
loc,
);
return new ErrorType();
}
}
if (!checkInlineRecordUnion(ctx, members, loc)) {
return new ErrorType();
}
return createUnionType(members, ctx, loc);
}
/**
* The rules for a union of inline records, laid out as tuples over all
* its fields (#811): every member a record, none with optional or inline
* fields, and a boolean or integer literal field every member has, with
* a different literal in each, which its slot is tested against. False,
* having reported it, when one is broken.
*/
function checkInlineRecordUnion(ctx: CheckerContext, members: Array<Type>,
loc: SourceLocation | null): boolean {
var records = 0;
for (let member in members) {
if (inlineRecordOf(member) != null) {
records += 1;
}
}
if (records == 0) {
return true;
}
if (records != members.length) {
ctx.error("An inline union cannot mix records with tuples.",
DiagnosticCode.TypeMismatch, loc);
return false;
}
let recs = new Array<RecordT>();
for (let member in members) {
let rec = inlineRecordOf(member) as RecordT;
let {optionalProperties} = rec;
if (optionalProperties != null && optionalProperties.size > 0) {
ctx.error("A member of a union of inline records cannot have optional fields yet.",
DiagnosticCode.TypeMismatch, loc);
return false;
}
for (let pair in rec.properties) {
if (isInlineType(pair.value)) {
ctx.error("A member of a union of inline records cannot have inline fields yet.",
DiagnosticCode.TypeMismatch, loc);
return false;
}
}
recs.push(rec);
}
for (let field in recs[0].properties.keys()) {
var allDiffer = true;
let seen = new Array<String>();
for (let rec in recs) {
let t = if (rec.properties.has(field)) rec.properties[field] else null;
let key = if (t != null) inlineDiscriminantKey(t) else null;
if (key == null || seen.contains(key)) {
allDiffer = false;
break;
}
seen.push(key);
}
if (allDiffer) {
return true;
}
}
ctx.error("A union of inline records needs a boolean or integer literal field with a different literal in each member, to tell them apart.",
DiagnosticCode.TypeMismatch, loc);
return false;
}
/** A boolean or integer literal type as text, or null for anything else. */
function inlineDiscriminantKey(t: Type): String | null {
if (let BoolLiteralType {value} = t) {
return if (value) 'true' else 'false';
}
if (let NumLiteralType {raw} = t) {
return if (raw.contains('.')) null else raw;
}
return null;
}
/**
* The unboxed form of a resolved type: a tuple becomes an inline tuple, a
* union converts member by member, and an alias converts its target. Every
* other type is an error rather than a no-op, so `inline` always means the
* value is unboxed.
*/
function inlineFormOf(ctx: CheckerContext, t: Type, loc: SourceLocation | null, aliasHops: i32 = 0): Type {
return match (t) {
case InlineTupleT {}: t
case ErrorType {}: t
case TupleT as tuple: {
if (tuple.row != null) {
ctx.error(
"'inline' cannot apply to a tuple type with a rest: its length is not known.",
DiagnosticCode.TypeMismatch,
loc,
);
new ErrorType() as Type
} else {
new InlineTupleT(tuple.elementTypes)
}
}
case TypeAliasType as alias: {
// `resolving` and the hop bound guard a recursive alias, as in
// `isInlineType`.
if (alias.resolving || aliasHops >= maxAliasHops) {
new ErrorType() as Type
} else {
alias.resolving = true;
let converted = inlineFormOf(ctx, alias.target, loc, aliasHops + 1);
alias.resolving = false;
converted
}
}
case UnionT {types}: {
let members = new Array<Type>();
for (let member in types) {
members.push(inlineFormOf(ctx, member, loc, aliasHops));
}
unionOfInlineMembers(ctx, members, loc)
}
// `inline` requires the unboxed form, and an argument for the
// parameter may not have one. Once types other than tuples can be
// inline, this becomes a constraint on the argument.
case TypeParameterType as tp: {
ctx.error(
`'inline' cannot apply to type parameter '${tp.name}': its argument may not be inlinable. Write 'inline? ${tp.name}' to unbox it when it is a tuple.`,
DiagnosticCode.TypeMismatch,
loc,
);
new ErrorType() as Type
}
case InlineParamType {}: t
case RecordT as record: {
if (record.isExistential || record.row != null) {
ctx.error(
if (record.isExistential) {
"'inline' cannot apply to an existential record type."
} else {
"'inline' cannot apply to a record with a row: its fields are not known."
},
DiagnosticCode.TypeMismatch,
loc,
);
new ErrorType() as Type
} else {
// Passed as its fields (docs/design/inline-parameters.md).
new InlineRecordType(record)
}
}
case InlineRecordType {}: t
case _: {
ctx.error(
`'inline' applies to tuple types and unions of them, not '${typeToString(t)}'.`,
DiagnosticCode.TypeMismatch,
loc,
);
new ErrorType() as Type
}
};
}
/**
* Whether a type parameter's bound is `record`, a contextual keyword in
* bound position (docs/design/row-types.md, Decisions: Syntax).
*/
export function isRecordRowBound(ann: TypeAnnotation | null): boolean {
if (ann is NamedTypeAnnotation) {
return ann.name == "record" && ann.typeArguments == null;
}
return false;
}
/** As `isRecordRowBound`, for `tuple`. */
export function isTupleRowBound(ann: TypeAnnotation | null): boolean {
if (ann is NamedTypeAnnotation) {
return ann.name == "tuple" && ann.typeArguments == null;
}
return false;
}
/**
* A type parameter's bound as a constraint type, or null for `record` or
* `tuple`, which are no types: the parameter is a row (`isRecordRow`,
* `isTupleRow`) instead.
*/
export function resolveBound(ctx: CheckerContext, ann: TypeAnnotation): Type | null {
if (isRecordRowBound(ann) || isTupleRowBound(ann)) {
return null;
}
return resolveTypeAnnotation(ctx, ann, false);
}
function reportRepeatedRecordField(ctx: CheckerContext, name: String, loc: SourceLocation | null) {
ctx.error(`Record type declares field '${name}' more than once.`, DiagnosticCode.DuplicateField, loc);
}
export function reportTwoRows(ctx: CheckerContext, loc: SourceLocation | null): void {
ctx.error("A record type can spread at most one row type parameter.",
DiagnosticCode.TypeMismatch, loc);
}
function reportTwoTupleRows(ctx: CheckerContext, loc: SourceLocation | null) {
ctx.error("A tuple type can spread at most one row type parameter.",
DiagnosticCode.TypeMismatch, loc);
}
function reportNotTupleSpreadable(ctx: CheckerContext, t: Type, loc: SourceLocation | null) {
ctx.error(`Cannot spread '${typeToString(t)}' into a tuple type: only a tuple type or a type parameter declared 'extends tuple' can be spread.`,
DiagnosticCode.TypeMismatch, loc);
}
function reportNotSpreadable(ctx: CheckerContext, t: Type, loc: SourceLocation | null) {
ctx.error(`Cannot spread '${typeToString(t)}' into a record type: only a record type or a type parameter declared 'extends record' can be spread.`,
DiagnosticCode.TypeMismatch, loc);
}
/**
* Records the fields a record type names beside `...row`. In the
* signature of the declaration that introduces the row parameter they
* are fields the row must lack, which instantiation checks
* (`checkRecordRowArgument`). Anywhere else, in a body, the caller has
* already chosen the row, so only fields it is known to lack can be
* named.
*/
export function noteFieldsBesideRow(
ctx: CheckerContext,
row: TypeParameterType,
fields: HashMap<String, Type>,
ann: Node
): void {
let decl = ctx.rowParameterDeclaration(row);
if (decl == null || isInRowSignature(ctx, ann, decl)) {
var lacks = row.rowLacks;
if (lacks == null) {
let created = new Set<String>();
row.rowLacks = created;
lacks = created;
}
for (let key in fields.keys()) {
lacks.add(key);
}
return;
}
let lacks = row.rowLacks;
for (let key in fields.keys()) {
if (lacks == null || !lacks.has(key)) {
ctx.error(`'${row.name}' may have a field '${key}'. Only the fields declared beside '...${row.name}' in its declaration's signature are known to be missing from it.`,
DiagnosticCode.TypeMismatch, ann.loc);
return;
}
}
}
/**
* Whether `node` is in the signature of the declaration that owns the
* type parameter `decl`, rather than in a body inside it: walking up from
* `node` reaches the owner without leaving a function body or a field
* initializer.
*/
export function isInRowSignature(ctx: CheckerContext, node: Node, decl: TypeParameter): boolean {
let owner = ctx.model.getParent(decl);
if (owner == null) {
return true;
}
var child = node;
var parent = ctx.model.getParent(node);
while (parent != null) {
let p = parent;
if (isBodyOf(p, child)) {
return false;
}
if (p === owner) {
return true;
}
child = p;
parent = ctx.model.getParent(p);
}
return true;
}
/** Whether `child` is the body of `parent`: code rather than a signature. */
function isBodyOf(parent: Node, child: Node): boolean {
return match (parent) {
case FunctionExpression {body}: body === child
case MethodDefinition {body}: body != null && body === child
case FieldDefinition {value}: value != null && value === child
case AccessorDeclaration {getter, setter}:
(getter != null && getter === child) || (setter != null && setter === child)
case _: false
};
}
/**
* The canonical `TypeParameterType`s a declaration's type parameters
* stand for, each declared in the current type scope. Every parameter
* is created before any bound or default resolves, so a bound may name
* a parameter declared after it. A bound names a class, not a value, so
* a resource class is spellable in one, as in an extends clause. Null
* for a declaration with no parameters.
*/
export function resolveTypeParameters(
ctx: CheckerContext,
declared: GrowableArray<TypeParameter> | null
): Array<TypeParameterType> | null {
if (declared == null || declared.length == 0) {
return null;
}
let params = new Array<TypeParameterType>(declared.length);
for (let tp in declared) {
let tpType = ctx.getOrCreateTypeParameter(tp, null, null);
ctx.declareTypeName(tp.name, tpType);
params.push(tpType);
}
for (var i = 0; i < declared.length; i += 1) {
let tp = declared[i];
let bound = tp.constraint;
if (bound != null) {
ctx.resourceMentionOk = true;
let constraint = resolveBound(ctx, bound);
ctx.resourceMentionOk = false;
if (constraint != null) {
params[i].constraint = constraint;
}
}
let defaultAnn = tp.defaultType;
if (defaultAnn != null) {
params[i].defaultType = resolveTypeAnnotation(ctx, defaultAnn, false);
}
}
return params;
}
export function resolveTypeAnnotation(ctx: CheckerContext, ann: TypeAnnotation, allowInlineTuple: boolean = false): Type {
let t = resolveTypeAnnotationInner(ctx, ann);
if (!allowInlineTuple) {
if (isInlineType(t)) {
ctx.error("An inline type can only be a function's result, a parameter or a local, not here.", DiagnosticCode.TypeMismatch, ann.loc);
}
}
return t;
}
/** `record` with each inline field boxed, or `record` itself when it has none. */
export function boxedRecordOf(record: RecordT): RecordT {
var changed = false;
let props = new Map<String, Type>();
for (let pair in record.properties) {
let boxed = if (isInlineType(pair.value)) boxedFormOf(pair.value) else pair.value;
if (boxed !== pair.value) {
changed = true;
}
props[pair.key] = boxed;
}
if (!changed) {
return record;
}
return new RecordT(props, record.optionalProperties, record.row, record.isExistential);
}
/** Whether `t` is an inline record, through aliases. */
export function isInlineRecordType(t: Type): boolean {
return inlineRecordOf(t) != null;
}
/**
* The union of the records a union of inline records holds, through
* aliases, or null for any other type: what a literal for it is checked
* against.
*/
export function inlineRecordUnionBoxed(t: Type): Type | null {
let u = unwrapAliases(t);
if (!(u is UnionT)) {
return null;
}
for (let member in u.types) {
if (inlineRecordOf(member) == null) {
return null;
}
}
return boxedFormOf(t);
}
/** The record an inline record type is, through aliases, or null. */
export function inlineRecordOf(t: Type): RecordT | null {
var curr = t;
var hops = 0;
while (curr is TypeAliasType && hops < maxAliasHops) {
curr = curr.target;
hops += 1;
}
return if (let InlineRecordType {record} = curr) record else null;
}
/**
* The rules an `inline` parameter's type follows beyond an inline
* result's (docs/design/inline-parameters.md). `inline? T` is decided
* per specialization, which no parameter list follows yet. An optional
* parameter is nullable, and an inline value has no absent form.
*/
export function checkInlineParameterType(ctx: CheckerContext, t: Type, optional: boolean,
loc: SourceLocation | null): void {
if (!isInlineType(t)) {
return;
}
if (t is InlineParamType) {
ctx.error("'inline?' on a parameter is not supported yet.", DiagnosticCode.TypeMismatch, loc);
} else if (optional) {
ctx.error("An inline parameter cannot be optional: an inline value has no absent form.",
DiagnosticCode.TypeMismatch, loc);
}
}
/**
* Resolve an explicit type argument at a call or `new` site.
*
* A `resource class` may be named bare here. The bare-mention rule exists so
* that the ownership regime of a *value* is always visible at its type — but a
* type argument is not a value's type, it is the thing a handle wraps.
* `adopt<Descriptor>(u)` fills a parameter the signature already spells
* `Unmanaged<T>`, and inference produces that same binding from `adopt(u)`
* with nothing written down. Rejecting the spelled-out form would reject only
* the spelling.
*
* The relaxation stops at the argument itself: it is `ctx.resourceMentionOk`,
* which `resolveTypeAnnotationInner` consumes on the outermost name and then
* re-derives per nested argument, so `Array<Descriptor>` is still an error.
*/
export function resolveExplicitTypeArgument(ctx: CheckerContext, ann: TypeAnnotation): Type {
ctx.resourceMentionOk = true;
let t = resolveTypeAnnotation(ctx, ann);
ctx.resourceMentionOk = false;
return t;
}
function resolveTypeAnnotationInner(ctx: CheckerContext, ann: TypeAnnotation): Type {
return match (ann) {
case NamedTypeAnnotation {name, typeArguments, elementsMutable}: {
if (name == "") { return new ErrorType(); }
// Resolve through scope analysis so lexical shadowing is honored.
let resolved = ctx.resolveTypeReference(ann, name);
if (resolved == null) {
ctx.error(
`Type '${name}' not found.`,
DiagnosticCode.SymbolNotFound,
ann.loc
);
return new ErrorType();
}
// `var` marks the element slot of the builtin array type constructor
// (`array<var T>`) and modifies nothing anywhere else: a type argument
// to a user generic does not name a slot. See
// docs/design/array-mutability.md.
if (elementsMutable &&
!(resolved is ArrayType && resolved.elementType is TypeParameterType)) {
ctx.error(
"The 'var' element modifier only applies to the builtin 'array' type constructor.",
DiagnosticCode.TypeMismatch,
ann.loc
);
}
// A `resource class` has no unwrapped form, so naming it bare is an
// error. Own/Borrow/Unmanaged are the only contexts that may.
let resourceOk = ctx.resourceMentionOk;
ctx.resourceMentionOk = false;
if (resolved is ClassType && resolved.isResource && !resourceOk) {
ctx.error(
"'" + name + "' is a resource class and has no unwrapped form. " +
"Write 'Own<" + name + ">', 'Borrow<" + name + ">' or 'Unmanaged<" + name + ">'.",
DiagnosticCode.TypeMismatch,
ann.loc
);
return new ErrorType();
}
// Handle generic type arguments
let resolvedArgs = new Array<Type>();
var hasTypeArgs = false;
let argsMayBeResource = isOwnershipWrapper(resolved);
if (typeArguments != null) {
if (typeArguments.length > 0) {
hasTypeArgs = true;
for (let typeArgument in typeArguments) {
ctx.resourceMentionOk = argsMayBeResource;
let resolvedArg = resolveTypeAnnotation(ctx, typeArgument);
// A generic type is a heap object, so its arguments are storage:
// `Array<Borrow<File>>` is the array case of the field rule. The
// handles themselves are the exception -- `Borrow<File>` is the
// borrow, not something holding one.
if (!argsMayBeResource) {
// Annotation position: `Array<T>` under a `scoped T` is a
// sanctioned signature shape, so the check ignores scoped
// type parameters; concrete second-class arguments still
// reject.
rejectConcreteSecondClassStorage(ctx, resolvedArg, "a container", typeArgument.loc);
}
resolvedArgs.push(resolvedArg);
}
}
}
match (resolved) {
case ClassType as ct: {
let {typeParameters} = ct;
if (typeParameters != null) {
applyAndCheckTypeArguments(ctx, typeParameters, resolvedArgs, ann.loc, ann);
return instantiateClassType(ct, resolvedArgs);
}
}
case MixinType as mt: {
let {typeParameters} = mt;
if (typeParameters != null) {
applyAndCheckTypeArguments(ctx, typeParameters, resolvedArgs, ann.loc, ann);
let inst = instantiateMixinType(mt, resolvedArgs);
let {onType} = mt;
if (onType != null) {
inst.onType = substituteTypeParams(typeParameters, resolvedArgs, onType);
}
return inst;
}
}
case InterfaceType as it: {
let {typeParameters} = it;
if (typeParameters != null) {
applyAndCheckTypeArguments(ctx, typeParameters, resolvedArgs, ann.loc, ann);
return instantiateInterfaceType(it, resolvedArgs);
}
}
case TypeAliasType as tat: {
let {typeParameters} = tat;
if (typeParameters != null) {
applyAndCheckTypeArguments(ctx, typeParameters, resolvedArgs, ann.loc, ann);
if (tat.intrinsicName != null) {
// A builtin type operator: closed arguments normalize to
// the rule's result here and now; open ones stay a
// symbolic instance that substitution later re-applies.
let opInst = instantiateTypeAliasType(tat, resolvedArgs);
opInst.target = substituteTypeParams(typeParameters, resolvedArgs, tat.target);
return applyTypeIntrinsic(opInst);
}
let target = substituteTypeParams(typeParameters, resolvedArgs, tat.target);
if (tat.isDistinct || tat.resolving) {
let inst = instantiateTypeAliasType(tat, resolvedArgs);
inst.target = target;
return inst;
}
return target;
} else {
if (hasTypeArgs) {
ctx.error(`Type '${name}' is not generic.`, DiagnosticCode.TypeMismatch, ann.loc);
}
if (tat.resolving && !tat.isDistinct) {
tat.specializationKeyCache = recursiveAliasKey(tat);
tat.uniqueKeyCache = "alias_" + recursiveAliasKey(tat);
}
return if (tat.isDistinct || tat.resolving) tat else tat.target;
}
}
case ArrayType as at: {
// Only the bare `array` prelude type is generic — its element is
// a type parameter awaiting an argument. A name that already
// resolves to a concrete array (`ByteArray` is `array<u8>`) is
// complete, and demanding an argument for it would reject every
// use of the name.
if (!(at.elementType is TypeParameterType)) {
if (hasTypeArgs) {
ctx.error(`Type '${name}' is not generic.`, DiagnosticCode.TypeMismatch, ann.loc);
}
return at;
}
if (!hasTypeArgs || resolvedArgs.length != 1) {
ctx.error("Type 'array' is generic and requires exactly 1 type argument.", DiagnosticCode.TypeMismatch, ann.loc);
return new ErrorType();
}
// `array<var T>` has mutable elements — `(array (mut T))`; bare
// `array<T>` is the immutable-element `(array T)`. See
// docs/design/array-mutability.md.
return new ArrayType(resolvedArgs[0], elementsMutable);
}
case _: {
if (hasTypeArgs) {
ctx.error(`Type '${name}' is not generic.`, DiagnosticCode.TypeMismatch, ann.loc);
}
}
}
resolved
}
case UnionTypeAnnotation {types}: {
// A handle's type argument may be a union of resource classes —
// `Own<A | B>` — so the mention permission the argument position
// carries applies to each member, not only the first one resolved
// (the NamedTypeAnnotation arm consumes the flag per name).
let memberResourceOk = ctx.resourceMentionOk;
let resolved = new Array<Type>();
for (let member in types) {
ctx.resourceMentionOk = memberResourceOk;
// A member is not a position of its own: the union is checked
// as a whole, by whoever resolves it.
let t = resolveTypeAnnotation(ctx, member, true);
resolved.push(t);
}
ctx.resourceMentionOk = false;
// Check for multiple extension types on the same underlying type.
var ei = 0;
while (ei < resolved.length) {
var ext1: ClassType | null = null;
let ct = resolved[ei];
if (ct is ClassType) {
if (ct.isExtension && ct.onType != null) {
ext1 = ct;
}
}
if (ext1 != null) {
var ej = ei + 1;
while (ej < resolved.length) {
var ext2: ClassType | null = null;
let ct = resolved[ej];
if (ct is ClassType) {
if (ct.isExtension && ct.onType != null) {
ext2 = ct;
}
}
if (ext2 != null && ext1 != null) {
let onType1 = ext1.onType;
let onType2 = ext2.onType;
if (onType1 != null && onType2 != null) {
if (isAssignableTo(ctx, onType1, onType2) && isAssignableTo(ctx, onType2, onType1)) {
ctx.error(
"Union types cannot contain multiple extension types on the same underlying type: '" +
typeToString(ext1) + "' and '" + typeToString(ext2) + "'.",
DiagnosticCode.TypeMismatch,
ann.loc
);
}
}
}
ej += 1;
}
}
ei += 1;
}
createUnionType(resolved, ctx, ann.loc)
}
case NullableTypeAnnotation {typeAnnotation}: {
// T? desugars to T | null; T?? and (T | null)? collapse in the union.
let inner = resolveTypeAnnotation(ctx, typeAnnotation, true);
if (isNumericType(inner) || inner is BooleanType) {
ctx.error(
`'${typeToString(inner)}' is not nullable: primitives cannot be null.`,
DiagnosticCode.IncompatibleTypes,
ann.loc
);
new ErrorType() as Type
} else {
let arms = growable<Type>([inner, new NullType()]);
createUnionType(arms, ctx, ann.loc)
}
}
case FunctionTypeAnnotation {params, returnType}: {
let paramTypes = new Array<Type>();
for (let param in params) {
let paramType = resolveTypeAnnotation(ctx, param);
paramTypes.push(paramType);
}
let retType = resolveTypeAnnotation(ctx, returnType, true);
new FunctionType(paramTypes, retType)
}
case LiteralTypeAnnotation {value}: {
match (value) {
case BooleanLiteral {value}: new BoolLiteralType(value)
case NumberLiteral {raw, loc}: {
if (isBigIntLiteralRaw(raw)) {
ctx.error(
`BigInt literal '${raw}' cannot be used as a literal type.`,
DiagnosticCode.TypeMismatch,
loc
);
new ErrorType()
} else {
new NumLiteralType(raw)
}
}
case StringLiteral {value}: new StringLiteralType(value)
case NullLiteral {}: new NullType()
case _: new ErrorType()
}
}
case RecordTypeAnnotation {properties, spreads, isExistential, loc}: {
let props = new Map<String, Type>();
var optionals: HashSet<String> | null = null;
// Spreading an existential type makes this one existential too: its
// values have fields neither type names.
var existential = isExistential;
// The record `inline` applies to may have inline fields; a record
// inside one of them may not, unless `inline` applies to it too.
let inlineFields = ctx.inlineRecordFields;
ctx.inlineRecordFields = false;
var anyInlineField = false;
var anyOptionalField = false;
for (let prop in properties) {
let resolved = resolveTypeAnnotation(ctx, prop.typeAnnotation, inlineFields);
if (inlineFields && isInlineType(resolved)) {
anyInlineField = true;
checkInlineRecordField(ctx, resolved, prop.optional, prop.typeAnnotation.loc);
} else if (prop.optional) {
anyOptionalField = true;
}
rejectSecondClassStorage(ctx, resolved, "a record field", prop.typeAnnotation.loc);
rejectAffineStructuralMember(ctx, resolved, "a record member", prop.typeAnnotation.loc);
if (props.has(prop.name.name)) {
reportRepeatedRecordField(ctx, prop.name.name, prop.loc);
}
props[prop.name.name] = resolved;
if (prop.optional) {
optionals ??= new Set<String>();
optionals.add(prop.name.name);
}
}
// Such a record is laid out as the tuple of its fields (#1094), which
// has no position for a presence word.
if (anyInlineField && anyOptionalField) {
ctx.error("An inline record with inline fields cannot have optional fields yet: their presence has no slot.",
DiagnosticCode.TypeMismatch, loc);
}
var row: TypeParameterType | null = null;
if (spreads != null) {
for (let spread in spreads) {
let spreadType = resolveTypeAnnotation(ctx, spread);
match (baseType(spreadType)) {
case RecordT as rec: {
// A record type's fields merge in, and its row with them.
for (let key in rec.properties.keys()) {
if (props.has(key)) {
reportRepeatedRecordField(ctx, key, spread.loc);
}
props[key] = rec.properties[key];
if (rec.isOptional(key)) {
optionals ??= new Set<String>();
optionals.add(key);
}
}
let recRow = rec.row;
if (recRow != null) {
if (row != null) {
reportTwoRows(ctx, spread.loc);
}
row = recRow;
}
if (rec.isExistential) {
existential = true;
}
}
case TypeParameterType as tp: {
if (!tp.isRecordRow) {
reportNotSpreadable(ctx, spreadType, spread.loc);
} else {
if (row != null) {
reportTwoRows(ctx, spread.loc);
}
row = tp;
}
}
case ErrorType: {}
case _: {
reportNotSpreadable(ctx, spreadType, spread.loc);
}
}
}
}
if (existential && optionals != null) {
// A presence mask's bits are laid out per record type, so the
// packs that forward an existential's fields to the record held
// cannot forward its mask yet.
ctx.error("An existential record type cannot have optional fields yet.",
DiagnosticCode.TypeMismatch, loc);
}
if (row != null) {
noteFieldsBesideRow(ctx, row, props, ann);
if (existential) {
// A row names the other fields per call; an existential leaves
// them unnamed. One type cannot do both.
ctx.error("A record type cannot spread a row parameter and also be existential.",
DiagnosticCode.TypeMismatch, loc);
existential = false;
}
}
new RecordT(props, optionals, row, existential)
}
case TupleTypeAnnotation {elementTypes, spreadIndices}: {
let elTypes = new Array<Type>();
var row: TypeParameterType | null = null;
var rowIndex = 0;
var i = 0;
while (i < elementTypes.length) {
let resolved = resolveTypeAnnotation(ctx, elementTypes[i]);
if (spreadIndices != null && spreadIndices.contains(i)) {
// `...T`: a tuple type's elements splice in, and its row with
// them; a row parameter becomes this type's row.
match (baseType(resolved)) {
case TupleT as tuple: {
let tupleRow = tuple.row;
if (tupleRow != null) {
if (row != null) {
reportTwoTupleRows(ctx, elementTypes[i].loc);
}
row = tupleRow;
rowIndex = elTypes.length + tuple.rowIndex;
}
for (let element in tuple.elementTypes) {
elTypes.push(element);
}
}
case TypeParameterType as tp: {
if (!tp.isTupleRow) {
reportNotTupleSpreadable(ctx, resolved, elementTypes[i].loc);
} else {
if (row != null) {
reportTwoTupleRows(ctx, elementTypes[i].loc);
}
row = tp;
rowIndex = elTypes.length;
}
}
case ErrorType: {}
case _: {
reportNotTupleSpreadable(ctx, resolved, elementTypes[i].loc);
}
}
} else {
rejectSecondClassStorage(ctx, resolved, "a tuple", elementTypes[i].loc);
elTypes.push(resolved);
}
i += 1;
}
new TupleT(elTypes, row, rowIndex)
}
case InlineTypeAnnotation {typeAnnotation, ifPossible}: {
if (ifPossible) {
resolveInlineIfPossible(ctx, typeAnnotation)
} else {
resolveInlineOperand(ctx, typeAnnotation)
}
}
case ThisTypeAnnotation {loc}: {
let bound = ctx.bodyThisType;
bound ?? if (ctx.currentClass != null || ctx.inInterface) {
new ThisType()
} else {
ctx.error(
"'this' type is only valid inside a class or interface.",
DiagnosticCode.TypeMismatch,
loc
);
new ErrorType()
}
}
};
}
- zena-lang:wit-parser wit-parser.zena-lang.pkg.zena-lang.dev ~0.0.1-preview.1
| Version | Published | Dependencies | Approved by |
|---|---|---|---|
| 0.0.2-preview.1 | 2026-11-19 |
|
|