authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2019-10-09 14:46:12-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-09 17:36:13-04:00
log42f2814d9acc014c84034468b2923195aa547ce1
tree0825a836f09dbc1de025dda44929cef9419869a8
parente0ab685467567f6da02be865654a2c4dc5f8c3a9

stage1: fix root top-level-struct typename

- during diagnostics the string representation for root was empty and now is `(root)` - retrofitted all other namespace-qualified type naming to elide prefixing with root closes #2032

4 files changed, 33 insertions(+), 10 deletions(-)

src/analyze.cpp+16-6
......@@ -999,7 +999,12 @@ static ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *
999999 entry->data.structure.root_struct = root_struct;
10001000 entry->data.structure.layout = ContainerLayoutAuto;
10011001
1002 buf_init_from_str(&entry->name, full_name);
1002 if (full_name[0] == '\0') {
1003 buf_init_from_str(&entry->name, "(root)");
1004 } else {
1005 buf_init_from_str(&entry->name, full_name);
1006 }
1007
10031008 return entry;
10041009}
10051010
......@@ -3156,7 +3161,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31563161 return ErrorNone;
31573162}
31583163
3159static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, bool is_test) {
3164void append_namespace_qualification(CodeGen *g, Buf *buf, ZigType *container_type) {
3165 if (g->root_import == container_type || buf_len(&container_type->name) == 0) return;
3166 buf_append_buf(buf, &container_type->name);
3167 buf_append_char(buf, NAMESPACE_SEP_CHAR);
3168}
3169
3170static void get_fully_qualified_decl_name(CodeGen *g, Buf *buf, Tld *tld, bool is_test) {
31603171 buf_resize(buf, 0);
31613172
31623173 Scope *scope = tld->parent_scope;
......@@ -3164,8 +3175,7 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, bool is_test) {
31643175 scope = scope->parent;
31653176 }
31663177 ScopeDecls *decls_scope = reinterpret_cast<ScopeDecls *>(scope);
3167 buf_append_buf(buf, &decls_scope->container_type->name);
3168 if (buf_len(buf) != 0) buf_append_char(buf, NAMESPACE_SEP_CHAR);
3178 append_namespace_qualification(g, buf, decls_scope->container_type);
31693179 if (is_test) {
31703180 buf_append_str(buf, "test \"");
31713181 buf_append_buf(buf, tld->name);
......@@ -3288,7 +3298,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
32883298 if (fn_proto->is_export || is_extern) {
32893299 buf_init_from_buf(&fn_table_entry->symbol_name, tld_fn->base.name);
32903300 } else {
3291 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, false);
3301 get_fully_qualified_decl_name(g, &fn_table_entry->symbol_name, &tld_fn->base, false);
32923302 }
32933303
32943304 if (fn_proto->is_export) {
......@@ -3352,7 +3362,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
33523362 } else if (source_node->type == NodeTypeTestDecl) {
33533363 ZigFn *fn_table_entry = create_fn_raw(g, FnInlineAuto);
33543364
3355 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, true);
3365 get_fully_qualified_decl_name(g, &fn_table_entry->symbol_name, &tld_fn->base, true);
33563366
33573367 tld_fn->fn_entry = fn_table_entry;
33583368
src/analyze.hpp+1
......@@ -90,6 +90,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
9090ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
9191 bool is_const, ConstExprValue *init_value, Tld *src_tld, ZigType *var_type);
9292ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node);
93void append_namespace_qualification(CodeGen *g, Buf *buf, ZigType *container_type);
9394ZigFn *create_fn(CodeGen *g, AstNode *proto_node);
9495ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);
9596void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc);
src/ir.cpp+4-4
......@@ -7852,8 +7852,8 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
78527852{
78537853 if (exec->name) {
78547854 ZigType *import = get_scope_import(scope);
7855 Buf *namespace_name = buf_create_from_buf(&import->name);
7856 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
7855 Buf *namespace_name = buf_alloc();
7856 append_namespace_qualification(codegen, namespace_name, import);
78577857 buf_append_buf(namespace_name, exec->name);
78587858 buf_init_from_buf(out_bare_name, exec->name);
78597859 return namespace_name;
......@@ -7867,8 +7867,8 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
78677867 return name;
78687868 } else {
78697869 ZigType *import = get_scope_import(scope);
7870 Buf *namespace_name = buf_create_from_buf(&import->name);
7871 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);
7870 Buf *namespace_name = buf_alloc();
7871 append_namespace_qualification(codegen, namespace_name, import);
78727872 buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,
78737873 source_node->line + 1, source_node->column + 1);
78747874 buf_init_from_buf(out_bare_name, namespace_name);
test/compile_errors.zig+12
......@@ -6639,4 +6639,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66396639 "tmp.zig:2:18: error: opaque return type 'FooType' not allowed",
66406640 "tmp.zig:1:1: note: declared here",
66416641 );
6642
6643 // fixed bug #2032
6644 cases.add(
6645 "compile diagnostic string for top level decl type",
6646 \\export fn entry() void {
6647 \\ var foo: u32 = @This(){};
6648 \\}
6649 ,
6650 "tmp.zig:2:27: error: expected type 'u32', found '(root)'",
6651 "tmp.zig:1:1: note: (root) declared here",
6652 "tmp.zig:2:5: note: referenced here",
6653 );
66426654}