Skip to content

Commit 52a6430

Browse files
committed
Reject undefined as type
Make analyze_type_expr behave like ir_resolve_type when the user tries to use `undefined` as a type. Closes #2436
1 parent df7aa9a commit 52a6430

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/analyze.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,17 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
981981
return g->builtin_types.entry_invalid;
982982

983983
assert(result->special != ConstValSpecialRuntime);
984+
// Reject undefined as valid `type` type even though the specification
985+
// allows it to be casted to anything.
986+
// See also ir_resolve_type()
987+
if (result->special == ConstValSpecialUndef) {
988+
add_node_error(g, node,
989+
buf_sprintf("expected type 'type', found '%s'",
990+
buf_ptr(&g->builtin_types.entry_undef->name)));
991+
return g->builtin_types.entry_invalid;
992+
}
993+
994+
assert(result->data.x_type != nullptr);
984995
return result->data.x_type;
985996
}
986997

test/compile_errors.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ const tests = @import("tests.zig");
22
const builtin = @import("builtin");
33

44
pub fn addCases(cases: *tests.CompileErrorContext) void {
5+
cases.add(
6+
"undefined as field type is rejected",
7+
\\const Foo = struct {
8+
\\ a: undefined,
9+
\\};
10+
\\const Bar = union {
11+
\\ a: undefined,
12+
\\};
13+
\\pub fn main() void {
14+
\\ const foo: Foo = undefined;
15+
\\ const bar: Bar = undefined;
16+
\\}
17+
,
18+
"tmp.zig:2:8: error: expected type 'type', found '(undefined)'",
19+
"tmp.zig:5:8: error: expected type 'type', found '(undefined)'",
20+
);
21+
522
cases.add(
623
"@hasDecl with non-container",
724
\\export fn entry() void {

0 commit comments

Comments
 (0)