lang: Add unsafe function types
2ead9b54a48bbc7c581a2e365b2987954aaac289cc64990a27b764fef8204b57
Function values could not record that calling them needs an unsafe function body, so callbacks that perform unsafe operations had to be hidden behind safe signatures. Parse `unsafe fn(...)` types and keep the requirement in the function type. A safe function value can fill an unsafe slot, but not the other way around, and an indirect call through an unsafe type needs an unsafe caller. Address-of expressions with a `*unsafe` hint, or of storage reached through an unsafe pointer, now produce unsafe pointers directly, and stored pointers are accepted where a borrow is expected. Assisted-by: Claude:opus-5.5
1 parent
7ab99f8d
lib/std/lang/ast.rad
+6 -1
| 206 | 206 | fields: *mut [*Node], |
|
| 207 | 207 | /// Whether this record has labeled fields. |
|
| 208 | 208 | labeled: bool, |
|
| 209 | 209 | }, |
|
| 210 | 210 | /// Anonymous function type. |
|
| 211 | - | Fn(FnSig), |
|
| 211 | + | Fn { |
|
| 212 | + | /// Parameter, return, and error types. |
|
| 213 | + | sig: FnSig, |
|
| 214 | + | /// Whether a call requires an unsafe function body. |
|
| 215 | + | isUnsafe: bool, |
|
| 216 | + | }, |
|
| 212 | 217 | /// Trait object type, eg. `*opaque Allocator`, `&opaque Allocator`, or |
|
| 213 | 218 | /// `*unsafe opaque Allocator`. |
|
| 214 | 219 | TraitObject { |
|
| 215 | 220 | /// Ownership and safety class. |
|
| 216 | 221 | class: types::PointerClass, |
lib/std/lang/ast/printer.rad
+3 -2
| 105 | 105 | case super::TypeSig::Optional { valueType } => |
|
| 106 | 106 | return sexpr::list(a, "?", &[toExpr(a, valueType)]), |
|
| 107 | 107 | case super::TypeSig::Nominal(name) => return toExpr(a, name), |
|
| 108 | 108 | case super::TypeSig::Record { fields, .. } => |
|
| 109 | 109 | return sexpr::list(a, "record", nodeListToExprs(a, &fields[..])), |
|
| 110 | - | case super::TypeSig::Fn(sig) => { |
|
| 110 | + | case super::TypeSig::Fn { sig, isUnsafe } => { |
|
| 111 | 111 | let mut ret = sexpr::sym("void"); |
|
| 112 | 112 | if let rt = sig.returnType { |
|
| 113 | 113 | set ret = toExpr(a, rt); |
|
| 114 | 114 | } |
|
| 115 | - | return sexpr::list(a, "fn", &[sexpr::list(a, "params", nodeListToExprs(a, &sig.params[..])), ret]); |
|
| 115 | + | let head = "unsafe-fn" if isUnsafe else "fn"; |
|
| 116 | + | return sexpr::list(a, head, &[sexpr::list(a, "params", nodeListToExprs(a, &sig.params[..])), ret]); |
|
| 116 | 117 | } |
|
| 117 | 118 | case super::TypeSig::TraitObject { class, traitName, mutable } => { |
|
| 118 | 119 | let head = pointerClassHead(class, "obj", "obj-ref", "unsafe-obj"); |
|
| 119 | 120 | return sexpr::list(a, head, &[sexpr::sym("mut"), toExpr(a, traitName)]) if mutable |
|
| 120 | 121 | else sexpr::list(a, head, &[toExpr(a, traitName)]); |
lib/std/lang/parser.rad
+3 -2
| 1783 | 1783 | ||
| 1784 | 1784 | /// Parse a function type signature. |
|
| 1785 | 1785 | fn parseFnType(p: *mut Parser) -> *ast::Node |
|
| 1786 | 1786 | throws (ParseError) |
|
| 1787 | 1787 | { |
|
| 1788 | + | let isUnsafe = consume(p, scanner::TokenKind::Unsafe); |
|
| 1788 | 1789 | try expect(p, scanner::TokenKind::Fn, "expected `fn`"); |
|
| 1789 | 1790 | let params = try parseList( |
|
| 1790 | 1791 | p, |
|
| 1791 | 1792 | scanner::TokenKind::LParen, |
|
| 1792 | 1793 | scanner::TokenKind::RParen, |
| 1798 | 1799 | set returnType = try parseType(p); |
|
| 1799 | 1800 | } |
|
| 1800 | 1801 | let throwList = try parseThrowList(p); |
|
| 1801 | 1802 | let sig = ast::FnSig { params, returnType, throwList }; |
|
| 1802 | 1803 | return node(p, ast::NodeValue::TypeSig( |
|
| 1803 | - | ast::TypeSig::Fn(sig) |
|
| 1804 | + | ast::TypeSig::Fn { sig, isUnsafe } |
|
| 1804 | 1805 | )); |
|
| 1805 | 1806 | } |
|
| 1806 | 1807 | ||
| 1807 | 1808 | /// Parse a function signature following the function name. |
|
| 1808 | 1809 | fn parseFnTypeSig(p: *mut Parser) -> ast::FnSig |
| 2012 | 2013 | } |
|
| 2013 | 2014 | case scanner::TokenKind::Opaque => { |
|
| 2014 | 2015 | advance(p); |
|
| 2015 | 2016 | return node(p, ast::NodeValue::TypeSig(ast::TypeSig::Opaque)); |
|
| 2016 | 2017 | } |
|
| 2017 | - | case scanner::TokenKind::Fn => { |
|
| 2018 | + | case scanner::TokenKind::Fn, scanner::TokenKind::Unsafe => { |
|
| 2018 | 2019 | return try parseFnType(p); |
|
| 2019 | 2020 | } |
|
| 2020 | 2021 | else => { |
|
| 2021 | 2022 | throw failParsing(p, "expected type"); |
|
| 2022 | 2023 | } |
lib/std/lang/parser/tests.rad
+14 -3
| 1011 | 1011 | /// Test parsing a function type with parameters and return type. |
|
| 1012 | 1012 | @test fn testParseTypeFn() throws (testing::TestError) { |
|
| 1013 | 1013 | let node = try! parseTypeStr("fn (i32, *u8) -> bool"); |
|
| 1014 | 1014 | let case ast::NodeValue::TypeSig(sigValue) = node.value |
|
| 1015 | 1015 | else throw testing::TestError::Failed; |
|
| 1016 | - | let case ast::TypeSig::Fn(sig) = sigValue |
|
| 1016 | + | let case ast::TypeSig::Fn { sig, .. } = sigValue |
|
| 1017 | 1017 | else throw testing::TestError::Failed; |
|
| 1018 | 1018 | ||
| 1019 | 1019 | try testing::expect(sig.params.len == 2); |
|
| 1020 | 1020 | ||
| 1021 | 1021 | let param0 = sig.params[0]; |
| 1034 | 1034 | /// Test parsing a function type with a throws clause. |
|
| 1035 | 1035 | @test fn testParseTypeFnThrows() throws (testing::TestError) { |
|
| 1036 | 1036 | let node = try! parseTypeStr("fn (i32) -> bool throws (Error, Other)"); |
|
| 1037 | 1037 | let case ast::NodeValue::TypeSig(sigValue) = node.value |
|
| 1038 | 1038 | else throw testing::TestError::Failed; |
|
| 1039 | - | let case ast::TypeSig::Fn(sig) = sigValue |
|
| 1039 | + | let case ast::TypeSig::Fn { sig, .. } = sigValue |
|
| 1040 | 1040 | else throw testing::TestError::Failed; |
|
| 1041 | 1041 | ||
| 1042 | 1042 | try testing::expect(sig.params.len == 1); |
|
| 1043 | 1043 | try expectIntType(sig.params[0], 4, ast::Signedness::Signed); |
|
| 1044 | 1044 |
| 2928 | 2928 | try testing::expect(items.len == 3); |
|
| 2929 | 2929 | ||
| 2930 | 2930 | // Function type parameters. |
|
| 2931 | 2931 | let fnType = try! parseTypeStr("fn (i32, bool,)"); |
|
| 2932 | 2932 | let case ast::NodeValue::TypeSig(sigValue) = fnType.value else throw testing::TestError::Failed; |
|
| 2933 | - | let case ast::TypeSig::Fn(sig) = sigValue else throw testing::TestError::Failed; |
|
| 2933 | + | let case ast::TypeSig::Fn { sig, .. } = sigValue else throw testing::TestError::Failed; |
|
| 2934 | 2934 | try testing::expect(sig.params.len == 2); |
|
| 2935 | 2935 | try testing::expect(sig.returnType == nil); |
|
| 2936 | 2936 | ||
| 2937 | 2937 | // Throws lists. |
|
| 2938 | 2938 | let throwsNode = try! parseStmtStr("fn handle() throws (Error, Other,) {}"); |
|
| 2939 | 2939 | let case ast::NodeValue::FnDecl(throwsDecl) = throwsNode.value else throw testing::TestError::Failed; |
|
| 2940 | 2940 | try testing::expect(throwsDecl.sig.throwList.len == 2); |
|
| 2941 | 2941 | } |
|
| 2942 | + | ||
| 2943 | + | /// Unsafe function types preserve their call requirement and signature. |
|
| 2944 | + | @test fn testParseUnsafeFunctionType() throws (testing::TestError) { |
|
| 2945 | + | let node = try! parseTypeStr("unsafe fn(&u32) -> u32 throws (Error)"); |
|
| 2946 | + | let case ast::NodeValue::TypeSig(ast::TypeSig::Fn { sig, isUnsafe }) = node.value |
|
| 2947 | + | else throw testing::TestError::Failed; |
|
| 2948 | + | assert isUnsafe; |
|
| 2949 | + | assert sig.params.len == 1; |
|
| 2950 | + | assert sig.returnType <> nil; |
|
| 2951 | + | assert sig.throwList.len == 1; |
|
| 2952 | + | } |
lib/std/lang/resolver.rad
+91 -12
| 1772 | 1772 | from: types::PointerClass, |
|
| 1773 | 1773 | ) -> bool { |
|
| 1774 | 1774 | return to == from or ( |
|
| 1775 | 1775 | to == types::PointerClass::Owned |
|
| 1776 | 1776 | and from == types::PointerClass::Ref |
|
| 1777 | + | ) or ( |
|
| 1778 | + | to == types::PointerClass::Ref |
|
| 1779 | + | and from == types::PointerClass::Owned |
|
| 1777 | 1780 | ); |
|
| 1778 | 1781 | } |
|
| 1779 | 1782 | ||
| 1780 | 1783 | /// Check if the `from` type is assignable to the `to` type, and return a |
|
| 1781 | 1784 | /// coercion plan if so. |
|
| 1782 | 1785 | fn isAssignable(self: *mut Resolver, to: Type, from: Type, rval: *ast::Node) -> ?Coercion { |
|
| 1786 | + | return isAssignableValue(self, to, from, rval, true); |
|
| 1787 | + | } |
|
| 1788 | + | ||
| 1789 | + | /// Check assignment while preserving function safety in referenced storage. |
|
| 1790 | + | fn isAssignableValue( |
|
| 1791 | + | self: *mut Resolver, to: Type, from: Type, rval: *ast::Node, |
|
| 1792 | + | allowFnSafetyCoercion: bool |
|
| 1793 | + | ) -> ?Coercion { |
|
| 1783 | 1794 | if to == Type::Unknown or from == Type::Unknown { |
|
| 1784 | 1795 | return nil; |
|
| 1785 | 1796 | } |
|
| 1786 | 1797 | if from == Type::Undefined { |
|
| 1787 | 1798 | // TODO: Don't let `undefined` be used in place of functions and other |
| 1810 | 1821 | return Coercion::Identity; |
|
| 1811 | 1822 | } |
|
| 1812 | 1823 | if lhsMutable and not rhsMutable { |
|
| 1813 | 1824 | return nil; |
|
| 1814 | 1825 | } |
|
| 1815 | - | return isAssignable(self, *lhsTarget, *rhsTarget, rval); |
|
| 1826 | + | return isAssignableValue(self, *lhsTarget, *rhsTarget, rval, false); |
|
| 1816 | 1827 | } |
|
| 1817 | 1828 | if let case Type::TraitObject { class: lhsClass, traitInfo: lhsTraitInfo, mutable: lhsMutable } = to { |
|
| 1818 | 1829 | if let case Type::Pointer { class: rhsClass, target: rhsTarget, mutable: rhsMutable } = from { |
|
| 1819 | 1830 | if not pointerClassesAssignable(lhsClass, rhsClass) |
|
| 1820 | 1831 | or (lhsMutable and not rhsMutable) |
| 1848 | 1859 | } |
|
| 1849 | 1860 | // Allow coercion from `*[T]` to `*[opaque]`, and mutable counterparts. |
|
| 1850 | 1861 | if *lhsItem == Type::Opaque { |
|
| 1851 | 1862 | return Coercion::Identity; |
|
| 1852 | 1863 | } |
|
| 1853 | - | return isAssignable(self, *lhsItem, *rhsItem, rval); |
|
| 1864 | + | return isAssignableValue(self, *lhsItem, *rhsItem, rval, false); |
|
| 1854 | 1865 | } |
|
| 1855 | 1866 | match to { |
|
| 1856 | 1867 | case Type::Array(lhs) => { |
|
| 1857 | 1868 | let case Type::Array(rhs) = from |
|
| 1858 | 1869 | else return nil; |
| 1874 | 1885 | return Coercion::Identity; |
|
| 1875 | 1886 | } |
|
| 1876 | 1887 | return nil; |
|
| 1877 | 1888 | } |
|
| 1878 | 1889 | case ast::NodeValue::ArrayRepeatLit(repeat) => { |
|
| 1879 | - | return isAssignable(self, *lhs.item, *rhs.item, repeat.item); |
|
| 1890 | + | return isAssignableValue(self, *lhs.item, *rhs.item, repeat.item, allowFnSafetyCoercion); |
|
| 1880 | 1891 | } |
|
| 1881 | 1892 | else => { |
|
| 1882 | 1893 | if typesEqual(*lhs.item, *rhs.item) { |
|
| 1883 | 1894 | return Coercion::Identity; |
|
| 1884 | 1895 | } |
| 1889 | 1900 | ||
| 1890 | 1901 | case Type::Optional(inner) => { |
|
| 1891 | 1902 | if from == Type::Nil { |
|
| 1892 | 1903 | return Coercion::OptionalLift(to); |
|
| 1893 | 1904 | } |
|
| 1894 | - | if let _ = isAssignable(self, *inner, from, rval) { |
|
| 1905 | + | if let _ = isAssignableValue(self, *inner, from, rval, allowFnSafetyCoercion) { |
|
| 1895 | 1906 | return Coercion::OptionalLift(to); |
|
| 1896 | 1907 | } |
|
| 1897 | 1908 | if let case Type::Optional(fromInner) = from { |
|
| 1898 | - | return isAssignable(self, *inner, *fromInner, rval); |
|
| 1909 | + | return isAssignableValue(self, *inner, *fromInner, rval, allowFnSafetyCoercion); |
|
| 1899 | 1910 | } |
|
| 1900 | 1911 | return nil; |
|
| 1901 | 1912 | } |
|
| 1902 | 1913 | ||
| 1903 | 1914 | case Type::Fn(toInfo) => { |
|
| 1904 | 1915 | // Allow function type structural matching. |
|
| 1905 | 1916 | if let case Type::Fn(fromInfo) = from { |
|
| 1906 | - | if fnTypeEqual(toInfo, fromInfo) { |
|
| 1917 | + | if fnTypeEqual(toInfo, fromInfo) or ( |
|
| 1918 | + | allowFnSafetyCoercion and toInfo.isUnsafe and not fromInfo.isUnsafe |
|
| 1919 | + | and fnSignatureEqual(toInfo, fromInfo) |
|
| 1920 | + | ) { |
|
| 1907 | 1921 | return Coercion::Identity; |
|
| 1908 | 1922 | } |
|
| 1909 | 1923 | } |
|
| 1910 | 1924 | return nil; |
|
| 1911 | 1925 | } |
| 1945 | 1959 | /// Check if two function type descriptors are structurally equivalent. |
|
| 1946 | 1960 | fn fnTypeEqual(a: *FnType, b: *FnType) -> bool { |
|
| 1947 | 1961 | if a.isUnsafe <> b.isUnsafe { |
|
| 1948 | 1962 | return false; |
|
| 1949 | 1963 | } |
|
| 1964 | + | return fnSignatureEqual(a, b); |
|
| 1965 | + | } |
|
| 1966 | + | ||
| 1967 | + | /// Compare parameter, return, and error types of functions. |
|
| 1968 | + | fn fnSignatureEqual(a: *FnType, b: *FnType) -> bool { |
|
| 1950 | 1969 | if a.paramTypes.len <> b.paramTypes.len { |
|
| 1951 | 1970 | return false; |
|
| 1952 | 1971 | } |
|
| 1953 | 1972 | if a.throwList.len <> b.throwList.len { |
|
| 1954 | 1973 | return false; |
| 6295 | 6314 | return false; |
|
| 6296 | 6315 | } |
|
| 6297 | 6316 | } |
|
| 6298 | 6317 | } |
|
| 6299 | 6318 | ||
| 6319 | + | /// Return the storage class of an addressed location. |
|
| 6320 | + | fn addressStorageClass(self: *Resolver, node: *ast::Node) -> types::PointerClass { |
|
| 6321 | + | match node.value { |
|
| 6322 | + | case ast::NodeValue::Ident(_), ast::NodeValue::ScopeAccess(_) => { |
|
| 6323 | + | if let sym = symbolFor(self, node) { |
|
| 6324 | + | match sym.node.value { |
|
| 6325 | + | case ast::NodeValue::StaticDecl(_), ast::NodeValue::ConstDecl(_) => |
|
| 6326 | + | return types::PointerClass::Owned, |
|
| 6327 | + | else => {} |
|
| 6328 | + | } |
|
| 6329 | + | } |
|
| 6330 | + | } |
|
| 6331 | + | case ast::NodeValue::FieldAccess(access) => { |
|
| 6332 | + | if let ty = typeFor(self, access.parent) { |
|
| 6333 | + | if let case Type::Pointer { class, .. } = ty { |
|
| 6334 | + | return class; |
|
| 6335 | + | } |
|
| 6336 | + | } |
|
| 6337 | + | return addressStorageClass(self, access.parent); |
|
| 6338 | + | } |
|
| 6339 | + | case ast::NodeValue::Subscript { container, .. } => { |
|
| 6340 | + | if let ty = typeFor(self, container) { |
|
| 6341 | + | if let case Type::Slice { class, .. } = autoDeref(ty) { |
|
| 6342 | + | return class; |
|
| 6343 | + | } |
|
| 6344 | + | if let case Type::Pointer { class, .. } = ty { |
|
| 6345 | + | return class; |
|
| 6346 | + | } |
|
| 6347 | + | } |
|
| 6348 | + | return addressStorageClass(self, container); |
|
| 6349 | + | } |
|
| 6350 | + | case ast::NodeValue::Deref(target) => { |
|
| 6351 | + | if let ty = typeFor(self, target) { |
|
| 6352 | + | if let case Type::Pointer { class, .. } = ty { |
|
| 6353 | + | return class; |
|
| 6354 | + | } |
|
| 6355 | + | } |
|
| 6356 | + | return addressStorageClass(self, target); |
|
| 6357 | + | } |
|
| 6358 | + | else => {} |
|
| 6359 | + | } |
|
| 6360 | + | return types::PointerClass::Ref; |
|
| 6361 | + | } |
|
| 6362 | + | ||
| 6363 | + | /// Select the pointer class of an address-of expression. Addresses of |
|
| 6364 | + | /// storage reached through an unsafe pointer are unsafe pointers. |
|
| 6365 | + | fn addressClass(self: *mut Resolver, target: *ast::Node, hint: Type) -> types::PointerClass |
|
| 6366 | + | throws (ResolveError) |
|
| 6367 | + | { |
|
| 6368 | + | if isUnsafePointerType(hint) { |
|
| 6369 | + | try requireUnsafe(self, target); |
|
| 6370 | + | return types::PointerClass::Unsafe; |
|
| 6371 | + | } |
|
| 6372 | + | if isRefType(hint) { |
|
| 6373 | + | return types::PointerClass::Ref; |
|
| 6374 | + | } |
|
| 6375 | + | if addressStorageClass(self, target) == types::PointerClass::Unsafe { |
|
| 6376 | + | return types::PointerClass::Unsafe; |
|
| 6377 | + | } |
|
| 6378 | + | return types::PointerClass::Owned; |
|
| 6379 | + | } |
|
| 6380 | + | ||
| 6300 | 6381 | /// Analyze an address-of expression. |
|
| 6301 | 6382 | fn resolveAddressOf(self: *mut Resolver, node: *ast::Node, addr: ast::AddressOf, hint: Type) -> Type |
|
| 6302 | 6383 | throws (ResolveError) |
|
| 6303 | 6384 | { |
|
| 6304 | - | // Checked and unsafe pointer contexts require a reference source. |
|
| 6305 | - | let class = types::PointerClass::Ref |
|
| 6306 | - | if isRefType(hint) or isUnsafePointerType(hint) |
|
| 6307 | - | else types::PointerClass::Owned; |
|
| 6308 | 6385 | if addr.mutable { |
|
| 6309 | 6386 | if not try canBorrowMutFrom(self, addr.target) { |
|
| 6310 | 6387 | throw emitError(self, addr.target, ErrorKind::ImmutableBinding); |
|
| 6311 | 6388 | } |
|
| 6312 | 6389 | } |
| 6335 | 6412 | else => { |
|
| 6336 | 6413 | throw emitError(self, container, ErrorKind::ExpectedIndexable); |
|
| 6337 | 6414 | } |
|
| 6338 | 6415 | } |
|
| 6339 | 6416 | } |
|
| 6417 | + | let class = try addressClass(self, addr.target, hint); |
|
| 6340 | 6418 | let sliceTy = Type::Slice { class, item, mutable: addr.mutable }; |
|
| 6341 | 6419 | let alloc = allocType(self, sliceTy); |
|
| 6342 | 6420 | setSliceRangeInfo(self, node, SliceRangeInfo { |
|
| 6343 | 6421 | itemType: item, |
|
| 6344 | 6422 | mutable: addr.mutable, |
| 6352 | 6430 | let mut targetHint: Type = Type::Unknown; |
|
| 6353 | 6431 | if let case Type::Slice { item, .. } = hint { |
|
| 6354 | 6432 | set targetHint = Type::Array(ArrayType { item, length: 0 }); |
|
| 6355 | 6433 | } |
|
| 6356 | 6434 | let targetTy = try visit(self, addr.target, targetHint); |
|
| 6435 | + | let class = try addressClass(self, addr.target, hint); |
|
| 6357 | 6436 | ||
| 6358 | 6437 | // Mark local variable symbols as address-taken so the lowerer |
|
| 6359 | 6438 | // allocates a stack slot eagerly. |
|
| 6360 | 6439 | if let case ast::NodeValue::Ident(name) = addr.target.value { |
|
| 6361 | 6440 | if let sym = findValueSymbol(self.scope, name) { |
| 7197 | 7276 | } |
|
| 7198 | 7277 | } |
|
| 7199 | 7278 | let nominalTy = allocNominalType(self, NominalType::Record(recordType)); |
|
| 7200 | 7279 | return Type::Nominal(nominalTy); |
|
| 7201 | 7280 | } |
|
| 7202 | - | case ast::TypeSig::Fn(t) => { |
|
| 7281 | + | case ast::TypeSig::Fn { sig: t, isUnsafe } => { |
|
| 7203 | 7282 | let a = alloc::arenaAllocator(&mut self.arena); |
|
| 7204 | 7283 | let mut paramTypes: *mut [*Type] = &mut []; |
|
| 7205 | 7284 | let mut throwList: *mut [*Type] = &mut []; |
|
| 7206 | 7285 | ||
| 7207 | 7286 | if t.params.len > MAX_FN_PARAMS { |
| 7234 | 7313 | } |
|
| 7235 | 7314 | let fnType = FnType { |
|
| 7236 | 7315 | paramTypes: ¶mTypes[..], |
|
| 7237 | 7316 | returnType: retType, |
|
| 7238 | 7317 | throwList: &throwList[..], |
|
| 7239 | - | isUnsafe: false, |
|
| 7318 | + | isUnsafe, |
|
| 7240 | 7319 | localCount: 0, |
|
| 7241 | 7320 | }; |
|
| 7242 | 7321 | return Type::Fn(allocFnType(self, fnType)); |
|
| 7243 | 7322 | } |
|
| 7244 | 7323 | // Resolve an opaque trait object signature. |
lib/std/lang/resolver/tests.rad
+54 -0
| 5483 | 5483 | let mut a = testResolver(); |
|
| 5484 | 5484 | let program = "record Marker: Once {} record Value { number: u32 } trait Read { unsafe fn (&Read) get() -> u32; } instance Read for Value { unsafe fn (value: &Value) get() -> u32 { return value.number; } } fn inspect(object: &opaque Read) -> u32 { return object.get(); }"; |
|
| 5485 | 5485 | let result = try resolveProgramStr(&mut a, program); |
|
| 5486 | 5486 | try expectErrorKind(&result, super::ErrorKind::UnsafeCall); |
|
| 5487 | 5487 | } |
|
| 5488 | + | ||
| 5489 | + | /// An unsafe callback retains its requirement at an indirect call. |
|
| 5490 | + | @test fn testUnsafeCallbackCallRejected() throws (testing::TestError) { |
|
| 5491 | + | let mut a = testResolver(); |
|
| 5492 | + | let result = try resolveProgramStr(&mut a, |
|
| 5493 | + | "fn run(callback: unsafe fn() -> u32) -> u32 { return callback(); }"); |
|
| 5494 | + | try expectErrorKind(&result, super::ErrorKind::UnsafeCall); |
|
| 5495 | + | } |
|
| 5496 | + | ||
| 5497 | + | /// An unsafe function cannot enter a safe callback slot. |
|
| 5498 | + | @test fn testUnsafeCallbackAssignmentRejected() throws (testing::TestError) { |
|
| 5499 | + | let mut a = testResolver(); |
|
| 5500 | + | let result = try resolveProgramStr(&mut a, |
|
| 5501 | + | "unsafe fn load() -> u32 { return 1; } fn run() { let callback: fn() -> u32 = load; }"); |
|
| 5502 | + | let err = try expectError(&result); |
|
| 5503 | + | let case super::ErrorKind::TypeMismatch(_) = err.kind |
|
| 5504 | + | else throw testing::TestError::Failed; |
|
| 5505 | + | } |
|
| 5506 | + | ||
| 5507 | + | /// A safe function can enter an unsafe callback slot. |
|
| 5508 | + | @test fn testSafeCallbackIntoUnsafeSlot() throws (testing::TestError) { |
|
| 5509 | + | try expectAnalyzeOk( |
|
| 5510 | + | "fn load() -> u32 { return 1; } unsafe fn run() -> u32 { let callback: unsafe fn() -> u32 = load; return callback(); }"); |
|
| 5511 | + | } |
|
| 5512 | + | ||
| 5513 | + | /// Borrowed callback storage must preserve its function safety type. |
|
| 5514 | + | @test fn testBorrowedCallbackSafetyInvariant() throws (testing::TestError) { |
|
| 5515 | + | let mut a = testResolver(); |
|
| 5516 | + | let result = try resolveProgramStr(&mut a, |
|
| 5517 | + | "fn replace(slot: &mut unsafe fn()) {} fn load() {} fn run() { let mut callback: fn() = load; replace(&mut callback); }"); |
|
| 5518 | + | let err = try expectError(&result); |
|
| 5519 | + | let case super::ErrorKind::TypeMismatch(_) = err.kind |
|
| 5520 | + | else throw testing::TestError::Failed; |
|
| 5521 | + | } |
|
| 5522 | + | ||
| 5523 | + | /// Stack values can be borrowed for a call. |
|
| 5524 | + | @test fn testStackBorrowAllowed() throws (testing::TestError) { |
|
| 5525 | + | try expectAnalyzeOk("fn read(value: &u32) -> u32 { return *value; } fn run() -> u32 { let value: u32 = 7; return read(&value); }"); |
|
| 5526 | + | try expectAnalyzeOk("fn write(values: &mut [u32]) { set values[0] = 7; } fn run() { let mut values: [u32; 2] = [1, 2]; write(&mut values[..]); }"); |
|
| 5527 | + | } |
|
| 5528 | + | ||
| 5529 | + | /// Unsafe declarations can store raw pointers to stack values. |
|
| 5530 | + | @test fn testUnsafeStackPointerAllowed() throws (testing::TestError) { |
|
| 5531 | + | try expectAnalyzeOk("unsafe fn run() { let mut value: u32 = 0; let pointer: *unsafe mut u32 = &mut value as *unsafe mut u32; set *pointer = 7; }"); |
|
| 5532 | + | try expectAnalyzeOk("unsafe fn run() { let mut values: [u32; 2] = [1, 2]; let slice: *unsafe mut [u32] = &mut values[..] as *unsafe mut [u32]; set slice[0] = 7; }"); |
|
| 5533 | + | try expectAnalyzeOk("unsafe fn run() { let value: u32 = 7; let pointer: *unsafe u32 = &value; }"); |
|
| 5534 | + | try expectAnalyzeOk("unsafe fn run() { let values: [u32; 2] = [1, 2]; let slice: *unsafe [u32] = &values[..]; }"); |
|
| 5535 | + | } |
|
| 5536 | + | ||
| 5537 | + | /// Safe stored pointers can be passed where a borrow is expected. |
|
| 5538 | + | @test fn testOwnedPointerCoercesToBorrow() throws (testing::TestError) { |
|
| 5539 | + | try expectAnalyzeOk("fn read(value: &u32) -> u32 { return *value; } fn run(value: *u32) -> u32 { return read(value); }"); |
|
| 5540 | + | try expectAnalyzeOk("fn len(values: &[u8]) -> u32 { return values.len; } fn run() -> u32 { return len(\"abc\"); }"); |
|
| 5541 | + | } |
test/tests/fn.unsafe.callbacks.rad
added
+30 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | ||
| 3 | + | mod operations; |
|
| 4 | + | ||
| 5 | + | record Callback: Copy { |
|
| 6 | + | /// Function called with a raw pointer to a live integer. |
|
| 7 | + | invoke: unsafe fn(*unsafe u32) -> u32, |
|
| 8 | + | } |
|
| 9 | + | ||
| 10 | + | /// Call a safe function from a module that also exports unsafe functions. |
|
| 11 | + | fn readBorrow(value: &u32) -> u32 { |
|
| 12 | + | return operations::readBorrow(value); |
|
| 13 | + | } |
|
| 14 | + | ||
| 15 | + | /// Return a constant without reading the pointer. |
|
| 16 | + | fn constantValue(value: *unsafe u32) -> u32 { |
|
| 17 | + | return 7; |
|
| 18 | + | } |
|
| 19 | + | ||
| 20 | + | @default unsafe fn main() -> i32 { |
|
| 21 | + | let value: u32 = 42; |
|
| 22 | + | assert readBorrow(&value) == 42; |
|
| 23 | + | ||
| 24 | + | let raw = Callback { invoke: operations::readRaw }; |
|
| 25 | + | assert raw.invoke(&value) == 42; |
|
| 26 | + | ||
| 27 | + | let checked = Callback { invoke: constantValue }; |
|
| 28 | + | assert checked.invoke(&value) == 7; |
|
| 29 | + | return 0; |
|
| 30 | + | } |
test/tests/fn.unsafe.callbacks/operations.rad
added
+9 -0
| 1 | + | /// Read an integer through a checked borrow. |
|
| 2 | + | export fn readBorrow(value: &u32) -> u32 { |
|
| 3 | + | return *value; |
|
| 4 | + | } |
|
| 5 | + | ||
| 6 | + | /// Read an integer through a raw pointer that must remain valid for the call. |
|
| 7 | + | export unsafe fn readRaw(value: *unsafe u32) -> u32 { |
|
| 8 | + | return *value; |
|
| 9 | + | } |
test/tests/pointer.stack.borrow.rad
added
+38 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | ||
| 3 | + | /// Permanent values used by stored pointers. |
|
| 4 | + | static VALUES: [u32; 3] = [3, 5, 7]; |
|
| 5 | + | ||
| 6 | + | /// Add a value to a stack location during a call. |
|
| 7 | + | fn increment(value: &mut u32, amount: u32) { |
|
| 8 | + | set *value += amount; |
|
| 9 | + | } |
|
| 10 | + | ||
| 11 | + | /// Sum borrowed values during a call. |
|
| 12 | + | fn sum(values: &[u32]) -> u32 { |
|
| 13 | + | let mut result: u32 = 0; |
|
| 14 | + | for value in values { |
|
| 15 | + | set result += value; |
|
| 16 | + | } |
|
| 17 | + | return result; |
|
| 18 | + | } |
|
| 19 | + | ||
| 20 | + | /// Return a slice backed by permanent storage. |
|
| 21 | + | fn values() -> *[u32] { |
|
| 22 | + | return &VALUES[1..]; |
|
| 23 | + | } |
|
| 24 | + | ||
| 25 | + | /// Exercise stack borrows and stored pointers to permanent data. |
|
| 26 | + | @default fn main() -> i32 { |
|
| 27 | + | let mut value: u32 = 4; |
|
| 28 | + | increment(&mut value, 6); |
|
| 29 | + | if value <> 10 { return 1; } |
|
| 30 | + | ||
| 31 | + | let local: [u32; 3] = [value, 2, 3]; |
|
| 32 | + | if sum(&local[..]) <> 15 { return 2; } |
|
| 33 | + | if sum(values()) <> 12 { return 3; } |
|
| 34 | + | ||
| 35 | + | let pointer: *u32 = &VALUES[0]; |
|
| 36 | + | if *pointer <> 3 { return 4; } |
|
| 37 | + | return 0; |
|
| 38 | + | } |
test/tests/pointer.stack.unsafe.rad
added
+18 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | ||
| 3 | + | /// Exercise raw pointers while their stack storage is alive. |
|
| 4 | + | @default unsafe fn main() -> i32 { |
|
| 5 | + | let mut value: u32 = 4; |
|
| 6 | + | let pointer: *unsafe mut u32 = &mut value; |
|
| 7 | + | set *pointer = 9; |
|
| 8 | + | if value <> 9 { return 1; } |
|
| 9 | + | ||
| 10 | + | let mut values: [u32; 2] = [value, 2]; |
|
| 11 | + | let slice: *unsafe mut [u32] = &mut values[..]; |
|
| 12 | + | set slice[1] = 7; |
|
| 13 | + | if values[1] <> 7 { return 2; } |
|
| 14 | + | ||
| 15 | + | let cast = &value as *unsafe u32; |
|
| 16 | + | if *cast <> 9 { return 3; } |
|
| 17 | + | return 0; |
|
| 18 | + | } |