import { HashMap, HashSet, OrderedHashMap, Set } from 'zena:collections'; import { StringBuilder, growable } from 'zena:core'; import { AccessorDeclaration, AccessorSignature, ClassDeclaration, EnumDeclaration, Expression, FieldDefinition, Identifier, ImportSpecifier, IndexExpression, InterfaceDeclaration, MemberExpression, MethodDefinition, MethodSignature, MixinDeclaration, Node, NumberLiteral, PropertySignature, RecordPattern, BooleanLiteral, StringLiteral, PropertyAssignment, LiteralPattern, SourceLocation, SymbolDeclaration, SymbolPropertyName, ThisExpression, isSymbolFieldDeclaration, samePostfixChain, syntacticMemberName } from '../ast.zena'; import { ArrayType, BoolLiteralType, ClassType, EnumType, ErrorType, FieldInfo, NumLiteralType, StringLiteralType, sameNumericLiteralRaw, FunctionType, InlineTupleType as InlineTupleT, InterfaceType, MemberInfo, MemberTable, MethodInfo, MixinType, NeverType, NullType, OwnershipHandle, RecordType as RecordT, SymbolType, ThisType, TupleType as TupleT, Type, TypeAliasType, TypeParameterType, UnionType as UnionT, UnknownType, baseType, instantiateClassType, isAffineValue, isIntegerType, ownershipHandleInner, ownershipHandleOf, parseTupleIndexLiteral, typeCarriesOwner, typeToString, unwrapAliases } from '../types.zena'; import { DiagnosticCode } from '../diagnostics.zena'; import { noSymbolId } from '../scope/symbol.zena'; import { WellKnown, protocolSymbolKey, wellKnownName } from '../well-known.zena'; import { boxedFormOf } from './annotations.zena'; import { CheckerContext } from './context.zena'; import { checkExpression } from './expressions.zena'; import { substituteThisType, substituteTypeParams } from './generics.zena'; import { getMemberPathKeyFromAccess } from './narrowing.zena'; import { overloadCandidates, collectApplicableOperatorOverloads, selectMostSpecificIndex } from './overloads.zena'; import { borrowWrapFieldRead, fieldReadsAsTake, memberConsumesReceiver, rejectConsumingThroughNonOwner, rejectFirstClassOnlyAccess, tupleElementRead } from './ownership.zena'; import { argumentAdaptsToParameter } from './literals.zena'; import { isAssignableTo, isBoxablePrimitive, typesEqual } from './relations.zena'; import { createUnionType, stripNull, subtractNullFromType } from './unions.zena'; /** * A parent's member re-typed for the subtype that inherits it. * * Carries `symbolTypeId` across, so a symbol-keyed member keeps its * symbol's identity one level below the type that declared it — which is * where most calls to an inherited protocol method are. */ /** * The member key a symbol makes: its name, then its identity. * * Shaped like `getTypeUniqueKey`'s `Name_id`, for the same reason — the * name is what a reader needs and the id is what keeps two of them * apart. `displayMemberName` takes the id back off for diagnostics, so * the mangling never reaches a message. * * The protocol symbols the compiler invokes itself carry a fixed key * instead (see `SymbolType.protocolKey`). No id-keyed symbol can collide * with one, because every id-keyed key ends in `_`. */ function symbolMemberKey(sym: SymbolType) { let fixed = sym.protocolKey; return fixed ?? `:${sym.name}_${i32ToString(sym.id as i32)}`; } /** * A member key as a reader should see it: `:dispose` rather than * `:dispose_87`. The identity suffix exists so two same-named symbols * key two members, and it is never what someone wrote. */ export function displayMemberName(key: String): String { if (key.length == 0 || key.sliceBytes(0, 1) != ":") { return key; } // Scan back over the trailing digits to the underscore that precedes // them. Anything else — no underscore, no digits, a name-keyed member // — is already what a reader should see. let zero = 48; let nine = 57; let underscore = 95; var i = key.length; while (i > 0) { let c = key.getByteAt(i - 1); if (c < zero || c > nine) { break; } i -= 1; } if (i == key.length || i == 0) { return key; } if (key.getByteAt(i - 1) != underscore) { return key; } return key.sliceBytes(0, i - 1); } /** * The key a member is registered under, and the only place it is minted. * * A name-keyed member is keyed by its name. A symbol-keyed one is keyed * by the symbol's identity, because two symbols may share a name and a * type may hold a member from each — so `[Reader.act]` and `[Writer.act]` * must not land in the same slot. The key is recorded on the name node, * and every later pass reads it back rather than rebuilding it from the * text, which cannot tell the two apart. * * A symbol that does not resolve falls back to its spelling. The failure * is reported at the declaration; the key only has to stay consistent. */ export function getMemberName(ctx: CheckerContext, node: Node): String { if (!(node is SymbolPropertyName)) { return syntacticMemberName(node); } let recorded = ctx.model.memberKey(node); if (recorded != null) { return recorded; } let sym = lookupSymbolPath(ctx, node.symbolRef); if (sym == null) { return syntacticMemberName(node); } let key = symbolMemberKey(sym); ctx.model.setMemberKey(node, key); return key; } /** * The key a member ACCESS looks a member up by — the declaration-site * key seen from the other side, so the two have to agree. * * `x.[Reader.act]()` and `x.[Writer.act]()` name two members, and the * property name is `act` for both. The symbol the path resolves to is * the difference, so it is what the key carries. The result is recorded * on the access node, because lowering looks the same member up later * and cannot resolve anything itself. */ export function accessMemberKey( ctx: CheckerContext, expr: Node, isSymbolAccess: boolean, property: Identifier, symbolPath: Expression | null): String { if (!isSymbolAccess) { return property.name; } let recorded = ctx.model.memberKey(expr); if (recorded != null) { return recorded; } if (symbolPath == null) { return ":" + property.name; } let sym = lookupSymbolPath(ctx, symbolPath); if (sym == null) { return ":" + property.name; } let key = symbolMemberKey(sym); ctx.model.setMemberKey(expr, key); return key; } // ============================================================================ // Member Expression // ============================================================================ /** * The `staticSymbols` map of a named type, or null for anything else. */ function staticSymbolsOf(t: Type): OrderedHashMap | null { return match (t) { case ClassType as ct: ct.staticSymbols case InterfaceType as it: it.staticSymbols case MixinType as mt: mt.staticSymbols case _: null }; } /** * Whether `t` declares a static member — field, method, accessor or * symbol — called `name`. */ function declaresStaticMember(t: Type, name: String): boolean { let symbols = staticSymbolsOf(t); if (symbols != null && symbols.has(name)) { return true; } let members: MemberTable | null = match (t) { case ClassType as ct: ct.staticMembers case InterfaceType as it: it.members case MixinType as mt: mt.staticMembers case _: null }; if (members == null) { return false; } for (let key in [name, "get#" + name]) { if (let (true, info) = members.get(key)) { if (info.isStatic) { return true; } } } return false; } /** The declared name of a class, interface or mixin; "" for anything else. */ function declaredTypeName(t: Type) { return match (t) { case ClassType as ct: ct.name case InterfaceType as it: it.name case MixinType as mt: mt.name case _: "" }; } /** * The suffix for a missing-name error when the name is a static member of * a type whose body encloses the reference — the usual cause of that * error inside a type, since a static member is only ever named through * its type. Empty when no enclosing type declares it. */ export function staticMemberHint(ctx: CheckerContext, name: String): String { let candidates = new Array(); let declaring = ctx.declaringTypes; var i = declaring.length - 1; while (i >= 0) { candidates.push(declaring[i]); i -= 1; } let {currentClass} = ctx; if (currentClass != null) { candidates.push(currentClass as Type); } for (let t in candidates) { let typeName = declaredTypeName(t); if (typeName != "" && declaresStaticMember(t, name)) { return ` Did you mean '${typeName}.${name}'?`; } } return ""; } /** * The symbol a member key names, found without reporting anything. * * Member keys are minted during registration, before the path itself is * checked, so this has to be a quiet lookup. It finds exactly what * `checkSymbolPath` finds — a top-level `symbol` in scope for `sym`, a * static symbol of `Owner` for `Owner.sym` — and a path it cannot find is * reported there, as an ordinary expression. */ function lookupSymbolPath(ctx: CheckerContext, path: Expression): SymbolType | null { match (path) { case Identifier as ident: { let t = ctx.resolveIdentifier(ident as Node); if (t != null && t is SymbolType) { return t; } return null; } case MemberExpression {object, property}: { if (!(object is Identifier)) { return null; } let ownerName = object.name; var ownerType = ctx.resolveTypeReference(object as Node, ownerName); if (ownerType == null) { ownerType = ctx.resolveIdentifier(object as Node); } if (ownerType == null) { return null; } let symbols = staticSymbolsOf(baseType(ownerType)); if (symbols == null) { return null; } if (let (true, sym) = symbols.get(property.name)) { if (sym is SymbolType) { return sym; } } return null; } case _: { return null; } } } /** * Checks the path inside `[...]` as the expression it is, and requires it * to be a symbol. * * `sym` and `Owner.sym` are an ordinary name and an ordinary static member * read, so they are checked like any other, and a name or member that does * not exist is reported the way it would be anywhere. The one thing that * is particular to a member key is that what it names has to be a symbol. * Returns null after reporting when either fails. */ function checkSymbolPath(ctx: CheckerContext, path: Expression): SymbolType | null { let t = checkExpression(ctx, path, null); if (t is SymbolType) { return t; } if (!(t is ErrorType)) { ctx.error( `'${symbolPathText(path)}' is not a symbol.`, DiagnosticCode.TypeMismatch, path.loc ); } return null; } /** The source spelling of a symbol path, for a message. */ function symbolPathText(path: Expression): String { return match (path) { case Identifier {name}: name case MemberExpression {object, property}: `${symbolPathText(object)}.${property.name}` case _: "" }; } /** * The symbol reference a member's name node carries, or null for a * name-keyed member. */ function symbolRefOfMemberName(nameNode: Node): Expression | null { if (!(nameNode is SymbolPropertyName)) { return null; } return nameNode.symbolRef; } /** The name node a declaration body member is declared under. */ function memberNameNodeOf(member: Node): Node | null { return match (member) { case FieldDefinition as fd: fd.name case MethodDefinition as md: md.name case AccessorDeclaration as ad: ad.name case MethodSignature as ms: ms.name case AccessorSignature as asig: asig.name as Node case PropertySignature as ps: ps.name as Node case _: null }; } /** * Registers the `static symbol` declarations a body makes on the type * that declares them, so `Owner.sym` resolves and the members it keys * can carry its identity. * * A symbol is an identity rather than storage, so it occupies nothing on * the type that declares it and any type can declare one. An interface * spells `static symbol sym;` as a `SymbolDeclaration`; a class or mixin * body parses the same words into a static `FieldDefinition` annotated * `symbol`. Both land here and answer `Owner.sym` the same way. */ export function registerStaticSymbols( ctx: CheckerContext, ownerName: String, symbols: HashMap, body: GrowableArray): void { for (let member in body) { var symName = ""; var node: Node | null = null; match (member) { case SymbolDeclaration as sd: { symName = sd.name.name; node = sd as Node; } case FieldDefinition as fd: { if (isSymbolFieldDeclaration(fd)) { symName = syntacticMemberName(fd.name); node = fd as Node; } } case _: {} } if (node == null || symName == "") { continue; } let sym = ctx.canonicalSymbolType( node, `${ownerName}.${symName}`, symName, protocolSymbolKey(ownerName, symName, ctx.filePath)); if (let (true, existing) = symbols.get(symName)) { // A declaration is registered again whenever its module is // re-checked, and `canonicalSymbolType` is keyed by source // location, so the same declaration hands back the same type. A // different one is a real duplicate. if (!typesEqual(existing, sym)) { ctx.error( `'${ownerName}' already declares a static symbol '${symName}'.`, DiagnosticCode.DuplicateDeclaration, node.loc ); } continue; } symbols[symName] = sym; } } /** * Records, on each symbol-keyed member a declaration body declares, the * identity of the symbol that named it, and reports a key that names no * symbol at all. * * Run as one pass over the body rather than at each member-registration * site: the same key is registered from a dozen places (fields, methods, * accessors, and their `get#`/`set#` companions), and every one of them * would otherwise have to remember to carry the symbol along. */ export function recordSymbolKeyedMemberIdentities( ctx: CheckerContext, owner: Type, members: MemberTable, body: GrowableArray): void { for (let member in body) { let nameNode = memberNameNodeOf(member); if (nameNode == null) { continue; } let ref = symbolRefOfMemberName(nameNode); if (ref == null) { continue; } let isStaticMember = match (member) { case FieldDefinition as fd: fd.isStatic case MethodDefinition as md: md.isStatic case AccessorDeclaration as acc: acc.isStatic case _: false }; let targetMembers = match (owner) { case ClassType as ct: ct.memberTable(isStaticMember) case MixinType as mt: mt.memberTable(isStaticMember) case _: members }; let key = getMemberName(ctx, nameNode); // A body is walked once per registration pass, and a module in an // import cycle is registered more than once. Stamping is idempotent; // reporting is not, so a member that already carries its identity is // left alone. The key carries the symbol's identity, so a member // stamped here can only have been stamped by this same symbol. var settled = false; if (let (true, seen) = targetMembers.get(key)) { settled = seen.symbolTypeId >= 0; } if (settled) { continue; } ctx.pushDeclaringType(owner); let sym = checkSymbolPath(ctx, ref); ctx.popDeclaringType(); if (sym == null) { continue; } let id = sym.id as i32; let candidates = growable([key, "get#" + key, "set#" + key]); for (let candidate in candidates) { if (let (true, info) = targetMembers.get(candidate)) { info.symbolTypeId = id; } } } } /** * A symbol-keyed access names a symbol, and must be able to SEE it — * `:sym` a symbol in lexical scope, `:Owner.sym` a static symbol of a type * in scope. An unexported owner withholds its symbols the way an * unexported `symbol` withholds itself (`classes.md` §9.4), and * `zena:core` relies on that to keep the resource lifecycle flag * unwritable. * * Which symbol the access names needs no check of its own: the member key * carries the symbol's identity, so naming a different symbol of the same * name looks up a different key and misses. * * Returns false when the path names nothing, having reported why, so the * member lookup does not report the same mistake a second time. */ function checkSymbolKeyAccess(ctx: CheckerContext, path: Expression) { return checkSymbolPath(ctx, path) != null; } /** * Whether `object` names a type that has static members — a class, an * enum, an interface or a mixin — so that a member read on it is a * static read. An import is judged by the declaration it resolves to, * never by its name: another dependency exporting a class called * `Config` does not make an imported `Config` instance a type. */ export function isStaticReceiver(ctx: CheckerContext, object: Expression): boolean { if (!(object is Identifier)) { return false; } let sym = ctx.model.resolve(object); if (sym == null) { return false; } let decl = sym.resolveTarget().declaration; return decl is ClassDeclaration || decl is EnumDeclaration || decl is InterfaceDeclaration || decl is MixinDeclaration; } /** Whether `object` names a type whose static symbols a member read * can reach: `isStaticReceiver`'s answer, under the name its callers * ask the question by. */ function namesDeclaredType(ctx: CheckerContext, object: Expression) { return isStaticReceiver(ctx, object); } export function checkMemberExpression( ctx: CheckerContext, expr: MemberExpression, object: Expression, property: Identifier, optional: boolean, isSymbolAccess: boolean, loc: SourceLocation | null, inChain: boolean = false ): Type { if (inChain && samePostfixChain(expr, object)) { ctx.chainInteriorNode = object as Node; } let objectTypeRaw = checkExpression(ctx, object, null); var objectType = objectTypeRaw; if (objectType is ThisType) { let {currentClass} = ctx; if (currentClass != null) { objectType = currentClass as Type; } } let isStatic = isStaticReceiver(ctx, object); ctx.model.setStaticAccess(expr, isStatic); // `Owner.sym` reads a static symbol, the same way `Owner.count` reads a // static field. A symbol exists only at compile time, so there is no // member to look up at run time; its value is its type. if (!isSymbolAccess && namesDeclaredType(ctx, object)) { let symbols = staticSymbolsOf(baseType(objectTypeRaw)); if (symbols != null) { if (let (true, sym) = symbols.get(property.name)) { if (optional) { ctx.warning( "Receiver of '?.' is not nullable; optional chaining is unnecessary.", DiagnosticCode.UnnecessaryNullish, loc ); } return sym; } } } let propName = accessMemberKey( ctx, expr as Node, isSymbolAccess, property, expr.symbolPath); rejectFirstClassOnlyAccess(ctx, objectType, propName, loc); // `then` hands a future's value to a callback while the future keeps // it, so on a future of an owner it would mint a second owner; such a // future is consumed by its `await` (docs/design/owner-futures.md). if (!isSymbolAccess && propName == "then" && isAffineValue(objectType)) { ctx.error( "A future whose value holds an owner is consumed by 'await': " + "'then' would hand the owner to a callback while the future " + "still holds it.", DiagnosticCode.TypeMismatch, loc ); } // Reaching a `this: Own` method through an owned binding moves // the binding: the call ends the receiver's life on this path. The // move is recorded at the access — the caller-side counterpart of // rejectConsumingThroughNonOwner's callee-side gate. if (object is Identifier && ownershipHandleOf(objectType) == OwnershipHandle.Own && memberConsumesReceiver(objectType, propName)) { let recvSym = ctx.model.resolve(object); if (recvSym != null) { ctx.recordFlowMove(recvSym.id, recvSym.name, object.loc); } } // The same through a consuming method's own `this`: calling another // consuming method on it, its dispose included, ends its life here. if (object is ThisExpression && ownershipHandleOf(objectType) == OwnershipHandle.Own && memberConsumesReceiver(objectType, propName)) { let rs = ctx.receiverSymbol; if (rs != null) { ctx.recordFlowMove(rs.id, "this", object.loc); } } let pathKey = getMemberPathKeyFromAccess(ctx, object, property, isSymbolAccess); let narrowedPathType = if (pathKey != null) { ctx.flowTypeOfPath(pathKey) } else { null }; // Check for private field access outside class if (propName.length > 0 && propName.sliceBytes(0, 1) == "#") { if (ctx.currentClass == null) { ctx.error( `Private field '${displayMemberName(propName)}' can only be accessed within a class.`, DiagnosticCode.PrivateAccess, loc ); return new ErrorType(); } } // Unwrap union types for optional chaining. let (receiverType, isNullable) = stripNull(ctx, objectType); if (isSymbolAccess && expr.symbolPath != null) { if (!checkSymbolKeyAccess(ctx, expr.symbolPath as Expression)) { return new ErrorType(); } } // Private access is lexical-only (docs/design/member-lookup.md §6): // the enclosing class must itself declare the private, and the // receiver must be that same lexical class — any specialization of // it, since privacy is unaffected by specialization — or a subclass // instance, which physically carries the ancestor's private state. // Receivers that are not plain class types (type parameters, mixin // This) are left to the receiver-side member lookup below. if (propName.length > 0 && propName.sliceBytes(0, 1) == "#" && ctx.currentClass != null && !(receiverType is ErrorType)) { let cur = ctx.currentClass as ClassType; if (!cur.members.has(propName) && !cur.members.has("get#" + propName) && !cur.members.has("set#" + propName) && !cur.staticMembers.has(propName) && !cur.staticMembers.has("get#" + propName) && !cur.staticMembers.has("set#" + propName)) { ctx.error( `Property '${displayMemberName(propName)}' does not exist on type '${cur.name}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } if (receiverType is ClassType) { // The receiver shares the lexical class when its own class — // or any superclass, for subclass instances carrying an // ancestor's private state — is a specialization of it. let lexicalCanonical = cur.canonical(); var shares = false; var r: ClassType | null = receiverType; while (r != null && !shares) { if (r.canonical() == lexicalCanonical) { shares = true; } r = r.superType; } if (!shares) { ctx.error( "Type '" + typeToString(receiverType) + "' does not have private member '" + propName + "' from class '" + cur.name + "'.", DiagnosticCode.TypeMismatch, loc ); return new ErrorType(); } } } // An optional record field is readable only as the immediate left // operand of `??` (the sanction below): there the miss is the // coalesce arm, so the maybe-absent value never escapes. Everywhere // else resolveMemberType rejects the read. if (!isNullable && !optional && ctx.nullishSanctioned.has(expr.id as i32)) { let recBase = baseType(unwrapAliases(subtractNullFromType(receiverType))); if (recBase is RecordT && recBase.isOptional(propName)) { return recBase.properties[propName]; } } let ownerRead = ctx.owningReceiver && object is ThisExpression; let resultType = resolveMemberType(ctx, receiverType, propName, loc, ownerRead, isStatic); if (ownerRead && typeCarriesOwner(resultType)) { // The read kept its owner type for move-out; a read after the // field was moved is the path use-after-move. let pathKey = getMemberPathKeyFromAccess(ctx, object, property, isSymbolAccess); if (pathKey != null) { ctx.checkPathMoveUse(expr as Node, pathKey, "this." + propName); } } if (isNullable && !optional) { ctx.error( `Object may be null when accessing '${displayMemberName(propName)}'.`, DiagnosticCode.TypeMismatch, loc ); return new ErrorType(); } if (optional && !isNullable && !(resultType is ErrorType) && !(objectType is ErrorType) && !(objectType is UnknownType) && !(objectType is NeverType)) { ctx.warning( "Receiver of '?.' is not nullable; optional chaining is unnecessary.", DiagnosticCode.UnnecessaryNullish, loc ); } if (narrowedPathType != null) { return narrowedPathType; } // If the original type was nullable and this is optional chaining, // wrap the result in a union with null. A boxable-primitive result // has no wasm representation for that union, so the chain is only // legal as the immediate left operand of `??` — where the miss is // the coalesce arm and no `primitive | null` value ever exists. if (isNullable && optional) { if (inChain) { ctx.chainShortCircuits = true; return resultType; } if (isBoxablePrimitive(resultType) && !ctx.nullishSanctioned.has(expr.id as i32)) { ctx.error( "Optional access to a primitive member requires immediate coalescence: follow it with '?? '.", DiagnosticCode.InvalidOperator, loc ); return new ErrorType(); } let types = growable([resultType, new NullType()]); return createUnionType(types); } return resultType; } /** * The type a field of a record union has across two members: their type * when they agree, the union of the two when both are literals of one base * type, as a discriminant is (`{kind: 0, ...} | {kind: 1, ...}`), and null * when they disagree otherwise. */ export function joinRecordFieldTypes(a: Type, b: Type): Type | null { if (typesEqual(a, b)) { return a; } if (isLiteralUnionOf(a) && isLiteralUnionOf(b) && literalKind(literalBaseOf(a)) == literalKind(literalBaseOf(b))) { return createUnionType(growable([a, b])); } // A literal beside its own base type reads as the base type. if (isLiteralUnionOf(a) && !isLiteralUnionOf(b) && literalKind(literalBaseOf(a)) == typeToString(b)) { return b; } if (isLiteralUnionOf(b) && !isLiteralUnionOf(a) && literalKind(literalBaseOf(b)) == typeToString(a)) { return a; } return null; } /** What a literal type is a literal of: booleans, strings, or one number type. */ function literalKind(t: Type): String { if (t is BoolLiteralType) { return 'boolean'; } if (t is StringLiteralType) { return 'String'; } return typeToString(baseType(t)); } /** Whether `t` is a literal type, or a union of literal types. */ function isLiteralUnionOf(t: Type): boolean { if (t is UnionT) { for (let member in t.types) { if (!isLiteralType(member)) { return false; } } return true; } return isLiteralType(t); } function isLiteralType(t: Type): boolean { return t is BoolLiteralType || t is NumLiteralType || t is StringLiteralType; } /** A literal type of `t`, a literal or a union of them, to read its base off. */ function literalBaseOf(t: Type): Type { if (t is UnionT && t.types.length > 0) { return t.types[0]; } return t; } /** * Whether the literal `value` (a literal expression, or a literal pattern * around one) can be a value of `t`: 1 for a literal type equal to it, 0 * for a different literal of the same kind, and -1 when `t` is not a * literal type or `value` not a literal, which tells members nothing. */ export function literalFits(t: Type, value: Node | null): i32 { var v = value; if (v is LiteralPattern) { v = v.value; } if (let BoolLiteralType {value as literal} = t) { if (let BooleanLiteral {value as written} = v) { return if (literal == written) 1 else 0; } return -1; } if (let NumLiteralType {raw} = t) { if (let NumberLiteral {raw as written} = v) { return if (sameNumericLiteralRaw(raw, written)) 1 else 0; } return -1; } if (let StringLiteralType {value as literal} = t) { if (let StringLiteral {value as written} = v) { return if (literal == written) 1 else 0; } return -1; } return -1; } /** * The members of a record union whose fields can hold the literals a * record pattern or literal names at literal-typed fields: `{kind: 0, ..}` * keeps the members whose `kind` is `0`. `fields` and `values` are the * names and the nodes written for them. */ export function membersMatchingLiterals(members: GrowableArray, fields: GrowableArray, values: GrowableArray): GrowableArray { let out = new Array(); for (let rec in members) { var ok = true; for (var i = 0; i < fields.length; i += 1) { if (!rec.properties.has(fields[i])) { continue; } if (literalFits(rec.properties[fields[i]], values[i]) == 0) { ok = false; } } if (ok) { out.push(rec); } } return out; } /** * The member of the record union `expected` a record literal with these * properties is: the one whose literal-typed fields its literal values * match, when exactly one does and some literal tells it apart. Null for * anything else, which leaves the literal checked against `expected`. */ export function discriminatedRecordMember(expected: Type, properties: GrowableArray): RecordT | null { let base = baseType(unwrapAliases(expected)); if (!(base is UnionT)) { return null; } let members = recordUnionMembersOf(base); if (members == null) { return null; } let fields = new Array(); let values = new Array(); for (let property in properties) { if (let PropertyAssignment {name, value} = property) { fields.push(name.name); values.push(value); } } let matching = membersMatchingLiterals(members, fields, values); return if (matching.length == 1 && matching.length < members.length) matching[0] else null; } /** * The members of a union whose every member is a record type, or null * for any other union. A record union is read, destructured and * repacked one member at a time; see `RecordUnionAdaptation`. */ export function recordUnionMembersOf(ut: UnionT): GrowableArray | null { let members = new Array(); for (let memberType in ut.types) { // An inline record member is read and matched as its record (#811). let rec = baseType(unwrapAliases(memberType)); if (!(rec is RecordT)) { return null; } members.push(rec); } if (members.length < 2) { return null; } return members; } /** * The one record shape a record pattern runs against for every member * of a record union: the fields the pattern names, each at the type the * members agree on, and optional when any member lacks it or has it * optional. * * Every member is assignable to that shape, so the member a value is * repacks to it in one hop (`RecordUnionAdaptation`) and the pattern * then works as it does against any single record type. The presence * mask carries which member the value came from, which is what makes * `{rank = 9}` take its default on the members with no `rank` and * `{rank}` a presence test. * * `ErrorType` when the members disagree about a field's type or no * member has one the pattern names, both of which are reported. Null * when the pattern names nothing. */ function recordUnionPatternShape( ctx: CheckerContext, ut: UnionT, members: GrowableArray, recordPat: RecordPattern ): Type | null { let props = new Map(); let optionals = new Set(); for (let prop in recordPat.properties) { let propName = prop.name.name; if (props.has(propName)) { continue; } var fieldType: Type | null = null; var anyAbsent = false; for (let rec in members) { if (!rec.properties.has(propName)) { anyAbsent = true; continue; } if (rec.isOptional(propName)) { anyAbsent = true; } let memberFieldType = rec.properties[propName]; let prior = fieldType; let joined = if (prior != null) joinRecordFieldTypes(prior, memberFieldType) else memberFieldType; if (joined != null) { fieldType = joined; } else { ctx.error( "Field '" + displayMemberName(propName) + "' is '" + typeToString(prior as Type) + "' in one member of the " + "union and '" + typeToString(memberFieldType) + "' in another, so one pattern cannot bind it.", DiagnosticCode.TypeMismatch, prop.loc ); return new ErrorType(); } } if (fieldType == null) { ctx.error( "No member of '" + typeToString(ut) + "' has property '" + displayMemberName(propName) + "'.", DiagnosticCode.PropertyNotFound, prop.loc ); return new ErrorType(); } props[propName] = fieldType; if (anyAbsent) { optionals.add(propName); } } if (props.size == 0) { return null; } let optionalNames: HashSet | null = if (optionals.size > 0) { optionals } else { null }; return new RecordT(props, optionalNames, null, false); } /** * The type of a record pattern's `...rest`: the matched record's fields * the pattern does not name, optional where they were, and its row. The * rest is the matched value converted to that narrower type, so the * conversion is recorded on the rest pattern for codegen. Anything but * a record is an error. */ export function recordRestType(ctx: CheckerContext, recordPat: RecordPattern, matched: Type): Type { let rest = recordPat.rest as Node; let base = baseType(unwrapAliases(matched)); if (!(base is RecordT)) { if (!(base is ErrorType) && !(base is UnknownType)) { ctx.error(`A '...rest' pattern needs a record, not '${typeToString(matched)}'.`, DiagnosticCode.TypeMismatch, rest.loc); } return new ErrorType(); } if (base.isExistential) { ctx.error(`A '...rest' pattern needs a record whose fields are all known, not '${typeToString(matched)}'.`, DiagnosticCode.TypeMismatch, rest.loc); return new ErrorType(); } let named = new HashSet(); for (let prop in recordPat.properties) { named.add(prop.name.name); } let props = new Map(); var optionals: HashSet | null = null; for (let key in base.properties.keys()) { if (!named.has(key)) { props[key] = base.properties[key]; if (base.isOptional(key)) { optionals ??= new Set(); optionals.add(key); } } } let restType = new RecordT(props, optionals, base.row, false); ctx.model.setRecordAdaptation(rest, base, restType); return restType; } /** * The matched type for a record pattern whose value is a union of * record types, and the adaptation that gets a value there: the shape * above, recorded on the pattern node so codegen repacks the member * before destructuring. `t` itself for anything else. */ export function recordPatternMatchedType( ctx: CheckerContext, t: Type, recordPat: RecordPattern ): Type { // Transparent aliases and inline records match as the record they name. let unwrapped = unwrapAliases(t); if (!(unwrapped is UnionT)) { return unwrapped; } let members = recordUnionMembersOf(unwrapped); if (members == null) { return unwrapped; } let shape = recordUnionPatternShape(ctx, unwrapped, members, recordPat); if (shape == null) { return unwrapped; } if (!(shape is RecordT)) { return shape; } ctx.model.setRecordUnionAdaptation(recordPat, members, shape); return shape; } /** * The type of `propName` read off a union of record types: the field * every member holds, required, at one type. * * Null when some member is not a record type, which leaves the caller * its own diagnostic. `ErrorType` when the members are all records but * disagree about the field — those name what is wrong, which the * caller's "not supported" cannot. */ function resolveRecordUnionMemberType( ctx: CheckerContext, ut: UnionT, propName: String, loc: SourceLocation | null ): Type | null { let membersOpt = recordUnionMembersOf(ut); if (membersOpt == null) { return null; } var shared: Type | null = null; for (let rec in membersOpt) { if (!rec.properties.has(propName)) { ctx.error( "Property '" + displayMemberName(propName) + "' does not exist on '" + typeToString(rec) + "', one member of '" + typeToString(ut) + "'.", DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } if (rec.isOptional(propName)) { // Same rule as a plain record: the field may be absent, so a // direct read has no value to produce. ctx.error( "Property '" + displayMemberName(propName) + "' is optional in '" + typeToString(rec) + "', one member of '" + typeToString(ut) + "', so it may be absent. Destructure it with a default or match " + "on its presence.", DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } let fieldType = rec.properties[propName]; let prior = shared; let joined = if (prior != null) joinRecordFieldTypes(prior, fieldType) else fieldType; if (joined != null) { shared = joined; } else { ctx.error( "Property '" + displayMemberName(propName) + "' is '" + typeToString(prior as Type) + "' in one member of '" + typeToString(ut) + "' and '" + typeToString(fieldType) + "' in another, so the read has no one type.", DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } } return shared; } /** * Resolve a field's type if it is still to be inferred from its * initializer (FieldInfo.pendingInit). Called where a member access * reads a field's type, so a class from another member of an import * cycle answers before its own bodies are checked. */ function settleFieldType(fi: FieldInfo) { let pending = fi.pendingInit; if (pending != null) { fi.pendingInit = null; fi.type = pending(); } } /** * Reports a static member read on an instance, or an instance member * read on the class, and says so: a member is reached through the one * of the two it belongs to. `code` is the diagnostic code the caller * reports under. */ export function staticAccessMismatch( ctx: CheckerContext, isStaticAccess: boolean, propName: String, className: String, code: DiagnosticCode, loc: SourceLocation | null ): void { if (isStaticAccess) { ctx.error( `Cannot access instance member '${displayMemberName(propName)}' on class '${className}'.`, code, loc ); } else { ctx.error( `Cannot access static member '${displayMemberName(propName)}' on an instance of '${className}'.`, code, loc ); } } /** * Look up a member (field or method) on a type. */ function resolveMemberType( ctx: CheckerContext, objectType: Type, propName: String, loc: SourceLocation | null, ownerRead: boolean, isStaticAccess: boolean = false ): Type { if (propName == "") { return new ErrorType(); } // Member access forwards through the ownership handles. `Own`, // `Borrow` and `Unmanaged` carry no members of their own — they // differ in permissions, not data — so `d.read()` on an `Own` // resolves against `Descriptor`. A resource class has no unwrapped form, so // forwarding is the only way its members are reachable at all. // See docs/design/ownership.md §Handles. let handleInner = ownershipHandleInner(objectType); if (handleInner != null) { rejectConsumingThroughNonOwner(ctx, objectType, handleInner, propName, loc); return resolveMemberType(ctx, handleInner, propName, loc, ownerRead, isStaticAccess); } match (objectType) { case ClassType as ct: { let members = ct.memberTable(isStaticAccess); let otherMembers = ct.memberTable(!isStaticAccess); let getterName = "get#" + propName; let deps = ctx.currentDependencies(); if (deps != null) { var classSymId = (ct.genericSource ?? ct).symbolId; // A mixin's body is checked against a synthetic `This` class // whose members are the mixin's, so a member read on it // references the mixin's declaration. let mixinSelf = (ct.genericSource ?? ct).mixinSelf; if (mixinSelf != null && mixinSelf.symbolId != noSymbolId) { classSymId = mixinSelf.symbolId; } deps.referencedSymbols.add(classSymId); if (ct.isExtension) { deps.referencedTypes.add(ct); } if (members.has(propName) && members[propName] is MethodInfo) { deps.referencedClassMethods.add(`${i32ToString(classSymId as i32)}.${propName}`); } else if (members.has(propName) && members[propName] is FieldInfo) { let fi = members[propName] as FieldInfo; if (ct.isExtension || fi.isAbstract) { deps.referencedClassMethods.add(`${i32ToString(classSymId as i32)}.${getterName}`); } } else if (members.has(getterName)) { deps.referencedClassMethods.add(`${i32ToString(classSymId as i32)}.${getterName}`); } } // Check fields first. if (members.has(propName) && members[propName] is FieldInfo) { let fi = members[propName] as FieldInfo; settleFieldType(fi); let fieldType = fi.type; // Substitute type parameters if this is an instantiated generic class. let subField = substituteTypeParams(ct.typeParameters, ct.typeArguments, fieldType); let resolved = substituteThisType(ct as Type, subField); // Inside a consuming method, `this.field` keeps its owner type // so it can be moved out — at most once per field per path, // like a local (ownership.md §Affine fields). Everywhere else // an affine field reads as a borrow. if (ownerRead && typeCarriesOwner(resolved)) { return resolved; } // A field in the class's `scoped T` is taken, not borrowed. if (fieldReadsAsTake(fieldType)) { return resolved; } return borrowWrapFieldRead(ctx, resolved); } // Check methods. if (members.has(propName) && members[propName] is MethodInfo) { let methodInfo = members[propName] as MethodInfo; let subMethod = substituteTypeParams(ct.typeParameters, ct.typeArguments, methodInfo.type); return substituteThisType(ct as Type, subMethod); } // Check for getter method (get#propName) if (members.has(getterName)) { let getterMember = members[getterName]; if (getterMember is MethodInfo) { // Return the return type of the getter let subGetter = substituteTypeParams(ct.typeParameters, ct.typeArguments, (getterMember.type as FunctionType).returnType); return substituteThisType(ct as Type, subGetter); } } // If not found in the receiver's namespace, check the other namespace to report a static/instance mismatch. if (otherMembers.has(propName) || otherMembers.has(getterName)) { staticAccessMismatch(ctx, isStaticAccess, propName, ct.name, DiagnosticCode.PropertyNotFound, loc); return new ErrorType(); } ctx.error( `Property '${displayMemberName(propName)}' does not exist on type '${ct.name}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case InterfaceType as it: { let deps = ctx.currentDependencies(); // The member this reads is recorded by codegen's walk of the body // (noteInterfaceDispatch), which gives it a vtable slot. if (deps != null) { deps.referencedSymbols.add((it.genericSource ?? it).symbolId); } if (it.members.has(propName) && it.members[propName] is FieldInfo) { let fieldType = it.members[propName].type; let subField = substituteTypeParams(it.typeParameters, it.typeArguments, fieldType); let resolved = substituteThisType(it as Type, subField); if (fieldReadsAsTake(fieldType)) { return resolved; } return borrowWrapFieldRead(ctx, resolved); } if (it.members.has(propName) && it.members[propName] is MethodInfo) { let methodInfo = it.members[propName]; let subMethod = substituteTypeParams(it.typeParameters, it.typeArguments, methodInfo.type); return substituteThisType(it as Type, subMethod); } // Check for getter method let getterName = "get#" + propName; if (it.members.has(getterName)) { let getterInfo = it.members[getterName]; let subGetter = substituteTypeParams(it.typeParameters, it.typeArguments, (getterInfo.type as FunctionType).returnType); return substituteThisType(it as Type, subGetter); } ctx.error( `Property '${displayMemberName(propName)}' does not exist on interface '${it.name}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case RecordT as rec: { let {properties} = rec; if (properties.has(propName)) { if (rec.isOptional(propName)) { // The field may be absent, so a direct read has no value to // produce; presence is consumed through destructuring with a // default or an `if (let {...} = ...)` pattern. ctx.error( "Property '" + displayMemberName(propName) + "' is optional and may be absent. " + "Destructure it with a default or match on its presence.", DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } return properties[propName]; } ctx.error( `Property '${displayMemberName(propName)}' does not exist on record type.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case UnionT as ut: { // A union of record types is read one field at a time, by a // shape every member has: the read dispatches on the member the // value is, the way a use that passes the whole union repacks // the member it gets (RecordUnionAdaptation). A union of // anything else has no member to read. let shared = resolveRecordUnionMemberType(ctx, ut, propName, loc); if (shared != null) { return shared; } ctx.error( `Property access not supported on type '${typeToString(objectType)}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case ArrayType {elementType, elementsMutable}: { // Delegate to the backing extension class — FixedArray for // mutable elements, ImmutableArray for immutable ones. let faType = resolveArrayBackingClass(ctx, elementsMutable); if (faType != null) { let faCt = faType as Type; if (faCt is ClassType) { let classSymId = (faCt.genericSource ?? faCt).symbolId; let deps = ctx.currentDependencies(); if (deps != null) { deps.referencedSymbols.add(classSymId); if (faCt.members.has(propName) && faCt.members[propName] is MethodInfo) { deps.referencedClassMethods.add(`${i32ToString(classSymId as i32)}.${propName}`); } else if (faCt.members.has(propName) && faCt.members[propName] is FieldInfo) { let fi = faCt.members[propName] as FieldInfo; if (faCt.isExtension || fi.isAbstract) { deps.referencedClassMethods.add(`${i32ToString(classSymId as i32)}.get#${propName}`); } } else if (faCt.members.has("get#" + propName)) { deps.referencedClassMethods.add(`${i32ToString(classSymId as i32)}.get#${propName}`); } } // Build type arguments [elementType] for substitution. let typeArgs = growable([elementType]); // Check getter first. let getterName = "get#" + propName; if (faCt.members.has(getterName)) { let getterInfo = faCt.members[getterName]; return substituteTypeParams(faCt.typeParameters, typeArgs, (getterInfo.type as FunctionType).returnType); } if (faCt.members.has(propName)) { let memberInfo = faCt.members[propName]; return substituteTypeParams(faCt.typeParameters, typeArgs, memberInfo.type); } } } ctx.error( `Property '${displayMemberName(propName)}' does not exist on type 'Array<${typeToString(elementType)}>'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case TypeParameterType {name as tname, constraint}: { if (constraint != null) { return resolveMemberType(ctx, constraint, propName, loc, ownerRead, isStaticAccess); } ctx.error( `Property '${displayMemberName(propName)}' does not exist on type '${tname}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case TypeAliasType as tat: { return resolveMemberType(ctx, tat.target, propName, loc, ownerRead, isStaticAccess); } case EnumType as et: { // `Color.Red`: a member read off the enum's name. A value of the // enum has no members of its own. if (isStaticAccess) { if (let (true, member) = et.members.get(propName)) { return member.type; } ctx.error( `Enum '${et.name}' has no member '${displayMemberName(propName)}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } ctx.error( `Property '${displayMemberName(propName)}' does not exist on type '${et.name}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case ErrorType: { return new ErrorType(); } case _: { ctx.error( `Property access not supported on type '${typeToString(objectType)}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } } } export function checkIndexExpression( ctx: CheckerContext, object: Expression, index: Expression, optional: boolean, loc: SourceLocation | null, node: Node | null = null, inChain: boolean = false ): Type { if (inChain && node is IndexExpression && samePostfixChain(node, object)) { ctx.chainInteriorNode = object as Node; } let objectType = checkExpression(ctx, object, null); // Handle optional chaining on nullable object. let (baseType_, isNullable) = stripNull(ctx, objectType); let resultType = resolveIndexType(ctx, baseType_, index, loc, node); if (optional && !isNullable && !(resultType is ErrorType) && !(objectType is ErrorType) && !(objectType is UnknownType) && !(objectType is NeverType)) { ctx.warning( "Receiver of '?[' is not nullable; optional chaining is unnecessary.", DiagnosticCode.UnnecessaryNullish, loc ); } if (isNullable && optional) { if (inChain) { ctx.chainShortCircuits = true; return resultType; } if (isBoxablePrimitive(resultType) && (node == null || !ctx.nullishSanctioned.has(node.id as i32))) { ctx.error( "Optional access to a primitive element requires immediate coalescence: follow it with '?? '.", DiagnosticCode.InvalidOperator, loc ); return new ErrorType(); } let types = growable([resultType, new NullType()]); return createUnionType(types); } return resultType; } /** * The index types an `operator []` accepts, for a diagnostic: the * first parameter of every candidate, joined with " or ". */ function describeIndexParameters( candidates: GrowableArray, typeParams: Array | null, typeArgs: Array | null, at: i32 = 0 ) { let described = new StringBuilder(); var first = true; for (let candidate in candidates) { let resolved = substituteTypeParams(typeParams, typeArgs, candidate) as FunctionType; if (resolved.parameters.length <= at) { continue; } if (!first) { described.append(" or "); } described.append(`'${typeToString(resolved.parameters[at])}'`); first = false; } return described.toString(); } /** * The write half of `recv[i] = v` (issue #121): selects an `operator * []=` of the receiver over the index and the value, records it on the * assignment for codegen, and answers the setter's value parameter * type, which is the assignment's type. The `[]` read takes no part: a * `[]=` whose index no `[]` accepts is reachable, and the value is * checked against the setter's parameter rather than the read's result. * * Null when the receiver declares no `[]=`, which leaves the assignment * to the read-based path: a bare array, or a receiver the read reports * as unindexable. The value is unchecked then. */ export function checkIndexWrite( ctx: CheckerContext, assignment: Expression, target: IndexExpression, value: Expression, loc: SourceLocation | null ): Type | null { let objectType = ctx.model.getNodeType(target.object); if (objectType == null) { return null; } // A type parameter writes through its bound, as a read does. var receiver = unwrapAliases(objectType); while (receiver is TypeParameterType) { let bound = receiver.constraint; if (bound == null) { break; } receiver = unwrapAliases(bound); } var setter: FunctionType | null = null; var ownerParams: Array | null = null; var ownerArgs: Array | null = null; var ownerName = ""; match (receiver) { case ClassType as ct: { if (ct.members.has("[]=")) { setter = ct.members["[]="].type as FunctionType; ownerParams = ct.typeParameters; ownerArgs = ct.typeArguments; ownerName = ct.name; } } case InterfaceType as it: { if (it.members.has("[]=")) { setter = it.members["[]="].type as FunctionType; ownerParams = it.typeParameters; ownerArgs = it.typeArguments; ownerName = it.name; } } case _: {} } if (setter == null) { return null; } let setterFt = setter; let candidates = overloadCandidates(setterFt); let indexType = checkExpression(ctx, target.index, null); // The index alone narrows the candidates. When one is left, its value // parameter is the context the value is checked in, as a call's // parameter is for its argument. var byIndex = 0; var valueContext: Type | null = null; for (let candidate in candidates) { let resolved = substituteTypeParams(ownerParams, ownerArgs, candidate) as FunctionType; if (resolved.parameters.length != 2) { continue; } let indexParam = resolved.parameters[0]; if (isAssignableTo(ctx, indexType, indexParam) || argumentAdaptsToParameter(target.index, indexParam)) { byIndex += 1; valueContext = resolved.parameters[1]; } } let valueType = checkExpression(ctx, value, if (byIndex == 1) valueContext else null); // Most-specific selection over (index, value); the index adapts, the // value, already typed in context, does not. let applicableRaw = new Array(); let applicableRes = new Array(); collectApplicableOperatorOverloads( ctx, candidates, ownerParams, ownerArgs, growable([indexType, valueType]), growable([target.index, value]), 1, applicableRaw, applicableRes); let selIdx = selectMostSpecificIndex(ctx, applicableRes, 2, loc); if (selIdx < 0) { if (!(indexType is ErrorType) && !(valueType is ErrorType)) { if (byIndex == 0) { ctx.error( `Type mismatch: '${ownerName}' cannot be indexed by '${typeToString(indexType)}'; 'operator []=' expects ${describeIndexParameters(candidates, ownerParams, ownerArgs)}.`, DiagnosticCode.TypeMismatch, loc ); } else { ctx.error( `Type mismatch: '${typeToString(valueType)}' is not assignable to the value of 'operator []=', which expects ${describeIndexParameters(candidates, ownerParams, ownerArgs, 1)}.`, DiagnosticCode.TypeMismatch, loc ); } } ctx.setNodeType(target, new ErrorType()); return new ErrorType(); } if (candidates.length > 1) { ctx.model.setResolvedOverload(assignment, applicableRaw[selIdx]); } let valueParam = applicableRes[selIdx].parameters[1]; ctx.setNodeType(target, valueParam); return valueParam; } /** * The extension class whose members serve a bare array receiver: * FixedArray for mutable elements, ImmutableArray for immutable ones. * * Matched against `currentClass` by name for the reason given at the * array-literal site: the declaring file can be reached under two * module ids, producing two nominal copies of the class. */ export function resolveArrayBackingClass( ctx: CheckerContext, elementsMutable: boolean ): ClassType | null { let w = if (elementsMutable) WellKnown.FixedArrayClass else WellKnown.ImmutableArrayClass; let name = wellKnownName(w); let {currentClass} = ctx; if (currentClass != null && currentClass.name == name) { return currentClass; } // Nullable: this answers "which class backs a bare array", and a // compilation without that library has no answer. Callers that cannot // proceed without one say so themselves. let resolved = ctx.tryResolveWellKnownType(w); if (resolved is ClassType) { return resolved; } return null; } /** An index expression as a message shows it: a literal's digits. */ function indexText(index: Expression) { return if (let NumberLiteral {raw} = index) raw else "expression"; } /** * The type `recv[index]` reads through `indexMethod`, an `operator []` * of the class or interface `ownerName` at `typeArgs`: the most * specific of its overloads the index is applicable to * (member-lookup.md §5.1), recorded on `node` for codegen when there is * more than one. An index no overload accepts is reported; the * first-declared signature would type the expression off a method the * read can never reach. */ function selectIndexRead( ctx: CheckerContext, ownerName: String, indexMethod: FunctionType, typeParams: Array | null, typeArgs: Array | null, indexType: Type, index: Expression, node: Node | null, loc: SourceLocation | null ): Type { let candidates = overloadCandidates(indexMethod); let applicableRaw = new Array(); let applicableRes = new Array(); collectApplicableOperatorOverloads( ctx, candidates, typeParams, typeArgs, growable([indexType]), growable([index]), 1, applicableRaw, applicableRes); let selIdx = selectMostSpecificIndex(ctx, applicableRes, 1, loc); if (selIdx < 0) { if (!(indexType is ErrorType)) { ctx.error( `Type mismatch: '${ownerName}' cannot be indexed by \ '${typeToString(indexType)}'; 'operator []' expects \ ${describeIndexParameters(candidates, typeParams, typeArgs)}.`, DiagnosticCode.TypeMismatch, loc ); } return new ErrorType(); } let selectedMethod = applicableRaw[selIdx]; if (node != null && candidates.length > 1) { ctx.model.setResolvedOverload(node, selectedMethod); } return substituteTypeParams(typeParams, typeArgs, selectedMethod.returnType); } function resolveIndexType( ctx: CheckerContext, objectType: Type, index: Expression, loc: SourceLocation | null, node: Node | null = null ): Type { // Check the index. let indexType = checkExpression(ctx, index, null); // The single operand every overloaded `[]` below selects over, // shared by the class, extension and interface branches. let operandTypes = growable([indexType]); let operands = growable([index]); match (objectType) { case ArrayType {elementType, elementsMutable}: { // Check for extension class operator[] on the backing array class // (e.g., FixedArray.operator[](Range)). let fixedArrayPrelude = resolveArrayBackingClass(ctx, elementsMutable); if (fixedArrayPrelude is ClassType) { let typeArgs = growable([elementType]); let ct = instantiateClassType(fixedArrayPrelude, typeArgs); if (ct.members.has("[]")) { let opMethodInfo = ct.members["[]"]; let baseMethod = opMethodInfo.type as FunctionType; let candidates = overloadCandidates(baseMethod); // Most-specific selection over the applicable candidates // (member-lookup.md §5.1). let applicableRaw = new Array(); let applicableRes = new Array(); collectApplicableOperatorOverloads( ctx, candidates, ct.typeParameters, ct.typeArguments, operandTypes, operands, 1, applicableRaw, applicableRes); if (applicableRaw.length > 0) { let selIdx = selectMostSpecificIndex(ctx, applicableRes, 1, loc); if (node != null && candidates.length > 1) { // Record the subtype-sensitive selection for codegen. ctx.model.setResolvedOverload(node, applicableRaw[selIdx]); } return substituteTypeParams(ct.typeParameters, ct.typeArguments, applicableRaw[selIdx].returnType); } } } // Array indexing — index must be i32. if (!isIntegerType(indexType)) { ctx.error( `Array index must be an integer, got '${typeToString(indexType)}'.`, DiagnosticCode.TypeMismatch, loc ); } return elementType; } case TupleT {elementTypes, row, rowIndex}: { // A row's elements are the caller's, so only the positions before // it are known. if (row != null) { if (let NumberLiteral {raw} = index) { while (let (true, idx) = parseTupleIndexLiteral(raw)) { if (idx >= 0 && idx < rowIndex) { return tupleElementRead(ctx, elementTypes[idx], loc); } break; } } ctx.error( `Index ${indexText(index)} is at or past the row '${row.name}' of type '${typeToString(objectType)}': the elements from there on are the caller's.`, DiagnosticCode.IndexOutOfBounds, loc ); return new ErrorType(); } // Tuple indexing — return specific element type for literal index. match (index) { case NumberLiteral {raw}: { while (let (true, idx) = parseTupleIndexLiteral(raw)) { if (idx >= 0 && idx < elementTypes.length) { return tupleElementRead(ctx, elementTypes[idx], loc); } break; } ctx.error( `Tuple index out of bounds: ${raw} (tuple has ${elementTypes.length} elements).`, DiagnosticCode.IndexOutOfBounds, loc ); return new ErrorType(); } case _: { // Non-literal index — return union of all element types. let reads = new Array(); for (let el in elementTypes) { reads.push(tupleElementRead(ctx, el, loc)); } return createUnionType(reads); } } } case InlineTupleT {elementTypes}: { // An inline result's positions are separate values, so only a // literal names one. A nested inline tuple is read boxed. if (let NumberLiteral {raw} = index) { while (let (true, idx) = parseTupleIndexLiteral(raw)) { if (idx >= 0 && idx < elementTypes.length) { if (isAffineValue(objectType)) { // Reading one element of the temporary would leave its // other owners unreleased. ctx.error( `'${typeToString(objectType)}' holds an owner, so it is destructured rather than indexed.`, DiagnosticCode.TypeMismatch, loc ); return new ErrorType(); } return tupleElementRead(ctx, boxedFormOf(elementTypes[idx]), loc); } break; } ctx.error( `Tuple index out of bounds: ${raw} (tuple has ${elementTypes.length} elements).`, DiagnosticCode.IndexOutOfBounds, loc ); return new ErrorType(); } ctx.error( "An inline tuple is indexed with a literal: its positions are separate values.", DiagnosticCode.TypeMismatch, loc ); return new ErrorType(); } case ClassType as ct: { // Check for operator [] method. if (ct.members.has("[]")) { ctx.referenceClassMethod(ct, "[]"); return selectIndexRead(ctx, ct.name, ct.members["[]"].type as FunctionType, ct.typeParameters, ct.typeArguments, indexType, index, node, loc); } ctx.error( `Type '${ct.name}' does not support indexing (no 'operator []' method).`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case InterfaceType as it: { if (it.members.has("[]")) { return selectIndexRead(ctx, it.name, it.members["[]"].type as FunctionType, it.typeParameters, it.typeArguments, indexType, index, node, loc); } ctx.error( `Interface '${it.name}' does not support indexing (no 'operator []' method).`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case ErrorType: { return new ErrorType(); } case TypeParameterType {constraint}: { // Index access resolves through the bound, the same way member // access does: the generic body is checked once against the // constraint, and every specialization's receiver is a concrete // type that lowers directly. This is what makes // `>` the signature that accepts every array // representation at no cost — see docs/design/array-mutability.md. if (constraint != null) { return resolveIndexType(ctx, constraint, index, loc, node); } ctx.error( `Index access not supported on type '${typeToString(objectType)}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } case _: { ctx.error( `Index access not supported on type '${typeToString(objectType)}'.`, DiagnosticCode.PropertyNotFound, loc ); return new ErrorType(); } } }