resolver: Reject duplicate union discriminants
becd39748c5fbe01271f305207a92bf6d6ab23d4f6264617f84c59febca4a86d
Distinct union variants must use distinct tags so that matching a value selects the correct payload. Assisted-by: Codex:gpt-6
1 parent
7a1d3e52
lib/std/lang/resolver.rad
+7 -0
| 509 | 509 | ||
| 510 | 510 | /// High-level classification for semantic diagnostics. |
|
| 511 | 511 | export union ErrorKind: Copy { |
|
| 512 | 512 | /// Identifier declared more than once in the same scope. |
|
| 513 | 513 | DuplicateBinding(*[u8]), |
|
| 514 | + | /// Union variants have the same discriminant. |
|
| 515 | + | DuplicateDiscriminant, |
|
| 514 | 516 | /// Identifier referenced before it was declared. |
|
| 515 | 517 | UnresolvedSymbol(*[u8]), |
|
| 516 | 518 | /// A function signature or constant depends on itself. |
|
| 517 | 519 | DeclarationCycle(*[u8]), |
|
| 518 | 520 | /// Attempted to assign to an immutable binding. |
| 5107 | 5109 | }); |
|
| 5108 | 5110 | ||
| 5109 | 5111 | assert decl.variants.len <= MAX_UNION_VARIANTS, |
|
| 5110 | 5112 | "resolveUnionBody: maximum union variants exceeded"; |
|
| 5111 | 5113 | let mut iota: u32 = 0; |
|
| 5114 | + | let mut tags: [bool; 256] = [false; 256]; |
|
| 5112 | 5115 | for variantNode, i in decl.variants { |
|
| 5113 | 5116 | let case ast::NodeValue::UnionDeclVariant(variantDecl) = variantNode.value |
|
| 5114 | 5117 | else panic "resolveUnionBody: invalid union variant"; |
|
| 5115 | 5118 | let variantName = try nodeName(self, variantDecl.name); |
|
| 5116 | 5119 | // Resolve the variant's payload type if present. |
| 5129 | 5132 | } |
|
| 5130 | 5133 | } else if iota > U8_MAX as u32 { |
|
| 5131 | 5134 | throw emitError(self, variantNode, ErrorKind::NumericLiteralOverflow); |
|
| 5132 | 5135 | } |
|
| 5133 | 5136 | let tag = variantTag(variantDecl, &mut iota); |
|
| 5137 | + | if tags[tag] { |
|
| 5138 | + | throw emitError(self, variantNode, ErrorKind::DuplicateDiscriminant); |
|
| 5139 | + | } |
|
| 5140 | + | set tags[tag] = true; |
|
| 5134 | 5141 | // Create a symbol for this variant. |
|
| 5135 | 5142 | let data = SymbolData::Variant { type: variantType, decl: node, ordinal: i, index: tag }; |
|
| 5136 | 5143 | let variantSym = allocSymbol(self, data, variantName, variantNode, 0); |
|
| 5137 | 5144 | ||
| 5138 | 5145 | try! variants.append( |
lib/std/lang/resolver/printer.rad
+3 -0
| 659 | 659 | io::print("a composite cannot be both `Copy` and `Once`"); |
|
| 660 | 660 | } |
|
| 661 | 661 | case super::ErrorKind::InvalidRefPosition => { |
|
| 662 | 662 | io::print("reference type is only allowed as a parameter or local binding"); |
|
| 663 | 663 | } |
|
| 664 | + | case super::ErrorKind::DuplicateDiscriminant => { |
|
| 665 | + | io::print("duplicate union discriminant"); |
|
| 666 | + | } |
|
| 664 | 667 | case super::ErrorKind::RefBinding => { |
|
| 665 | 668 | io::print("reference local requires an immutable binding to existing storage"); |
|
| 666 | 669 | } |
|
| 667 | 670 | case super::ErrorKind::BorrowConflict(name) => { |
|
| 668 | 671 | printQuoted("conflicting borrow of '", name); |
lib/std/lang/resolver/tests/core.rad
+1 -1
| 316 | 316 | "union U { A, B(*u8) } @default fn main(a: *mut U) -> i32 { return 0; }", |
|
| 317 | 317 | "union U { A(u32), B(*u8) } @default fn main(a: *mut U) -> i32 { return 0; }", |
|
| 318 | 318 | "union U { A(?*u8), B(*u8) } @default fn main(a: *mut U) -> i32 { return 0; }", |
|
| 319 | 319 | "union U { A, B = 5 } @default fn main(a: *mut U) -> i32 { return 0; }", |
|
| 320 | 320 | "union U { A = 0b10, B = 0 } @default fn main(a: *mut U) -> i32 { return 0; }", |
|
| 321 | - | "union U { A = 1, B = 0, C(*u8) } @default fn main(a: *mut U) -> i32 { return 0; }", |
|
| 321 | + | "union U { A = 2, B = 0, C(*u8) } @default fn main(a: *mut U) -> i32 { return 0; }", |
|
| 322 | 322 | // Records of zero-valid fields, nested records, and mixed storage. |
|
| 323 | 323 | "record R { ok: u8, flag: bool } @default fn main(a: *mut [R; 4]) -> i32 { return 0; }", |
|
| 324 | 324 | "record Entry { key: *[u8], next: ?*Entry, live: bool } @default fn main(a: *mut [Entry; 4]) -> i32 { return 0; }", |
|
| 325 | 325 | "record In { a: [u8; 4], b: ?*u8 } record Out { i: In, j: [In; 2], s: *[u8] } @default fn main(a: *mut Out) -> i32 { return 0; }", |
|
| 326 | 326 | "union V { Off, On(u32) } record Storage { ast: [u8; 64], table: [*[u8]; 16], entries: [?*u8; 8], flags: [bool; 8], mode: V } @default fn main(env: u64, s: *mut Storage) -> i32 { return 0; }", |
lib/std/lang/resolver/tests/structure.rad
+14 -0
| 75 | 75 | let mut d = testResolver(); |
|
| 76 | 76 | let wide = try resolveProgramStr(&mut d, "union R { A = 4294967296 } R::A;"); |
|
| 77 | 77 | try expectErrorKind(&wide, resolver::ErrorKind::NumericLiteralOverflow); |
|
| 78 | 78 | } |
|
| 79 | 79 | ||
| 80 | + | /// Union variants must have distinct discriminants. |
|
| 81 | + | @test unsafe fn testResolveUnionDuplicateDiscriminants() throws (testing::TestError) { |
|
| 82 | + | let programs = &[ |
|
| 83 | + | "union R { A(u64), B = 0 }", |
|
| 84 | + | "union R { A = 2, B = 2 }", |
|
| 85 | + | "union R { A = 2, B, C = 3 }", |
|
| 86 | + | ]; |
|
| 87 | + | for program in programs { |
|
| 88 | + | let mut a = testResolver(); |
|
| 89 | + | let result = try resolveProgramStr(&mut a, program); |
|
| 90 | + | try expectError(&result); |
|
| 91 | + | } |
|
| 92 | + | } |
|
| 93 | + | ||
| 80 | 94 | @test unsafe fn testResolveUnionVariantPayloadTypeMismatch() throws (testing::TestError) { |
|
| 81 | 95 | let mut a = testResolver(); |
|
| 82 | 96 | let program = "union R { Ok(i32), Error(bool) } R::Ok(true);"; |
|
| 83 | 97 | let result = try resolveProgramStr(&mut a, program); |
|
| 84 | 98 | let err = try expectError(&result); |