From 34eb9f18acc2370973adcc7be0d010d62300e9a9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 9 Feb 2019 20:41:26 -0500 Subject: [PATCH] fix not updating debug info type of optional error sets There's an unfortunate footgun in the current design of error sets. The debug info type for every error set is the same as the debug info type of the global error set, which is essentially an enum forward declaration. The problem is that when we "replace" the forward declaration with the final value, once we know all the possible errors, we have to update the pointers of every error set. So the footgun is that if you ever copy the debug info type of the global error set, you have to add the address of the pointer to a list of pointers that need to be updated once we "replace" the forward declaration. I activated the footgun when I introduced the optimization that `?anyerror` types are the same size as `anyerror` types (using 0 as the null value), because I introduced a pointer copy of the global error set debug info type, but forgot to add it to the list. I'm sure that there is a better way to code this, which does not have the footgun, but this commit contains only a fix, not a reworking of the logic. closes #1937 --- src/analyze.cpp | 3 +++ test/stage1/behavior/error.zig | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/analyze.cpp b/src/analyze.cpp index 0c493ebda1..970d1cc382 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -594,6 +594,9 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) { // function types are technically pointers entry->type_ref = child_type->type_ref; entry->di_type = child_type->di_type; + if (entry->di_type == g->builtin_types.entry_global_error_set->di_type) { + g->error_di_types.append(&entry->di_type); + } } else { assert(child_type->di_type); // create a struct with a boolean whether this is the null value diff --git a/test/stage1/behavior/error.zig b/test/stage1/behavior/error.zig index 265ddd9d6c..7d9dae7bdd 100644 --- a/test/stage1/behavior/error.zig +++ b/test/stage1/behavior/error.zig @@ -330,3 +330,8 @@ test "optional error set is the same size as error set" { expect(S.returnsOptErrSet() == null); comptime expect(S.returnsOptErrSet() == null); } + +test "debug info for optional error set" { + const SomeError = error{Hello}; + var a_local_variable: ?SomeError = null; +}