Skip to content

Reject undefined as type #2566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,17 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
return g->builtin_types.entry_invalid;

assert(result->special != ConstValSpecialRuntime);
// Reject undefined as valid `type` type even though the specification
// allows it to be casted to anything.
// See also ir_resolve_type()
if (result->special == ConstValSpecialUndef) {
add_node_error(g, node,
buf_sprintf("expected type 'type', found '%s'",
buf_ptr(&g->builtin_types.entry_undef->name)));
return g->builtin_types.entry_invalid;
}

assert(result->data.x_type != nullptr);
return result->data.x_type;
}

Expand Down
17 changes: 17 additions & 0 deletions test/compile_errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ const tests = @import("tests.zig");
const builtin = @import("builtin");

pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"undefined as field type is rejected",
\\const Foo = struct {
\\ a: undefined,
\\};
\\const Bar = union {
\\ a: undefined,
\\};
\\pub fn main() void {
\\ const foo: Foo = undefined;
\\ const bar: Bar = undefined;
\\}
,
"tmp.zig:2:8: error: expected type 'type', found '(undefined)'",
"tmp.zig:5:8: error: expected type 'type', found '(undefined)'",
);

cases.add(
"@hasDecl with non-container",
\\export fn entry() void {
Expand Down