lang: Check union discriminant range

74d917cd44d953ddbea062e41360244a9befa47e1c93f6261c5fae17555abe00
Reject explicit and implicit discriminants that do not fit in the
one-byte union tag.

Assisted-by: Codex:gpt-6
Alexis Sellier committed ago 1 parent cfcb131a
lib/std/lang/resolver.rad +9 -0
5119 5119
            set variantType = try infer(self, typeNode);
5120 5120
            try ensureStorableType(self, typeNode, variantType);
5121 5121
        }
5122 5122
        // Process the variant's explicit discriminant value if present.
5123 5123
        try visitOptional(self, variantDecl.value, variantType);
5124 +
        if let valueNode = variantDecl.value {
5125 +
            let case ast::NodeValue::Number(lit) = valueNode.value
5126 +
                else panic "resolveUnionBody: expected number literal";
5127 +
            if lit.magnitude > U8_MAX as u64 {
5128 +
                throw emitError(self, valueNode, ErrorKind::NumericLiteralOverflow);
5129 +
            }
5130 +
        } else if iota > U8_MAX as u32 {
5131 +
            throw emitError(self, variantNode, ErrorKind::NumericLiteralOverflow);
5132 +
        }
5124 5133
        let tag = variantTag(variantDecl, &mut iota);
5125 5134
        // Create a symbol for this variant.
5126 5135
        let data = SymbolData::Variant { type: variantType, decl: node, ordinal: i, index: tag };
5127 5136
        let variantSym = allocSymbol(self, data, variantName, variantNode, 0);
5128 5137
lib/std/lang/resolver/tests/structure.rad +15 -0
60 60
    let ty = try getTypeInScopeOf(&a, result.root, "R");
61 61
    let stmt = try getBlockStmt(result.root, 1);
62 62
    try expectExprStmtType(&a, stmt, resolver::Type::Nominal(ty));
63 63
}
64 64
65 +
@test unsafe fn testResolveUnionDiscriminantRange() throws (testing::TestError) {
66 +
    let mut a = testResolver();
67 +
    let valid = try resolveProgramStr(&mut a, "union R { A = 255 } R::A;");
68 +
    try expectNoErrors(&valid);
69 +
    let mut b = testResolver();
70 +
    let explicit = try resolveProgramStr(&mut b, "union R { A = 256 } R::A;");
71 +
    try expectErrorKind(&explicit, resolver::ErrorKind::NumericLiteralOverflow);
72 +
    let mut c = testResolver();
73 +
    let implicit = try resolveProgramStr(&mut c, "union R { A = 255, B } R::B;");
74 +
    try expectErrorKind(&implicit, resolver::ErrorKind::NumericLiteralOverflow);
75 +
    let mut d = testResolver();
76 +
    let wide = try resolveProgramStr(&mut d, "union R { A = 4294967296 } R::A;");
77 +
    try expectErrorKind(&wide, resolver::ErrorKind::NumericLiteralOverflow);
78 +
}
79 +
65 80
@test unsafe fn testResolveUnionVariantPayloadTypeMismatch() throws (testing::TestError) {
66 81
    let mut a = testResolver();
67 82
    let program = "union R { Ok(i32), Error(bool) } R::Ok(true);";
68 83
    let result = try resolveProgramStr(&mut a, program);
69 84
    let err = try expectError(&result);
seed/README +15 -5
39 39
40 40
FILES
41 41
42 42
  seed/radiance.rv64           Self-contained RV64 image containing the seed's
43 43
                               text, read-only data, and read-write data.
44 -
  seed/radiance.rv64.git       SHA-256 of the git commit whose *source*
44 +
  seed/radiance.rv64.git       Full Git tree ID of the source commit that
45 45
                               was compiled to produce this seed.
46 46
  seed/update                  Tool that finds the fixed point and
47 47
                               updates the seed.
48 48
49 49
128 128
  3. Stage 2: Runs S1 to compile the source. Outputs S2.
129 129
  4. Compares S1 and S2. If identical, done.
130 130
  5. Continues up to a certain number of stages. Fails if no fixed point is reached.
131 131
132 132
When a fixed point is found, it copies the converged binary to
133 -
`seed/radiance.rv64` and writes the current HEAD in `seed/radiance.rv64.git`.
133 +
`seed/radiance.rv64` and writes the full tree ID of HEAD in
134 +
`seed/radiance.rv64.git`. Commit source changes before running the updater.
135 +
The updater compiles an archive of that complete committed tree.
134 136
135 137
Why might it take multiple stages?
136 138
137 139
  * Stage 1 differs from seed: The source changed, so the compiler
138 140
    binary changed. Normal.
178 180
    Its source can be audited, and any C99 compiler can be used to compile it.
179 181
    To use it as seed, pass `--from-s0` like so:
180 182
181 183
      seed/update --from-s0 --seed ./radiance.s0
182 184
183 -
  Verify the source commit
185 +
  Verify the source tree
184 186
185 -
    The file `seed/radiance.rv64.git` records which commit's source was
186 -
    compiled to produce the seed.
187 +
    The file `seed/radiance.rv64.git` records the complete tree of the
188 +
    actual source commit used to build the seed. This can be an earlier
189 +
    source snapshot when source and seed changes share one final commit.
190 +
    The source commit is identified in the seed commit message.
191 +
192 +
      git rev-parse 'SOURCE_COMMIT^{tree}'
193 +
194 +
    Compare that result with the recorded tree ID. The title uses the
195 +
    first eight characters: Create new seed from `<tree ID>`.
196 +
    The recorded object must be inspectable with `git cat-file -p TREE_ID`.
187 197
188 198
189 199
TROUBLESHOOTING
190 200
191 201
  "No fixed point reached after N stages"