authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-05 14:07:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-05 14:07:36-07:00
log0ff9a4d21c1b65c574659be295446f2dd7591723
treef9e68028c1ac06222194b00015fd741f7dae4f27
parent0e5fa87ac9695378683e770fbe0d577521860d35

stage1: resolve lazy values before comptime fn call


3 files changed, 29 insertions(+), 6 deletions(-)

src/stage1/analyze.cpp+1-4
......@@ -5674,7 +5674,7 @@ static uint32_t hash_combine_const_val(uint32_t hash_val, ZigValue *const_val) {
56745674 // return hash_combine(hash_val, &const_val);
56755675 // }
56765676 if (const_val->special != ConstValSpecialStatic) {
5677 printf("\nInvalid special: %d\n", const_val->special);
5677 fprintf(stderr, "\nInvalid special: %d\n", const_val->special);
56785678 }
56795679 assert(const_val->special == ConstValSpecialStatic);
56805680 hash_val = hash_combine(hash_val, &const_val->type->id);
......@@ -5776,9 +5776,6 @@ static uint32_t hash_combine_const_val(uint32_t hash_val, ZigValue *const_val) {
57765776 }
57775777 zig_unreachable();
57785778}
5779static uint32_t hash_const_val(ZigValue *const_val) {
5780 return hash_combine_const_val(HASH_INIT, const_val);
5781}
57825779
57835780uint32_t generic_fn_type_id_hash(GenericFnTypeId *id) {
57845781 uint32_t result = HASH_INIT;
src/stage1/ir.cpp+24-1
......@@ -12754,6 +12754,29 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, Scope *scope, AstNode *sour
1275412754 bool cacheable = fn_eval_cacheable(exec_scope, return_type);
1275512755 ZigValue *result = nullptr;
1275612756 if (cacheable) {
12757 // We are about to put ZigValues into a hash map. The hash of a lazy value and a
12758 // fully resolved value must equal, and so we must resolve the lazy values here.
12759 // The hash function asserts that none of the values are lazy.
12760 {
12761 Scope *scope = exec_scope;
12762 while (scope) {
12763 if (scope->id == ScopeIdVarDecl) {
12764 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
12765 if ((err = ir_resolve_lazy_recurse(ira,
12766 var_scope->var->decl_node,
12767 var_scope->var->const_value)))
12768 {
12769 return ira->codegen->invalid_inst_gen;
12770 }
12771 } else if (scope->id == ScopeIdFnDef) {
12772 break;
12773 } else {
12774 zig_unreachable();
12775 }
12776 scope = scope->parent;
12777 }
12778 }
12779
1275712780 auto entry = ira->codegen->memoized_fn_eval_table.maybe_get(exec_scope);
1275812781 if (entry)
1275912782 result = entry->value;
......@@ -25558,7 +25581,7 @@ static Error ir_resolve_lazy_recurse(IrAnalyze *ira, AstNode *source_node, ZigVa
2555825581 // This shouldn't be possible, it indicates an ICE.
2555925582 // NO_COMMIT
2556025583 ir_add_error_node(ira, source_node,
25561 buf_sprintf("This is a bug in the Zig compiler. Runtime value found in comptime known parameter."));
25584 buf_sprintf("This is a bug in the Zig compiler. Runtime value found in comptime known value."));
2556225585 return ErrorSemanticAnalyzeFail;
2556325586 }
2556425587 if (val->special != ConstValSpecialStatic)
src/stage1/zig0.cpp+4-1
......@@ -265,6 +265,7 @@ int main(int argc, char **argv) {
265265 const char *override_lib_dir = nullptr;
266266 const char *mcpu = nullptr;
267267 bool single_threaded = false;
268 bool is_test_build = false;
268269
269270 for (int i = 1; i < argc; i += 1) {
270271 char *arg = argv[i];
......@@ -272,6 +273,8 @@ int main(int argc, char **argv) {
272273 if (arg[0] == '-') {
273274 if (strcmp(arg, "--") == 0) {
274275 fprintf(stderr, "Unexpected end-of-parameter mark: %s\n", arg);
276 } else if (strcmp(arg, "--test") == 0) {
277 is_test_build = true;
275278 } else if (strcmp(arg, "-ODebug") == 0) {
276279 optimize_mode = BuildModeDebug;
277280 } else if (strcmp(arg, "-OReleaseFast") == 0) {
......@@ -446,7 +449,7 @@ int main(int argc, char **argv) {
446449 nullptr, 0,
447450 in_file, strlen(in_file),
448451 override_lib_dir, strlen(override_lib_dir),
449 &target, false);
452 &target, is_test_build);
450453
451454 stage1->main_progress_node = root_progress_node;
452455 stage1->root_name_ptr = out_name;