Allow identity casts
b5aaa5b81b60d5c6846fa734a2c0ac73c468b23fbfe345cf0498af9d32c0399e
There are cases where this helps type inference.
1 parent
08a54eea
lib/std/lang/resolver.rad
+4 -0
| 5600 | 5600 | return false; |
|
| 5601 | 5601 | } |
|
| 5602 | 5602 | ||
| 5603 | 5603 | /// Check if an `as` cast between two types is valid. |
|
| 5604 | 5604 | fn isValidCast(source: Type, target: Type) -> bool { |
|
| 5605 | + | // Allow identity casts. |
|
| 5606 | + | if source == target { |
|
| 5607 | + | return true; |
|
| 5608 | + | } |
|
| 5605 | 5609 | // Allow numeric to numeric. |
|
| 5606 | 5610 | if isNumericType(source) and isNumericType(target) { |
|
| 5607 | 5611 | return true; |
|
| 5608 | 5612 | } |
|
| 5609 | 5613 | // Allow `void` union to numeric. |
lib/std/lang/resolver/tests.rad
+14 -0
| 4456 | 4456 | } { // *[*u8] to *[*i32] |
|
| 4457 | 4457 | let mut a = testResolver(); |
|
| 4458 | 4458 | let result = try resolveBlockStr(&mut a, "let s: *[*u8] = undefined; s as *[*i32];"); |
|
| 4459 | 4459 | try expectNoErrors(&result); |
|
| 4460 | 4460 | } |
|
| 4461 | + | ||
| 4462 | + | { // Identity cast: *mut [i32] to *mut [i32]. |
|
| 4463 | + | let mut a = testResolver(); |
|
| 4464 | + | let result = try resolveBlockStr(&mut a, "let s: *mut [i32] = undefined; s as *mut [i32];"); |
|
| 4465 | + | try expectNoErrors(&result); |
|
| 4466 | + | } { // Identity cast: *i32 to *i32. |
|
| 4467 | + | let mut a = testResolver(); |
|
| 4468 | + | let result = try resolveBlockStr(&mut a, "let p: *i32 = undefined; p as *i32;"); |
|
| 4469 | + | try expectNoErrors(&result); |
|
| 4470 | + | } { // Identity cast: i32 to i32. |
|
| 4471 | + | let mut a = testResolver(); |
|
| 4472 | + | let result = try resolveBlockStr(&mut a, "let x: i32 = 0; x as i32;"); |
|
| 4473 | + | try expectNoErrors(&result); |
|
| 4474 | + | } |
|
| 4461 | 4475 | } |
|
| 4462 | 4476 | ||
| 4463 | 4477 | /// Tests for invalid `as` casts that should be rejected. |
|
| 4464 | 4478 | @test fn testResolveAsCastsInvalid() throws (testing::TestError) { |
|
| 4465 | 4479 | { // Pointer to slice is invalid. |