Attempt 1 rejected only LITERAL initializers that mismatch a typed module
const's annotation; a const-EXPRESSION initializer escaped, so the same
issue-0088 root remained for `M :: 2; N : string : M + 2` — accepted at exit 0,
folding `[N]s64` to 4 and printing N as an integer.
Root cause: `registerTypedModuleConst` validated only the enumerated literal
node kinds; any other kind fell through to `else => {}`, and pass 0
pre-registers binary_op/unary_op consts as a `.s64` placeholder that was never
reconciled with the annotation.
Fix — validate by TYPE, not by node kind:
- lower.zig: `registerTypedModuleConst` now covers literals AND const-expressions
(binary_op/unary_op) through one path. `typedConstInitFits` keeps the literal
arms and routes any non-literal through the new `constExprInitFits`, which
compares the initializer's INFERRED type (`inferExprType`, the existing
type-inference facility — no second const evaluator) to the annotation with the
same integer/float compatibility. A mismatch emits the `type mismatch` diagnostic
(a const-expression is described by its inferred type, e.g. "an integer
expression") and evicts the pass-0 placeholder; a match registers the const at
its resolved annotation type (the same `put` the literal path always did), so a
const-expression folds and emits at its declared type.
- `literalKindName` → `initializerDescription` (+ `constExprDescription`) so the
message is accurate for both a literal and a const-expression initializer.
Regression:
- examples/1143: extended with `E : string : M + 2` and `V : string : -M`
(const-expr mismatches → exit 1, pinned diagnostics).
- examples/0162: extended with `KE : s64 : M + 2` (used as a count + printed) and
`WE : f32 : M + 2` (over-rejection guard — valid const-exprs still work).
- program_index.test.zig: count-gate test extended with a binary_op value node
declared `string` (must not fold as a count).
Docs: specs.md §3 + readme.md generalized from "initializer literal" to cover
constant expressions; issues/0088 RESOLVED banner updated.
28 lines
1.3 KiB
Plaintext
28 lines
1.3 KiB
Plaintext
// A typed module-level constant whose initializer does not match its
|
|
// annotation is a compile-time type error — not a silently-accepted const.
|
|
// Each declaration below pairs an initializer with an incompatible annotation,
|
|
// so the compiler must emit a `type mismatch` diagnostic at the initializer and
|
|
// abort (exit 1) rather than registering a usable const.
|
|
//
|
|
// Regression (issue 0088): `N : string : 4` was accepted; `print(N)` then
|
|
// segfaulted (an integer emitted as a `string` const → a bogus pointer) and
|
|
// `[N]s64` folded `N` to 4. The fix rejects the declaration at the root. The
|
|
// validation is type-based, so a const-EXPRESSION initializer (`E : string :
|
|
// M + 2`, `V : string : -M`) is rejected just like a literal — not skipped
|
|
// because its node kind isn't a literal (issue 0088, attempt 2).
|
|
|
|
#import "modules/std.sx";
|
|
|
|
M :: 2;
|
|
|
|
N : string : 4; // integer literal where a string is annotated
|
|
F : s64 : "x"; // string literal where an integer is annotated
|
|
B : s64 : true; // boolean literal where an integer is annotated
|
|
G : s64 : 1.5; // float literal where an integer is annotated
|
|
E : string : M + 2; // integer EXPRESSION where a string is annotated
|
|
V : string : -M; // integer (unary) expression where a string is annotated
|
|
|
|
main :: () {
|
|
print("unreachable\n");
|
|
}
|