lang: Resolve explicit function type arguments
f47f7a2cb8df3106d4d38612626318e3f1ca39d309f7d9121958d6236db3d203
Select function applications by lexical lookup of the bracket base. Check explicit arguments and Copy obligations before interning the substituted signature. Record dependencies from generic bodies. Assisted-by: Codex:gpt-6 Change-Id: I7169f2169519b93ed473b3d59435d2ce5c709f89
1 parent
dc223208
lib/std/lang/lower.rad
+5 -0
| 11325 | 11325 | } |
|
| 11326 | 11326 | case ast::NodeValue::UnOp(unop) => { |
|
| 11327 | 11327 | set val = try lowerUnOp(self, node, unop); |
|
| 11328 | 11328 | } |
|
| 11329 | 11329 | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 11330 | + | if let case resolver::NodeExtra::FunctionApplication(_) = |
|
| 11331 | + | resolver::nodeData(&self.resolver, node).extra |
|
| 11332 | + | { |
|
| 11333 | + | throw LowerError::Unsupported; |
|
| 11334 | + | } |
|
| 11330 | 11335 | set val = try lowerSubscript(self, node, container, items[0]); |
|
| 11331 | 11336 | } |
|
| 11332 | 11337 | case ast::NodeValue::BuiltinCall { kind, args } => { |
|
| 11333 | 11338 | set val = try lowerBuiltinCall(self, node, kind, args); |
|
| 11334 | 11339 | } |
lib/std/lang/resolver.rad
+260 -8
| 63 | 63 | export constant MAX_LOOP_DEPTH: u32 = 16; |
|
| 64 | 64 | /// Maximum trait instances. |
|
| 65 | 65 | export constant MAX_INSTANCES: u32 = 128; |
|
| 66 | 66 | /// Maximum standalone methods (across all types). |
|
| 67 | 67 | export constant MAX_METHODS: u32 = 256; |
|
| 68 | + | /// Maximum number of checked function applications per package. |
|
| 69 | + | constant MAX_FUNCTION_APPLICATIONS: u32 = 4096; |
|
| 70 | + | ||
| 68 | 71 | /// Maximum number of linear bindings active in one function. |
|
| 69 | 72 | constant MAX_LINEAR_BINDINGS: u32 = 32; |
|
| 70 | 73 | /// Maximum inline field depth used to prove borrow separation. |
|
| 71 | 74 | constant MAX_BORROW_FIELDS: u32 = 16; |
|
| 72 | 75 | /// Maximum nesting depth tracked for loops. |
| 393 | 396 | /// Index of the sources that the function may store into the objects of its |
|
| 394 | 397 | /// parameters, in the store table of the resolver. |
|
| 395 | 398 | stores: ?u32, |
|
| 396 | 399 | } |
|
| 397 | 400 | ||
| 401 | + | /// The ordered replacements for the type parameters of one declaration. |
|
| 402 | + | export record TypeSubstitution: Copy { |
|
| 403 | + | /// Abstract parameter identities. |
|
| 404 | + | params: *[NominalId], |
|
| 405 | + | /// Arguments in parameter order. |
|
| 406 | + | args: *[Type], |
|
| 407 | + | } |
|
| 408 | + | ||
| 409 | + | /// Checked application of a generic function. |
|
| 410 | + | export record FunctionApplication: Copy { |
|
| 411 | + | /// Package that requests the application. |
|
| 412 | + | packageId: u16, |
|
| 413 | + | /// Template symbol in its defining module. |
|
| 414 | + | template: SymbolId, |
|
| 415 | + | /// Enclosing template that requests this application, if any. |
|
| 416 | + | owner: ?SymbolId, |
|
| 417 | + | /// Parameter replacements in the applied signature. |
|
| 418 | + | substitution: TypeSubstitution, |
|
| 419 | + | /// Signature with the type arguments substituted. |
|
| 420 | + | signature: *FnType, |
|
| 421 | + | } |
|
| 422 | + | ||
| 398 | 423 | /// Describes a type computed during semantic analysis. |
|
| 399 | 424 | export union Type: Copy { |
|
| 400 | 425 | /// A type that couldn't be decided. |
|
| 401 | 426 | Unknown, |
|
| 402 | 427 | /// Types only used during inference. |
| 643 | 668 | Arity, |
|
| 644 | 669 | /// A type or region occurs in a value expression. |
|
| 645 | 670 | ExpectedValue, |
|
| 646 | 671 | /// A generic declaration was used without type arguments. |
|
| 647 | 672 | MissingArguments, |
|
| 673 | + | /// Number of type arguments differs from the number of binders. |
|
| 674 | + | ArgumentCount(CountMismatch), |
|
| 675 | + | /// A type argument must satisfy the Copy bound. |
|
| 676 | + | RequiresCopy, |
|
| 677 | + | /// Expected type syntax in an argument. |
|
| 678 | + | ExpectedType, |
|
| 648 | 679 | /// An operation needs a concrete layout. |
|
| 649 | 680 | LayoutDependsOnParameter, |
|
| 650 | 681 | /// The compiler cannot resolve a generic declaration or application. |
|
| 651 | 682 | Generic, |
|
| 652 | 683 | } |
| 965 | 996 | export union NodeExtra: Copy { |
|
| 966 | 997 | /// Byte capacity of the static heap produced by a constant call. |
|
| 967 | 998 | StaticHeap(u32), |
|
| 968 | 999 | /// No extra data for this node. |
|
| 969 | 1000 | None, |
|
| 1001 | + | /// Index of a checked generic function application. |
|
| 1002 | + | FunctionApplication(u32), |
|
| 970 | 1003 | /// Resolved field index for record literal fields. |
|
| 971 | 1004 | RecordField { index: u32 }, |
|
| 972 | 1005 | /// Slice range metadata for subscript expressions with ranges. |
|
| 973 | 1006 | SliceRange(SliceRangeInfo), |
|
| 974 | 1007 | /// Cached union variant metadata for patterns/constructors. |
| 1291 | 1324 | nodeData: metadata::Table, |
|
| 1292 | 1325 | /// Source types of move-only places bound by `let` at a different type. |
|
| 1293 | 1326 | /// The place node keeps the binding type, so the linear checker reads the |
|
| 1294 | 1327 | /// source type from here to see the move. Storage comes from `arena`. |
|
| 1295 | 1328 | movedSources: *mut [MovedSource], |
|
| 1296 | - | /// Collision chains for interned types. |
|
| 1297 | - | types: *mut [?*TypeNode], |
|
| 1329 | + | /// Interned types and checked template applications. |
|
| 1330 | + | typeTables: TypeTables, |
|
| 1298 | 1331 | /// Diagnostics recorded so far. |
|
| 1299 | 1332 | errors: DiagnosticBuffer, |
|
| 1300 | 1333 | /// Frozen module entries for the packages being resolved, set by [`resolve`]. |
|
| 1301 | 1334 | moduleTable: module::ModuleTable, |
|
| 1302 | 1335 | /// Cache of module scopes indexed by module ID. |
| 1319 | 1352 | /// Internal error sentinel thrown when analysis cannot proceed. |
|
| 1320 | 1353 | export union ResolveError: Copy { |
|
| 1321 | 1354 | Failure, |
|
| 1322 | 1355 | } |
|
| 1323 | 1356 | ||
| 1357 | + | /// Type interning and generic applications for the resolver. |
|
| 1358 | + | export record TypeTables { |
|
| 1359 | + | /// Collision chains for interned types. |
|
| 1360 | + | interned: *mut [?*TypeNode], |
|
| 1361 | + | /// Checked function applications in discovery order. |
|
| 1362 | + | functionApplications: *mut [FunctionApplication], |
|
| 1363 | + | } |
|
| 1364 | + | ||
| 1324 | 1365 | /// Number of collision chains in the type table. |
|
| 1325 | 1366 | constant TYPE_BUCKETS: u32 = 1024; |
|
| 1326 | 1367 | ||
| 1327 | 1368 | /// Select a collision chain from the declaration ID of a type or its item. |
|
| 1328 | 1369 | fn typeBucket(ty: Type) -> u32 { |
| 1351 | 1392 | ||
| 1352 | 1393 | /// Allocate and intern a type in the arena, returning a pointer for deduplication. |
|
| 1353 | 1394 | export fn allocType(self: &mut Resolver, ty: Type) -> *Type { |
|
| 1354 | 1395 | // Search existing types for a match. |
|
| 1355 | 1396 | let bucket = typeBucket(ty); |
|
| 1356 | - | let mut cursor = self.types[bucket]; |
|
| 1397 | + | let mut cursor = self.typeTables.interned[bucket]; |
|
| 1357 | 1398 | while let node = cursor { |
|
| 1358 | 1399 | if node.ty == ty { |
|
| 1359 | 1400 | return &node.ty; |
|
| 1360 | 1401 | } |
|
| 1361 | 1402 | set cursor = node.next; |
|
| 1362 | 1403 | } |
|
| 1363 | 1404 | // Allocate a new type node from the arena. A node is its own one-element |
|
| 1364 | 1405 | // slice, so its address stays fixed. |
|
| 1365 | 1406 | let mut nodes: *mut [TypeNode] = &mut []; |
|
| 1366 | - | try! nodes.append(TypeNode { ty, next: self.types[bucket] }, self.arena); |
|
| 1407 | + | try! nodes.append(TypeNode { ty, next: self.typeTables.interned[bucket] }, self.arena); |
|
| 1367 | 1408 | let frozen: *TypeNode = &mut nodes[0]; |
|
| 1368 | - | set self.types[bucket] = frozen; |
|
| 1409 | + | set self.typeTables.interned[bucket] = frozen; |
|
| 1369 | 1410 | ||
| 1370 | 1411 | return &frozen.ty; |
|
| 1371 | 1412 | } |
|
| 1372 | 1413 | ||
| 1373 | 1414 | /// Append a nominal type descriptor and return its ID. |
| 1515 | 1556 | phase: Phase::Graph, |
|
| 1516 | 1557 | nominals, |
|
| 1517 | 1558 | traits, |
|
| 1518 | 1559 | nodeData: metadata, |
|
| 1519 | 1560 | movedSources: &mut [], |
|
| 1520 | - | types: interned, |
|
| 1561 | + | typeTables: TypeTables { interned, functionApplications: &mut [] }, |
|
| 1521 | 1562 | errors: DiagnosticBuffer { entries: errors, len: 0 }, |
|
| 1522 | 1563 | moduleTable: module::ModuleTable { entries: &[] }, |
|
| 1523 | 1564 | moduleScopes, |
|
| 1524 | 1565 | instances, |
|
| 1525 | 1566 | methods, |
| 2715 | 2756 | } |
|
| 2716 | 2757 | else => return false, |
|
| 2717 | 2758 | } |
|
| 2718 | 2759 | } |
|
| 2719 | 2760 | ||
| 2761 | + | /// Substitute abstract parameter identities in a resolved type. |
|
| 2762 | + | export fn substituteType(self: &mut Resolver, ty: Type, sub: TypeSubstitution) -> Type { |
|
| 2763 | + | match ty { |
|
| 2764 | + | case Type::Nominal(id) => { |
|
| 2765 | + | for parameter, i in sub.params { |
|
| 2766 | + | if id == parameter { |
|
| 2767 | + | return sub.args[i]; |
|
| 2768 | + | } |
|
| 2769 | + | } |
|
| 2770 | + | return ty; |
|
| 2771 | + | } |
|
| 2772 | + | case Type::Pointer { class, target, mutable } => { |
|
| 2773 | + | let replaced = substituteType(self, *target, sub); |
|
| 2774 | + | return Type::Pointer { class, target: allocType(self, replaced), mutable }; |
|
| 2775 | + | } |
|
| 2776 | + | case Type::Slice { class, item, mutable } => { |
|
| 2777 | + | let replaced = substituteType(self, *item, sub); |
|
| 2778 | + | return Type::Slice { class, item: allocType(self, replaced), mutable }; |
|
| 2779 | + | } |
|
| 2780 | + | case Type::Array(array) => { |
|
| 2781 | + | let replaced = substituteType(self, *array.item, sub); |
|
| 2782 | + | return Type::Array(ArrayType { |
|
| 2783 | + | item: allocType(self, replaced), length: array.length |
|
| 2784 | + | }); |
|
| 2785 | + | } |
|
| 2786 | + | case Type::Optional(item) => { |
|
| 2787 | + | let replaced = substituteType(self, *item, sub); |
|
| 2788 | + | return Type::Optional(allocType(self, replaced)); |
|
| 2789 | + | } |
|
| 2790 | + | case Type::Fn(sig) => return Type::Fn(substituteSignature(self, sig, sub)), |
|
| 2791 | + | else => return ty, |
|
| 2792 | + | } |
|
| 2793 | + | } |
|
| 2794 | + | ||
| 2795 | + | /// Substitute the types in a checked function signature. |
|
| 2796 | + | fn substituteSignature(self: &mut Resolver, sig: *FnType, sub: TypeSubstitution) -> *FnType { |
|
| 2797 | + | let mut params: *mut [*Type] = &mut []; |
|
| 2798 | + | let mut errors: *mut [*Type] = &mut []; |
|
| 2799 | + | let mut changed = false; |
|
| 2800 | + | for parameter in sig.paramTypes { |
|
| 2801 | + | let ty = substituteType(self, *parameter, sub); |
|
| 2802 | + | set changed = changed or ty <> *parameter; |
|
| 2803 | + | try! params.append(allocType(self, ty), self.arena); |
|
| 2804 | + | } |
|
| 2805 | + | for error in sig.throwList { |
|
| 2806 | + | let ty = substituteType(self, *error, sub); |
|
| 2807 | + | set changed = changed or ty <> *error; |
|
| 2808 | + | try! errors.append(allocType(self, ty), self.arena); |
|
| 2809 | + | } |
|
| 2810 | + | let result = substituteType(self, *sig.returnType, sub); |
|
| 2811 | + | if not changed and result == *sig.returnType { |
|
| 2812 | + | return sig; |
|
| 2813 | + | } |
|
| 2814 | + | let mut info = *sig; |
|
| 2815 | + | set info.paramTypes = ¶ms[..]; |
|
| 2816 | + | set info.throwList = &errors[..]; |
|
| 2817 | + | set info.returnType = allocType(self, result); |
|
| 2818 | + | return allocFnType(self, info); |
|
| 2819 | + | } |
|
| 2820 | + | ||
| 2720 | 2821 | /// Return whether `ty` is a direct reference. |
|
| 2721 | 2822 | export fn isRefType(ty: Type) -> bool { |
|
| 2722 | 2823 | match ty { |
|
| 2723 | 2824 | case |
|
| 2724 | 2825 | Type::Pointer { class: types::PointerClass::Ref, .. }, |
| 4202 | 4303 | if let case Type::Array(_) = autoDeref(parent) { |
|
| 4203 | 4304 | return true; |
|
| 4204 | 4305 | } |
|
| 4205 | 4306 | return false; |
|
| 4206 | 4307 | } |
|
| 4207 | - | case ast::NodeValue::Bracket { .. }, ast::NodeValue::Deref(_) => |
|
| 4208 | - | return false, |
|
| 4308 | + | case ast::NodeValue::Bracket { .. } => { |
|
| 4309 | + | if let case NodeExtra::FunctionApplication(_) = nodeData(self, node).extra { |
|
| 4310 | + | return true; |
|
| 4311 | + | } |
|
| 4312 | + | return false; |
|
| 4313 | + | } |
|
| 4314 | + | case ast::NodeValue::Deref(_) => return false, |
|
| 4209 | 4315 | else => |
|
| 4210 | 4316 | return true, |
|
| 4211 | 4317 | } |
|
| 4212 | 4318 | } |
|
| 4213 | 4319 |
| 8902 | 9008 | }); |
|
| 8903 | 9009 | } |
|
| 8904 | 9010 | } |
|
| 8905 | 9011 | } |
|
| 8906 | 9012 | ||
| 9013 | + | /// Resolve a bracket operand as a type in the caller's scope. |
|
| 9014 | + | fn resolveTypeArgument(self: &mut Resolver, node: *ast::Node) -> Type |
|
| 9015 | + | throws (ResolveError) |
|
| 9016 | + | { |
|
| 9017 | + | match node.value { |
|
| 9018 | + | case ast::NodeValue::BracketArgument(arg) => { |
|
| 9019 | + | let case ast::NodeValue::TypeSig(_) = arg.value |
|
| 9020 | + | else throw emitError(self, node, ErrorKind::Bracket(BracketError::ExpectedType)); |
|
| 9021 | + | return try resolveValueType(self, arg); |
|
| 9022 | + | } |
|
| 9023 | + | case ast::NodeValue::Ident(_), ast::NodeValue::ScopeAccess(_) => |
|
| 9024 | + | return Type::Nominal(try resolveTypeName(self, node)), |
|
| 9025 | + | case ast::NodeValue::ArrayRepeatLit(array) => { |
|
| 9026 | + | let item = try resolveTypeArgument(self, array.item); |
|
| 9027 | + | let length = try checkArrayLength(self, array.count); |
|
| 9028 | + | return Type::Array(ArrayType { item: allocType(self, item), length }); |
|
| 9029 | + | } |
|
| 9030 | + | case ast::NodeValue::Deref(target) => |
|
| 9031 | + | return try resolvePointerArgument(self, target, types::PointerClass::Owned, false), |
|
| 9032 | + | case ast::NodeValue::AddressOf(addr) => |
|
| 9033 | + | return try resolvePointerArgument( |
|
| 9034 | + | self, addr.target, types::PointerClass::Ref, addr.mutable |
|
| 9035 | + | ), |
|
| 9036 | + | else => throw emitError(self, node, ErrorKind::Bracket(BracketError::ExpectedType)), |
|
| 9037 | + | } |
|
| 9038 | + | } |
|
| 9039 | + | ||
| 9040 | + | /// Resolve an operand after a pointer prefix shared with value syntax. |
|
| 9041 | + | fn resolvePointerArgument( |
|
| 9042 | + | self: &mut Resolver, target: *ast::Node, class: types::PointerClass, mutable: bool |
|
| 9043 | + | ) -> Type throws (ResolveError) { |
|
| 9044 | + | if let case ast::NodeValue::ArrayLit(items) = target.value; items.len == 1 { |
|
| 9045 | + | let item = try resolveTypeArgument(self, items[0]); |
|
| 9046 | + | return Type::Slice { class, item: allocType(self, item), mutable }; |
|
| 9047 | + | } |
|
| 9048 | + | let pointee = try resolveTypeArgument(self, target); |
|
| 9049 | + | return Type::Pointer { class, target: allocType(self, pointee), mutable }; |
|
| 9050 | + | } |
|
| 9051 | + | ||
| 9052 | + | /// Find a generic function by ordinary lexical lookup of the bracket base. |
|
| 9053 | + | fn bracketFunction(self: &mut Resolver, base: *ast::Node) -> ?SymbolId |
|
| 9054 | + | throws (ResolveError) |
|
| 9055 | + | { |
|
| 9056 | + | let mut found: ?SymbolId = nil; |
|
| 9057 | + | match base.value { |
|
| 9058 | + | case ast::NodeValue::Ident(name) => set found = findAnySymbol(self, self.scope, name), |
|
| 9059 | + | case ast::NodeValue::ScopeAccess(access) => { |
|
| 9060 | + | let scope = self.scope; |
|
| 9061 | + | set found = try resolveAccess(self, base, access, scope); |
|
| 9062 | + | } |
|
| 9063 | + | else => {} |
|
| 9064 | + | } |
|
| 9065 | + | let sym = found else return nil; |
|
| 9066 | + | let case ast::NodeValue::FnDecl(decl) = self.symbols[*sym].node.value else return nil; |
|
| 9067 | + | return sym if decl.binders.len > 0 else nil; |
|
| 9068 | + | } |
|
| 9069 | + | ||
| 9070 | + | /// Resolve explicit arguments and intern a checked function application. |
|
| 9071 | + | fn resolveFunctionApplication( |
|
| 9072 | + | self: &mut Resolver, node: *ast::Node, template: SymbolId, items: *[*ast::Node] |
|
| 9073 | + | ) -> Type throws (ResolveError) { |
|
| 9074 | + | try resolveDeclaration(self, template, node); |
|
| 9075 | + | let case ast::NodeValue::FnDecl(decl) = self.symbols[*template].node.value else panic; |
|
| 9076 | + | let case SymbolData::Value { type: Type::Fn(sig), .. } = self.symbols[*template].data |
|
| 9077 | + | else throw emitError(self, node, ErrorKind::Internal); |
|
| 9078 | + | if items.len <> decl.binders.len { |
|
| 9079 | + | throw emitError(self, node, ErrorKind::Bracket(BracketError::ArgumentCount( |
|
| 9080 | + | CountMismatch { expected: decl.binders.len, actual: items.len } |
|
| 9081 | + | ))); |
|
| 9082 | + | } |
|
| 9083 | + | let mut params: *mut [NominalId] = &mut []; |
|
| 9084 | + | let mut args: *mut [Type] = &mut []; |
|
| 9085 | + | for item, i in items { |
|
| 9086 | + | let binder = decl.binders[i]; |
|
| 9087 | + | let sym = symbolFor(self, binder) |
|
| 9088 | + | else throw emitError(self, item, ErrorKind::Bracket(BracketError::Generic)); |
|
| 9089 | + | let case SymbolData::Type(id) = self.symbols[*sym].data else panic; |
|
| 9090 | + | let arg = try resolveTypeArgument(self, item); |
|
| 9091 | + | try ensureNoHeapValue(self, item, arg); |
|
| 9092 | + | // Call contracts must account for regions carried by a replacement. |
|
| 9093 | + | if refs::containsRef(self, arg) { |
|
| 9094 | + | throw emitError(self, item, ErrorKind::Bracket(BracketError::Generic)); |
|
| 9095 | + | } |
|
| 9096 | + | if isCopy(self, Type::Nominal(id)) and not isCopy(self, arg) { |
|
| 9097 | + | throw emitError(self, item, ErrorKind::Bracket(BracketError::RequiresCopy)); |
|
| 9098 | + | } |
|
| 9099 | + | try! params.append(id, self.arena); |
|
| 9100 | + | try! args.append(arg, self.arena); |
|
| 9101 | + | } |
|
| 9102 | + | let entry = module::get(self.moduleTable, self.currentMod); |
|
| 9103 | + | let mut packageId: u16 = 0; |
|
| 9104 | + | if let entry = entry { |
|
| 9105 | + | set packageId = entry.packageId; |
|
| 9106 | + | } |
|
| 9107 | + | let mut owner: ?SymbolId = nil; |
|
| 9108 | + | if let current = self.currentFnNode { |
|
| 9109 | + | if let case ast::NodeValue::FnDecl(currentDecl) = |
|
| 9110 | + | current.value; |
|
| 9111 | + | currentDecl.binders.len > 0 |
|
| 9112 | + | { |
|
| 9113 | + | set owner = symbolFor(self, current); |
|
| 9114 | + | } |
|
| 9115 | + | } |
|
| 9116 | + | let sub = TypeSubstitution { params: ¶ms[..], args: &args[..] }; |
|
| 9117 | + | let mut count: u32 = 0; |
|
| 9118 | + | for application, index in &self.typeTables.functionApplications[..] { |
|
| 9119 | + | if application.packageId <> packageId { |
|
| 9120 | + | continue; |
|
| 9121 | + | } |
|
| 9122 | + | set count += 1; |
|
| 9123 | + | if application.template <> template or application.owner <> owner { |
|
| 9124 | + | continue; |
|
| 9125 | + | } |
|
| 9126 | + | let mut equal = true; |
|
| 9127 | + | for arg, i in sub.args { |
|
| 9128 | + | if not typesEqual(self, arg, application.substitution.args[i]) { |
|
| 9129 | + | set equal = false; |
|
| 9130 | + | break; |
|
| 9131 | + | } |
|
| 9132 | + | } |
|
| 9133 | + | if equal { |
|
| 9134 | + | set self.nodeData.getMut(node.id).extra = NodeExtra::FunctionApplication(index); |
|
| 9135 | + | return setNodeType(self, node, Type::Fn(application.signature)); |
|
| 9136 | + | } |
|
| 9137 | + | } |
|
| 9138 | + | if count >= MAX_FUNCTION_APPLICATIONS { |
|
| 9139 | + | throw emitError(self, node, ErrorKind::LimitExceeded( |
|
| 9140 | + | "too many generic function applications in this package" |
|
| 9141 | + | )); |
|
| 9142 | + | } |
|
| 9143 | + | let signature = substituteSignature(self, sig, sub); |
|
| 9144 | + | let index = self.typeTables.functionApplications.len; |
|
| 9145 | + | try! self.typeTables.functionApplications.append( |
|
| 9146 | + | FunctionApplication { packageId, template, owner, substitution: sub, signature }, |
|
| 9147 | + | self.arena |
|
| 9148 | + | ); |
|
| 9149 | + | set self.nodeData.getMut(node.id).extra = NodeExtra::FunctionApplication(index); |
|
| 9150 | + | return setNodeType(self, node, Type::Fn(signature)); |
|
| 9151 | + | } |
|
| 9152 | + | ||
| 8907 | 9153 | /// Resolve postfix brackets from the type of their base. |
|
| 8908 | 9154 | fn resolveBracket( |
|
| 8909 | 9155 | self: &mut Resolver, node: *ast::Node, base: *ast::Node, items: *[*ast::Node] |
|
| 8910 | 9156 | ) -> Type |
|
| 8911 | 9157 | throws (ResolveError) |
|
| 8912 | 9158 | { |
|
| 9159 | + | if let template = try bracketFunction(self, base) { |
|
| 9160 | + | return try resolveFunctionApplication(self, node, template, items); |
|
| 9161 | + | } |
|
| 8913 | 9162 | let containerTy = try infer(self, base); |
|
| 8914 | 9163 | if items.len <> 1 { |
|
| 8915 | 9164 | throw emitError(self, node, ErrorKind::Bracket(BracketError::Arity)); |
|
| 8916 | 9165 | } |
|
| 8917 | 9166 | let indexNode = items[0]; |
| 12380 | 12629 | if usage == LinearUse::Consume { |
|
| 12381 | 12630 | try checkStaticMove(self, node); |
|
| 12382 | 12631 | } |
|
| 12383 | 12632 | } |
|
| 12384 | 12633 | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 12634 | + | if let case NodeExtra::FunctionApplication(_) = nodeData(self, node).extra { |
|
| 12635 | + | return; |
|
| 12636 | + | } |
|
| 12385 | 12637 | if let resultTy = movedType(self, node) { |
|
| 12386 | 12638 | if isMoveOnly(self, resultTy) and usage == LinearUse::Consume { |
|
| 12387 | 12639 | throw emitError(self, node, ErrorKind::LinearPartialMove); |
|
| 12388 | 12640 | } |
|
| 12389 | 12641 | } |
lib/std/lang/resolver/printer.rad
+10 -0
| 527 | 527 | io::print("expected a value, found a type or region"), |
|
| 528 | 528 | case super::BracketError::MissingArguments => |
|
| 529 | 529 | io::print("generic declaration requires type arguments"), |
|
| 530 | 530 | case super::BracketError::LayoutDependsOnParameter => |
|
| 531 | 531 | io::print("operation requires a concrete type layout"), |
|
| 532 | + | case super::BracketError::ArgumentCount(count) => { |
|
| 533 | + | io::print("expected "); |
|
| 534 | + | io::printU32(count.expected); |
|
| 535 | + | io::print(" type arguments, got "); |
|
| 536 | + | io::printU32(count.actual); |
|
| 537 | + | } |
|
| 538 | + | case super::BracketError::RequiresCopy => |
|
| 539 | + | io::print("type argument must satisfy the Copy bound"), |
|
| 540 | + | case super::BracketError::ExpectedType => |
|
| 541 | + | io::print("expected a type argument"), |
|
| 532 | 542 | case super::BracketError::Generic => |
|
| 533 | 543 | io::print("generic declarations and types cannot be resolved"), |
|
| 534 | 544 | } |
|
| 535 | 545 | } |
|
| 536 | 546 | case super::ErrorKind::InvalidAlignmentValue(val) => { |
lib/std/lang/resolver/tests/core.rad
+53 -0
| 3717 | 3717 | let escape = try resolveProgramStr( |
|
| 3718 | 3718 | &mut b, "fn escape[T: Copy](x: &'r T, y: T) -> &'r T { return &y; }" |
|
| 3719 | 3719 | ); |
|
| 3720 | 3720 | try expectError(&escape); |
|
| 3721 | 3721 | } |
|
| 3722 | + | ||
| 3723 | + | /// Check signature substitution in both direct calls and function values. |
|
| 3724 | + | @test unsafe fn testResolveGenericFunctionApplication() throws (testing::TestError) { |
|
| 3725 | + | for program in [ |
|
| 3726 | + | @embed("lang/resolver/tests/fixtures/generic_application/scalar.rad"), |
|
| 3727 | + | @embed("lang/resolver/tests/fixtures/generic_application/function_value.rad"), |
|
| 3728 | + | @embed("lang/resolver/tests/fixtures/generic_application/array.rad"), |
|
| 3729 | + | @embed("lang/resolver/tests/fixtures/generic_application/optional.rad"), |
|
| 3730 | + | @embed("lang/resolver/tests/fixtures/generic_application/forward.rad"), |
|
| 3731 | + | ] { |
|
| 3732 | + | let mut a = testResolver(); |
|
| 3733 | + | let result = try resolveProgramStr(&mut a, program); |
|
| 3734 | + | try expectNoErrors(&result); |
|
| 3735 | + | } |
|
| 3736 | + | } |
|
| 3737 | + | ||
| 3738 | + | /// Check lexical selection and reuse of ordered type applications. |
|
| 3739 | + | @test unsafe fn testResolveGenericFunctionApplicationIdentity() throws (testing::TestError) { |
|
| 3740 | + | let mut a = testResolver(); |
|
| 3741 | + | let result = try resolveProgramStr( |
|
| 3742 | + | &mut a, |
|
| 3743 | + | "fn id[T](x: T) -> T { return x; } fn checks() { let a = id[u32](1); let b = id[u32](2); let c = id[bool](true); }" |
|
| 3744 | + | ); |
|
| 3745 | + | try expectNoErrors(&result); |
|
| 3746 | + | try testing::expect(a.typeTables.functionApplications.len == 2); |
|
| 3747 | + | ||
| 3748 | + | let mut b = testResolver(); |
|
| 3749 | + | let shadowed = try resolveProgramStr( |
|
| 3750 | + | &mut b, |
|
| 3751 | + | "fn id[T](x: T) -> T { return x; } fn check() { let id: [u32; 2] = [1, 2]; let x = id[0]; }" |
|
| 3752 | + | ); |
|
| 3753 | + | try expectNoErrors(&shadowed); |
|
| 3754 | + | try testing::expect(b.typeTables.functionApplications.len == 0); |
|
| 3755 | + | } |
|
| 3756 | + | ||
| 3757 | + | /// Check Copy obligations at concrete and symbolic call sites. |
|
| 3758 | + | @test unsafe fn testResolveGenericFunctionBounds() throws (testing::TestError) { |
|
| 3759 | + | for program in [ |
|
| 3760 | + | @embed("lang/resolver/tests/fixtures/generic_application/linear_copy.rad"), |
|
| 3761 | + | @embed("lang/resolver/tests/fixtures/generic_application/unbounded_copy.rad"), |
|
| 3762 | + | ] { |
|
| 3763 | + | let mut a = testResolver(); |
|
| 3764 | + | let result = try resolveProgramStr(&mut a, program); |
|
| 3765 | + | try expectErrorKind(&result, resolver::ErrorKind::Bracket( |
|
| 3766 | + | resolver::BracketError::RequiresCopy |
|
| 3767 | + | )); |
|
| 3768 | + | } |
|
| 3769 | + | let mut a = testResolver(); |
|
| 3770 | + | let result = try resolveProgramStr( |
|
| 3771 | + | &mut a, "fn f[T: Copy]() {} fn forward[T: Copy]() { f[T](); }" |
|
| 3772 | + | ); |
|
| 3773 | + | try expectNoErrors(&result); |
|
| 3774 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_application/array.rad
added
+5 -0
| 1 | + | fn id[T](x: T) -> T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
|
| 4 | + | ||
| 5 | + | let x = id[[u8; 2]]([1, 2]); |
lib/std/lang/resolver/tests/fixtures/generic_application/forward.rad
added
+7 -0
| 1 | + | fn id[T](x: T) -> T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
|
| 4 | + | ||
| 5 | + | fn forward[T](x: T) -> T { |
|
| 6 | + | return id[T](x); |
|
| 7 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_application/function_value.rad
added
+5 -0
| 1 | + | fn id[T](x: T) -> T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
|
| 4 | + | ||
| 5 | + | let f: fn(bool) -> bool = id[bool]; |
lib/std/lang/resolver/tests/fixtures/generic_application/linear_copy.rad
added
+7 -0
| 1 | + | record Token: Once { number: u32 } |
|
| 2 | + | ||
| 3 | + | fn f[T: Copy]() {} |
|
| 4 | + | ||
| 5 | + | fn useIt() { |
|
| 6 | + | f[Token](); |
|
| 7 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_application/optional.rad
added
+5 -0
| 1 | + | fn id[T](x: T) -> T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
|
| 4 | + | ||
| 5 | + | let x = id[?u8](nil); |
lib/std/lang/resolver/tests/fixtures/generic_application/scalar.rad
added
+5 -0
| 1 | + | fn id[T](x: T) -> T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
|
| 4 | + | ||
| 5 | + | let x: u32 = id[u32](17); |
lib/std/lang/resolver/tests/fixtures/generic_application/unbounded_copy.rad
added
+5 -0
| 1 | + | fn f[T: Copy]() {} |
|
| 2 | + | ||
| 3 | + | fn forward[T]() { |
|
| 4 | + | f[T](); |
|
| 5 | + | } |
lib/std/std.manifest
+7 -0
| 127 | 127 | "lang/gen/bitset.ras", |
|
| 128 | 128 | ], |
|
| 129 | 129 | embed: [ |
|
| 130 | 130 | "lang/manifest/tests/fixtures/kernel.manifest", |
|
| 131 | 131 | "lang/manifest/tests/fixtures/module_order.manifest", |
|
| 132 | + | "lang/resolver/tests/fixtures/generic_application/array.rad", |
|
| 133 | + | "lang/resolver/tests/fixtures/generic_application/forward.rad", |
|
| 134 | + | "lang/resolver/tests/fixtures/generic_application/function_value.rad", |
|
| 135 | + | "lang/resolver/tests/fixtures/generic_application/linear_copy.rad", |
|
| 136 | + | "lang/resolver/tests/fixtures/generic_application/optional.rad", |
|
| 137 | + | "lang/resolver/tests/fixtures/generic_application/scalar.rad", |
|
| 138 | + | "lang/resolver/tests/fixtures/generic_application/unbounded_copy.rad", |
|
| 132 | 139 | "lang/resolver/tests/fixtures/generic_body/borrow.rad", |
|
| 133 | 140 | "lang/resolver/tests/fixtures/generic_body/callback.rad", |
|
| 134 | 141 | "lang/resolver/tests/fixtures/generic_body/copy.rad", |
|
| 135 | 142 | "lang/resolver/tests/fixtures/generic_body/identity.rad", |
|
| 136 | 143 | "lang/resolver/tests/fixtures/generic_body/region_binder.rad", |