lang: Check symbolic generic function bodies
dc2232085468e3cfee3a32c241bab85dc37ec7ba6ac7fef97f8f2190e231183e
Bind abstract type parameters for signature and body checking. Use ownership rules to require transfer of abstract values and allow duplication under Copy. Skip emission of templates in ordinary lowering. Assisted-by: Codex:gpt-6 Change-Id: Id0e201e533d59eac221bab748810c44cc54880ed
1 parent
8754ed52
lib/std/lang/lower.rad
+6 -0
| 1247 | 1247 | let stmtsList = block.statements; |
|
| 1248 | 1248 | ||
| 1249 | 1249 | for node in stmtsList { |
|
| 1250 | 1250 | match node.value { |
|
| 1251 | 1251 | case ast::NodeValue::FnDecl(decl) => { |
|
| 1252 | + | if decl.binders.len > 0 { |
|
| 1253 | + | continue; |
|
| 1254 | + | } |
|
| 1252 | 1255 | if let f = try lowerFnDecl(low, node, decl) { |
|
| 1253 | 1256 | let role = lowerFnRole(isRoot, decl.attrs); |
|
| 1254 | 1257 | let case resolver::Type::Fn(fnType) = |
|
| 1255 | 1258 | resolver::nodeData( |
|
| 1256 | 1259 | &low.resolver, node |
| 11898 | 11901 | throw LowerError::AllocationFailed; |
|
| 11899 | 11902 | }; |
|
| 11900 | 11903 | } |
|
| 11901 | 11904 | } |
|
| 11902 | 11905 | case resolver::NominalType::Placeholder(_) => {} |
|
| 11906 | + | case resolver::NominalType::Parameter { .. } => { |
|
| 11907 | + | throw LowerError::MissingMetadata; |
|
| 11908 | + | } |
|
| 11903 | 11909 | } |
|
| 11904 | 11910 | return; |
|
| 11905 | 11911 | } |
|
| 11906 | 11912 | else => {} |
|
| 11907 | 11913 | } |
lib/std/lang/resolver.rad
+105 -18
| 255 | 255 | /// Metadata for user-defined types. |
|
| 256 | 256 | export union NominalType: Copy { |
|
| 257 | 257 | /// Placeholder for a type that hasn't been fully resolved yet. |
|
| 258 | 258 | /// Stores the declaration node for lazy resolution. |
|
| 259 | 259 | Placeholder(*ast::Node), |
|
| 260 | + | /// Abstract type bound in a generic declaration's scope. |
|
| 261 | + | Parameter { |
|
| 262 | + | /// Name in the defining declaration. |
|
| 263 | + | name: *[u8], |
|
| 264 | + | /// Whether the parameter has a Copy bound. |
|
| 265 | + | copy: bool, |
|
| 266 | + | }, |
|
| 260 | 267 | Record(RecordType), |
|
| 261 | 268 | Union(UnionType), |
|
| 262 | 269 | } |
|
| 263 | 270 | ||
| 264 | 271 | /// Coercion plan, when coercion from one type to another. |
| 634 | 641 | Base, |
|
| 635 | 642 | /// A subscript must have exactly one index or range. |
|
| 636 | 643 | Arity, |
|
| 637 | 644 | /// A type or region occurs in a value expression. |
|
| 638 | 645 | ExpectedValue, |
|
| 646 | + | /// A generic declaration was used without type arguments. |
|
| 647 | + | MissingArguments, |
|
| 648 | + | /// An operation needs a concrete layout. |
|
| 649 | + | LayoutDependsOnParameter, |
|
| 639 | 650 | /// The compiler cannot resolve a generic declaration or application. |
|
| 640 | 651 | Generic, |
|
| 641 | 652 | } |
|
| 642 | 653 | ||
| 643 | 654 | /// High-level classification for semantic diagnostics. |
| 2066 | 2077 | return a; |
|
| 2067 | 2078 | } |
|
| 2068 | 2079 | return b; |
|
| 2069 | 2080 | } |
|
| 2070 | 2081 | ||
| 2082 | + | /// Check if a type's layout depends on an abstract parameter. |
|
| 2083 | + | fn layoutDependsOnParameter(self: &Resolver, ty: Type) -> bool { |
|
| 2084 | + | match ty { |
|
| 2085 | + | case Type::Nominal(id) => { |
|
| 2086 | + | if let case NominalType::Parameter { .. } = self.nominals[*id] { |
|
| 2087 | + | return true; |
|
| 2088 | + | } |
|
| 2089 | + | return false; |
|
| 2090 | + | } |
|
| 2091 | + | case Type::Array(array) => return layoutDependsOnParameter(self, *array.item), |
|
| 2092 | + | case Type::Optional(inner) => return layoutDependsOnParameter(self, *inner), |
|
| 2093 | + | else => return false, |
|
| 2094 | + | } |
|
| 2095 | + | } |
|
| 2096 | + | ||
| 2071 | 2097 | /// Get the layout of a type. |
|
| 2072 | 2098 | export fn getTypeLayout(self: &Resolver, ty: Type) -> Layout { |
|
| 2073 | 2099 | match ty { |
|
| 2074 | 2100 | case Type::Pointer { .. } => return Layout { size: PTR_SIZE, alignment: PTR_SIZE }, |
|
| 2075 | 2101 | case Type::Slice { .. }, Type::TraitObject { .. } => |
| 2174 | 2200 | } |
|
| 2175 | 2201 | ||
| 2176 | 2202 | /// Get the layout of a nominal type. |
|
| 2177 | 2203 | export fn getNominalLayout(self: &Resolver, id: NominalId) -> Layout { |
|
| 2178 | 2204 | match self.nominals[*id] { |
|
| 2179 | - | case NominalType::Placeholder(_) => { |
|
| 2180 | - | panic "getNominalLayout: placeholder type"; |
|
| 2205 | + | case NominalType::Placeholder(_), NominalType::Parameter { .. } => { |
|
| 2206 | + | panic "getNominalLayout: type has no concrete layout"; |
|
| 2181 | 2207 | } |
|
| 2182 | 2208 | case NominalType::Record(recordType) => { |
|
| 2183 | 2209 | return recordType.layout; |
|
| 2184 | 2210 | } |
|
| 2185 | 2211 | case NominalType::Union(unionType) => { |
| 2713 | 2739 | return class == types::PointerClass::Unsafe or not mutable, |
|
| 2714 | 2740 | case Type::Array(array) => return isCopy(self, *array.item), |
|
| 2715 | 2741 | case Type::Optional(inner) => return isCopy(self, *inner), |
|
| 2716 | 2742 | case Type::Nominal(id) => { |
|
| 2717 | 2743 | match self.nominals[*id] { |
|
| 2718 | - | case NominalType::Record(recInfo) => return recInfo.declaredCopy, |
|
| 2719 | - | case NominalType::Union(unionType) => return unionType.declaredCopy, |
|
| 2720 | - | case NominalType::Placeholder(_) => return false, |
|
| 2744 | + | case NominalType::Record(recInfo) => return recInfo.declaredCopy, |
|
| 2745 | + | case NominalType::Union(unionType) => return unionType.declaredCopy, |
|
| 2746 | + | case NominalType::Parameter { copy, .. } => return copy, |
|
| 2747 | + | case NominalType::Placeholder(_) => return false, |
|
| 2721 | 2748 | } |
|
| 2722 | 2749 | } |
|
| 2723 | 2750 | else => return true, |
|
| 2724 | 2751 | } |
|
| 2725 | 2752 | } |
| 2788 | 2815 | return true; |
|
| 2789 | 2816 | } |
|
| 2790 | 2817 | } |
|
| 2791 | 2818 | return false; |
|
| 2792 | 2819 | } |
|
| 2793 | - | else => return false, |
|
| 2820 | + | case NominalType::Parameter { copy, .. } => return not copy, |
|
| 2821 | + | else => return false, |
|
| 2794 | 2822 | } |
|
| 2795 | 2823 | } |
|
| 2796 | 2824 | else => return false, |
|
| 2797 | 2825 | } |
|
| 2798 | 2826 | } |
| 2945 | 2973 | fn isComparable(self: &Resolver, left: Type, right: Type) -> bool { |
|
| 2946 | 2974 | if left == Type::Unknown or right == Type::Unknown { |
|
| 2947 | 2975 | return false; |
|
| 2948 | 2976 | } |
|
| 2949 | 2977 | if left == right { |
|
| 2950 | - | return true; |
|
| 2978 | + | return not layoutDependsOnParameter(self, left); |
|
| 2951 | 2979 | } |
|
| 2952 | 2980 | // Comparisons with optionals. |
|
| 2953 | 2981 | if let case Type::Optional(l) = left { |
|
| 2954 | 2982 | if let case Type::Optional(r) = right { |
|
| 2955 | 2983 | return isComparable(self, *l, *r); |
| 3613 | 3641 | if not self.inUnsafeContext { |
|
| 3614 | 3642 | throw emitError(self, node, ErrorKind::UnsafeOperation); |
|
| 3615 | 3643 | } |
|
| 3616 | 3644 | } |
|
| 3617 | 3645 | ||
| 3618 | - | /// Check access to unsafe statics and constant function declarations. |
|
| 3646 | + | /// Check whether a declaration can be used as a value at this site. |
|
| 3619 | 3647 | fn checkSymbolAccess(self: &mut Resolver, node: *ast::Node, sym: SymbolId) |
|
| 3620 | 3648 | throws (ResolveError) |
|
| 3621 | 3649 | { |
|
| 3650 | + | match self.symbols[*sym].data { |
|
| 3651 | + | case SymbolData::Type(id) => { |
|
| 3652 | + | if let case NominalType::Parameter { .. } = self.nominals[*id] { |
|
| 3653 | + | throw emitError(self, node, ErrorKind::Bracket(BracketError::ExpectedValue)); |
|
| 3654 | + | } |
|
| 3655 | + | } |
|
| 3656 | + | case SymbolData::Value { type: Type::Fn(_), .. } => { |
|
| 3657 | + | if let case ast::NodeValue::FnDecl(fnDecl) = |
|
| 3658 | + | self.symbols[*sym].node.value; |
|
| 3659 | + | fnDecl.binders.len > 0 |
|
| 3660 | + | { |
|
| 3661 | + | throw emitError(self, node, ErrorKind::Bracket(BracketError::MissingArguments)); |
|
| 3662 | + | } |
|
| 3663 | + | } |
|
| 3664 | + | else => {} |
|
| 3665 | + | } |
|
| 3622 | 3666 | let attrs = self.symbols[*sym].attrs; |
|
| 3623 | 3667 | if (attrs & ((1 << ast::Attribute::Unsafe as u32) |
|
| 3624 | 3668 | | (1 << ast::Attribute::Constant as u32))) == 0 |
|
| 3625 | 3669 | { |
|
| 3626 | 3670 | return; |
| 4653 | 4697 | break; |
|
| 4654 | 4698 | } |
|
| 4655 | 4699 | } |
|
| 4656 | 4700 | } |
|
| 4657 | 4701 | // A body that is not resolved yet has no answer to keep. |
|
| 4658 | - | case NominalType::Placeholder(_) => return false, |
|
| 4702 | + | case NominalType::Placeholder(_), NominalType::Parameter { .. } => return false, |
|
| 4659 | 4703 | } |
|
| 4660 | 4704 | if memo { |
|
| 4661 | 4705 | set self.slotMemo[*id] = found; |
|
| 4662 | 4706 | } |
|
| 4663 | 4707 | return found; |
| 5100 | 5144 | return nil; |
|
| 5101 | 5145 | } |
|
| 5102 | 5146 | return borrowedInput(self, node, info.borrows[0].param); |
|
| 5103 | 5147 | } |
|
| 5104 | 5148 | ||
| 5149 | + | /// Bind abstract parameters in their declaration's lexical scope. |
|
| 5150 | + | fn bindTypeParameters(self: &mut Resolver, binders: *[*ast::Node]) |
|
| 5151 | + | throws (ResolveError) |
|
| 5152 | + | { |
|
| 5153 | + | for binder in binders { |
|
| 5154 | + | let case ast::NodeValue::TypeBinder { name, bound } = binder.value else continue; |
|
| 5155 | + | let mut copy = false; |
|
| 5156 | + | if let traitName = bound { |
|
| 5157 | + | let case ast::NodeValue::Ident(text) = traitName.value |
|
| 5158 | + | else throw emitError(self, traitName, ErrorKind::Bracket(BracketError::Generic)); |
|
| 5159 | + | if not mem::eq(text, "Copy") { |
|
| 5160 | + | throw emitError(self, traitName, ErrorKind::Bracket(BracketError::Generic)); |
|
| 5161 | + | } |
|
| 5162 | + | set copy = true; |
|
| 5163 | + | } |
|
| 5164 | + | let text = try nodeName(self, name); |
|
| 5165 | + | let id = allocNominalType(self, NominalType::Parameter { name: text, copy }); |
|
| 5166 | + | try bindTypeIdent(self, name, binder, id, 0); |
|
| 5167 | + | } |
|
| 5168 | + | } |
|
| 5169 | + | ||
| 5105 | 5170 | /// Analyze a function declaration signature and bind the function name. |
|
| 5106 | 5171 | fn resolveFnDecl(self: &mut Resolver, node: *ast::Node, decl: ast::FnDecl) -> Type |
|
| 5107 | 5172 | throws (ResolveError) |
|
| 5108 | 5173 | { |
|
| 5109 | - | if decl.binders.len > 0 { |
|
| 5174 | + | let attrMask = resolveAttributes(self, decl.attrs); |
|
| 5175 | + | if decl.binders.len > 0 |
|
| 5176 | + | and (ast::hasAttribute(attrMask, ast::Attribute::Default) |
|
| 5177 | + | or ast::hasAttribute(attrMask, ast::Attribute::Test) |
|
| 5178 | + | or ast::hasAttribute(attrMask, ast::Attribute::Extern) |
|
| 5179 | + | or ast::hasAttribute(attrMask, ast::Attribute::Intrinsic)) |
|
| 5180 | + | { |
|
| 5110 | 5181 | throw emitError(self, decl.binders[0], ErrorKind::Bracket(BracketError::Generic)); |
|
| 5111 | 5182 | } |
|
| 5112 | - | let attrMask = resolveAttributes(self, decl.attrs); |
|
| 5113 | 5183 | let mut fnType = FnType { |
|
| 5114 | 5184 | paramTypes: &[], |
|
| 5115 | 5185 | returnType: allocType(self, Type::Void), |
|
| 5116 | 5186 | throwList: &[], |
|
| 5117 | 5187 | isUnsafe: ast::hasAttribute(attrMask, ast::Attribute::Unsafe), |
| 5120 | 5190 | stores: nil, |
|
| 5121 | 5191 | }; |
|
| 5122 | 5192 | // The body reuses the function scope, so the regions and parameters of |
|
| 5123 | 5193 | // the signature stay in scope there. |
|
| 5124 | 5194 | enterFn(self, node, &fnType); |
|
| 5125 | - | try resolveFnSig(self, node, decl.sig, &mut fnType) catch e { |
|
| 5195 | + | try resolveFnSig(self, node, decl.sig, decl.binders, &mut fnType) catch e { |
|
| 5126 | 5196 | exitFn(self); |
|
| 5127 | 5197 | throw e; |
|
| 5128 | 5198 | }; |
|
| 5129 | 5199 | exitFn(self); |
|
| 5130 | 5200 |
| 5163 | 5233 | /// in `fnType`. A region name already in scope cannot be declared again. |
|
| 5164 | 5234 | fn resolveFnSig( |
|
| 5165 | 5235 | self: &mut Resolver, |
|
| 5166 | 5236 | node: *ast::Node, |
|
| 5167 | 5237 | sig: &ast::FnSig, |
|
| 5238 | + | binders: *[*ast::Node], |
|
| 5168 | 5239 | fnType: &mut FnType |
|
| 5169 | 5240 | ) throws (ResolveError) { |
|
| 5241 | + | try bindTypeParameters(self, binders); |
|
| 5242 | + | for region in binders { |
|
| 5243 | + | if let case ast::NodeValue::Ident(name) = region.value { |
|
| 5244 | + | try declareRegion(self, region, refs::signatureNamesMutRegion(nil, sig.params, name)); |
|
| 5245 | + | } |
|
| 5246 | + | } |
|
| 5170 | 5247 | for region in refs::signatureRegions(self, nil, sig.params) { |
|
| 5171 | 5248 | let name = try nodeName(self, region); |
|
| 5172 | - | try declareRegion(self, region, refs::signatureNamesMutRegion(nil, sig.params, name)); |
|
| 5249 | + | if binders.len == 0 or findSymbolInScope(self, self.scope, name) == nil { |
|
| 5250 | + | try declareRegion(self, region, refs::signatureNamesMutRegion(nil, sig.params, name)); |
|
| 5251 | + | } |
|
| 5173 | 5252 | } |
|
| 5174 | 5253 | if let retNode = sig.returnType { |
|
| 5175 | 5254 | let retTy = try infer(self, retNode); |
|
| 5176 | 5255 | set fnType.returnType = allocType(self, retTy); |
|
| 5177 | 5256 | set fnType.borrows = try returnBorrow(self, nil, sig.params, retNode, retTy); |
| 5211 | 5290 | let sym = symbolFor(self, node) else { |
|
| 5212 | 5291 | // The function declaration failed to type check, therefore |
|
| 5213 | 5292 | // no symbol was associated with it. |
|
| 5214 | 5293 | return; |
|
| 5215 | 5294 | }; |
|
| 5216 | - | let case SymbolData::Value { type: Type::Fn(fnType), .. } = self.symbols[*sym].data else { |
|
| 5217 | - | panic "resolveFnDeclBody: unexpected symbol data for function"; |
|
| 5218 | - | }; |
|
| 5295 | + | let case SymbolData::Value { type: Type::Fn(fnType), .. } = self.symbols[*sym].data else return; |
|
| 5219 | 5296 | let isExtern = ast::hasAttribute(self.symbols[*sym].attrs, ast::Attribute::Extern); |
|
| 5220 | 5297 | let isIntrinsic = ast::hasAttribute(self.symbols[*sym].attrs, ast::Attribute::Intrinsic); |
|
| 5221 | 5298 | ||
| 5222 | 5299 | // Record fields are resolved after signatures, so the storage element |
|
| 5223 | 5300 | // types are checked here. |
| 5405 | 5482 | ||
| 5406 | 5483 | // Ensure field type is fully resolved before computing layout. |
|
| 5407 | 5484 | try ensureTypeResolved(self, fieldType, typeNode); |
|
| 5408 | 5485 | ||
| 5409 | 5486 | // Compute field offset by aligning to field's alignment. |
|
| 5487 | + | if layoutDependsOnParameter(self, fieldType) { |
|
| 5488 | + | throw emitError(self, typeNode, ErrorKind::Bracket( |
|
| 5489 | + | BracketError::LayoutDependsOnParameter |
|
| 5490 | + | )); |
|
| 5491 | + | } |
|
| 5410 | 5492 | let fieldLayout = getTypeLayout(self, fieldType); |
|
| 5411 | 5493 | set currentOffset = mem::alignUp(currentOffset, fieldLayout.alignment); |
|
| 5412 | 5494 | ||
| 5413 | 5495 | try! result.append( |
|
| 5414 | 5496 | RecordField { |
| 7911 | 7993 | // TODO: Somehow, ensuring the type is resolved should just happen all |
|
| 7912 | 7994 | // the time, lazily. |
|
| 7913 | 7995 | try ensureTypeResolved(self, ty, args[0]); |
|
| 7914 | 7996 | // TODO: This should be stored in `symbol` instead of having to recompute it. |
|
| 7915 | 7997 | // That way there's a canonical place to look for code gen. |
|
| 7998 | + | if layoutDependsOnParameter(self, ty) { |
|
| 7999 | + | throw emitError(self, args[0], ErrorKind::Bracket( |
|
| 8000 | + | BracketError::LayoutDependsOnParameter |
|
| 8001 | + | )); |
|
| 8002 | + | } |
|
| 7916 | 8003 | let layout = getTypeLayout(self, ty); |
|
| 7917 | 8004 | ||
| 7918 | 8005 | // Evaluate the built-in. |
|
| 7919 | 8006 | let mut value: u32 = undefined; |
|
| 7920 | 8007 | match kind { |
| 8327 | 8414 | return false; |
|
| 8328 | 8415 | } |
|
| 8329 | 8416 | } |
|
| 8330 | 8417 | return true; |
|
| 8331 | 8418 | } |
|
| 8332 | - | case NominalType::Placeholder(_) => return false, |
|
| 8419 | + | case NominalType::Placeholder(_), NominalType::Parameter { .. } => return false, |
|
| 8333 | 8420 | } |
|
| 8334 | 8421 | } |
|
| 8335 | 8422 | else => return true, |
|
| 8336 | 8423 | } |
|
| 8337 | 8424 | } |
| 10742 | 10829 | // The result cannot hold references. |
|
| 10743 | 10830 | let params = ast::FnSig { |
|
| 10744 | 10831 | params: t.params, returnType: nil, throwList: t.throwList |
|
| 10745 | 10832 | }; |
|
| 10746 | 10833 | enterScope(self, node); |
|
| 10747 | - | try resolveFnSig(self, node, ¶ms, &mut fnType) catch e { |
|
| 10834 | + | try resolveFnSig(self, node, ¶ms, &[], &mut fnType) catch e { |
|
| 10748 | 10835 | exitScope(self); |
|
| 10749 | 10836 | throw e; |
|
| 10750 | 10837 | }; |
|
| 10751 | 10838 | if let ret = t.returnType { |
|
| 10752 | 10839 | let resolvedRet = try resolveValueType(self, ret) catch e { |
lib/std/lang/resolver/printer.rad
+10 -0
| 225 | 225 | fn printNominalType(res: &super::Resolver, info: super::NominalId) { |
|
| 226 | 226 | match res.nominals[*info] { |
|
| 227 | 227 | case super::NominalType::Placeholder(_) => { |
|
| 228 | 228 | io::print("<placeholder>"); |
|
| 229 | 229 | } |
|
| 230 | + | case super::NominalType::Parameter { name, .. } => { |
|
| 231 | + | io::print(name); |
|
| 232 | + | } |
|
| 230 | 233 | case super::NominalType::Record(recordType) => { |
|
| 231 | 234 | io::print("record {"); |
|
| 232 | 235 | if recordType.fields.len > 0 { |
|
| 233 | 236 | io::print(" "); |
|
| 234 | 237 | for i in 0 .. recordType.fields.len { |
| 280 | 283 | fn printNominalTypeName(res: &super::Resolver, info: super::NominalId) { |
|
| 281 | 284 | match res.nominals[*info] { |
|
| 282 | 285 | case super::NominalType::Placeholder(_) => { |
|
| 283 | 286 | io::print("<placeholder>"); |
|
| 284 | 287 | } |
|
| 288 | + | case super::NominalType::Parameter { name, .. } => { |
|
| 289 | + | io::print(name); |
|
| 290 | + | } |
|
| 285 | 291 | case super::NominalType::Record(_) => { |
|
| 286 | 292 | io::print("<record>"); |
|
| 287 | 293 | } |
|
| 288 | 294 | case super::NominalType::Union(_) => { |
|
| 289 | 295 | io::print("<union>"); |
| 517 | 523 | io::print("expected array or slice"), |
|
| 518 | 524 | case super::BracketError::Arity => |
|
| 519 | 525 | io::print("a subscript requires exactly one index or range"), |
|
| 520 | 526 | case super::BracketError::ExpectedValue => |
|
| 521 | 527 | io::print("expected a value, found a type or region"), |
|
| 528 | + | case super::BracketError::MissingArguments => |
|
| 529 | + | io::print("generic declaration requires type arguments"), |
|
| 530 | + | case super::BracketError::LayoutDependsOnParameter => |
|
| 531 | + | io::print("operation requires a concrete type layout"), |
|
| 522 | 532 | case super::BracketError::Generic => |
|
| 523 | 533 | io::print("generic declarations and types cannot be resolved"), |
|
| 524 | 534 | } |
|
| 525 | 535 | } |
|
| 526 | 536 | case super::ErrorKind::InvalidAlignmentValue(val) => { |
lib/std/lang/resolver/refs.rad
+9 -6
| 358 | 358 | case NominalType::Union(info) => { |
|
| 359 | 359 | for variant in info.variants { |
|
| 360 | 360 | set found = found or reachesMutable(self, variant.valueType, visit); |
|
| 361 | 361 | } |
|
| 362 | 362 | } |
|
| 363 | - | case NominalType::Placeholder(_) => set found = true, |
|
| 363 | + | case NominalType::Placeholder(_), NominalType::Parameter { .. } => |
|
| 364 | + | set found = true, |
|
| 364 | 365 | } |
|
| 365 | 366 | set visit.len -= 1; |
|
| 366 | 367 | return found; |
|
| 367 | 368 | } |
|
| 368 | 369 | else => return false, |
| 433 | 434 | ||
| 434 | 435 | /// Whether a nominal type declares a region. An unresolved declaration is read |
|
| 435 | 436 | /// from its syntax. |
|
| 436 | 437 | fn isBorrowedNominal(self: &Resolver, id: super::NominalId) -> bool { |
|
| 437 | 438 | match self.nominals[*id] { |
|
| 438 | - | case NominalType::Record(r) => return r.borrowed, |
|
| 439 | - | case NominalType::Union(u) => return u.borrowed, |
|
| 440 | - | case NominalType::Placeholder(_) => return nominalRegions(self, id).len > 0, |
|
| 439 | + | case NominalType::Record(r) => return r.borrowed, |
|
| 440 | + | case NominalType::Union(u) => return u.borrowed, |
|
| 441 | + | case NominalType::Placeholder(_) => return nominalRegions(self, id).len > 0, |
|
| 442 | + | case NominalType::Parameter { .. } => return false, |
|
| 441 | 443 | } |
|
| 442 | 444 | } |
|
| 443 | 445 | ||
| 444 | 446 | /// Whether `ty` is a reference to a value, or to elements, of a type with a region. |
|
| 445 | 447 | export fn refersToBorrowed(self: &Resolver, ty: Type) -> bool { |
| 569 | 571 | } |
|
| 570 | 572 | ||
| 571 | 573 | /// Return the declared region parameters of a nominal type. |
|
| 572 | 574 | export fn nominalRegions(self: &Resolver, id: super::NominalId) -> *[*ast::Node] { |
|
| 573 | 575 | match self.nominals[*id] { |
|
| 574 | - | case NominalType::Record(r) => return r.regions, |
|
| 575 | - | case NominalType::Union(u) => return u.regions, |
|
| 576 | + | case NominalType::Record(r) => return r.regions, |
|
| 577 | + | case NominalType::Union(u) => return u.regions, |
|
| 578 | + | case NominalType::Parameter { .. } => return &[], |
|
| 576 | 579 | case NominalType::Placeholder(node) => { |
|
| 577 | 580 | match node.value { |
|
| 578 | 581 | case ast::NodeValue::RecordDecl(r) => return r.binders, |
|
| 579 | 582 | case ast::NodeValue::UnionDecl(u) => return u.binders, |
|
| 580 | 583 | else => return &[], |
lib/std/lang/resolver/tests/core.rad
+65 -0
| 3652 | 3652 | ) |
|
| 3653 | 3653 | ); |
|
| 3654 | 3654 | try expectErrorKind(&result, resolver::ErrorKind::HeapValue); |
|
| 3655 | 3655 | } |
|
| 3656 | 3656 | } |
|
| 3657 | + | ||
| 3658 | + | /// Check that a generic body can transfer an abstract parameter and borrow it. |
|
| 3659 | + | @test unsafe fn testResolveGenericFunctionBody() throws (testing::TestError) { |
|
| 3660 | + | for program in [ |
|
| 3661 | + | @embed("lang/resolver/tests/fixtures/generic_body/identity.rad"), |
|
| 3662 | + | @embed("lang/resolver/tests/fixtures/generic_body/borrow.rad"), |
|
| 3663 | + | @embed("lang/resolver/tests/fixtures/generic_body/region_binder.rad"), |
|
| 3664 | + | @embed("lang/resolver/tests/fixtures/generic_body/callback.rad"), |
|
| 3665 | + | @embed("lang/resolver/tests/fixtures/generic_body/copy.rad"), |
|
| 3666 | + | ] { |
|
| 3667 | + | let mut a = testResolver(); |
|
| 3668 | + | let result = try resolveProgramStr(&mut a, program); |
|
| 3669 | + | try expectNoErrors(&result); |
|
| 3670 | + | } |
|
| 3671 | + | } |
|
| 3672 | + | ||
| 3673 | + | /// Check an unused generic body for arithmetic on an abstract type. |
|
| 3674 | + | @test unsafe fn testResolveGenericFunctionOperators() throws (testing::TestError) { |
|
| 3675 | + | let mut a = testResolver(); |
|
| 3676 | + | let result = try resolveProgramStr( |
|
| 3677 | + | &mut a, "fn add[T](a: T, b: T) -> T { return a + b; }" |
|
| 3678 | + | ); |
|
| 3679 | + | try expectErrorKind(&result, resolver::ErrorKind::ExpectedNumeric); |
|
| 3680 | + | } |
|
| 3681 | + | ||
| 3682 | + | /// Check that every path transfers an owned abstract parameter. |
|
| 3683 | + | @test unsafe fn testResolveGenericFunctionDiscard() throws (testing::TestError) { |
|
| 3684 | + | let mut a = testResolver(); |
|
| 3685 | + | let result = try resolveProgramStr( |
|
| 3686 | + | &mut a, "fn discard[T](value: T) {}" |
|
| 3687 | + | ); |
|
| 3688 | + | try expectErrorKind(&result, resolver::ErrorKind::LinearNotConsumed("value")); |
|
| 3689 | + | } |
|
| 3690 | + | ||
| 3691 | + | /// Reject equality without a corresponding bound and a bare generic function. |
|
| 3692 | + | @test unsafe fn testResolveGenericInvalidOperations() throws (testing::TestError) { |
|
| 3693 | + | let mut a = testResolver(); |
|
| 3694 | + | let equality = try resolveProgramStr( |
|
| 3695 | + | &mut a, "fn equal[T: Copy](a: T, b: T) -> bool { return a == b; }" |
|
| 3696 | + | ); |
|
| 3697 | + | try expectError(&equality); |
|
| 3698 | + | ||
| 3699 | + | let mut b = testResolver(); |
|
| 3700 | + | let bare = try resolveProgramStr( |
|
| 3701 | + | &mut b, "fn identity[T](x: T) -> T { return x; } fn hold() { let f = identity; }" |
|
| 3702 | + | ); |
|
| 3703 | + | try expectErrorKind(&bare, resolver::ErrorKind::Bracket( |
|
| 3704 | + | resolver::BracketError::MissingArguments |
|
| 3705 | + | )); |
|
| 3706 | + | } |
|
| 3707 | + | ||
| 3708 | + | /// Reject a duplicated use or the escape of a borrowed abstract local. |
|
| 3709 | + | @test unsafe fn testResolveGenericBodyOwnership() throws (testing::TestError) { |
|
| 3710 | + | let mut a = testResolver(); |
|
| 3711 | + | let duplicate = try resolveProgramStr( |
|
| 3712 | + | &mut a, "fn duplicate[T](x: T) -> [T; 2] { return [x, x]; }" |
|
| 3713 | + | ); |
|
| 3714 | + | try expectErrorKind(&duplicate, resolver::ErrorKind::LinearUseAfterConsume("x")); |
|
| 3715 | + | ||
| 3716 | + | let mut b = testResolver(); |
|
| 3717 | + | let escape = try resolveProgramStr( |
|
| 3718 | + | &mut b, "fn escape[T: Copy](x: &'r T, y: T) -> &'r T { return &y; }" |
|
| 3719 | + | ); |
|
| 3720 | + | try expectError(&escape); |
|
| 3721 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_body/borrow.rad
added
+3 -0
| 1 | + | fn read[T](x: &'r T) -> &'r T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_body/callback.rad
added
+3 -0
| 1 | + | fn apply[T](x: T, f: fn(T) -> T) -> T { |
|
| 2 | + | return f(x); |
|
| 3 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_body/copy.rad
added
+3 -0
| 1 | + | fn duplicate[T: Copy](x: T) -> [T; 2] { |
|
| 2 | + | return [x, x]; |
|
| 3 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_body/identity.rad
added
+3 -0
| 1 | + | fn identity[T](x: T) -> T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
lib/std/lang/resolver/tests/fixtures/generic_body/region_binder.rad
added
+3 -0
| 1 | + | fn read['r, T](x: &'r T) -> &'r T { |
|
| 2 | + | return x; |
|
| 3 | + | } |
lib/std/std.manifest
+5 -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_body/borrow.rad", |
|
| 133 | + | "lang/resolver/tests/fixtures/generic_body/callback.rad", |
|
| 134 | + | "lang/resolver/tests/fixtures/generic_body/copy.rad", |
|
| 135 | + | "lang/resolver/tests/fixtures/generic_body/identity.rad", |
|
| 136 | + | "lang/resolver/tests/fixtures/generic_body/region_binder.rad", |
|
| 132 | 137 | "lang/resolver/tests/fixtures/subscript/empty.rad", |
|
| 133 | 138 | "lang/resolver/tests/fixtures/subscript/range_and_index.rad", |
|
| 134 | 139 | "lang/resolver/tests/fixtures/subscript/region_index.rad", |
|
| 135 | 140 | "lang/resolver/tests/fixtures/subscript/store_two_indices.rad", |
|
| 136 | 141 | "lang/resolver/tests/fixtures/subscript/type_in_index.rad", |