Restrict safe pointers to permanent storage
110c985e41dc66dd9cc2b8ccf86c294fb5bd4f0d87197b85ab70c39cef038523
Use borrows for call-scoped stack access and explicit unsafe pointers for retained storage. Migrate compiler, library, and test callers to the storage rules. Scope unsafe operations to individual functions and preserve call requirements in unsafe function types. Refresh the compiler seed and bootstrap inputs. Assisted-by: Codex:gpt-6-astra
1 parent
decca497
lib/std/lang/resolver.rad
+9 -8
| 1770 | 1770 | fn pointerClassesAssignable( |
|
| 1771 | 1771 | to: types::PointerClass, |
|
| 1772 | 1772 | from: types::PointerClass, |
|
| 1773 | 1773 | ) -> bool { |
|
| 1774 | 1774 | return to == from or ( |
|
| 1775 | - | to == types::PointerClass::Owned |
|
| 1776 | - | and from == types::PointerClass::Ref |
|
| 1777 | - | ) or ( |
|
| 1778 | 1775 | to == types::PointerClass::Ref |
|
| 1779 | 1776 | and from == types::PointerClass::Owned |
|
| 1780 | 1777 | ); |
|
| 1781 | 1778 | } |
|
| 1782 | 1779 |
| 6352 | 6349 | else => {} |
|
| 6353 | 6350 | } |
|
| 6354 | 6351 | return types::PointerClass::Ref; |
|
| 6355 | 6352 | } |
|
| 6356 | 6353 | ||
| 6357 | - | /// Select the pointer class of an address-of expression. Addresses of |
|
| 6358 | - | /// storage reached through an unsafe pointer are unsafe pointers. |
|
| 6354 | + | /// Select an address type without extending the target storage lifetime. |
|
| 6359 | 6355 | fn addressClass(self: &mut Resolver, target: *ast::Node, hint: Type) -> types::PointerClass |
|
| 6360 | 6356 | throws (ResolveError) |
|
| 6361 | 6357 | { |
|
| 6362 | 6358 | if isUnsafePointerType(hint) { |
|
| 6363 | 6359 | try requireUnsafe(self, target); |
|
| 6364 | 6360 | return types::PointerClass::Unsafe; |
|
| 6365 | 6361 | } |
|
| 6366 | 6362 | if isRefType(hint) { |
|
| 6367 | 6363 | return types::PointerClass::Ref; |
|
| 6368 | 6364 | } |
|
| 6369 | - | if addressStorageClass(self, target) == types::PointerClass::Unsafe { |
|
| 6370 | - | return types::PointerClass::Unsafe; |
|
| 6365 | + | match target.value { |
|
| 6366 | + | case ast::NodeValue::ArrayLit(_), ast::NodeValue::ArrayRepeatLit(_) => { |
|
| 6367 | + | if isConstExpr(self, target) { |
|
| 6368 | + | return types::PointerClass::Owned; |
|
| 6369 | + | } |
|
| 6370 | + | } |
|
| 6371 | + | else => {} |
|
| 6371 | 6372 | } |
|
| 6372 | - | return types::PointerClass::Owned; |
|
| 6373 | + | return addressStorageClass(self, target); |
|
| 6373 | 6374 | } |
|
| 6374 | 6375 | ||
| 6375 | 6376 | /// Analyze an address-of expression. |
|
| 6376 | 6377 | unsafe fn resolveAddressOf(self: &mut Resolver, node: *ast::Node, addr: ast::AddressOf, hint: Type) -> Type |
|
| 6377 | 6378 | throws (ResolveError) |
lib/std/lang/resolver/tests.rad
+32 -0
| 5302 | 5302 | let program = "record Token: Once + Once { value: u32 }"; |
|
| 5303 | 5303 | let result = try resolveProgramStr(&mut a, program); |
|
| 5304 | 5304 | try expectErrorKind(&result, super::ErrorKind::DuplicateBinding("Once")); |
|
| 5305 | 5305 | } |
|
| 5306 | 5306 | ||
| 5307 | + | /// Stack storage cannot produce a safe stored pointer or slice. |
|
| 5308 | + | @test unsafe fn testStackPointerRejected() throws (testing::TestError) { |
|
| 5309 | + | let programs = &[ |
|
| 5310 | + | "fn run() { let value: u32 = 0; let pointer: *u32 = &value; }", |
|
| 5311 | + | "fn run() -> *u32 { let value: u32 = 0; return &value; }", |
|
| 5312 | + | "fn run(value: u32) -> *u32 { return &value; }", |
|
| 5313 | + | "record Cell { value: u32 } fn run() { let cell = Cell { value: 1 }; let pointer: *u32 = &cell.value; }", |
|
| 5314 | + | "fn run() { let values = [1, 2]; let pointer: *i32 = &values[0]; }", |
|
| 5315 | + | "fn run() { let values = [1, 2]; let slice: *[i32] = &values[..]; }", |
|
| 5316 | + | "fn run(value: i32) { let slice: *[i32] = &[value]; }", |
|
| 5317 | + | "fn run(value: &u32) -> *u32 { return value; }", |
|
| 5318 | + | "fn run(value: &u32) -> *u32 { return &*value; }", |
|
| 5319 | + | "fn run(values: &[u32]) -> *[u32] { return values; }", |
|
| 5320 | + | "fn run(values: &[u32]) -> *[u32] { return &values[..]; }", |
|
| 5321 | + | "fn run(values: &[u32]) -> *u32 { return values.ptr; }", |
|
| 5322 | + | "fn run() -> *i32 { return &[1, 2][0]; }", |
|
| 5323 | + | "fn run() -> *[i32] { return &[1, 2][..]; }", |
|
| 5324 | + | "fn run(value: *u32) -> **u32 { return &value; }", |
|
| 5325 | + | "fn run(values: *[u32]) -> *u32 { return &values.len; }", |
|
| 5326 | + | "unsafe fn run(value: *unsafe u32) -> *u32 { return &*value; }", |
|
| 5327 | + | "fn run() { let value: u32 = 7; let pointer: *unsafe u32 = &value; }", |
|
| 5328 | + | "record Cell { value: u32 } fn run(value: &Cell) -> *u32 { return &value.value; }", |
|
| 5329 | + | "record Cell { value: *u32 } fn run(value: &u32) -> Cell { return Cell { value }; }", |
|
| 5330 | + | "fn run(value: &u32) -> *[u32] { return @sliceOf(value, 1); }", |
|
| 5331 | + | ]; |
|
| 5332 | + | for program in programs { |
|
| 5333 | + | let mut resolver = testResolver(); |
|
| 5334 | + | let result = try resolveProgramStr(&mut resolver, program); |
|
| 5335 | + | let _ = try expectError(&result); |
|
| 5336 | + | } |
|
| 5337 | + | } |
|
| 5338 | + | ||
| 5307 | 5339 | /// Stack values can be borrowed for a call. |
|
| 5308 | 5340 | @test unsafe fn testStackBorrowAllowed() throws (testing::TestError) { |
|
| 5309 | 5341 | try expectAnalyzeOk("fn read(value: &u32) -> u32 { return *value; } fn run() -> u32 { let value: u32 = 7; return read(&value); }"); |
|
| 5310 | 5342 | try expectAnalyzeOk("fn write(values: &mut [u32]) { set values[0] = 7; } fn run() { let mut values: [u32; 2] = [1, 2]; write(&mut values[..]); }"); |
|
| 5311 | 5343 | } |