| author | |
| committer | |
| log | 528c151a55a76451b278057c40b29242241a70fd |
| tree | 6a65d948a79147b405e3c9b3e3d025cafc73d9b1 |
| parent | a169d844c70a8e2a6e29be639b4d0a1c26df0662 |
Make analyze_type_expr behave like ir_resolve_type when the user tries
to use `undefined` as a type.
Closes #24362 files changed, 28 insertions(+), 0 deletions(-)
src/analyze.cpp+11| ... | @@ -981,6 +981,17 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) { | ... | @@ -981,6 +981,17 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) { |
| 981 | return g->builtin_types.entry_invalid; | 981 | return g->builtin_types.entry_invalid; |
| 982 | 982 | ||
| 983 | assert(result->special != ConstValSpecialRuntime); | 983 | 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); | ||
| 984 | return result->data.x_type; | 995 | return result->data.x_type; |
| 985 | } | 996 | } |
| 986 | 997 |
test/compile_errors.zig+17| ... | @@ -2,6 +2,23 @@ const tests = @import("tests.zig"); | ... | @@ -2,6 +2,23 @@ const tests = @import("tests.zig"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | 3 | ||
| 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { | 4 | 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 | |||
| 5 | cases.add( | 22 | cases.add( |
| 6 | "@hasDecl with non-container", | 23 | "@hasDecl with non-container", |
| 7 | \\export fn entry() void { | 24 | \\export fn entry() void { |