authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-27 12:28:05-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-27 12:28:05-07:00
logd27b76fc31c33d9381a95ecd5e8567c93bf12eb0
treec5fdb9ef94a968c7d706eff57c3758b529d11c7a
parentfe0c6a3df9480cc3d0be7412cd24bba4366c18a1

add error for `@typeof` or `&` of number literal

closes #85

2 files changed, 43 insertions(+), 5 deletions(-)

src/analyze.cpp+30-4
...@@ -3426,10 +3426,30 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry...@@ -3426,10 +3426,30 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
3426 AstNode *expr_node = node->data.fn_call_expr.params.at(0);3426 AstNode *expr_node = node->data.fn_call_expr.params.at(0);
3427 TypeTableEntry *type_entry = analyze_expression(g, import, context, nullptr, expr_node);3427 TypeTableEntry *type_entry = analyze_expression(g, import, context, nullptr, expr_node);
34283428
3429 if (type_entry->id == TypeTableEntryIdInvalid) {3429 switch (type_entry->id) {
3430 return g->builtin_types.entry_invalid;3430 case TypeTableEntryIdInvalid:
3431 } else {3431 return type_entry;
3432 return resolve_expr_const_val_as_type(g, node, type_entry);3432 case TypeTableEntryIdNumLitFloat:
3433 case TypeTableEntryIdNumLitInt:
3434 case TypeTableEntryIdUndefLit:
3435 add_node_error(g, expr_node,
3436 buf_sprintf("type '%s' not eligible for @typeof", buf_ptr(&type_entry->name)));
3437 return g->builtin_types.entry_invalid;
3438 case TypeTableEntryIdMetaType:
3439 case TypeTableEntryIdVoid:
3440 case TypeTableEntryIdBool:
3441 case TypeTableEntryIdUnreachable:
3442 case TypeTableEntryIdInt:
3443 case TypeTableEntryIdFloat:
3444 case TypeTableEntryIdPointer:
3445 case TypeTableEntryIdArray:
3446 case TypeTableEntryIdStruct:
3447 case TypeTableEntryIdMaybe:
3448 case TypeTableEntryIdErrorUnion:
3449 case TypeTableEntryIdPureError:
3450 case TypeTableEntryIdEnum:
3451 case TypeTableEntryIdFn:
3452 return resolve_expr_const_val_as_type(g, node, type_entry);
3433 }3453 }
3434 }3454 }
3435 case BuiltinFnIdCInclude:3455 case BuiltinFnIdCInclude:
...@@ -3729,6 +3749,12 @@ static TypeTableEntry *analyze_prefix_op_expr(CodeGen *g, ImportTableEntry *impo...@@ -3729,6 +3749,12 @@ static TypeTableEntry *analyze_prefix_op_expr(CodeGen *g, ImportTableEntry *impo
3729 return resolve_expr_const_val_as_type(g, node,3749 return resolve_expr_const_val_as_type(g, node,
3730 get_pointer_to_type(g, meta_type, is_const));3750 get_pointer_to_type(g, meta_type, is_const));
3731 }3751 }
3752 } else if (child_type->id == TypeTableEntryIdNumLitInt ||
3753 child_type->id == TypeTableEntryIdNumLitFloat)
3754 {
3755 add_node_error(g, expr_node,
3756 buf_sprintf("unable to get address of type '%s'", buf_ptr(&child_type->name)));
3757 return g->builtin_types.entry_invalid;
3732 } else {3758 } else {
3733 return get_pointer_to_type(g, child_type, is_const);3759 return get_pointer_to_type(g, child_type, is_const);
3734 }3760 }
test/run_tests.cpp+13-1
...@@ -999,7 +999,7 @@ pub fn main(args: [][]u8) -> %void {...@@ -999,7 +999,7 @@ pub fn main(args: [][]u8) -> %void {
999999
1000 add_simple_case("order-independent declarations", R"SOURCE(1000 add_simple_case("order-independent declarations", R"SOURCE(
1001import "std.zig";1001import "std.zig";
1002const z : @typeof(stdin_fileno) = 0;1002const z = stdin_fileno;
1003const x : @typeof(y) = 1234;1003const x : @typeof(y) = 1234;
1004const y : u16 = 5678;1004const y : u16 = 5678;
1005pub fn main(args: [][]u8) -> %void {1005pub fn main(args: [][]u8) -> %void {
...@@ -1683,6 +1683,18 @@ c_import {...@@ -1683,6 +1683,18 @@ c_import {
1683 add_compile_fail_case("empty file", "",1683 add_compile_fail_case("empty file", "",
1684 1, ".tmp_source.zig:1:1: error: missing export declaration and output name not provided");1684 1, ".tmp_source.zig:1:1: error: missing export declaration and output name not provided");
16851685
1686 add_compile_fail_case("address of number literal", R"SOURCE(
1687const x = 3;
1688const y = &x;
1689 )SOURCE", 1, ".tmp_source.zig:3:12: error: unable to get address of type '(integer literal)'");
1690
1691 add_compile_fail_case("@typeof number literal", R"SOURCE(
1692const x = 3;
1693struct Foo {
1694 index: @typeof(x),
1695}
1696 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeof");
1697
1686}1698}
16871699
1688static void print_compiler_invocation(TestCase *test_case) {1700static void print_compiler_invocation(TestCase *test_case) {