compiler 0.0.2-preview.1

The Zena self-hosted compiler

Readme Files Dependencies Versions

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.json manifest 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

48.8 KiB Raw
import { isValidIntrinsic } from '../intrinsics.zena';
import { HashSet, Set } from 'zena:collections';
import { growable } from 'zena:core';
import {
  AccessorDeclaration, AccessorSignature, BlockStatement, DeclareFunction,
  Decorator, EnumDeclaration, Expression, FieldDefinition, Identifier,
  InterfaceDeclaration, MethodDefinition, MethodSignature, MixinDeclaration,
  Node, NumberLiteral, PropertySignature, SetterInfo, SourceLocation,
  StringLiteral, SymbolDeclaration, TypeAliasDeclaration, TypeAnnotation,
  TypeParameter, UnaryExpression, decoratorStringArg,
  isSymbolFieldDeclaration, syntacticMemberName
} from '../ast.zena';
import {
  AnyRefType, ClassType, EnumType, ErrorType, FieldInfo, FunctionDeclInfo,
  FunctionType, HoleType, I32Type, InterfaceType, MethodInfo, MixinType,
  RecordType as RecordT, Type, TypeAliasType, TypeParameterType, VoidType,
  baseType, copyRecursiveAliasKeys, instantiateInterfaceType,
  isBigIntLiteralRaw, typeToString
} from '../types.zena';
import { DiagnosticCode } from '../diagnostics.zena';
import { noSymbolId } from '../scope/symbol.zena';
import { WellKnown, protocolSymbolKey } from '../well-known.zena';
import { adaptToExpected, checkAndRegisterInterfaceAdaptation } from './adaptation.zena';
import { checkInlineParameterType, resolveTypeAnnotation, resolveTypeParameters } from './annotations.zena';
import {
  checkAccessorBodies, checkMethodBody, checkMethodParameterDefaults,
  checkMethodSignature, validateDecoratorBody
} from './classes.zena';
import { BodyContext, CheckerContext, newEnumType } from './context.zena';
import { rejectContracts } from './contracts.zena';
import { checkExpression } from './expressions.zena';
import { selfTypeOfClass } from './generics.zena';
import {
  displayMemberName, getMemberName, recordSymbolKeyedMemberIdentities,
  registerStaticSymbols
} from './members.zena';
import { prescanPendingFunctions } from './module.zena';
import { branchDefinitelyExits } from './narrowing.zena';
import { registerOwnParamCandidates } from './ownership.zena';
import { bindPattern } from './patterns.zena';
import { isAssignableTo } from './relations.zena';
import { checkStatement } from './statements.zena';

/**
 * Fills a transparent alias's canonical type object in place, the way
 * registerClassDeclaration fills a class shell: the object's identity
 * is stable across import-cycle re-checks, its type parameters and
 * target are re-resolved each pass. This is what lets a transparent
 * alias cross an import cycle's back edge — an importer holds the shell
 * and the origin fills its body here (docs/design/import-cycles.md).
 */
/**
 * The target a body-less type operator carries.
 *
 * `type.scopedFrom` expands to `Scoped<R>`, so its target names the
 * `Scoped` declaration, which substitution instantiates without a checker
 * context (types.zena `scopedFromTypeOf`). Any other operator's target is
 * its own first parameter, which is what makes an open instance erase and
 * key exactly as its argument does (docs/design/type-operators.md); a
 * closed instance never survives to read the target at all.
 *
 * Registration sets this, not the statement check: on an import cycle an
 * importer registers signatures naming the operator before the declaring
 * module's statements are checked, and an instance copies the target it
 * finds.
 */
export function typeOperatorTarget(
  ctx: CheckerContext,
  alias: TypeAliasType,
  typeParams: Array<TypeParameterType> | null
): Type {
  if (alias.intrinsicName == 'type.scopedFrom') {
    let scopedDecl = ctx.tryResolveWellKnownType(WellKnown.ScopedAlias);
    if (scopedDecl != null && scopedDecl is TypeAliasType) {
      return scopedDecl;
    }
  }
  if (typeParams != null && typeParams.length > 0) {
    return typeParams[0];
  }
  return new HoleType();
}

export function registerTypeAliasDeclaration(ctx: CheckerContext, ta: TypeAliasDeclaration): void {
  let t = ctx.declaredTypeOf(ta);
  if (t == null) {
    return;
  }
  var aliasType: TypeAliasType | null = null;
  match (t) {
    case TypeAliasType as a: { aliasType = a; }
    case _: { return; }
  }
  let alias = aliasType;
  ctx.enterScope();
  let typeParams = resolveTypeParameters(ctx, ta.typeParameters);
  alias.typeParameters = typeParams;
  // The alias may be the shell an importer across an import cycle made,
  // which knows only the name and parameters: registration supplies the
  // rest the declaration determines, the operator a decorated alias
  // names included, or `Awaited<T>` would read as an ordinary alias of
  // its placeholder.
  alias.intrinsicName = null;
  for (let dec in ta.decorators) {
    if (dec.name == 'intrinsic' && dec.args.length == 1) {
      alias.intrinsicName = decoratorStringArg(dec, 0);
    }
  }
  alias.resolving = true;
  let bodyAnn = ta.typeAnnotation;
  if (bodyAnn != null) {
    // Re-resolve the target so a cross-cycle reference picks up the
    // origin's now-filled types on the second pass. Diagnostics are
    // suppressed: resolveTypeName above already materialized the alias
    // and reported any target errors (union-mixing, inline-tuple
    // position) once — re-reporting them here doubles them (and this
    // runs on every cyclic re-check pass besides).
    ctx.setDiagnosticsSuppressed(true);
    alias.target = resolveTypeAnnotation(ctx, bodyAnn, true);
    ctx.setDiagnosticsSuppressed(false);
    copyRecursiveAliasKeys(alias, alias.target);
  } else {
    // A `declare type` operator has no body.
    alias.target = typeOperatorTarget(ctx, alias, typeParams);
  }
  alias.resolving = false;
  ctx.exitScope();
}

// ============================================================================
// Enum Declaration
// ============================================================================

// ============================================================================
// Type Alias Declaration
// ============================================================================
export function checkTypeAliasDeclaration(ctx: CheckerContext, ta: TypeAliasDeclaration): void {
  ctx.declaredTypeOf(ta);
  if (ta.typeAnnotation != null) {
    // Decorators on an ordinary alias never parse ("Expected 'declare'
    // after decorator"), so a body means there is nothing to validate.
    return;
  }
  // The bodyless form declares a builtin type operator, and is
  // meaningless without one.
  if (ta.decorators.length == 0) {
    ctx.error("A 'declare type' requires an @intrinsic type-operator decorator.",
        DiagnosticCode.TypeMismatch, ta.name.loc);
    return;
  }
  for (let dec in ta.decorators) {
    if (dec.name != 'intrinsic') {
      ctx.error(`Unknown decorator '@${dec.name}' on a type alias.`,
          DiagnosticCode.TypeMismatch, dec.loc);
    } else if (dec.args.length != 1 || decoratorStringArg(dec, 0) == null) {
      ctx.error("Intrinsic decorator must have exactly one argument (the intrinsic name).",
          DiagnosticCode.TypeMismatch, dec.loc);
    } else if (decoratorStringArg(dec, 0) == 'type.scopedFrom') {
      let tps2 = ta.typeParameters;
      if (tps2 == null || tps2.length != 2) {
        ctx.error("The 'type.scopedFrom' operator takes exactly two type parameters.",
            DiagnosticCode.TypeMismatch, ta.name.loc);
      }
      // Registration points the operator at the Scoped declaration
      // (typeOperatorTarget); here it only has to exist.
      let scopedDecl = ctx.tryResolveWellKnownType(WellKnown.ScopedAlias);
      if (scopedDecl == null || !(scopedDecl is TypeAliasType)) {
        ctx.error("The 'type.scopedFrom' operator needs 'zena:core', which declares 'Scoped'.",
            DiagnosticCode.TypeMismatch, ta.name.loc);
      }
    } else if (decoratorStringArg(dec, 0) != 'type.awaited' &&
        decoratorStringArg(dec, 0) != 'type.withDefault') {
      ctx.error(`Unknown type intrinsic '${decoratorStringArg(dec, 0) as String}'.`,
          DiagnosticCode.TypeMismatch, dec.loc);
    } else {
      let tps = ta.typeParameters;
      if (tps == null || tps.length != 1) {
        ctx.error(`The '${decoratorStringArg(dec, 0) as String}' operator takes exactly one type parameter.`,
            DiagnosticCode.TypeMismatch, ta.name.loc);
      }
    }
  }
}

// ============================================================================
// Enum Declaration
// ============================================================================

/**
 * Whether an integer enum member's initializer is a number literal,
 * which may have a minus sign: `-32700` parses as a negation.
 */
function isEnumNumberLiteral(initializer: Expression): boolean {
  if (initializer is NumberLiteral) {
    return !isBigIntLiteralRaw(initializer.raw);
  }
  if (let UnaryExpression {operator, argument} = initializer) {
    return operator == '-' && argument is NumberLiteral && !isBigIntLiteralRaw(argument.raw);
  }
  return false;
}

export function checkEnumDeclaration(ctx: CheckerContext, ed: EnumDeclaration): void {
  let i32Type = new I32Type();
  let stringType = ctx.tryResolveWellKnownType(WellKnown.StringClass);
  let {name} = ed.name;
  let {members} = ed;
  var isStringEnum = false;

  if (members.length == 0) {
    ctx.error(
      `Enum '${name}' must have at least one member.`,
      DiagnosticCode.TypeMismatch,
      ed.loc
    );
    // We don't return early so that we still register type and value names for
    // the enum.
  } else {
    // Validate members in a single pass. The first enum member determins the
    // enum type: numeric by default, or based on the initializer.
    isStringEnum = members[0].initializer is StringLiteral;

    if (isStringEnum) {
      for (let member in members) {
        if (member.initializer == null) {
          ctx.error(
            `String enum member '${member.name.name}' must have an initializer.`,
            DiagnosticCode.TypeMismatch,
            member.loc
          );
        } else {
          let initType = checkExpression(ctx, member.initializer, null);
          if (!(member.initializer is StringLiteral)) {
            ctx.error(
              "Enum member initializer must be a string literal.",
              DiagnosticCode.TypeMismatch,
              member.loc
            );
          }
        }
      }
    } else {
      for (let member in members) {
        if (member.initializer != null) {
          let initType = checkExpression(ctx, member.initializer, null);
          if (!isAssignableTo(ctx, initType, i32Type) ||
              !isEnumNumberLiteral(member.initializer as Expression)) {
            ctx.error(
              "Enum member initializer must be an i32 literal.",
              DiagnosticCode.TypeMismatch,
              member.loc
            );
          }
        }
      }
    }
  }

  let strType = stringType ?? new ErrorType() as Type;
  let backingType = if (isStringEnum) { strType } else { i32Type as Type };
  // The enum's canonical type, which an importer across an import cycle
  // may already hold: fill in the backing type.
  let info = ctx.model.resolveDeclaration(ed);
  let declared = if (info != null) { ctx.declaredType(info) } else { null };
  let enumType = if (declared is EnumType) {
    declared
  } else {
    newEnumType(noSymbolId, ctx.filePath, ed)
  };
  enumType.backingType = backingType;

  // The enum's name is the type, and the value its members are read off.
  ctx.declareTypeName(name, enumType);
  if (info != null) {
    ctx.registerType(info, enumType as Type);
    ctx.declareBinding(info, enumType as Type);
    ctx.model.addDeclaredEnum(info.id, ed);
  }
}

// ============================================================================
// Mixin Declaration
// ============================================================================
export function registerMixinDeclaration(ctx: CheckerContext, md: MixinDeclaration): void {
  let mixinName = md.name.name;

  // Look up pre-registered MixinType, or create one.
  var mixinType = new MixinType(mixinName);
  let preRegistered = ctx.declaredTypeOf(md);
  if (preRegistered != null) {
    if (preRegistered is MixinType) { mixinType = preRegistered; }
  }

  // Record the declaring file for cross-module lookup and reachability.
  mixinType.sourcePath = ctx.filePath;

  // Registration must be idempotent, as a class's is: a member of an
  // import cycle registers twice against the one canonical MixinType
  // (SharedCheckerState.declaredNominalTypes), so each pass resets what
  // it rebuilds; appending to the previous pass's state would list every
  // composed mixin twice. The members map is cleared in place because
  // instantiations share it.
  mixinType.members.clear();
  mixinType.staticMembers.clear();
  mixinType.mixins = new Array<MixinType>();
  mixinType.sharedMembers = null;

  // Before the body, for the same reason as a class's.
  registerStaticSymbols(
      ctx, mixinName, mixinType.staticSymbols, md.body);

  // Type parameters.
  ctx.enterScope();
  if (md.typeParameters != null) {
    mixinType.typeParameters = resolveTypeParameters(ctx, md.typeParameters);
  }

  // A decorator's subject: the placeholder field its body is written
  // against. Resolved under the type parameters, so `(var value: T)`
  // names the parameter the decorated field's type will bind.
  let {decoratorSubject} = md;
  if (decoratorSubject != null) {
    let subject = decoratorSubject;
    mixinType.decoratorSubjectName = syntacticMemberName(subject.name);
    mixinType.decoratorSubjectMutable =
        subject.mutability != null && subject.mutability == "var";
    mixinType.decoratorSubjectType = if (subject.typeAnnotation != null) {
      resolveTypeAnnotation(ctx, subject.typeAnnotation as TypeAnnotation)
    } else {
      new ErrorType() as Type
    };
  }

  // Resolve 'on' type.
  let {on} = md;
  if (on != null) {
    let onTypeId = on;
    let onResolved = resolveTypeAnnotation(ctx, onTypeId);
    if (onResolved != null) {
      match (onResolved) {
        case ClassType as onClass: {
          mixinType.onType = onClass;
        }
        case InterfaceType as onIface: {
          mixinType.onType = onIface;
        }
        case _: {
          ctx.error(
            `Mixin 'on' type must be a class or interface, got '${typeToString(onResolved)}'.`,
            DiagnosticCode.TypeMismatch,
            md.loc
          );
        }
      }
    } else {
      ctx.error(
        "Type not found in 'on' clause.",
        DiagnosticCode.SymbolNotFound,
        md.loc
      );
    }
  }

  // A decorator body's `context`: the decorated field's name and its
  // accessors as functions over the host, `{name: String, get: (host:
  // Host) => T, set: (host: Host, value: T) => void}`, with `set` only
  // for a `var` subject. `Host` is the `on` type; `anyref` when the
  // decorator constrains nothing. One record type serves every
  // application: codegen substitutes `T` per application and
  // materializes the record from the field's name and accessors
  // (docs/design/decorators.md, "The decorated field's context").
  if (decoratorSubject != null && decoratorSubject.name is Identifier) {
    let contextSym = ctx.model.resolveDeclaration(decoratorSubject.name);
    if (contextSym != null && contextSym.isDecoratorContext) {
      let stringType = ctx.tryResolveWellKnownType(WellKnown.StringClass);
      let hostType: Type = mixinType.onType ?? new AnyRefType();
      let subjectT: Type = mixinType.decoratorSubjectType ?? new ErrorType();
      let props: HashMap<String, Type> = {
        "name" => stringType ?? new ErrorType(),
        "get" => new FunctionType(growable<Type>([hostType]), subjectT),
      };
      if (mixinType.decoratorSubjectMutable) {
        props["set"] = new FunctionType(growable<Type>([hostType, subjectT]), new VoidType());
      }
      ctx.declareBinding(contextSym, new RecordT(props, null, null, false));
    }
  }

  // Resolve composed mixins.
  let {mixins} = md;
  if (mixins != null) {
    let mixinAnns = mixins;
    for (let ma in mixinAnns) {
      let composedType = resolveTypeAnnotation(ctx, ma);
      match (composedType) {
        case MixinType as composedMixin: {
          // The composed mixin's members are read through `mixins` by
          // the mixin's member table, its privates under the composed
          // mixin's scope key (MemberTable).
          mixinType.mixins.push(composedMixin);
        }
        case _: {
          if (!(composedType is ErrorType)) {
            ctx.error(
              `'${typeToString(composedType)}' is not a mixin.`,
              DiagnosticCode.TypeMismatch,
              md.loc
            );
          }
        }
      }
    }
  }

  // 1. Create a synthetic ClassType for "this" inside the mixin early
  let thisType = new ClassType(ctx.freshTypeId(), noSymbolId, mixinName + "_This");
  // Mixin privates are lexically scoped to the mixin declaration and
  // shared across applications (member-lookup.md §6). This key
  // namespaces the mixin's private FIELDS inside every host class:
  // the checker stores them under "<scopeKey>::#name" at application
  // and codegen names the struct fields identically, so host and
  // mixin same-named privates never collide. Names alone are not
  // unique, so the key is the same identity MixinKey uses — the
  // declaration's name plus its source path — making two same-named
  // mixins from different modules distinct scopes. Every consumer
  // reads the key from the MixinType / This type (never rederives it
  // from the name).
  let mixinScopeKey = `${mixinName}_This@${mixinType.sourcePath}`;
  thisType.privateScopeKey = mixinScopeKey;
  mixinType.privateScopeKey = mixinScopeKey;
  
  let {onType} = mixinType;
  if (onType != null) {
    match (baseType(onType)) {
      case ClassType as ct: {
        // The class's members reach `this` through the supertype.
        thisType.superType = onType as ClassType;
      }
      case InterfaceType as it: {
        // The interface's members reach `this` through `onType`
        // (MemberTable), which a class inherits nothing from otherwise.
        thisType.implementsList.push(onType as InterfaceType);
        thisType.onType = onType;
      }
      case _: {}
    }
  }

  // `this` sees the mixin's members, composed ones included, through
  // the mixin itself (MemberTable).
  thisType.mixinSelf = mixinType;
  mixinType.selfType = thisType;

  // Process body members.
  ctx.enterInterface();
  var b_idx = 0;
  while (b_idx < md.body.length) {
    let member = md.body[b_idx];
    match (member) {
      case FieldDefinition as fd: {
        // `static symbol sym;` is shaped as a static field annotated
        // `symbol`, but it declares a symbol rather than storage: it is
        // registered on the type's staticSymbols and has no member and no
        // type of its own. Registering it as a field asks for a type
        // called `symbol`, which does not exist.
        if (isSymbolFieldDeclaration(fd)) {
          b_idx = b_idx + 1;
          continue;
        }
        let fieldName = getMemberName(ctx, fd.name);
        let fieldType = if (fd.typeAnnotation != null) {
          resolveTypeAnnotation(ctx, fd.typeAnnotation as TypeAnnotation)
        } else {
          new ErrorType() as Type
        };
        let isMutable = fd.mutability != null && fd.mutability == "var";
        let isPrivate = fieldName.length > 0 && fieldName.sliceBytes(0, 1) == "#";
        let fdInfo = new FieldInfo(
          fieldType,
          isMutable,
          fd.isAbstract,
          fd.isStatic,
          isPrivate,
          fd.value != null // hasInitializer
        );
        mixinType.memberTable(fd.isStatic)[fieldName] = fdInfo;
        if (fd.isShared) {
          if (md.decoratorSubject == null) {
            ctx.error("'shared' applies only to a decorator's private members.",
                DiagnosticCode.TypeMismatch, fd.loc);
          } else {
            let shared = mixinType.sharedMembers ?? new Set<String>();
            mixinType.sharedMembers = shared;
            shared.add(fieldName);
          }
        }
      }
      case MethodDefinition as methDef: {
        let methodName = getMemberName(ctx, methDef.name);

        // Reject constructors in mixins.
        if (methDef.isConstructor || methodName == "<constructor>") {
          ctx.error(
            "Mixins cannot define constructors.",
            DiagnosticCode.IncompatibleTypes,
            methDef.loc
          );
        } else {
          // A mixin's method may not declare a receiver. Which handle `this`
          // is depends on the class the mixin is applied to, so the answer
          // is not this declaration's to give — and a mixin on a resource is
          // ownership.md's own open question. Rejected rather than ignored:
          // a silently dropped `Own<this>` would read as consuming.
          if (methDef.receiver != null) {
            ctx.error(
              "A mixin's method may not declare a receiver: a mixin is applied to " +
              "classes it does not know, so what 'this' is there is the host class's " +
              "to say.",
              DiagnosticCode.TypeMismatch,
              methDef.loc
            );
          }
          let ft = checkMethodSignature(ctx, thisType, methDef);
          let isPrivate = methodName.length > 0 && methodName.sliceBytes(0, 1) == "#";
          let mi = new MethodInfo(
            ft,
            methDef.isAbstract,
            methDef.isStatic,
            isPrivate,
            methDef.isFinal
          );
          mixinType.memberTable(methDef.isStatic)[methodName] = mi;
          if (methDef.isShared) {
            if (md.decoratorSubject == null) {
              ctx.error("'shared' applies only to a decorator's private members.",
                  DiagnosticCode.TypeMismatch, methDef.loc);
            } else {
              let shared = mixinType.sharedMembers ?? new Set<String>();
              mixinType.sharedMembers = shared;
              shared.add(methodName);
            }
          }
        }
      }
      case AccessorDeclaration as acc: {
        let accType = resolveTypeAnnotation(ctx, acc.typeAnnotation);
        let accName = getMemberName(ctx, acc.name);
        let isPrivate = accName.length > 0 && accName.sliceBytes(0, 1) == "#";
        let members = mixinType.memberTable(acc.isStatic);
        if (isPrivate) {
          // Private accessors register only their get#/set# methods,
          // like class accessors: no bare-name entry (a bare
          // FieldInfo would create a phantom struct field at
          // application, and the bare getter-MethodInfo mistyped
          // reads as '() => T').
          if (acc.getter != null) {
            let getterFt = new FunctionType(new Array<Type>(), accType);
            let gi = new MethodInfo(getterFt, false, acc.isStatic, true, acc.isFinal);
            members["get#" + accName] = gi;
          }
          if (acc.setter != null) {
            let setterParams = growable<Type>([accType]);
            let setterFt = new FunctionType(setterParams, new VoidType());
            let si = new MethodInfo(setterFt, false, acc.isStatic, true, acc.isFinal);
            members["set#" + accName] = si;
          }
        } else {
          // Public accessors register get#/set# too, like class
          // accessors and like the private ones above. A bare-name
          // entry does not describe an accessor: a FieldInfo mints a
          // phantom struct field at application, and a getter
          // MethodInfo reads as '() => T' at the use site instead of
          // T. The setter had no entry at all.
          if (acc.getter != null) {
            let getterFt = new FunctionType(new Array<Type>(), accType);
            let gi = new MethodInfo(getterFt, false, acc.isStatic, isPrivate, acc.isFinal);
            members["get#" + accName] = gi;
          }
          if (acc.setter != null) {
            let setterParams = growable<Type>([accType]);
            let setterFt = new FunctionType(setterParams, new VoidType());
            let si = new MethodInfo(setterFt, false, acc.isStatic, isPrivate, acc.isFinal);
            members["set#" + accName] = si;
          }
        }
      }
      case _: {}
    }
    b_idx += 1;
  }
  ctx.exitInterface();

  // Register mixin type early so it's available in model during body checking.
  ctx.declareTypeName(mixinName, mixinType);
  let info = ctx.model.resolveDeclaration(md);
  if (info != null) {
    mixinType.symbolId = info.id;
    ctx.declareBinding(info, mixinType);
    ctx.model.addDeclaredMixin(info.id, md);
  }

  // As for an interface: members are registered and the mixin's name is
  // bound, so its symbol keys can be checked, stamped and reported.
  recordSymbolKeyedMemberIdentities(
      ctx, mixinType as Type, mixinType.members, md.body);

  if (md.decoratorSubject != null) {
    validateDecoratorBody(ctx, md, mixinType);
  }

  ctx.exitScope();
}

export function checkMixinBodies(ctx: CheckerContext, md: MixinDeclaration): void {
  let resolved = ctx.declaredTypeOf(md) ?? ctx.resolveTypeName(md.name.name);
  if (!(resolved is MixinType)) {
    return;
  }
  let mixinType = resolved;
  let {selfType} = mixinType;
  if (selfType == null) {
    return;
  }
  let thisType = selfType;

  ctx.enterScope();
  let {typeParameters} = mixinType;
  if (typeParameters != null) {
    for (let tp in typeParameters) {
      ctx.declareTypeName(tp.name, tp);
    }
  }

  let {decoratorSubject} = md;
  if (decoratorSubject != null && decoratorSubject.name is Identifier) {
    let contextSym = ctx.model.resolveDeclaration(decoratorSubject.name);
    if (contextSym != null && contextSym.isDecoratorContext) {
      let existing = ctx.model.getBindingTypeDirect(contextSym);
      if (existing != null) {
        ctx.declareBinding(contextSym, existing);
      }
    }
  }

  // Mixin and decorator field initializers run before `this` is bound.
  ctx.enterInterface();
  for (let member in md.body) {
    if (member is FieldDefinition) {
      if (!isSymbolFieldDeclaration(member) && member.value != null) {
        let fieldName = getMemberName(ctx, member.name);
        let members = mixinType.memberTable(member.isStatic);
        if (members.has(fieldName)) {
          let fieldType = members[fieldName].type;
          let value = member.value as Expression;
          ctx.enterNodeReferrer(member.id as i32);
          let initType = checkExpression(ctx, value, fieldType);
          if (!isAssignableTo(ctx, initType, fieldType)) {
            ctx.error(
              `Type mismatch: field '${displayMemberName(fieldName)}' has type '${typeToString(fieldType)}' but initializer has type '${typeToString(initType)}'.`,
              DiagnosticCode.TypeMismatch,
              member.loc
            );
          } else {
            adaptToExpected(ctx, value, initType, fieldType);
          }
          ctx.exitNodeReferrer(member.id as i32);
        }
      }
    }
  }
  ctx.exitInterface();

  ctx.enterClass(thisType);
  let prevBodyThis = ctx.bodyThisType;
  ctx.setBodyThisType(selfTypeOfClass(thisType));

  for (let member in md.body) {
    match (member) {
      case MethodDefinition as methDef: {
        let methodName = getMemberName(ctx, methDef.name);
        let members = mixinType.memberTable(methDef.isStatic);
        if (!methDef.isConstructor && methodName != "<constructor>" && members.has(methodName)) {
          if (let (true, methodTypeInfo) = members.get(methodName)) {
            let ft = methodTypeInfo.type as FunctionType;
            checkMethodParameterDefaults(ctx, thisType, ft, methDef);
            if (methDef.body != null) {
              ctx.enterNodeReferrer(methDef.id as i32);
              checkMethodBody(ctx, thisType, ft, methDef);
              ctx.exitNodeReferrer(methDef.id as i32);
            }
          }
        }
      }
      case AccessorDeclaration as acc: {
        checkAccessorBodies(ctx, acc, resolveTypeAnnotation(ctx, acc.typeAnnotation));
      }
      case _: {}
    }
  }

  ctx.setBodyThisType(prevBodyThis);
  ctx.exitClass();
  ctx.exitScope();
}

export function checkMixinDeclaration(ctx: CheckerContext, md: MixinDeclaration): void {
  registerMixinDeclaration(ctx, md);
  checkMixinBodies(ctx, md);
}

export function validateFunctionDecorators(ctx: CheckerContext, decorators: GrowableArray<Decorator> | null, needsIntrinsicOrExternal: boolean, loc: SourceLocation | null): void {
  var paramCount = 0;
  var hasExternal = false;
  if (decorators != null) {
    for (let dec in decorators) {
      if (dec.name == 'intrinsic') {
        paramCount += 1;
        // isDeclare function check is omitted since it is only allowed on declare functions and methods
        if (false) {
          ctx.error("@intrinsic decorator is only allowed on declare functions and methods.", DiagnosticCode.TypeMismatch, dec.loc);
        }
        let intrinsicName = decoratorStringArg(dec, 0);
        if (dec.args.length != 1 || intrinsicName == null) {
          ctx.error("Intrinsic decorator must have exactly one argument (the intrinsic name).", DiagnosticCode.TypeMismatch, dec.loc);
        } else {
          let name = intrinsicName;
          if (!isValidIntrinsic(name)) {
            ctx.error(`Unknown intrinsic '${name}'.`, DiagnosticCode.TypeMismatch, dec.loc);
          }
        }
      } else if (dec.name == 'external') {
        hasExternal = true;
      }
    }
  }

  if (needsIntrinsicOrExternal) {
    var count = paramCount;
    if (hasExternal) {
      count += 1;
    }
    if (count != 1) {
      ctx.error("A declare function must have exactly one of an @intrinsic or @external decorator.", DiagnosticCode.TypeMismatch, loc);
    }
  } else if (paramCount > 1) {
    ctx.error("A function cannot have more than one @intrinsic decorator.", DiagnosticCode.TypeMismatch, loc);
  }
}

// ============================================================================
// Declare Function (External Declarations)
// ============================================================================
export function checkDeclareFunction(ctx: CheckerContext, df: DeclareFunction): void {
  // Resolve from annotations (type params, params, return type).
  // DeclareFunction always has full annotations — no body to check.
  ctx.enterScope();

  let resolvedTypeParams = resolveTypeParameters(ctx, df.typeParameters);

  // Parameter types.
  let paramTypes = new Array<Type>();
  let paramNames = new Array<String>();
  let optionals = new Array<boolean>();
  let initializers = new Array<Expression | null>();
  for (let param in df.params) {
    paramNames.push(param.name.name);
    optionals.push(param.optional || param.defaultValue != null);
    initializers.push(param.defaultValue);
    let {typeAnnotation} = param;
    if (typeAnnotation != null) {
      paramTypes.push(resolveTypeAnnotation(ctx, typeAnnotation));
    } else {
      paramTypes.push(new ErrorType());
    }
  }

  // Return type.
  let retType = resolveTypeAnnotation(ctx, df.returnType, true);

  ctx.exitScope();

  // Build function type.
  let funcType = new FunctionType(paramTypes, retType,
      new FunctionDeclInfo(paramNames, null, initializers, optionals),
      resolvedTypeParams);

  // Declare in value namespace.
  let info = ctx.model.resolveDeclaration(df);
  if (info != null) {
    ctx.model.addDeclaredGlobal(info.id, df);
    let existingType = ctx.model.getBindingType(info);
    if (existingType is FunctionType) {
      if (existingType.overloads == null) {
        let ovArray = growable<FunctionType>([existingType]);
        existingType.overloads = ovArray;
      }
      let ovs = existingType.overloads as GrowableArray<FunctionType>;
      ovs.push(funcType);
      ctx.setNodeType(df, funcType);
    } else {
      ctx.declareBinding(info, funcType);
      if (info.declaration !== df) {
        ctx.setNodeType(df, funcType);
      }
    }
  }

  // Check decorators
  validateFunctionDecorators(ctx, df.decorators, true, df.loc);
}

// ============================================================================
// Symbol Declaration
// ============================================================================
export function checkSymbolDeclaration(ctx: CheckerContext, sd: SymbolDeclaration): void {
  let {name} = sd.name;
  // Declare in value namespace.
  let info = ctx.model.resolveDeclaration(sd);
  if (info == null) {
    return;
  }
  ctx.declareBinding(info, ctx.canonicalSymbolType(
      sd as Node, name, name, protocolSymbolKey("", name, ctx.filePath)));
}

/**
 * Check an interface declaration: build an InterfaceType with method/field signatures.
 */
export function checkInterfaceDeclaration(ctx: CheckerContext, id: InterfaceDeclaration): void {
  let ifaceName = id.name.name;

  // Look up the pre-registered InterfaceType from the forward-declaration pass.
  var ifaceType = new InterfaceType(ctx.freshTypeId(), noSymbolId, ifaceName);
  let preRegistered = ctx.declaredTypeOf(id);
  if (preRegistered != null) {
    if (preRegistered is InterfaceType) { ifaceType = preRegistered; }
  }

  ifaceType.sourcePath = ctx.filePath;

  // Before type parameters and before the body: a member's key may name a
  // static symbol this very interface declares, and `Owner.sym` has to
  // resolve by the time that member is registered.
  registerStaticSymbols(
      ctx, ifaceName, ifaceType.staticSymbols, id.body);

  // Type parameters.
  ctx.enterScope();
  if (id.typeParameters != null) {
    ifaceType.typeParameters = resolveTypeParameters(ctx, id.typeParameters);
  }

  // Resolve extends list.
  let {extendsList} = id;
  if (extendsList != null) {
    let extends_ = extendsList;
    for (let currentExtends in extends_) {
      let parentType = resolveTypeAnnotation(ctx, currentExtends);
      if (parentType is InterfaceType) {
        // Registration can run twice on one canonical object (a
        // member of an import cycle registers again once every
        // member has); the parents list is shared with the
        // interface's instantiations, so it is deduplicated rather
        // than replaced.
        var alreadyListed = false;
        for (let listed in ifaceType.extendsList) {
          if (listed === parentType) { alreadyListed = true; }
        }
        if (!alreadyListed) {
          ifaceType.extendsList.push(parentType);
        }
        // The parent's members are read through `extendsList` by the
        // interface's member table, substituted at the read.
      }
    }
  }

  // Process body members.
  ctx.enterInterface();
  let ownConstructorNames = new Set<String>();
  let ownMethodNames = new Set<String>();
  for (let member in id.body) {
    match (member) {
      case MethodDefinition as md: {
        // The parser admits exactly one kind of MethodDefinition into an
        // interface body: a constructor.
        registerInterfaceConstructor(ctx, ifaceType, md, ownConstructorNames);
      }
      case MethodSignature as ms: {
        rejectContracts(ctx, ms.contracts, 'a method without a body');
        ctx.enterScope();
        let typeParams = resolveTypeParameters(ctx, ms.typeParameters);
        let paramTypes = new Array<Type>();
        for (let param in ms.params) {
          var paramType: Type = new ErrorType();
          let {typeAnnotation} = param;
          if (typeAnnotation != null) {
            paramType = resolveTypeAnnotation(ctx, typeAnnotation, true);
            checkInlineParameterType(ctx, paramType,
                param.optional || param.defaultValue != null, typeAnnotation.loc);
                      }
          paramTypes.push(paramType);
        }
        var retType: Type = new VoidType();
        let {returnType} = ms;
        if (returnType != null) {
          retType = resolveTypeAnnotation(ctx, returnType, true);
        }
        let methodName = getMemberName(ctx, ms.name);
        let ft = new FunctionType(paramTypes, retType, null, typeParams);
        // A second signature with the same name is an overload, collected
        // the way a class's are. The first one in this pass replaces any
        // earlier registration, so registering twice adds nothing.
        if (ownMethodNames.has(methodName)) {
          let existing = ifaceType.members[methodName].type;
          if (existing is FunctionType) {
            (existing.overloads ??= growable([existing])).push(ft);
          }
        } else {
          ownMethodNames.add(methodName);
          let isPrivate = methodName.length > 0 && methodName.sliceBytes(0, 1) == "#";
          ifaceType.members[methodName] = new MethodInfo(
            ft,
            true, // isAbstract
            false, // isStatic
            isPrivate,
            false // isFinal
          );
        }
        ctx.exitScope();
      }
      case PropertySignature as ps: {
        let propType = resolveTypeAnnotation(ctx, ps.typeAnnotation);
        let isPrivate = ps.name.name.length > 0 && ps.name.name.sliceBytes(0, 1) == "#";
        ifaceType.members[ps.name.name] = new FieldInfo(
          propType,
          false, // isMutable
          true, // isAbstract
          false, // isStatic
          isPrivate,
          false // hasInitializer
        );
      }
      case FieldDefinition as fd: {
        // See the class body: a static symbol declaration is not storage.
        if (isSymbolFieldDeclaration(fd)) { continue; }
        let fieldName = getMemberName(ctx, fd.name);
        let fieldType = resolveTypeAnnotation(ctx, fd.typeAnnotation as TypeAnnotation);
        let isMutable = fd.mutability != null && fd.mutability == "var";
        let isPrivate = fieldName.length > 0 && fieldName.sliceBytes(0, 1) == "#";
        ifaceType.members[fieldName] = new FieldInfo(
          fieldType,
          isMutable,
          true, // isAbstract
          fd.isStatic,
          isPrivate,
          false // hasInitializer
        );
      }
      case AccessorSignature as as_: {
        let accType = resolveTypeAnnotation(ctx, as_.typeAnnotation);
        if (as_.hasGetter) {
          let getterFt = new FunctionType(new Array<Type>(), accType);
          let isPrivate = as_.name.name.length > 0 && as_.name.name.sliceBytes(0, 1) == "#";
          ifaceType.members["get#" + as_.name.name] = new MethodInfo(
            getterFt,
            true, // isAbstract
            false, // isStatic
            isPrivate,
            false // isFinal
          );
        }
      }
      case _: {}
    }
  }
  ctx.exitInterface();

  // Register interface in type namespace.
  ctx.declareTypeName(ifaceName, ifaceType);
  let ifaceInfo = ctx.model.resolveDeclaration(id);
  if (ifaceInfo != null) {
    ctx.declareBinding(ifaceInfo, ifaceType);
    ctx.model.addDeclaredInterface(ifaceInfo.id, id);
  }

  // Every member is registered by now, so each symbol-keyed one can be
  // given the identity of the symbol that named it — and a key that names
  // no symbol is reported. After the binding above, because the key is
  // checked as an ordinary expression and `Iface.sym` names this interface.
  recordSymbolKeyedMemberIdentities(
      ctx, ifaceType as Type, ifaceType.members, id.body);

  ctx.exitScope();
}

/**
 * Register one constructor of an interface. An interface constructor is
 * a factory: `new(...)` or `new name(...)` with a body that returns an
 * instance of some class implementing the interface. Its signature is
 * checked like a class constructor's, and its return type is the
 * interface at its own type parameters. The body is checked later, by
 * checkInterfaceConstructorBodies, once every declaration in the module
 * has a signature, so the body may construct a class declared below the
 * interface.
 */
function registerInterfaceConstructor(
  ctx: CheckerContext,
  ifaceType: InterfaceType,
  md: MethodDefinition,
  ownConstructorNames: HashSet<String>
): void {
  let ctorName = if (let Identifier {name} = md.name) name else "<unknown>";
  let shown = if (ctorName == "<constructor>") "new" else "new " + ctorName;
  let where = `Constructor '${shown}' of interface '${ifaceType.name}'`;
  var thisParams = 0;
  for (let param in md.params) {
    if (param.isThisParam) {
      thisParams += 1;
      ctx.error(
        where + " cannot take a 'this.' parameter: an interface has no fields.",
        DiagnosticCode.IncompatibleTypes,
        param.loc
      );
    }
  }
  // The parser synthesizes one initializer entry per `this.` parameter,
  // reported above; anything beyond those was written.
  let {initializerList} = md;
  if (initializerList != null && initializerList.length > thisParams) {
    ctx.error(
      where + " cannot have an initializer list: an interface has no fields to initialize. Return the instance from the body instead.",
      DiagnosticCode.IncompatibleTypes,
      md.loc
    );
  }
  if (md.superInitializer != null) {
    ctx.error(
      where + " cannot call 'super': an interface has no superclass.",
      DiagnosticCode.IncompatibleTypes,
      md.loc
    );
  }
  // A body-less `new bare();` parses as an empty block, so the
  // every-path-returns check on the body reports it.
  if (ownConstructorNames.has(ctorName)) {
    ctx.error(
      `Duplicate constructor '${shown}' in interface '${ifaceType.name}'.`,
      DiagnosticCode.DuplicateMethod,
      md.loc
    );
    return;
  }
  ownConstructorNames.add(ctorName);
  var ctorType = checkMethodSignature(ctx, null, md);
  // The constructor's type parameters are the interface's own — codegen
  // mints one copy per type-argument list, the way it does for a
  // generic function, and finds the parameters those arguments bind
  // here — unless the constructor restates them with bounds:
  // `new<K extends Hashable, V>(...)` on `Map<K, V>`, whose body builds
  // a HashMap and so needs more of K than the interface asks. A
  // restatement names the interface's parameters, in order; its bounds
  // are checked at each `new`.
  var resultSet = false;
  if (md.typeParameters != null) {
    let restated = md.typeParameters as GrowableArray<TypeParameter>;
    let ifaceParams = ifaceType.typeParameters;
    var matches = ifaceParams != null && restated.length == ifaceParams.length;
    if (matches) {
      let ips = ifaceParams as Array<TypeParameterType>;
      var i = 0;
      while (i < restated.length) {
        if (restated[i].name != ips[i].name) {
          matches = false;
        }
        i += 1;
      }
    }
    if (!matches) {
      var expected = "";
      if (ifaceParams != null) {
        var first = true;
        for (let tp in ifaceParams) {
          if (!first) { expected = expected + ", "; }
          first = false;
          expected = expected + tp.name;
        }
      }
      ctx.error(
        where + " must restate the interface's type parameters, in order: <" +
          expected + ">.",
        DiagnosticCode.IncompatibleTypes,
        md.loc
      );
      // The body resolves the names the restatement declared, so the
      // result is the interface at those parameters, each standing
      // where the interface's parameter of its name stands; one the
      // restatement left out is the interface's own. Only the error
      // above is then reported for the constructor, not every use of a
      // parameter in its body.
      if (md.returnType == null && ifaceParams != null) {
        resultSet = true;
        let own = ctorType.typeParameters;
        let args = new Array<Type>();
        for (let ip in ifaceParams) {
          var stands: Type = ip;
          if (own != null) {
            for (let tp in own) {
              if (tp.name == ip.name) { stands = tp; }
            }
          }
          args.push(stands);
        }
        ctorType = ctorType.withReturnType(instantiateInterfaceType(ifaceType, args) as Type);
      }
      ctorType = ctorType.withTypeParameters(ifaceType.typeParameters);
    }
  } else {
    ctorType = ctorType.withTypeParameters(ifaceType.typeParameters);
  }
  // The result is the interface at the constructor's type parameters
  // unless the constructor declares a narrower type — `new():
  // GrowableArray<T>` on `Array<T>`, so that the caller gets the class's
  // full API. A declared type must still be an instance of the
  // interface; that is checked with the body, in the second phase,
  // because a class declared below the interface has no `implements`
  // list yet.
  if (md.returnType == null && !resultSet) {
    ctorType = ctorType.withReturnType(constructorSelfType(ifaceType, ctorType));
  }
  ifaceType.constructors[ctorName] = ctorType;
  ctx.setNodeType(md, ctorType);
}

/**
 * The interface at a constructor's type parameters — the interface's
 * own, or the constructor's restatement of them.
 */
function constructorSelfType(ifaceType: InterfaceType, ctorType: FunctionType): Type {
  let params = ctorType.typeParameters;
  if (params == null || params.length == 0) {
    return ifaceType as Type;
  }
  let args = new Array<Type>();
  for (let tp in params) {
    args.push(tp as Type);
  }
  return instantiateInterfaceType(ifaceType, args) as Type;
}

/**
 * Check the bodies of an interface's constructors. Runs in the module's
 * second phase, after every declaration has a signature.
 */
export function checkInterfaceConstructorBodies(ctx: CheckerContext, id: InterfaceDeclaration): void {
  let resolved = ctx.declaredTypeOf(id);
  if (!(resolved is InterfaceType)) {
    return;
  }
  let ifaceType = resolved;
  let seen = new Set<String>();
  for (let member in id.body) {
    if (member is MethodDefinition) {
      let ctorName = if (let Identifier {name} = member.name) {
        name
      } else {
        "<unknown>"
      };
      // A duplicate registered no signature; its body is not checked.
      if (seen.has(ctorName)) {
        continue;
      }
      seen.add(ctorName);
      if (!ifaceType.constructors.has(ctorName)) {
        continue;
      }
      let ctorType = ifaceType.constructors[ctorName];
      ctx.enterInterface();
      checkMethodParameterDefaults(ctx, null, ctorType, member);
      ctx.exitInterface();
      if (member.body == null) {
        continue;
      }
      // A declared result type must be an instance of the interface.
      // Checked here rather than at registration because the class it
      // names may be declared below the interface, and only has its
      // `implements` list once every declaration in the module is
      // registered.
      if (member.returnType != null) {
        let selfType = constructorSelfType(ifaceType, ctorType);
        if (!isAssignableTo(ctx, ctorType.returnType, selfType)) {
          let shown = if (ctorName == "<constructor>") "new" else "new " + ctorName;
          ctx.error(
            "Constructor '" + shown + "' of interface '" + ifaceType.name +
              "' must return a '" + typeToString(selfType) +
              "', but declares '" + typeToString(ctorType.returnType) + "'.",
            DiagnosticCode.TypeMismatch,
            (member.returnType as Node).loc
          );
        }
      }
      ctx.enterNodeReferrer(member.id as i32);
      checkInterfaceConstructorBody(ctx, ifaceType, ctorType, member);
      ctx.exitNodeReferrer(member.id as i32);
    }
  }
}

/**
 * Check one interface constructor's body: a function body whose
 * parameters are the constructor's and whose return type is the
 * interface at its own type parameters. There is no `this` (the
 * interface context rejects it), and every path must return.
 */
function checkInterfaceConstructorBody(
  ctx: CheckerContext,
  ifaceType: InterfaceType,
  ctorType: FunctionType,
  md: MethodDefinition
): void {
  ctx.enterScope();
  // The constructor's own type parameters (a restatement with bounds
  // shadows the interface's by name, so the body sees the bounds).
  let {typeParameters} = ctorType;
  if (typeParameters != null) {
    for (let tp in typeParameters) {
      ctx.declareTypeName(tp.name, tp);
    }
  }
  var i = 0;
  while (i < md.params.length) {
    let param = md.params[i];
    let paramType = ctorType.parameters[i];
    let paramInfo = ctx.model.resolveDeclaration(param);
    if (paramInfo != null) {
      ctx.declareBinding(paramInfo, paramType);
    }
    let {pattern} = param;
    if (pattern != null) {
      bindPattern(ctx, pattern, paramType);
    }
    i += 1;
  }
  let body = new BodyContext();
  body.returnType = ctorType.returnType;
  body.bodyThisType = ctx.bodyThisType;
  let prevBody = ctx.enterBody(body);
  ctx.enterInterface();
  let prevFlowState = ctx.enterBodyFlow(false);
  match (md.body as Node) {
    case BlockStatement as block: {
      ctx.enterOwnScope();
      registerOwnParamCandidates(ctx, md.params, ctorType.parameters);
      prescanPendingFunctions(ctx, block.body);
      for (let stmt in block.body) {
        checkStatement(ctx, stmt);
      }
      ctx.exitOwnScope(block as Node);
      if (!branchDefinitelyExits(block, ctx.model)) {
        ctx.error(
          "An interface constructor must return an instance on every path.",
          DiagnosticCode.MissingReturnType,
          md.loc
        );
      }
    }
    case Expression as bodyExpr: {
      ctx.enterOwnScope();
      registerOwnParamCandidates(ctx, md.params, ctorType.parameters);
      let bodyType = checkExpression(ctx, bodyExpr, ctorType.returnType);
      if (!isAssignableTo(ctx, bodyType, ctorType.returnType)) {
        ctx.error(
          "Type mismatch: constructor body returns '" + typeToString(bodyType) +
          "' but expects '" + typeToString(ctorType.returnType) + "'.",
          DiagnosticCode.TypeMismatch,
          md.loc
        );
      } else {
        checkAndRegisterInterfaceAdaptation(ctx, bodyExpr, bodyType, ctorType.returnType);
      }
      ctx.exitOwnScope(bodyExpr as Node);
    }
    case _: {}
  }
  ctx.exitInterface();
  ctx.exitBody(prevBody);
  ctx.exitBodyFlow(prevFlowState);
  ctx.exitScope();
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19