lang: Emit specialized generic functions
2ee3620aac24f1f0db6d5b34b06b7c736ebb8712ff600b0667e41246d70added
Lower concrete function requests through a package work list. Reuse the registered signature and address on repeat and recursive requests. Report the application chain when expansion exceeds a compiler limit. Assisted-by: Codex:gpt-6 Change-Id: I11b39cf1ac9003fe41dc910b3201f68d73d59761
1 parent
f47f7a2c
compiler/radiance/radiance.rad
+29 -3
| 1056 | 1056 | throw error(&["no root module found"]); |
|
| 1057 | 1057 | }; |
|
| 1058 | 1058 | // Set lowerer's package context for qualified name generation. |
|
| 1059 | 1059 | // TODO: We shouldn't have to call this manually. |
|
| 1060 | 1060 | try lower::setPackage(low, modules, pkg.id, pkg.name) catch err { |
|
| 1061 | - | throw lowerError(err); |
|
| 1061 | + | throw lowerError(err, modules); |
|
| 1062 | 1062 | }; |
|
| 1063 | 1063 | try lowerModuleTreeInto(ctx, low, modules, rootId, isEntry, pkg, sink); |
|
| 1064 | 1064 | } |
|
| 1065 | 1065 | ||
| 1066 | 1066 | /// Recursively lower a module and all its children. Each function goes to `sink`. |
| 1080 | 1080 | throw error(&["module has no AST"]); |
|
| 1081 | 1081 | }; |
|
| 1082 | 1082 | pkgLog(pkg, &["lowering", "(", entry.filePath, ")", ".."]); |
|
| 1083 | 1083 | ||
| 1084 | 1084 | try lower::lowerModule(low, modId, modAst, isRoot, sink) catch err { |
|
| 1085 | - | throw lowerError(err); |
|
| 1085 | + | throw lowerError(err, modules); |
|
| 1086 | 1086 | }; |
|
| 1087 | 1087 | // Recurse into children. |
|
| 1088 | 1088 | for i in 0 .. entry.childrenLen { |
|
| 1089 | 1089 | let childId = module::childAt(entry, i); |
|
| 1090 | 1090 | try lowerModuleTreeInto(ctx, low, modules, childId, false, pkg, sink); |
| 1092 | 1092 | } |
|
| 1093 | 1093 | ||
| 1094 | 1094 | /// Report a lowering failure and return the driver error. A sink output failure has |
|
| 1095 | 1095 | /// already been reported. Arena exhaustion is not a compiler bug, so it is not an |
|
| 1096 | 1096 | /// internal error. |
|
| 1097 | - | fn lowerError(err: lower::LowerError) -> Error { |
|
| 1097 | + | fn lowerError(err: lower::LowerError, modules: module::ModuleTable) -> Error { |
|
| 1098 | + | if let case lower::LowerError::SpecializationLimit(trace) = err { |
|
| 1099 | + | for site, i in trace { |
|
| 1100 | + | if let entry = module::get(modules, site.moduleId) { |
|
| 1101 | + | io::print(entry.filePath); |
|
| 1102 | + | if let source = entry.source { |
|
| 1103 | + | if let loc = scanner::getLocation( |
|
| 1104 | + | scanner::SourceLoc::File(entry.filePath), source, site.offset |
|
| 1105 | + | ) { |
|
| 1106 | + | io::print(":"); |
|
| 1107 | + | io::printU32(loc.line as u32); |
|
| 1108 | + | io::print(":"); |
|
| 1109 | + | io::printU32(loc.col as u32); |
|
| 1110 | + | } |
|
| 1111 | + | } |
|
| 1112 | + | io::print(": "); |
|
| 1113 | + | } |
|
| 1114 | + | if i == 0 { |
|
| 1115 | + | io::print("error: "); |
|
| 1116 | + | io::print(lower::errorMessage(err)); |
|
| 1117 | + | io::print("\n"); |
|
| 1118 | + | } else { |
|
| 1119 | + | io::print("note: specialization requested here\n"); |
|
| 1120 | + | } |
|
| 1121 | + | } |
|
| 1122 | + | return Error::Other; |
|
| 1123 | + | } |
|
| 1098 | 1124 | if err == lower::LowerError::OutputFailed { |
|
| 1099 | 1125 | return Error::Other; |
|
| 1100 | 1126 | } |
|
| 1101 | 1127 | if err == lower::LowerError::AllocationFailed { |
|
| 1102 | 1128 | return error(&["lowering ran out of memory"]); |
lib/std/lang/lower.rad
+238 -35
| 174 | 174 | UnknownIntrinsic, |
|
| 175 | 175 | /// Allocation failure. |
|
| 176 | 176 | AllocationFailed, |
|
| 177 | 177 | /// Two different error types have the same tag. |
|
| 178 | 178 | ErrorTagCollision, |
|
| 179 | + | /// Generic requests exceed the package count or type nesting limit. |
|
| 180 | + | SpecializationLimit(*[il::SrcLoc]), |
|
| 179 | 181 | } |
|
| 180 | 182 | ||
| 181 | 183 | /// Describe a lowering error in one sentence. |
|
| 182 | 184 | /// |
|
| 183 | 185 | /// Each error has its own text, so a failure report names the failed check. |
|
| 184 | 186 | export fn errorMessage(err: LowerError) -> *[u8] { |
|
| 185 | 187 | match err { |
|
| 188 | + | case LowerError::SpecializationLimit(_) => |
|
| 189 | + | return "generic specialization exceeds compiler limits", |
|
| 186 | 190 | case LowerError::OutputFailed => return "the function output failed", |
|
| 187 | 191 | case LowerError::MissingSymbol(_) => return "a node has no symbol", |
|
| 188 | 192 | case LowerError::MissingType(_) => return "a node has no type", |
|
| 189 | 193 | case LowerError::MissingConst(_) => return "a node has no constant value", |
|
| 190 | 194 | case LowerError::ExpectedOptional => return "an optional type is expected", |
| 367 | 371 | kind: binary::ImportKind, |
|
| 368 | 372 | } |
|
| 369 | 373 | ||
| 370 | 374 | /// The layout, signature and trait tables of one lowering pass. |
|
| 371 | 375 | /// All indices are relative to these tables. Package-local passes lower one package. |
|
| 376 | + | /// A concrete function request, available by name before its body is lowered. |
|
| 377 | + | record FunctionSpecialization: Copy { |
|
| 378 | + | /// Symbol of the checked template. |
|
| 379 | + | template: resolver::SymbolId, |
|
| 380 | + | /// Concrete argument types. |
|
| 381 | + | substitution: resolver::TypeSubstitution, |
|
| 382 | + | /// Specialized signature. |
|
| 383 | + | signature: *resolver::FnType, |
|
| 384 | + | /// Location that first requested the specialization. |
|
| 385 | + | site: il::SrcLoc, |
|
| 386 | + | /// Specialization that was active at that location, if any. |
|
| 387 | + | parent: ?u32, |
|
| 388 | + | /// Name within this package's RIL. |
|
| 389 | + | name: *[u8], |
|
| 390 | + | } |
|
| 391 | + | ||
| 392 | + | /// Concrete function requests for the current package. |
|
| 393 | + | record Specializations { |
|
| 394 | + | /// Requests in discovery order. |
|
| 395 | + | queue: *mut [FunctionSpecialization], |
|
| 396 | + | /// Number of requests emitted. |
|
| 397 | + | emitted: u32, |
|
| 398 | + | /// Index of the checked body currently being lowered. |
|
| 399 | + | active: ?u32, |
|
| 400 | + | } |
|
| 401 | + | ||
| 402 | + | /// Layout and signature tables. |
|
| 372 | 403 | export record Tables { |
|
| 404 | + | /// Specialized functions of the current package. |
|
| 405 | + | specializations: Specializations, |
|
| 373 | 406 | /// Layouts of the pass. Entries are added and never removed. |
|
| 374 | 407 | layouts: *mut [il::Layout], |
|
| 375 | 408 | /// Signatures of the pass. |
|
| 376 | 409 | signatures: *mut [il::Signature], |
|
| 377 | 410 | /// Traits of the pass. |
| 1156 | 1189 | fnArena, |
|
| 1157 | 1190 | LowerOptions { debug: false, buildTest: false, tableScope: TableScope::Program } |
|
| 1158 | 1191 | ); |
|
| 1159 | 1192 | let mut fns = FnCollector { fns: &mut [] }; |
|
| 1160 | 1193 | try lowerDecls(&mut low, root, true, &mut fns); |
|
| 1194 | + | try emitSpecializations(&mut low, &mut fns); |
|
| 1161 | 1195 | ||
| 1162 | 1196 | return finalize(low, fns); |
|
| 1163 | 1197 | } |
|
| 1164 | 1198 | ||
| 1165 | 1199 | ///////////////////////////////// |
| 1189 | 1223 | packageDataStart: 0, |
|
| 1190 | 1224 | fnSyms: &mut [], |
|
| 1191 | 1225 | errTags: &mut [], |
|
| 1192 | 1226 | options, |
|
| 1193 | 1227 | tables: Tables { |
|
| 1228 | + | specializations: Specializations { queue: &mut [], emitted: 0, active: nil }, |
|
| 1194 | 1229 | layouts: &mut [], |
|
| 1195 | 1230 | signatures: &mut [], |
|
| 1196 | 1231 | traits: &mut [], |
|
| 1197 | 1232 | traitIndices: &mut [], |
|
| 1198 | 1233 | sharedLayouts: &mut [], |
| 1232 | 1267 | isRoot: bool, |
|
| 1233 | 1268 | sink: &mut opaque FnSink |
|
| 1234 | 1269 | ) throws (LowerError) { |
|
| 1235 | 1270 | set low.currentMod = moduleId; |
|
| 1236 | 1271 | try lowerDecls(low, root, isRoot, sink); |
|
| 1272 | + | try emitSpecializations(low, sink); |
|
| 1237 | 1273 | } |
|
| 1238 | 1274 | ||
| 1239 | 1275 | /// Lower all top-level declarations in a module. |
|
| 1240 | 1276 | fn lowerDecls( |
|
| 1241 | 1277 | low: &mut Lowerer, |
| 1412 | 1448 | set self.moduleTable = modules; |
|
| 1413 | 1449 | set self.package = package; |
|
| 1414 | 1450 | set self.pkgName = pkgName; |
|
| 1415 | 1451 | set self.currentMod = nil; |
|
| 1416 | 1452 | set self.packageDataStart = self.data.len; |
|
| 1453 | + | set self.tables.specializations = Specializations { queue: &mut [], emitted: 0, active: nil }; |
|
| 1417 | 1454 | if self.options.tableScope == TableScope::Package { |
|
| 1418 | 1455 | let mut entries: *mut [dict::Entry] = &mut []; |
|
| 1419 | 1456 | try entries.resize( |
|
| 1420 | 1457 | binary::collect::INDEX_SLOTS, dict::Entry { key: &[], value: 0 }, self.resolver.arena |
|
| 1421 | 1458 | ) catch { |
| 1543 | 1580 | throw LowerError::ExpectedIdentifier; |
|
| 1544 | 1581 | }; |
|
| 1545 | 1582 | let case resolver::Type::Fn(fnType) = resolver::nodeData(&self.resolver, node).ty else { |
|
| 1546 | 1583 | throw LowerError::ExpectedFunction; |
|
| 1547 | 1584 | }; |
|
| 1548 | - | let isExtern = checkAttr(decl.attrs, ast::Attribute::Extern); |
|
| 1549 | - | ||
| 1550 | 1585 | // Build qualified function name for multi-module compilation. |
|
| 1551 | 1586 | let qualName = try qualifyName(self, nil, name); |
|
| 1552 | 1587 | ||
| 1553 | 1588 | // Register function symbol for cross-package call resolution. |
|
| 1554 | 1589 | if let sym = resolver::symbolFor(&self.resolver, node) { |
|
| 1555 | 1590 | try registerFnSym(self, sym, qualName); |
|
| 1556 | 1591 | } |
|
| 1592 | + | return try lowerFunction(self, node, decl, fnType, qualName); |
|
| 1593 | + | } |
|
| 1594 | + | ||
| 1595 | + | /// Lower a checked function body with its concrete signature and name. |
|
| 1596 | + | fn lowerFunction( |
|
| 1597 | + | self: &mut Lowerer, |
|
| 1598 | + | node: *ast::Node, |
|
| 1599 | + | decl: ast::FnDecl, |
|
| 1600 | + | fnType: *resolver::FnType, |
|
| 1601 | + | qualName: *[u8] |
|
| 1602 | + | ) -> *il::Fn throws (LowerError) { |
|
| 1603 | + | let isExtern = checkAttr(decl.attrs, ast::Attribute::Extern); |
|
| 1557 | 1604 | try beginFn(self, node, fnType, qualName); |
|
| 1558 | 1605 | ||
| 1559 | 1606 | // If the function returns an aggregate or is throwing, prepend a hidden |
|
| 1560 | 1607 | // return parameter. The caller allocates the buffer and passes it |
|
| 1561 | 1608 | // as the first argument; the callee writes the return value into it. |
| 1589 | 1636 | }); |
|
| 1590 | 1637 | }; |
|
| 1591 | 1638 | return try finishFn(self, qualName, lowParams, returnType, isExtern, body); |
|
| 1592 | 1639 | } |
|
| 1593 | 1640 | ||
| 1641 | + | /// Report the application and the requests that led to it. |
|
| 1642 | + | fn specializationLimit(self: &mut Lowerer, site: il::SrcLoc) -> LowerError { |
|
| 1643 | + | let mut trace: *mut [il::SrcLoc] = &mut []; |
|
| 1644 | + | try trace.append(site, self.resolver.arena) catch { |
|
| 1645 | + | return LowerError::AllocationFailed; |
|
| 1646 | + | }; |
|
| 1647 | + | let mut parent = self.tables.specializations.active; |
|
| 1648 | + | while let index = parent { |
|
| 1649 | + | let request = self.tables.specializations.queue[index]; |
|
| 1650 | + | try trace.append(request.site, self.resolver.arena) catch { |
|
| 1651 | + | return LowerError::AllocationFailed; |
|
| 1652 | + | }; |
|
| 1653 | + | set parent = request.parent; |
|
| 1654 | + | } |
|
| 1655 | + | return LowerError::SpecializationLimit(&trace[..]); |
|
| 1656 | + | } |
|
| 1657 | + | ||
| 1658 | + | /// Register a concrete function request and return its RIL address. |
|
| 1659 | + | fn specializedFunction(self: &mut Lowerer, node: *ast::Node, index: u32) -> il::Val |
|
| 1660 | + | throws (LowerError) |
|
| 1661 | + | { |
|
| 1662 | + | let app = self.resolver.typeTables.functionApplications[index]; |
|
| 1663 | + | let mut args = app.substitution.args; |
|
| 1664 | + | if let parent = self.tables.specializations.active { |
|
| 1665 | + | let sub = self.tables.specializations.queue[parent].substitution; |
|
| 1666 | + | let mut concrete: *mut [resolver::Type] = &mut []; |
|
| 1667 | + | for ty in args { |
|
| 1668 | + | let actual = resolver::substituteType(&mut self.resolver, ty, sub); |
|
| 1669 | + | try concrete.append(actual, self.resolver.arena) catch { |
|
| 1670 | + | throw LowerError::AllocationFailed; |
|
| 1671 | + | }; |
|
| 1672 | + | } |
|
| 1673 | + | set args = &concrete[..]; |
|
| 1674 | + | } |
|
| 1675 | + | for request in &self.tables.specializations.queue[..] { |
|
| 1676 | + | if request.template <> app.template { |
|
| 1677 | + | continue; |
|
| 1678 | + | } |
|
| 1679 | + | let mut equal = true; |
|
| 1680 | + | for ty, i in args { |
|
| 1681 | + | if not resolver::typesEqual(&self.resolver, ty, request.substitution.args[i]) { |
|
| 1682 | + | set equal = false; |
|
| 1683 | + | break; |
|
| 1684 | + | } |
|
| 1685 | + | } |
|
| 1686 | + | if equal { |
|
| 1687 | + | return il::Val::FnAddr(request.name); |
|
| 1688 | + | } |
|
| 1689 | + | } |
|
| 1690 | + | let moduleId = self.currentMod else 0; |
|
| 1691 | + | let site = il::SrcLoc { |
|
| 1692 | + | moduleId, |
|
| 1693 | + | offset: node.span.offset, |
|
| 1694 | + | }; |
|
| 1695 | + | let ordinal = self.tables.specializations.queue.len; |
|
| 1696 | + | if ordinal >= resolver::MAX_FUNCTION_APPLICATIONS { |
|
| 1697 | + | throw specializationLimit(self, site); |
|
| 1698 | + | } |
|
| 1699 | + | for arg in args { |
|
| 1700 | + | if not resolver::fitsTypeDepth(arg, resolver::MAX_APPLICATION_TYPE_DEPTH) { |
|
| 1701 | + | throw specializationLimit(self, site); |
|
| 1702 | + | } |
|
| 1703 | + | } |
|
| 1704 | + | let symbol = self.resolver.symbols[*app.template]; |
|
| 1705 | + | if let provider = try foreignPackage(self, symbol.moduleId) { |
|
| 1706 | + | throw LowerError::Unsupported; |
|
| 1707 | + | } |
|
| 1708 | + | let base = try qualifyName(self, symbol.moduleId, symbol.name); |
|
| 1709 | + | let name = try numberedName(self, base, ordinal, "generic"); |
|
| 1710 | + | let case resolver::Type::Fn(signature) = specializeType(self, resolver::Type::Fn(app.signature)) |
|
| 1711 | + | else panic "specializedFunction: expected a function type"; |
|
| 1712 | + | let request = FunctionSpecialization { |
|
| 1713 | + | template: app.template, |
|
| 1714 | + | substitution: resolver::TypeSubstitution { params: app.substitution.params, args }, |
|
| 1715 | + | signature, |
|
| 1716 | + | site, |
|
| 1717 | + | parent: self.tables.specializations.active, |
|
| 1718 | + | name, |
|
| 1719 | + | }; |
|
| 1720 | + | try self.tables.specializations.queue.append(request, self.resolver.arena) catch { |
|
| 1721 | + | throw LowerError::AllocationFailed; |
|
| 1722 | + | }; |
|
| 1723 | + | return il::Val::FnAddr(name); |
|
| 1724 | + | } |
|
| 1725 | + | ||
| 1726 | + | /// Emit requested specializations. Lowering a body can add new requests. |
|
| 1727 | + | fn emitSpecializations(self: &mut Lowerer, sink: &mut opaque FnSink) throws (LowerError) { |
|
| 1728 | + | let module = self.currentMod; |
|
| 1729 | + | while self.tables.specializations.emitted < self.tables.specializations.queue.len { |
|
| 1730 | + | let i = self.tables.specializations.emitted; |
|
| 1731 | + | let request = self.tables.specializations.queue[i]; |
|
| 1732 | + | let symbol = self.resolver.symbols[*request.template]; |
|
| 1733 | + | let case ast::NodeValue::FnDecl(decl) = symbol.node.value else { |
|
| 1734 | + | panic "emitSpecializations: expected a template"; |
|
| 1735 | + | }; |
|
| 1736 | + | if decl.body == nil { |
|
| 1737 | + | throw LowerError::MissingMetadata; |
|
| 1738 | + | } |
|
| 1739 | + | set self.currentMod = symbol.moduleId; |
|
| 1740 | + | set self.tables.specializations.active = i; |
|
| 1741 | + | let f = try lowerFunction( |
|
| 1742 | + | self, symbol.node, decl, request.signature, request.name |
|
| 1743 | + | ) catch e { |
|
| 1744 | + | set self.tables.specializations.active = nil; |
|
| 1745 | + | set self.currentMod = module; |
|
| 1746 | + | throw e; |
|
| 1747 | + | }; |
|
| 1748 | + | set self.tables.specializations.active = nil; |
|
| 1749 | + | set self.currentMod = module; |
|
| 1750 | + | try emitFunction(self, f, FnRole::Normal, Linkage::Local, sink); |
|
| 1751 | + | set self.tables.specializations.emitted += 1; |
|
| 1752 | + | } |
|
| 1753 | + | } |
|
| 1754 | + | ||
| 1594 | 1755 | /// Store a complete function in the function arena and return its address. |
|
| 1595 | 1756 | /// The arena keeps the function until its owner resets the arena. |
|
| 1596 | 1757 | fn allocFn(self: &mut Lowerer, func: il::Fn) -> *il::Fn throws (LowerError) { |
|
| 1597 | 1758 | let mut funcs: *mut [il::Fn] = &mut []; |
|
| 1598 | 1759 | try funcs.append(func, self.fnArena) catch { |
| 1732 | 1893 | size: typeLayout.size, |
|
| 1733 | 1894 | alignment: typeLayout.alignment |
|
| 1734 | 1895 | if typeLayout.alignment > resolver::PTR_SIZE |
|
| 1735 | 1896 | else resolver::PTR_SIZE, |
|
| 1736 | 1897 | }; |
|
| 1737 | - | let name = try nextDeclDataName(self, entryName, i - first, STORAGE_NAMESPACE); |
|
| 1898 | + | let name = try numberedName(self, entryName, i - first, STORAGE_NAMESPACE); |
|
| 1738 | 1899 | let mut values: *mut [il::DataValue] = &mut []; |
|
| 1739 | 1900 | if layout.size > 0 { |
|
| 1740 | 1901 | try values.append( |
|
| 1741 | 1902 | il::DataValue { |
|
| 1742 | 1903 | item: il::DataItem::Val { typ: il::Type::W8, val: 0 }, |
| 2831 | 2992 | assert buf.len == totalLen; |
|
| 2832 | 2993 | ||
| 2833 | 2994 | return &buf[..]; |
|
| 2834 | 2995 | } |
|
| 2835 | 2996 | ||
| 2836 | - | /// Generate a unique name for declaration-local backing data entries. |
|
| 2837 | - | fn nextDeclDataName( |
|
| 2997 | + | /// Qualify a numbered item within a symbol and namespace. |
|
| 2998 | + | fn numberedName( |
|
| 2838 | 2999 | self: &mut Lowerer, |
|
| 2839 | 3000 | prefix: *[u8], |
|
| 2840 | 3001 | count: u32, |
|
| 2841 | 3002 | namespace: *[u8] |
|
| 2842 | 3003 | ) -> *[u8] throws (LowerError) { |
| 2859 | 3020 | values: *[il::DataValue], |
|
| 2860 | 3021 | dataPrefix: *[u8] |
|
| 2861 | 3022 | ) -> *[u8] throws (LowerError) { |
|
| 2862 | 3023 | let counter = self.dataCounter; |
|
| 2863 | 3024 | set self.dataCounter += 1; |
|
| 2864 | - | let name = try nextDeclDataName(self, dataPrefix, counter, "literal"); |
|
| 3025 | + | let name = try numberedName(self, dataPrefix, counter, "literal"); |
|
| 2865 | 3026 | let info = self.tables.layouts[layout]; |
|
| 2866 | 3027 | try addData( |
|
| 2867 | 3028 | self, |
|
| 2868 | 3029 | il::Data { |
|
| 2869 | 3030 | name, |
| 3035 | 3196 | /// Generate a unique data name for inline literals, eg. `fnName$literal$N`. |
|
| 3036 | 3197 | fn nextDataName(self: &mut Lowerer) -> *[u8] throws (LowerError) { |
|
| 3037 | 3198 | let counter = self.dataCounter; |
|
| 3038 | 3199 | set self.dataCounter += 1; |
|
| 3039 | 3200 | let fnName = self.fnName; |
|
| 3040 | - | return try nextDeclDataName(self, fnName, counter, "literal"); |
|
| 3201 | + | return try numberedName(self, fnName, counter, "literal"); |
|
| 3041 | 3202 | } |
|
| 3042 | 3203 | ||
| 3043 | 3204 | /// Assign a unique function-local data symbol name. |
|
| 3044 | 3205 | fn registerLocalDataDeclName(self: &mut Lowerer, node: *ast::Node) throws (LowerError) { |
|
| 3045 | 3206 | let sym = resolver::symbolFor(&self.resolver, node) else throw LowerError::MissingSymbol(node); |
|
| 3046 | 3207 | ||
| 3047 | - | let prefix = self.fnName; |
|
| 3048 | - | let segments = [prefix, "nominal", self.resolver.symbols[*sym].name]; |
|
| 3208 | + | let mut ident: *ast::Node = undefined; |
|
| 3209 | + | match node.value { |
|
| 3210 | + | case ast::NodeValue::ConstDecl(decl) => set ident = decl.ident, |
|
| 3211 | + | case ast::NodeValue::StaticDecl(decl) => set ident = decl.ident, |
|
| 3212 | + | else => throw LowerError::UnexpectedNodeValue(node), |
|
| 3213 | + | } |
|
| 3214 | + | let case ast::NodeValue::Ident(lexicalName) = ident.value |
|
| 3215 | + | else throw LowerError::ExpectedIdentifier; |
|
| 3216 | + | let segments = [self.fnName, "nominal", lexicalName]; |
|
| 3049 | 3217 | let name = try buildSegmentedName(self, &segments[..]); |
|
| 3050 | 3218 | ||
| 3051 | 3219 | set self.resolver.symbols[*sym].name = name; |
|
| 3052 | 3220 | } |
|
| 3053 | 3221 |
| 3056 | 3224 | let reg = il::Reg { n: self.regCounter }; |
|
| 3057 | 3225 | set self.regCounter += 1; |
|
| 3058 | 3226 | return reg; |
|
| 3059 | 3227 | } |
|
| 3060 | 3228 | ||
| 3229 | + | /// Apply the substitutions of the function currently being lowered. |
|
| 3230 | + | fn specializeType(self: &mut Lowerer, ty: resolver::Type) -> resolver::Type { |
|
| 3231 | + | let active = self.tables.specializations.active else return ty; |
|
| 3232 | + | let sub = self.tables.specializations.queue[active].substitution; |
|
| 3233 | + | return resolver::substituteType(&mut self.resolver, ty, sub); |
|
| 3234 | + | } |
|
| 3235 | + | ||
| 3236 | + | /// Apply the current substitutions to a type referenced by metadata. |
|
| 3237 | + | fn specializeTypePtr(self: &mut Lowerer, ty: *resolver::Type) -> *resolver::Type { |
|
| 3238 | + | let active = self.tables.specializations.active else return ty; |
|
| 3239 | + | let sub = self.tables.specializations.queue[active].substitution; |
|
| 3240 | + | let concrete = resolver::substituteType(&mut self.resolver, *ty, sub); |
|
| 3241 | + | return resolver::allocType(&mut self.resolver, concrete); |
|
| 3242 | + | } |
|
| 3243 | + | ||
| 3061 | 3244 | /// Look up the resolved type of an AST node, or throw `MissingType`. |
|
| 3062 | 3245 | fn typeOf(self: &mut Lowerer, node: *ast::Node) -> resolver::Type throws (LowerError) { |
|
| 3063 | 3246 | let mut ty: resolver::Type = undefined; |
|
| 3064 | 3247 | { |
|
| 3065 | 3248 | let data = resolver::nodeData(&self.resolver, node); |
| 3071 | 3254 | case resolver::Coercion::Borrow { from, .. } => set ty = from, |
|
| 3072 | 3255 | case resolver::Coercion::OptionalMap { from, .. } => set ty = *from, |
|
| 3073 | 3256 | else => {} |
|
| 3074 | 3257 | } |
|
| 3075 | 3258 | } |
|
| 3076 | - | return ty; |
|
| 3259 | + | if self.tables.specializations.active == nil { |
|
| 3260 | + | return ty; |
|
| 3261 | + | } |
|
| 3262 | + | return specializeType(self, ty); |
|
| 3077 | 3263 | } |
|
| 3078 | 3264 | ||
| 3079 | 3265 | /// Look up the symbol for an AST node, or throw `MissingSymbol`. |
|
| 3080 | 3266 | fn symOf(self: &mut Lowerer, node: *ast::Node) -> resolver::SymbolId throws (LowerError) { |
|
| 3081 | 3267 | let sym = resolver::symbolFor(&self.resolver, node) else throw LowerError::MissingSymbol(node); |
| 6187 | 6373 | case resolver::Coercion::TraitObject { traitInfo, class, mutable, .. } => |
|
| 6188 | 6374 | set ty = resolver::Type::TraitObject { class, traitInfo, mutable }, |
|
| 6189 | 6375 | else => {} |
|
| 6190 | 6376 | } |
|
| 6191 | 6377 | } |
|
| 6192 | - | return ty; |
|
| 6378 | + | if self.tables.specializations.active == nil { |
|
| 6379 | + | return ty; |
|
| 6380 | + | } |
|
| 6381 | + | return specializeType(self, ty); |
|
| 6193 | 6382 | } |
|
| 6194 | 6383 | ||
| 6195 | 6384 | /// Check if a resolver type lowers to an aggregate in memory. |
|
| 6196 | 6385 | export fn isAggregateType(res: &resolver::Resolver, typ: resolver::Type) -> bool { |
|
| 6197 | 6386 | match typ { |
| 7706 | 7895 | self: &mut Lowerer, |
|
| 7707 | 7896 | container: *ast::Node, |
|
| 7708 | 7897 | range: ast::Range, |
|
| 7709 | 7898 | sliceNode: *ast::Node |
|
| 7710 | 7899 | ) -> il::Val throws (LowerError) { |
|
| 7711 | - | let info = resolver::sliceRangeInfoFor(&self.resolver, sliceNode) else { |
|
| 7900 | + | let mut info = resolver::sliceRangeInfoFor(&self.resolver, sliceNode) else { |
|
| 7712 | 7901 | throw LowerError::MissingMetadata; |
|
| 7713 | 7902 | }; |
|
| 7903 | + | set info.itemType = specializeTypePtr(self, info.itemType); |
|
| 7714 | 7904 | let sliceTy = try typeOf(self, sliceNode); |
|
| 7715 | 7905 | if not resolver::isSlotType(sliceTy) { |
|
| 7716 | 7906 | let r = try resolveSliceRangePtr(self, container, range, info); |
|
| 7717 | 7907 | return try buildRawSliceValue( |
|
| 7718 | 7908 | self, info.itemType, info.mutable, il::Val::Reg(r.dataReg), r.count, r.capacity |
| 7972 | 8162 | throw LowerError::ExpectedIdentifier; |
|
| 7973 | 8163 | }; |
|
| 7974 | 8164 | let sym = resolver::symbolFor(&self.resolver, node) else { |
|
| 7975 | 8165 | throw LowerError::MissingSymbol(node); |
|
| 7976 | 8166 | }; |
|
| 7977 | - | let case resolver::SymbolData::Value { type: typ, .. } = self.resolver.symbols[*sym].data else { |
|
| 8167 | + | let case resolver::SymbolData::Value { type: bindingType, .. } = |
|
| 8168 | + | self.resolver.symbols[*sym].data else { |
|
| 7978 | 8169 | throw LowerError::MissingMetadata; |
|
| 7979 | 8170 | }; |
|
| 8171 | + | let typ = specializeType(self, bindingType); |
|
| 7980 | 8172 | let mut varVal = val; |
|
| 7981 | 8173 | let mut memory = VarMemory::Shared; |
|
| 7982 | 8174 | if let case ast::NodeValue::Call(_) = l.value.value { |
|
| 7983 | 8175 | set memory = VarMemory::Stable; |
|
| 7984 | 8176 | } |
| 8191 | 8383 | if let info = resolver::sliceRangeInfoFor(&self.resolver, node) { |
|
| 8192 | 8384 | let case ast::NodeValue::Bracket { base: container, items } = a.left.value |
|
| 8193 | 8385 | else panic "lowerAssign: slice assign without subscript"; |
|
| 8194 | 8386 | let case ast::NodeValue::Range(range) = items[0].value |
|
| 8195 | 8387 | else panic "lowerAssign: slice assign without range"; |
|
| 8196 | - | try lowerSliceAssign(self, a.right, container, range, info); |
|
| 8388 | + | let mut rangeInfo = info; |
|
| 8389 | + | set rangeInfo.itemType = specializeTypePtr(self, info.itemType); |
|
| 8390 | + | try lowerSliceAssign(self, a.right, container, range, rangeInfo); |
|
| 8197 | 8391 | ||
| 8198 | 8392 | return; |
|
| 8199 | 8393 | } |
|
| 8200 | 8394 | // Compound assignments are represented as `target = target op rhs`, with |
|
| 8201 | 8395 | // the exact same target node used in both places. Memory-backed targets |
| 8672 | 8866 | unsigned: isUnsignedType(&self.resolver, *valType), |
|
| 8673 | 8867 | }; |
|
| 8674 | 8868 | ||
| 8675 | 8869 | try lowerForLoop(self, &iter, f.body); |
|
| 8676 | 8870 | } |
|
| 8677 | - | case resolver::ForLoopInfo::Collection { elemType, length, bindingName, indexName } => { |
|
| 8871 | + | case resolver::ForLoopInfo::Collection { |
|
| 8872 | + | elemType: symbolic, length, bindingName, indexName |
|
| 8873 | + | } => { |
|
| 8874 | + | let elemType = specializeTypePtr(self, symbolic); |
|
| 8678 | 8875 | let containerVal = try lowerExpr(self, f.iterable); |
|
| 8679 | 8876 | let containerReg = try emitValToReg(self, containerVal); |
|
| 8680 | 8877 | ||
| 8681 | 8878 | let mut lengthVal: il::Val = undefined; |
|
| 8682 | 8879 | let mut rawData: ?il::Reg = nil; |
| 9358 | 9555 | // A discarded result uses `Void`. |
|
| 9359 | 9556 | // Check for trait method dispatch or standalone method call. |
|
| 9360 | 9557 | let mut resVal: il::Val = undefined; |
|
| 9361 | 9558 | let callNodeExtra = resolver::extraFor(&self.resolver, t.expr); |
|
| 9362 | 9559 | if let case resolver::NodeExtra::SliceAppend { elemType } = callNodeExtra { |
|
| 9363 | - | set resVal = try lowerSliceAppend(self, &callExpr, elemType); |
|
| 9560 | + | set resVal = try lowerSliceAppend(self, &callExpr, specializeTypePtr(self, elemType)); |
|
| 9364 | 9561 | } else if let case resolver::NodeExtra::SliceReserve { elemType } = callNodeExtra { |
|
| 9365 | - | set resVal = try lowerSliceReserve(self, &callExpr, elemType); |
|
| 9562 | + | set resVal = try lowerSliceReserve(self, &callExpr, specializeTypePtr(self, elemType)); |
|
| 9366 | 9563 | } else if let case resolver::NodeExtra::SliceResize { elemType } = callNodeExtra { |
|
| 9367 | - | set resVal = try lowerSliceResize(self, &callExpr, elemType); |
|
| 9564 | + | set resVal = try lowerSliceResize(self, &callExpr, specializeTypePtr(self, elemType)); |
|
| 9368 | 9565 | } else if let case resolver::NodeExtra::SliceExtend { elemType } = callNodeExtra { |
|
| 9369 | - | set resVal = try lowerSliceExtend(self, &callExpr, elemType); |
|
| 9566 | + | set resVal = try lowerSliceExtend(self, &callExpr, specializeTypePtr(self, elemType)); |
|
| 9370 | 9567 | } else if let case resolver::NodeExtra::AllocNew { elemType } = callNodeExtra { |
|
| 9371 | - | set resVal = try lowerAllocNew(self, &callExpr, elemType); |
|
| 9568 | + | set resVal = try lowerAllocNew(self, &callExpr, specializeTypePtr(self, elemType)); |
|
| 9372 | 9569 | } else if let case resolver::NodeExtra::TraitMethodCall { |
|
| 9373 | 9570 | traitInfo, methodIndex |
|
| 9374 | 9571 | } = |
|
| 9375 | 9572 | callNodeExtra |
|
| 9376 | 9573 | { |
| 10457 | 10654 | ) -> il::Val throws (LowerError) { |
|
| 10458 | 10655 | let nodeData = resolver::extraFor(&self.resolver, node); |
|
| 10459 | 10656 | ||
| 10460 | 10657 | // Check for slice method dispatch. |
|
| 10461 | 10658 | if let case resolver::NodeExtra::SliceAppend { elemType } = nodeData { |
|
| 10462 | - | return try lowerSliceAppend(self, call, elemType); |
|
| 10659 | + | return try lowerSliceAppend(self, call, specializeTypePtr(self, elemType)); |
|
| 10463 | 10660 | } |
|
| 10464 | 10661 | if let case resolver::NodeExtra::SliceReserve { elemType } = nodeData { |
|
| 10465 | - | return try lowerSliceReserve(self, call, elemType); |
|
| 10662 | + | return try lowerSliceReserve(self, call, specializeTypePtr(self, elemType)); |
|
| 10466 | 10663 | } |
|
| 10467 | 10664 | if let case resolver::NodeExtra::SliceResize { elemType } = nodeData { |
|
| 10468 | - | return try lowerSliceResize(self, call, elemType); |
|
| 10665 | + | return try lowerSliceResize(self, call, specializeTypePtr(self, elemType)); |
|
| 10469 | 10666 | } |
|
| 10470 | 10667 | if let case resolver::NodeExtra::AllocNew { elemType } = nodeData { |
|
| 10471 | - | return try lowerAllocNew(self, call, elemType); |
|
| 10668 | + | return try lowerAllocNew(self, call, specializeTypePtr(self, elemType)); |
|
| 10472 | 10669 | } |
|
| 10473 | 10670 | if let case resolver::NodeExtra::ScratchReset = nodeData { |
|
| 10474 | 10671 | return try lowerScratchReset(self, call); |
|
| 10475 | 10672 | } |
|
| 10476 | 10673 | if let case resolver::NodeExtra::SliceExtend { elemType } = nodeData { |
|
| 10477 | - | return try lowerSliceExtend(self, call, elemType); |
|
| 10674 | + | return try lowerSliceExtend(self, call, specializeTypePtr(self, elemType)); |
|
| 10478 | 10675 | } |
|
| 10479 | 10676 | if let case resolver::NodeExtra::SliceDelete { elemType } = nodeData { |
|
| 10480 | - | try lowerSliceDelete(self, call, elemType); |
|
| 10677 | + | try lowerSliceDelete(self, call, specializeTypePtr(self, elemType)); |
|
| 10481 | 10678 | return il::Val::Undef; |
|
| 10482 | 10679 | } |
|
| 10483 | 10680 | if let case resolver::NodeExtra::SliceTake { elemType } = nodeData { |
|
| 10484 | - | return try lowerSliceTake(self, call, elemType); |
|
| 10681 | + | return try lowerSliceTake(self, call, specializeTypePtr(self, elemType)); |
|
| 10485 | 10682 | } |
|
| 10486 | 10683 | // Check for trait method dispatch. |
|
| 10487 | 10684 | if let case resolver::NodeExtra::TraitMethodCall { traitInfo, methodIndex } = nodeData { |
|
| 10488 | 10685 | return try lowerTraitMethodCall(self, node, call, traitInfo, methodIndex); |
|
| 10489 | 10686 | } |
| 11055 | 11252 | /// Apply a resolved conversion, including conversions inside optional lifting. |
|
| 11056 | 11253 | fn convert(self: &mut Lowerer, val: il::Val, coerce: resolver::Coercion) -> il::Val |
|
| 11057 | 11254 | throws (LowerError) |
|
| 11058 | 11255 | { |
|
| 11059 | 11256 | match coerce { |
|
| 11060 | - | case resolver::Coercion::OptionalLift { target, inner } => { |
|
| 11257 | + | case resolver::Coercion::OptionalLift { target: symbolic, inner } => { |
|
| 11258 | + | let target = specializeType(self, symbolic); |
|
| 11061 | 11259 | let conversion = inner else return try buildOptional(self, nil, target); |
|
| 11062 | 11260 | let value = try convert(self, val, *conversion); |
|
| 11063 | 11261 | return try buildOptional(self, value, target); |
|
| 11064 | 11262 | } |
|
| 11065 | 11263 | case resolver::Coercion::OptionalMap { from, to, inner } => { |
|
| 11066 | - | return try mapOptional(self, val, *from, to, *inner); |
|
| 11264 | + | return try mapOptional( |
|
| 11265 | + | self, val, specializeType(self, *from), specializeType(self, to), *inner |
|
| 11266 | + | ); |
|
| 11067 | 11267 | } |
|
| 11068 | - | case resolver::Coercion::Borrow { from, to } => { |
|
| 11268 | + | case resolver::Coercion::Borrow { from: source, to: target } => { |
|
| 11269 | + | let from = specializeType(self, source); |
|
| 11270 | + | let to = specializeType(self, target); |
|
| 11069 | 11271 | if let case resolver::Type::Slice { .. } = from { |
|
| 11070 | 11272 | return try borrowSlice(self, val, from, to); |
|
| 11071 | 11273 | } |
|
| 11072 | 11274 | return try narrowObject(self, val, from, to); |
|
| 11073 | 11275 | } |
| 11189 | 11391 | ||
| 11190 | 11392 | match self.resolver.symbols[*sym].data { |
|
| 11191 | 11393 | case resolver::SymbolData::Constant { type, .. } => { |
|
| 11192 | 11394 | let src = try emitDataAddr(self, sym); |
|
| 11193 | 11395 | ||
| 11194 | - | return try emitRead(self, src, 0, type); |
|
| 11396 | + | return try emitRead(self, src, 0, specializeType(self, type)); |
|
| 11195 | 11397 | } |
|
| 11196 | 11398 | case resolver::SymbolData::Value { type, .. } => |
|
| 11197 | - | return try lowerGlobalValue(self, sym, type), |
|
| 11399 | + | return try lowerGlobalValue(self, sym, specializeType(self, type)), |
|
| 11198 | 11400 | else => throw LowerError::UnexpectedNodeValue(node), |
|
| 11199 | 11401 | } |
|
| 11200 | 11402 | } |
|
| 11201 | 11403 | ||
| 11202 | 11404 | /// Lower an assignment to a static variable. |
| 11325 | 11527 | } |
|
| 11326 | 11528 | case ast::NodeValue::UnOp(unop) => { |
|
| 11327 | 11529 | set val = try lowerUnOp(self, node, unop); |
|
| 11328 | 11530 | } |
|
| 11329 | 11531 | case ast::NodeValue::Bracket { base: container, items } => { |
|
| 11330 | - | if let case resolver::NodeExtra::FunctionApplication(_) = |
|
| 11532 | + | if let case resolver::NodeExtra::FunctionApplication(index) = |
|
| 11331 | 11533 | resolver::nodeData(&self.resolver, node).extra |
|
| 11332 | 11534 | { |
|
| 11333 | - | throw LowerError::Unsupported; |
|
| 11535 | + | set val = try specializedFunction(self, node, index); |
|
| 11536 | + | } else { |
|
| 11537 | + | set val = try lowerSubscript(self, node, container, items[0]); |
|
| 11334 | 11538 | } |
|
| 11335 | - | set val = try lowerSubscript(self, node, container, items[0]); |
|
| 11336 | 11539 | } |
|
| 11337 | 11540 | case ast::NodeValue::BuiltinCall { kind, args } => { |
|
| 11338 | 11541 | set val = try lowerBuiltinCall(self, node, kind, args); |
|
| 11339 | 11542 | } |
|
| 11340 | 11543 | case ast::NodeValue::Call(call) => { |
lib/std/lang/resolver.rad
+37 -1
| 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 | 68 | /// Maximum number of checked function applications per package. |
|
| 69 | - | constant MAX_FUNCTION_APPLICATIONS: u32 = 4096; |
|
| 69 | + | export constant MAX_FUNCTION_APPLICATIONS: u32 = 4096; |
|
| 70 | + | ||
| 71 | + | /// Maximum structural depth of a type used as a generic argument. |
|
| 72 | + | export constant MAX_APPLICATION_TYPE_DEPTH: u32 = 64; |
|
| 70 | 73 | ||
| 71 | 74 | /// Maximum number of linear bindings active in one function. |
|
| 72 | 75 | constant MAX_LINEAR_BINDINGS: u32 = 32; |
|
| 73 | 76 | /// Maximum inline field depth used to prove borrow separation. |
|
| 74 | 77 | constant MAX_BORROW_FIELDS: u32 = 16; |
| 2756 | 2759 | } |
|
| 2757 | 2760 | else => return false, |
|
| 2758 | 2761 | } |
|
| 2759 | 2762 | } |
|
| 2760 | 2763 | ||
| 2764 | + | /// Check the structural nesting depth of a type argument. |
|
| 2765 | + | export fn fitsTypeDepth(ty: Type, depth: u32) -> bool { |
|
| 2766 | + | if depth == 0 { |
|
| 2767 | + | return false; |
|
| 2768 | + | } |
|
| 2769 | + | let remaining = depth - 1; |
|
| 2770 | + | match ty { |
|
| 2771 | + | case Type::Pointer { target, .. } => return fitsTypeDepth(*target, remaining), |
|
| 2772 | + | case Type::Slice { item, .. } => return fitsTypeDepth(*item, remaining), |
|
| 2773 | + | case Type::Array(array) => return fitsTypeDepth(*array.item, remaining), |
|
| 2774 | + | case Type::Optional(item) => return fitsTypeDepth(*item, remaining), |
|
| 2775 | + | case Type::Fn(signature) => { |
|
| 2776 | + | for param in signature.paramTypes { |
|
| 2777 | + | if not fitsTypeDepth(*param, remaining) { |
|
| 2778 | + | return false; |
|
| 2779 | + | } |
|
| 2780 | + | } |
|
| 2781 | + | for error in signature.throwList { |
|
| 2782 | + | if not fitsTypeDepth(*error, remaining) { |
|
| 2783 | + | return false; |
|
| 2784 | + | } |
|
| 2785 | + | } |
|
| 2786 | + | return fitsTypeDepth(*signature.returnType, remaining); |
|
| 2787 | + | } |
|
| 2788 | + | else => return true, |
|
| 2789 | + | } |
|
| 2790 | + | } |
|
| 2791 | + | ||
| 2761 | 2792 | /// Substitute abstract parameter identities in a resolved type. |
|
| 2762 | 2793 | export fn substituteType(self: &mut Resolver, ty: Type, sub: TypeSubstitution) -> Type { |
|
| 2763 | 2794 | match ty { |
|
| 2764 | 2795 | case Type::Nominal(id) => { |
|
| 2765 | 2796 | for parameter, i in sub.params { |
| 9086 | 9117 | let binder = decl.binders[i]; |
|
| 9087 | 9118 | let sym = symbolFor(self, binder) |
|
| 9088 | 9119 | else throw emitError(self, item, ErrorKind::Bracket(BracketError::Generic)); |
|
| 9089 | 9120 | let case SymbolData::Type(id) = self.symbols[*sym].data else panic; |
|
| 9090 | 9121 | let arg = try resolveTypeArgument(self, item); |
|
| 9122 | + | if not fitsTypeDepth(arg, MAX_APPLICATION_TYPE_DEPTH) { |
|
| 9123 | + | throw emitError(self, item, ErrorKind::LimitExceeded( |
|
| 9124 | + | "generic type argument exceeds the nesting limit" |
|
| 9125 | + | )); |
|
| 9126 | + | } |
|
| 9091 | 9127 | try ensureNoHeapValue(self, item, arg); |
|
| 9092 | 9128 | // Call contracts must account for regions carried by a replacement. |
|
| 9093 | 9129 | if refs::containsRef(self, arg) { |
|
| 9094 | 9130 | throw emitError(self, item, ErrorKind::Bracket(BracketError::Generic)); |
|
| 9095 | 9131 | } |
test/tests/generic.function.growth.reject.rad
added
+11 -0
| 1 | + | //! rejects: 5:19: generic specialization exceeds compiler limits |
|
| 2 | + | ||
| 3 | + | /// Request a distinct specialization with a deeper argument at each step. |
|
| 4 | + | fn grow[T]() -> u32 { |
|
| 5 | + | return grow[*T](); |
|
| 6 | + | } |
|
| 7 | + | ||
| 8 | + | /// Request the initial specialization. |
|
| 9 | + | @default fn main() -> u32 { |
|
| 10 | + | return grow[u32](); |
|
| 11 | + | } |
test/tests/generic.function.rad
added
+63 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | ||
| 3 | + | /// Return a value of any type. |
|
| 4 | + | fn identity[T](value: T) -> T { |
|
| 5 | + | let local = value; |
|
| 6 | + | return local; |
|
| 7 | + | } |
|
| 8 | + | ||
| 9 | + | /// Transfer an argument through a second template. |
|
| 10 | + | fn forward[T](value: T) -> T { |
|
| 11 | + | return identity[T](value); |
|
| 12 | + | } |
|
| 13 | + | ||
| 14 | + | /// Use the same specialization on a recursive edge. |
|
| 15 | + | fn repeat[T](value: T, count: u32) -> T { |
|
| 16 | + | if count == 0 { |
|
| 17 | + | return value; |
|
| 18 | + | } |
|
| 19 | + | return repeat[T](value, count - 1); |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// Lift the value into an optional. |
|
| 23 | + | fn present[T](value: T) -> ?T { |
|
| 24 | + | return value; |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// Copy elements of a fixed array through a loop binding. |
|
| 28 | + | fn last[T: Copy](values: [T; 2], fallback: T) -> T { |
|
| 29 | + | let mut result = fallback; |
|
| 30 | + | for value in values { |
|
| 31 | + | set result = value; |
|
| 32 | + | } |
|
| 33 | + | return result; |
|
| 34 | + | } |
|
| 35 | + | ||
| 36 | + | /// Return a borrow with the lifetime of the input. |
|
| 37 | + | fn borrowed[T](value: &'r T) -> &'r T { |
|
| 38 | + | return value; |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Exercise concrete signatures and aggregate returns. |
|
| 42 | + | @default fn main() -> u32 { |
|
| 43 | + | assert identity[u32](17) == 17; |
|
| 44 | + | let id: fn(u32) -> u32 = identity[u32]; |
|
| 45 | + | assert id(18) == 18; |
|
| 46 | + | let same = identity[u32]; |
|
| 47 | + | assert same == identity[u32]; |
|
| 48 | + | assert identity[bool](true); |
|
| 49 | + | let values = forward[[u32; 2]]([7, 11]); |
|
| 50 | + | assert values[0] == 7; |
|
| 51 | + | assert values[1] == 11; |
|
| 52 | + | assert last[u32](values, 0) == 11; |
|
| 53 | + | let large = forward[[u32; 5]]([1, 2, 3, 4, 5]); |
|
| 54 | + | assert large[4] == 5; |
|
| 55 | + | assert repeat[u32](31, 3) == 31; |
|
| 56 | + | let input: u32 = 23; |
|
| 57 | + | assert *borrowed[u32](&input) == 23; |
|
| 58 | + | let value = present[u32](42) else { |
|
| 59 | + | panic "expected a value"; |
|
| 60 | + | }; |
|
| 61 | + | assert value == 42; |
|
| 62 | + | return 0; |
|
| 63 | + | } |