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 *...@@ -999,7 +999,12 @@ static ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *
999 entry->data.structure.root_struct = root_struct;999 entry->data.structure.root_struct = root_struct;
1000 entry->data.structure.layout = ContainerLayoutAuto;1000 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
1003 return entry;1008 return entry;
1004}1009}
10051010
...@@ -3156,7 +3161,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3156,7 +3161,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3156 return ErrorNone;3161 return ErrorNone;
3157}3162}
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) {
3160 buf_resize(buf, 0);3171 buf_resize(buf, 0);
31613172
3162 Scope *scope = tld->parent_scope;3173 Scope *scope = tld->parent_scope;
...@@ -3164,8 +3175,7 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, bool is_test) {...@@ -3164,8 +3175,7 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, bool is_test) {
3164 scope = scope->parent;3175 scope = scope->parent;
3165 }3176 }
3166 ScopeDecls *decls_scope = reinterpret_cast<ScopeDecls *>(scope);3177 ScopeDecls *decls_scope = reinterpret_cast<ScopeDecls *>(scope);
3167 buf_append_buf(buf, &decls_scope->container_type->name);3178 append_namespace_qualification(g, buf, decls_scope->container_type);
3168 if (buf_len(buf) != 0) buf_append_char(buf, NAMESPACE_SEP_CHAR);
3169 if (is_test) {3179 if (is_test) {
3170 buf_append_str(buf, "test \"");3180 buf_append_str(buf, "test \"");
3171 buf_append_buf(buf, tld->name);3181 buf_append_buf(buf, tld->name);
...@@ -3288,7 +3298,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {...@@ -3288,7 +3298,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
3288 if (fn_proto->is_export || is_extern) {3298 if (fn_proto->is_export || is_extern) {
3289 buf_init_from_buf(&fn_table_entry->symbol_name, tld_fn->base.name);3299 buf_init_from_buf(&fn_table_entry->symbol_name, tld_fn->base.name);
3290 } else {3300 } 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);
3292 }3302 }
32933303
3294 if (fn_proto->is_export) {3304 if (fn_proto->is_export) {
...@@ -3352,7 +3362,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {...@@ -3352,7 +3362,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
3352 } else if (source_node->type == NodeTypeTestDecl) {3362 } else if (source_node->type == NodeTypeTestDecl) {
3353 ZigFn *fn_table_entry = create_fn_raw(g, FnInlineAuto);3363 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
3357 tld_fn->fn_entry = fn_table_entry;3367 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...@@ -90,6 +90,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
90ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,90ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
91 bool is_const, ConstExprValue *init_value, Tld *src_tld, ZigType *var_type);91 bool is_const, ConstExprValue *init_value, Tld *src_tld, ZigType *var_type);
92ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node);92ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node);
93void append_namespace_qualification(CodeGen *g, Buf *buf, ZigType *container_type);
93ZigFn *create_fn(CodeGen *g, AstNode *proto_node);94ZigFn *create_fn(CodeGen *g, AstNode *proto_node);
94ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);95ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);
95void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc);96void 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...@@ -7852,8 +7852,8 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
7852{7852{
7853 if (exec->name) {7853 if (exec->name) {
7854 ZigType *import = get_scope_import(scope);7854 ZigType *import = get_scope_import(scope);
7855 Buf *namespace_name = buf_create_from_buf(&import->name);7855 Buf *namespace_name = buf_alloc();
7856 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);7856 append_namespace_qualification(codegen, namespace_name, import);
7857 buf_append_buf(namespace_name, exec->name);7857 buf_append_buf(namespace_name, exec->name);
7858 buf_init_from_buf(out_bare_name, exec->name);7858 buf_init_from_buf(out_bare_name, exec->name);
7859 return namespace_name;7859 return namespace_name;
...@@ -7867,8 +7867,8 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char...@@ -7867,8 +7867,8 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
7867 return name;7867 return name;
7868 } else {7868 } else {
7869 ZigType *import = get_scope_import(scope);7869 ZigType *import = get_scope_import(scope);
7870 Buf *namespace_name = buf_create_from_buf(&import->name);7870 Buf *namespace_name = buf_alloc();
7871 if (buf_len(namespace_name) != 0) buf_append_char(namespace_name, NAMESPACE_SEP_CHAR);7871 append_namespace_qualification(codegen, namespace_name, import);
7872 buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,7872 buf_appendf(namespace_name, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize, kind_name,
7873 source_node->line + 1, source_node->column + 1);7873 source_node->line + 1, source_node->column + 1);
7874 buf_init_from_buf(out_bare_name, namespace_name);7874 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 {...@@ -6639,4 +6639,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
6639 "tmp.zig:2:18: error: opaque return type 'FooType' not allowed",6639 "tmp.zig:2:18: error: opaque return type 'FooType' not allowed",
6640 "tmp.zig:1:1: note: declared here",6640 "tmp.zig:1:1: note: declared here",
6641 );6641 );
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 );
6642}6654}