authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-19 19:37:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-19 19:37:25-07:00
log04364c45cefbba9451af202ceab5ae528cc8bbaa
treec43f61b9f29d6754b23b55317602a22b5bfc7cb3
parent36c6acfc76eeeed1498b5cc9ecb3137214e69bbf

fix casting with imported symbol not working


4 files changed, 31 insertions(+), 40 deletions(-)

src/analyze.cpp+14-39
......@@ -755,7 +755,7 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node) {
755755
756756 ConstExprValue *const_val = &expr->const_val;
757757 if (!const_val->ok) {
758 add_node_error(g, node, buf_sprintf("unable to resolve constant expression"));
758 add_node_error(g, node, buf_sprintf("unable to evaluate constant expression"));
759759 return g->builtin_types.entry_invalid;
760760 }
761761
......@@ -867,7 +867,7 @@ static Buf *resolve_const_expr_str(CodeGen *g, ImportTableEntry *import, BlockCo
867867 ConstExprValue *const_str_val = &get_resolved_expr(*node)->const_val;
868868
869869 if (!const_str_val->ok) {
870 add_node_error(g, *node, buf_sprintf("unable to resolve constant expression"));
870 add_node_error(g, *node, buf_sprintf("unable to evaluate constant expression"));
871871 return nullptr;
872872 }
873873
......@@ -896,7 +896,7 @@ static bool resolve_const_expr_bool(CodeGen *g, ImportTableEntry *import, BlockC
896896 ConstExprValue *const_bool_val = &get_resolved_expr(*node)->const_val;
897897
898898 if (!const_bool_val->ok) {
899 add_node_error(g, *node, buf_sprintf("unable to resolve constant expression"));
899 add_node_error(g, *node, buf_sprintf("unable to evaluate constant expression"));
900900 return false;
901901 }
902902
......@@ -2424,8 +2424,6 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
24242424
24252425 if (child_type->id == TypeTableEntryIdInvalid) {
24262426 return g->builtin_types.entry_invalid;
2427 } else if (wrapped_in_fn_call) {
2428 return resolve_expr_const_val_as_type(g, node, child_type);
24292427 } else if (child_type->id == TypeTableEntryIdEnum) {
24302428 AstNode *container_init_node = node->data.field_access_expr.container_init_expr_node;
24312429 AstNode *value_node;
......@@ -2464,6 +2462,8 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
24642462 }
24652463 } else if (child_type->id == TypeTableEntryIdPureError) {
24662464 return analyze_error_literal_expr(g, import, context, node, field_name);
2465 } else if (wrapped_in_fn_call) { // this branch should go last, before the error in the else case
2466 return resolve_expr_const_val_as_type(g, node, child_type);
24672467 } else {
24682468 add_node_error(g, node,
24692469 buf_sprintf("type '%s' does not support field access", buf_ptr(&struct_type->name)));
......@@ -4605,13 +4605,13 @@ static TypeTableEntry *analyze_fn_call_ptr(CodeGen *g, ImportTableEntry *import,
46054605 }
46064606
46074607 FnTableEntry *fn_table_entry = node->data.fn_call_expr.fn_entry;
4608 ConstExprValue *result_val = &get_resolved_expr(node)->const_val;
46084609 if (ok_invocation && fn_table_entry && fn_table_entry->is_pure) {
46094610 if (fn_table_entry->anal_state == FnAnalStateReady) {
46104611 analyze_fn_body(g, fn_table_entry);
46114612 }
46124613 if (all_args_const_expr) {
46134614 if (fn_table_entry->is_pure && fn_table_entry->anal_state == FnAnalStateComplete) {
4614 ConstExprValue *result_val = &get_resolved_expr(node)->const_val;
46154615 if (eval_fn(g, node, fn_table_entry, result_val, 1000, struct_node)) {
46164616 // function evaluation generated an error
46174617 return g->builtin_types.entry_invalid;
......@@ -4626,7 +4626,11 @@ static TypeTableEntry *analyze_fn_call_ptr(CodeGen *g, ImportTableEntry *import,
46264626 }
46274627
46284628 if (handle_is_ptr(return_type)) {
4629 context->fn_entry->cast_alloca_list.append(node);
4629 if (context->fn_entry) {
4630 context->fn_entry->cast_alloca_list.append(node);
4631 } else if (!result_val->ok) {
4632 add_node_error(g, node, buf_sprintf("unable to evaluate constant expression"));
4633 }
46304634 }
46314635
46324636 return return_type;
......@@ -4693,7 +4697,7 @@ static TypeTableEntry *analyze_generic_fn_call(CodeGen *g, ImportTableEntry *imp
46934697 add_local_var(g, generic_param_decl_node, decl_node->owner, child_context,
46944698 &generic_param_decl_node->data.param_decl.name, param_type, true, *param_node);
46954699 } else {
4696 add_node_error(g, *param_node, buf_sprintf("unable to resolve constant expression"));
4700 add_node_error(g, *param_node, buf_sprintf("unable to evaluate constant expression"));
46974701
46984702 add_local_var(g, generic_param_decl_node, decl_node->owner, child_context,
46994703 &generic_param_decl_node->data.param_decl.name, g->builtin_types.entry_invalid,
......@@ -4754,36 +4758,7 @@ static TypeTableEntry *analyze_fn_call_expr(CodeGen *g, ImportTableEntry *import
47544758
47554759 if (const_val->ok) {
47564760 if (invoke_type_entry->id == TypeTableEntryIdMetaType) {
4757 if (fn_ref_expr->type == NodeTypeFieldAccessExpr) {
4758 TypeTableEntry *child_type = resolve_type(g, fn_ref_expr);
4759
4760 if (child_type->id == TypeTableEntryIdInvalid) {
4761 return g->builtin_types.entry_invalid;
4762 } else if (child_type->id == TypeTableEntryIdStruct) {
4763 Buf *field_name = &fn_ref_expr->data.field_access_expr.field_name;
4764 BlockContext *container_block_context = get_container_block_context(child_type);
4765 auto entry = container_block_context->decl_table.maybe_get(field_name);
4766 AstNode *decl_node = entry ? entry->value : nullptr;
4767 if (decl_node && decl_node->type == NodeTypeFnProto) {
4768 bool pointer_only = false;
4769 resolve_top_level_decl(g, decl_node, pointer_only);
4770
4771 FnTableEntry *fn_entry = decl_node->data.fn_proto.fn_table_entry;
4772 assert(fn_entry);
4773 return analyze_fn_call_raw(g, import, context, expected_type, node, fn_entry, nullptr);
4774 } else {
4775 add_node_error(g, node,
4776 buf_sprintf("struct '%s' has no function called '%s'",
4777 buf_ptr(&child_type->name), buf_ptr(field_name)));
4778 return g->builtin_types.entry_invalid;
4779 }
4780 } else {
4781 add_node_error(g, fn_ref_expr, buf_sprintf("member reference base type not struct or enum"));
4782 return g->builtin_types.entry_invalid;
4783 }
4784 } else {
4785 return analyze_cast_expr(g, import, context, node);
4786 }
4761 return analyze_cast_expr(g, import, context, node);
47874762 } else if (invoke_type_entry->id == TypeTableEntryIdFn) {
47884763 AstNode *struct_node;
47894764 if (fn_ref_expr->type == NodeTypeFieldAccessExpr &&
......@@ -5120,7 +5095,7 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
51205095 ConstExprValue *const_val = &get_resolved_expr(item_node)->const_val;
51215096 if (!const_val->ok) {
51225097 add_node_error(g, item_node,
5123 buf_sprintf("unable to resolve constant expression"));
5098 buf_sprintf("unable to evaluate constant expression"));
51245099 any_errors = true;
51255100 }
51265101 }
test/other.zig+1
......@@ -3,3 +3,4 @@ pub enum APubEnum {
33 Two,
44 Three,
55}
6pub const size_t = u64;
test/run_tests.cpp+10-1
......@@ -1128,7 +1128,7 @@ fn foo(x: i32)(y: i32) -> i32 { return x + y; }
11281128fn test1(a: i32, b: i32) -> i32 {
11291129 return foo(a)(b);
11301130}
1131 )SOURCE", 1, ".tmp_source.zig:4:16: error: unable to resolve constant expression");
1131 )SOURCE", 1, ".tmp_source.zig:4:16: error: unable to evaluate constant expression");
11321132
11331133 add_compile_fail_case("goto jumping into block", R"SOURCE(
11341134fn f() {
......@@ -1200,6 +1200,15 @@ struct Foo {
12001200const a = Foo {.x = get_it()};
12011201extern fn get_it() -> i32;
12021202 )SOURCE", 1, ".tmp_source.zig:5:27: error: unable to evaluate constant expression");
1203
1204 add_compile_fail_case("non-const expression function call with struct return value outside function", R"SOURCE(
1205struct Foo {
1206 x: i32,
1207}
1208const a = get_it();
1209#static_eval_enable(false)
1210fn get_it() -> Foo { Foo {.x = 13} }
1211 )SOURCE", 1, ".tmp_source.zig:5:17: error: unable to evaluate constant expression");
12031212}
12041213
12051214//////////////////////////////////////////////////////////////////////////////
test/self_hosted.zig+6
......@@ -1252,3 +1252,9 @@ fn pub_enum() {
12521252fn pub_enum_test(foo: other.APubEnum) {
12531253 assert(foo == other.APubEnum.Two);
12541254}
1255
1256
1257#attribute("test")
1258fn cast_with_imported_symbol() {
1259 assert(other.size_t(42) == 42);
1260}