lang: Make composites affine by default
52c552f9458db47807a15bd005a6c10805e3ad06a529b5ae59a044cea3655ee5
1 parent
0ed90d38
lib/std/lang/resolver.rad
+2 -4
| 2040 | 2040 | /// Return whether `ty` may be duplicated implicitly. |
|
| 2041 | 2041 | export fn isCopy(ty: Type) -> bool { |
|
| 2042 | 2042 | match ty { |
|
| 2043 | 2043 | case Type::Array(array) => return isCopy(*array.item), |
|
| 2044 | 2044 | case Type::Optional(inner) => return isCopy(*inner), |
|
| 2045 | - | // Records and unions are copyable unless linear. The `Copy` marker is |
|
| 2046 | - | // recorded and validated but not yet required. |
|
| 2047 | - | case Type::Nominal(NominalType::Record(_)) => return not isLinear(ty), |
|
| 2048 | - | case Type::Nominal(NominalType::Union(_)) => return not isLinear(ty), |
|
| 2045 | + | case Type::Nominal(NominalType::Record(recInfo)) => return recInfo.declaredCopy, |
|
| 2046 | + | case Type::Nominal(NominalType::Union(unionType)) => return unionType.declaredCopy, |
|
| 2049 | 2047 | case Type::Nominal(NominalType::Placeholder(_)) => return false, |
|
| 2050 | 2048 | else => return true, |
|
| 2051 | 2049 | } |
|
| 2052 | 2050 | } |
|
| 2053 | 2051 |
lib/std/lang/resolver/tests.rad
+16 -0
| 5250 | 5250 | @test fn testAffineCompositeMayBeDiscarded() throws (testing::TestError) { |
|
| 5251 | 5251 | let program = "record Value { number: u32 } fn run() { let value = Value { number: 1 }; }"; |
|
| 5252 | 5252 | try expectAnalyzeOk(program); |
|
| 5253 | 5253 | } |
|
| 5254 | 5254 | ||
| 5255 | + | /// A by-value use moves an unmarked composite value. |
|
| 5256 | + | @test fn testAffineCompositeUseAfterMoveRejected() throws (testing::TestError) { |
|
| 5257 | + | let mut a = testResolver(); |
|
| 5258 | + | let program = "record Value { number: u32 } fn take(value: Value) {} fn run() { let value = Value { number: 1 }; take(value); take(value); }"; |
|
| 5259 | + | let result = try resolveProgramStr(&mut a, program); |
|
| 5260 | + | try expectErrorKind(&result, super::ErrorKind::AffineUseAfterMove("value")); |
|
| 5261 | + | } |
|
| 5262 | + | ||
| 5255 | 5263 | /// Affine values may move on only one branch when not used later. |
|
| 5256 | 5264 | @test fn testAffineConditionalMoveMayBeDiscarded() throws (testing::TestError) { |
|
| 5257 | 5265 | let program = "record Value { number: u32 } fn take(value: Value) {} fn run(condition: bool) { let value = Value { number: 1 }; if condition { take(value); } }"; |
|
| 5258 | 5266 | try expectAnalyzeOk(program); |
|
| 5259 | 5267 | } |
| 5262 | 5270 | @test fn testCopyCompositeMayBeReused() throws (testing::TestError) { |
|
| 5263 | 5271 | let program = "record Value: Copy { number: u32 } fn take(value: Value) {} fn run() { let value = Value { number: 1 }; take(value); take(value); }"; |
|
| 5264 | 5272 | try expectAnalyzeOk(program); |
|
| 5265 | 5273 | } |
|
| 5266 | 5274 | ||
| 5275 | + | /// A `Copy` composite may contain only copy values. |
|
| 5276 | + | @test fn testCopyCompositeRejectsAffineField() throws (testing::TestError) { |
|
| 5277 | + | let mut a = testResolver(); |
|
| 5278 | + | let program = "record Inner { number: u32 } record Outer: Copy { inner: Inner }"; |
|
| 5279 | + | let result = try resolveProgramStr(&mut a, program); |
|
| 5280 | + | try expectErrorKind(&result, super::ErrorKind::CopyContainsNonCopy); |
|
| 5281 | + | } |
|
| 5282 | + | ||
| 5267 | 5283 | /// A composite cannot carry conflicting ownership markers. |
|
| 5268 | 5284 | @test fn testConflictingOwnershipMarkersRejected() throws (testing::TestError) { |
|
| 5269 | 5285 | let mut a = testResolver(); |
|
| 5270 | 5286 | let program = "record Value: Copy + Linear { number: u32 }"; |
|
| 5271 | 5287 | let result = try resolveProgramStr(&mut a, program); |