authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-12 13:30:52-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-12 13:30:52-07:00
logbe4df96e4b80a3307b3661fd5ca3114478499daf
treeb4a65aecebe565e7602b6ebe796e8dc96fb6b049
parentaa89fd3b3e4522fe9199049a4fcc6bdc69f4bfde

passing all tests


5 files changed, 240 insertions(+), 164 deletions(-)

src/analyze.cpp+46-25
......@@ -35,7 +35,8 @@ static TypeTableEntry *resolve_expr_const_val_as_type(CodeGen *g, AstNode *node,
3535static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, AstNode *node,
3636 TypeTableEntry *expected_type, uint64_t x);
3737static AstNode *find_decl(BlockContext *context, Buf *name);
38static TypeTableEntry *analyze_decl_ref(CodeGen *g, AstNode *source_node, AstNode *decl_node, bool pointer_only);
38static TypeTableEntry *analyze_decl_ref(CodeGen *g, AstNode *source_node, AstNode *decl_node,
39 bool pointer_only, BlockContext *block_context);
3940static TopLevelDecl *get_as_top_level_decl(AstNode *node);
4041static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTableEntry *import,
4142 BlockContext *context, AstNode *source_node,
......@@ -957,6 +958,19 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
957958 add_node_error(g, directive_node,
958959 buf_sprintf("#condition valid only on exported symbols"));
959960 }
961 } else if (buf_eql_str(name, "static_eval_enable")) {
962 if (fn_table_entry->is_extern) {
963 add_node_error(g, directive_node,
964 buf_sprintf("#static_val_enable invalid on extern functions"));
965 } else {
966 bool enable;
967 bool ok = resolve_const_expr_bool(g, import, import->block_context,
968 &directive_node->data.directive.expr, &enable);
969 if (!enable || !ok) {
970 fn_table_entry->is_pure = false;
971 }
972 // TODO cause compile error if enable is true and impure fn
973 }
960974 } else {
961975 add_node_error(g, directive_node,
962976 buf_sprintf("invalid directive: '%s'", buf_ptr(name)));
......@@ -2227,9 +2241,9 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
22272241 const_val->ok = false;
22282242 }
22292243 }
2230 if (!const_val->ok) {
2231 context->fn_entry->struct_val_expr_alloca_list.append(codegen);
2232 }
2244 }
2245 if (!const_val->ok) {
2246 context->fn_entry->struct_val_expr_alloca_list.append(codegen);
22332247 }
22342248
22352249 for (int i = 0; i < actual_field_count; i += 1) {
......@@ -2381,7 +2395,7 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
23812395 AstNode *decl_node = entry ? entry->value : nullptr;
23822396 if (decl_node) {
23832397 bool pointer_only = false;
2384 return analyze_decl_ref(g, node, decl_node, pointer_only);
2398 return analyze_decl_ref(g, node, decl_node, pointer_only, context);
23852399 } else {
23862400 add_node_error(g, node,
23872401 buf_sprintf("container '%s' has no member called '%s'",
......@@ -2408,7 +2422,7 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
24082422 add_error_note(g, msg, decl_node, buf_sprintf("declared here"));
24092423 }
24102424 bool pointer_only = false;
2411 return analyze_decl_ref(g, node, decl_node, pointer_only);
2425 return analyze_decl_ref(g, node, decl_node, pointer_only, context);
24122426 } else {
24132427 const char *import_name = namespace_import->path ? buf_ptr(namespace_import->path) : "(C import)";
24142428 add_node_error(g, node,
......@@ -2674,8 +2688,21 @@ static TypeTableEntry *analyze_error_literal_expr(CodeGen *g, ImportTableEntry *
26742688 return g->builtin_types.entry_invalid;
26752689}
26762690
2677static TypeTableEntry *analyze_var_ref(CodeGen *g, AstNode *source_node, VariableTableEntry *var) {
2691static bool var_is_pure(VariableTableEntry *var, BlockContext *context) {
2692 if (var->block_context->fn_entry == context->fn_entry) {
2693 // variable was declared in the current function, so it's OK.
2694 return true;
2695 }
2696 return var->is_const && var->type->deep_const;
2697}
2698
2699static TypeTableEntry *analyze_var_ref(CodeGen *g, AstNode *source_node, VariableTableEntry *var,
2700 BlockContext *context)
2701{
26782702 get_resolved_expr(source_node)->variable = var;
2703 if (!var_is_pure(var, context)) {
2704 mark_impure_fn(context);
2705 }
26792706 if (var->is_const && var->val_node) {
26802707 ConstExprValue *other_const_val = &get_resolved_expr(var->val_node)->const_val;
26812708 if (other_const_val->ok) {
......@@ -2686,7 +2713,7 @@ static TypeTableEntry *analyze_var_ref(CodeGen *g, AstNode *source_node, Variabl
26862713}
26872714
26882715static TypeTableEntry *analyze_decl_ref(CodeGen *g, AstNode *source_node, AstNode *decl_node,
2689 bool pointer_only)
2716 bool pointer_only, BlockContext *block_context)
26902717{
26912718 resolve_top_level_decl(g, decl_node, pointer_only);
26922719 TopLevelDecl *tld = get_as_top_level_decl(decl_node);
......@@ -2696,7 +2723,7 @@ static TypeTableEntry *analyze_decl_ref(CodeGen *g, AstNode *source_node, AstNod
26962723
26972724 if (decl_node->type == NodeTypeVariableDeclaration) {
26982725 VariableTableEntry *var = decl_node->data.variable_declaration.variable;
2699 return analyze_var_ref(g, source_node, var);
2726 return analyze_var_ref(g, source_node, var, block_context);
27002727 } else if (decl_node->type == NodeTypeFnProto) {
27012728 if (decl_node->data.fn_proto.generic_params.length > 0) {
27022729 TypeTableEntry *type_entry = decl_node->data.fn_proto.generic_fn_type;
......@@ -2716,14 +2743,6 @@ static TypeTableEntry *analyze_decl_ref(CodeGen *g, AstNode *source_node, AstNod
27162743 }
27172744}
27182745
2719static bool var_is_pure(VariableTableEntry *var, TypeTableEntry *var_type, BlockContext *context) {
2720 if (var->block_context->fn_entry == context->fn_entry) {
2721 // variable was declared in the current function, so it's OK.
2722 return true;
2723 }
2724 return var->is_const && var->type->deep_const;
2725}
2726
27272746static TypeTableEntry *analyze_symbol_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
27282747 TypeTableEntry *expected_type, AstNode *node, bool pointer_only)
27292748{
......@@ -2740,16 +2759,13 @@ static TypeTableEntry *analyze_symbol_expr(CodeGen *g, ImportTableEntry *import,
27402759
27412760 VariableTableEntry *var = find_variable(g, context, variable_name);
27422761 if (var) {
2743 TypeTableEntry *var_type = analyze_var_ref(g, node, var);
2744 if (!var_is_pure(var, var_type, context)) {
2745 mark_impure_fn(context);
2746 }
2762 TypeTableEntry *var_type = analyze_var_ref(g, node, var, context);
27472763 return var_type;
27482764 }
27492765
27502766 AstNode *decl_node = find_decl(context, variable_name);
27512767 if (decl_node) {
2752 return analyze_decl_ref(g, node, decl_node, pointer_only);
2768 return analyze_decl_ref(g, node, decl_node, pointer_only, context);
27532769 }
27542770
27552771 if (import->any_imports_failed) {
......@@ -2992,7 +3008,8 @@ static TypeTableEntry *analyze_bin_op_expr(CodeGen *g, ImportTableEntry *import,
29923008 }
29933009
29943010 analyze_expression(g, import, context, expected_rhs_type, node->data.bin_op_expr.op2);
2995 return resolve_expr_const_val_as_void(g, node);
3011 // not const ok because expression has side effects
3012 return g->builtin_types.entry_void;
29963013 }
29973014 case BinOpTypeBoolOr:
29983015 case BinOpTypeBoolAnd:
......@@ -4473,12 +4490,16 @@ static TypeTableEntry *analyze_fn_call_ptr(CodeGen *g, ImportTableEntry *import,
44734490 actual_param_count += 1;
44744491 }
44754492
4493 bool ok_invocation = true;
4494
44764495 if (fn_type->data.fn.fn_type_id.is_var_args) {
44774496 if (actual_param_count < src_param_count) {
4497 ok_invocation = false;
44784498 add_node_error(g, node,
44794499 buf_sprintf("expected at least %d arguments, got %d", src_param_count, actual_param_count));
44804500 }
44814501 } else if (src_param_count != actual_param_count) {
4502 ok_invocation = false;
44824503 add_node_error(g, node,
44834504 buf_sprintf("expected %d arguments, got %d", src_param_count, actual_param_count));
44844505 }
......@@ -4517,7 +4538,7 @@ static TypeTableEntry *analyze_fn_call_ptr(CodeGen *g, ImportTableEntry *import,
45174538 }
45184539
45194540 FnTableEntry *fn_table_entry = node->data.fn_call_expr.fn_entry;
4520 if (fn_table_entry && fn_table_entry->is_pure && all_args_const_expr) {
4541 if (ok_invocation && fn_table_entry && fn_table_entry->is_pure && all_args_const_expr) {
45214542 if (fn_table_entry->anal_state == FnAnalStateReady) {
45224543 analyze_fn_body(g, fn_table_entry);
45234544 } else if (fn_table_entry->anal_state == FnAnalStateProbing) {
......@@ -4726,7 +4747,7 @@ static TypeTableEntry *analyze_fn_call_expr(CodeGen *g, ImportTableEntry *import
47264747 if (fn_ref_expr->type == NodeTypeFieldAccessExpr &&
47274748 fn_ref_expr->data.field_access_expr.is_member_fn)
47284749 {
4729 struct_node = fn_ref_expr;
4750 struct_node = fn_ref_expr->data.field_access_expr.struct_expr;
47304751 } else {
47314752 struct_node = nullptr;
47324753 }
src/eval.cpp+93-12
......@@ -70,6 +70,7 @@ static bool eval_block(EvalFn *ef, AstNode *node, ConstExprValue *out) {
7070
7171 for (int i = 0; i < node->data.block.statements.length; i += 1) {
7272 AstNode *child = node->data.block.statements.at(i);
73 memset(out, 0, sizeof(ConstExprValue));
7374 if (eval_expr(ef, child, out)) return true;
7475 }
7576
......@@ -109,7 +110,6 @@ void eval_const_expr_bin_op(ConstExprValue *op1_val, TypeTableEntry *op1_type,
109110{
110111 assert(op1_val->ok);
111112 assert(op2_val->ok);
112 assert(op1_type == op2_type);
113113
114114 switch (bin_op) {
115115 case BinOpTypeAssign:
......@@ -275,6 +275,7 @@ static bool eval_symbol_expr(EvalFn *ef, AstNode *node, ConstExprValue *out_val)
275275
276276 Buf *name = &node->data.symbol_expr.symbol;
277277 EvalVar *var = find_var(ef, name);
278 assert(var);
278279
279280 *out_val = var->value;
280281
......@@ -374,6 +375,8 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
374375 *const_val = *other_val;
375376 break;
376377 case CastOpPointerReinterpret:
378 if (other_type->id == TypeTableEntryIdPointer &&
379 new_type->id == TypeTableEntryIdPointer)
377380 {
378381 TypeTableEntry *other_child_type = other_type->data.pointer.child_type;
379382 TypeTableEntry *new_child_type = new_type->data.pointer.child_type;
......@@ -393,8 +396,47 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
393396 } else {
394397 zig_panic("TODO");
395398 }
396 break;
399 } else if (other_type->id == TypeTableEntryIdMaybe &&
400 new_type->id == TypeTableEntryIdMaybe)
401 {
402 if (!other_val->data.x_maybe) {
403 *const_val = *other_val;
404 break;
405 }
406
407 TypeTableEntry *other_ptr_type = other_type->data.maybe.child_type;
408 TypeTableEntry *new_ptr_type = new_type->data.maybe.child_type;
409
410 if (other_ptr_type->id == TypeTableEntryIdPointer &&
411 new_ptr_type->id == TypeTableEntryIdPointer)
412 {
413 TypeTableEntry *other_child_type = other_ptr_type->data.pointer.child_type;
414 TypeTableEntry *new_child_type = new_ptr_type->data.pointer.child_type;
415
416 if ((other_child_type->id == TypeTableEntryIdInt ||
417 other_child_type->id == TypeTableEntryIdFloat) &&
418 (new_child_type->id == TypeTableEntryIdInt ||
419 new_child_type->id == TypeTableEntryIdFloat))
420 {
421 ConstExprValue *ptr_parent = allocate<ConstExprValue>(1);
422 ConstExprValue **ptr_val = allocate<ConstExprValue*>(1);
423 *ptr_val = other_val->data.x_maybe->data.x_ptr.ptr[0];
424 ptr_parent->data.x_ptr.ptr = ptr_val;
425 ptr_parent->data.x_ptr.len = 1;
426 ptr_parent->ok = true;
427
428 const_val->data.x_maybe = ptr_parent;
429 const_val->ok = true;
430 const_val->undef = other_val->undef;
431 const_val->depends_on_compile_var = other_val->depends_on_compile_var;
432 } else {
433 zig_panic("TODO");
434 }
435 } else {
436 zig_panic("TODO");
437 }
397438 }
439 break;
398440 case CastOpPtrToInt:
399441 case CastOpIntToPtr:
400442 // can't do it
......@@ -684,6 +726,7 @@ static bool eval_field_access_expr(EvalFn *ef, AstNode *node, ConstExprValue *ou
684726 if (eval_expr(ef, struct_expr, &struct_val)) return true;
685727 ConstExprValue *field_value = struct_val.data.x_struct.fields[tsf->src_index];
686728 *out_val = *field_value;
729 assert(out_val->ok);
687730 } else {
688731 zig_panic("TODO");
689732 }
......@@ -916,11 +959,45 @@ static bool eval_char_literal_expr(EvalFn *ef, AstNode *node, ConstExprValue *ou
916959 return false;
917960}
918961
962static bool eval_while_expr(EvalFn *ef, AstNode *node, ConstExprValue *out_val) {
963 assert(node->type == NodeTypeWhileExpr);
964
965 AstNode *cond_node = node->data.while_expr.condition;
966 AstNode *body_node = node->data.while_expr.body;
967
968 EvalScope *my_scope = allocate<EvalScope>(1);
969 my_scope->block_context = body_node->block_context;
970 ef->scope_stack.append(my_scope);
971
972 for (;;) {
973 my_scope->vars.resize(0);
974
975 ConstExprValue cond_val = {0};
976 if (eval_expr(ef, cond_node, &cond_val)) return true;
977
978 if (!cond_val.data.x_bool) break;
979
980 ConstExprValue body_val = {0};
981 if (eval_expr(ef, body_node, &body_val)) return true;
982
983 ef->root->branches_used += 1;
984 }
985
986 ef->scope_stack.pop();
987
988 return false;
989}
990
919991static bool eval_expr(EvalFn *ef, AstNode *node, ConstExprValue *out) {
920992 if (ef->root->branches_used > ef->root->branch_quota) {
921993 ef->root->exceeded_quota_node = node;
922994 return true;
923995 }
996 ConstExprValue *const_val = &get_resolved_expr(node)->const_val;
997 if (const_val->ok) {
998 *out = *const_val;
999 return false;
1000 }
9241001 switch (node->type) {
9251002 case NodeTypeBlock:
9261003 return eval_block(ef, node, out);
......@@ -952,23 +1029,16 @@ static bool eval_expr(EvalFn *ef, AstNode *node, ConstExprValue *out) {
9521029 return eval_number_literal_expr(ef, node, out);
9531030 case NodeTypeCharLiteral:
9541031 return eval_char_literal_expr(ef, node, out);
955 case NodeTypeRoot:
956 case NodeTypeFnProto:
957 case NodeTypeFnDef:
958 case NodeTypeFnDecl:
959 case NodeTypeParamDecl:
960 case NodeTypeDirective:
1032 case NodeTypeWhileExpr:
1033 return eval_while_expr(ef, node, out);
9611034 case NodeTypeDefer:
962 case NodeTypeTypeDecl:
9631035 case NodeTypeErrorValueDecl:
9641036 case NodeTypeUnwrapErrorExpr:
9651037 case NodeTypeStringLiteral:
9661038 case NodeTypeSliceExpr:
967 case NodeTypeUse:
9681039 case NodeTypeNullLiteral:
9691040 case NodeTypeUndefinedLiteral:
9701041 case NodeTypeIfVarExpr:
971 case NodeTypeWhileExpr:
9721042 case NodeTypeSwitchExpr:
9731043 case NodeTypeSwitchProng:
9741044 case NodeTypeSwitchRange:
......@@ -976,13 +1046,22 @@ static bool eval_expr(EvalFn *ef, AstNode *node, ConstExprValue *out) {
9761046 case NodeTypeGoto:
9771047 case NodeTypeBreak:
9781048 case NodeTypeContinue:
979 case NodeTypeAsmExpr:
9801049 case NodeTypeStructDecl:
9811050 case NodeTypeStructField:
9821051 case NodeTypeStructValueField:
9831052 case NodeTypeArrayType:
9841053 case NodeTypeErrorType:
9851054 case NodeTypeTypeLiteral:
1055 zig_panic("TODO");
1056 case NodeTypeRoot:
1057 case NodeTypeFnProto:
1058 case NodeTypeFnDef:
1059 case NodeTypeFnDecl:
1060 case NodeTypeUse:
1061 case NodeTypeAsmExpr:
1062 case NodeTypeParamDecl:
1063 case NodeTypeDirective:
1064 case NodeTypeTypeDecl:
9861065 zig_unreachable();
9871066 }
9881067}
......@@ -1054,6 +1133,8 @@ bool eval_fn(CodeGen *g, AstNode *node, FnTableEntry *fn, ConstExprValue *out_va
10541133 return true;
10551134 }
10561135
1136 assert(out_val->ok);
1137
10571138 return efr.abort;
10581139}
10591140
std/rand.zig+1
......@@ -7,6 +7,7 @@ pub struct Rand {
77 index: isize,
88
99 /// Initialize random state with the given seed.
10 #static_eval_enable(false)
1011 pub fn init(seed: u32) -> Rand {
1112 var r: Rand = undefined;
1213 r.index = 0;
test/run_tests.cpp+3-121
......@@ -218,6 +218,7 @@ pub fn bar_function() {
218218 )SOURCE");
219219
220220 add_source_file(tc, "other.zig", R"SOURCE(
221#static_eval_enable(false)
221222pub fn foo_function() -> bool {
222223 // this one conflicts with the one from foo
223224 return true;
......@@ -406,20 +407,6 @@ pub fn main(args: [][]u8) -> %void {
406407}
407408 )SOURCE", "loop\nloop\nloop\nloop\n");
408409
409 add_simple_case("implicit cast after unreachable", R"SOURCE(
410const io = @import("std").io;
411pub fn main(args: [][]u8) -> %void {
412 const x = outer();
413 if (x == 1234) {
414 %%io.stdout.printf("OK\n");
415 }
416}
417fn inner() -> i32 { 1234 }
418fn outer() -> isize {
419 return inner();
420}
421 )SOURCE", "OK\n");
422
423410 add_simple_case("@sizeof() and @typeof()", R"SOURCE(
424411const io = @import("std").io;
425412const x: u16 = 13;
......@@ -431,23 +418,6 @@ pub fn main(args: [][]u8) -> %void {
431418}
432419 )SOURCE", "2\n");
433420
434 add_simple_case("member functions", R"SOURCE(
435const io = @import("std").io;
436struct Rand {
437 seed: u32,
438 pub fn get_seed(r: Rand) -> u32 {
439 r.seed
440 }
441}
442pub fn main(args: [][]u8) -> %void {
443 const r = Rand {.seed = 1234};
444 if (r.get_seed() != 1234) {
445 %%io.stdout.printf("BAD seed\n");
446 }
447 %%io.stdout.printf("OK\n");
448}
449 )SOURCE", "OK\n");
450
451421 add_simple_case("pointer dereferencing", R"SOURCE(
452422const io = @import("std").io;
453423
......@@ -565,24 +535,6 @@ pub fn main(args: [][]u8) -> %void {
565535 "min i64: -9223372036854775808\n");
566536
567537
568 add_simple_case("else if expression", R"SOURCE(
569const io = @import("std").io;
570pub fn main(args: [][]u8) -> %void {
571 if (f(1) == 1) {
572 %%io.stdout.printf("OK\n");
573 }
574}
575fn f(c: u8) -> u8 {
576 if (c == 0) {
577 0
578 } else if (c == 1) {
579 1
580 } else {
581 2
582 }
583}
584 )SOURCE", "OK\n");
585
586538 add_simple_case("overflow intrinsics", R"SOURCE(
587539const io = @import("std").io;
588540pub fn main(args: [][]u8) -> %void {
......@@ -730,29 +682,6 @@ pub fn main(args: [][]u8) -> %void {
730682}
731683 )SOURCE", "OK\n");
732684
733 add_simple_case("%% binary operator", R"SOURCE(
734const io = @import("std").io;
735error ItBroke;
736fn g(x: bool) -> %isize {
737 if (x) {
738 error.ItBroke
739 } else {
740 10
741 }
742}
743pub fn main(args: [][]u8) -> %void {
744 const a = g(true) %% 3;
745 const b = g(false) %% 3;
746 if (a != 3) {
747 %%io.stdout.printf("BAD\n");
748 }
749 if (b != 10) {
750 %%io.stdout.printf("BAD\n");
751 }
752 %%io.stdout.printf("OK\n");
753}
754 )SOURCE", "OK\n");
755
756685 add_simple_case("string concatenation", R"SOURCE(
757686const io = @import("std").io;
758687pub fn main(args: [][]u8) -> %void {
......@@ -808,54 +737,6 @@ pub fn main(args: [][]u8) -> %void {
808737}
809738 )SOURCE", "OK\n");
810739
811 add_simple_case("unwrap simple value from error", R"SOURCE(
812const io = @import("std").io;
813fn do() -> %isize {
814 13
815}
816
817pub fn main(args: [][]u8) -> %void {
818 const i = %%do();
819 if (i != 13) {
820 %%io.stdout.printf("BAD\n");
821 }
822 %%io.stdout.printf("OK\n");
823}
824 )SOURCE", "OK\n");
825
826 add_simple_case("store member function in variable", R"SOURCE(
827const io = @import("std").io;
828struct Foo {
829 x: i32,
830 fn member(foo: Foo) -> i32 { foo.x }
831}
832pub fn main(args: [][]u8) -> %void {
833 const instance = Foo { .x = 1234, };
834 const member_fn = Foo.member;
835 const result = member_fn(instance);
836 if (result != 1234) {
837 %%io.stdout.printf("BAD\n");
838 }
839 %%io.stdout.printf("OK\n");
840}
841 )SOURCE", "OK\n");
842
843 add_simple_case("call member function directly", R"SOURCE(
844const io = @import("std").io;
845struct Foo {
846 x: i32,
847 fn member(foo: Foo) -> i32 { foo.x }
848}
849pub fn main(args: [][]u8) -> %void {
850 const instance = Foo { .x = 1234, };
851 const result = Foo.member(instance);
852 if (result != 1234) {
853 %%io.stdout.printf("BAD\n");
854 }
855 %%io.stdout.printf("OK\n");
856}
857 )SOURCE", "OK\n");
858
859740 add_simple_case("call result of if else expression", R"SOURCE(
860741const io = @import("std").io;
861742fn a() -> []u8 { "a\n" }
......@@ -1496,7 +1377,8 @@ fn a(x: i32) {
14961377struct Foo {
14971378 y: [get()]u8,
14981379}
1499fn get() -> isize { 1 }
1380var global_var: isize = 1;
1381fn get() -> isize { global_var }
15001382 )SOURCE", 1, ".tmp_source.zig:3:9: error: unable to evaluate constant expression");
15011383
15021384
test/self_hosted.zig+97-6
......@@ -123,18 +123,18 @@ fn short_circuit() {
123123 var hit_3 = false;
124124 var hit_4 = false;
125125
126 if (true || { assert(false); false }) {
126 if (true || {assert_runtime(false); false}) {
127127 hit_1 = true;
128128 }
129129 if (false || { hit_2 = true; false }) {
130 assert(false);
130 assert_runtime(false);
131131 }
132132
133133 if (true && { hit_3 = true; false }) {
134 %%io.stdout.printf("BAD 3\n");
134 assert_runtime(false);
135135 }
136 if (false && { assert(false); false }) {
137 assert(false);
136 if (false && {assert_runtime(false); false}) {
137 assert_runtime(false);
138138 } else {
139139 hit_4 = true;
140140 }
......@@ -144,6 +144,11 @@ fn short_circuit() {
144144 assert(hit_4);
145145}
146146
147#static_eval_enable(false)
148fn assert_runtime(b: bool) {
149 if (!b) unreachable{}
150}
151
147152#attribute("test")
148153fn modify_operators() {
149154 var i : i32 = 0;
......@@ -509,6 +514,7 @@ enum Fruit {
509514 Orange,
510515 Banana,
511516}
517#static_eval_enable(false)
512518fn non_const_switch_on_enum(fruit: Fruit) {
513519 switch (fruit) {
514520 Apple => unreachable{},
......@@ -521,6 +527,7 @@ fn non_const_switch_on_enum(fruit: Fruit) {
521527fn switch_statement() {
522528 non_const_switch(SwitchStatmentFoo.C);
523529}
530#static_eval_enable(false)
524531fn non_const_switch(foo: SwitchStatmentFoo) {
525532 const val: i32 = switch (foo) {
526533 A => 1,
......@@ -549,6 +556,7 @@ enum SwitchProngWithVarEnum {
549556 Two: f32,
550557 Meh,
551558}
559#static_eval_enable(false)
552560fn switch_prong_with_var_fn(a: SwitchProngWithVarEnum) {
553561 switch(a) {
554562 One => |x| {
......@@ -569,6 +577,7 @@ fn err_return_in_assignment() {
569577 %%do_err_return_in_assignment();
570578}
571579
580#static_eval_enable(false)
572581fn do_err_return_in_assignment() -> %void {
573582 var x : i32 = undefined;
574583 x = %return make_a_non_err();
......@@ -608,7 +617,7 @@ fn explicit_cast_maybe_pointers() {
608617
609618#attribute("test")
610619fn const_expr_eval_on_single_expr_blocks() {
611 if (const_expr_eval_on_single_expr_blocks_fn(1, true) != 3) unreachable{}
620 assert(const_expr_eval_on_single_expr_blocks_fn(1, true) == 3);
612621}
613622
614623fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 {
......@@ -736,6 +745,7 @@ fn generic_malloc_free() {
736745 mem_free(u8)(a);
737746}
738747const some_mem : [100]u8 = undefined;
748#static_eval_enable(false)
739749fn mem_alloc(T: type)(n: isize) -> %[]T {
740750 return (&T)(&some_mem[0])[0...n];
741751}
......@@ -789,6 +799,7 @@ var goto_counter: i32 = 0;
789799fn goto_leave_defer_scope() {
790800 test_goto_leave_defer_scope(true);
791801}
802#static_eval_enable(false)
792803fn test_goto_leave_defer_scope(b: bool) {
793804 var it_worked = false;
794805
......@@ -820,3 +831,83 @@ fn cast_small_unsigned_to_larger_signed() {
820831}
821832fn cast_small_unsigned_to_larger_signed_1(x: u8) -> i16 { x }
822833fn cast_small_unsigned_to_larger_signed_2(x: u16) -> isize { x }
834
835
836#attribute("test")
837fn implicit_cast_after_unreachable() {
838 assert(outer() == 1234);
839}
840fn inner() -> i32 { 1234 }
841fn outer() -> isize {
842 return inner();
843}
844
845
846#attribute("test")
847fn else_if_expression() {
848 assert(else_if_expression_f(1) == 1);
849}
850fn else_if_expression_f(c: u8) -> u8 {
851 if (c == 0) {
852 0
853 } else if (c == 1) {
854 1
855 } else {
856 2
857 }
858}
859
860#attribute("test")
861fn err_binary_operator() {
862 const a = err_binary_operator_g(true) %% 3;
863 const b = err_binary_operator_g(false) %% 3;
864 assert(a == 3);
865 assert(b == 10);
866}
867error ItBroke;
868fn err_binary_operator_g(x: bool) -> %isize {
869 if (x) {
870 error.ItBroke
871 } else {
872 10
873 }
874}
875
876#attribute("test")
877fn unwrap_simple_value_from_error() {
878 const i = %%unwrap_simple_value_from_error_do();
879 assert(i == 13);
880}
881fn unwrap_simple_value_from_error_do() -> %isize { 13 }
882
883
884#attribute("test")
885fn store_member_function_in_variable() {
886 const instance = MemberFnTestFoo { .x = 1234, };
887 const member_fn = MemberFnTestFoo.member;
888 const result = member_fn(instance);
889 assert(result == 1234);
890}
891struct MemberFnTestFoo {
892 x: i32,
893 fn member(foo: MemberFnTestFoo) -> i32 { foo.x }
894}
895
896#attribute("test")
897fn call_member_function_directly() {
898 const instance = MemberFnTestFoo { .x = 1234, };
899 const result = MemberFnTestFoo.member(instance);
900 assert(result == 1234);
901}
902
903#attribute("test")
904fn member_functions() {
905 const r = MemberFnRand {.seed = 1234};
906 assert(r.get_seed() == 1234);
907}
908struct MemberFnRand {
909 seed: u32,
910 pub fn get_seed(r: MemberFnRand) -> u32 {
911 r.seed
912 }
913}