authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-17 20:16:23+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-18 16:45:07+03:00
log596ca6cf70cf43c27e31bbcfc36bcdc70b13897a
tree1f6915d289cf77ac70d7a7f23ca7f2b6f73e4aa6
parent78962eeeda07d613ebdfd239082268a6702c19db

allow non-pointer extern opaque variables


4 files changed, 16 insertions(+), 6 deletions(-)

src/analyze.cpp+7-4
...@@ -3823,15 +3823,18 @@ static Error resolve_decl_container(CodeGen *g, TldContainer *tld_container) {...@@ -3823,15 +3823,18 @@ static Error resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
3823 }3823 }
3824}3824}
38253825
3826ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry) {3826ZigType *validate_var_type(CodeGen *g, AstNodeVariableDeclaration *source_node, ZigType *type_entry) {
3827 switch (type_entry->id) {3827 switch (type_entry->id) {
3828 case ZigTypeIdInvalid:3828 case ZigTypeIdInvalid:
3829 return g->builtin_types.entry_invalid;3829 return g->builtin_types.entry_invalid;
3830 case ZigTypeIdOpaque:
3831 if (source_node->is_extern)
3832 return type_entry;
3833 ZIG_FALLTHROUGH;
3830 case ZigTypeIdUnreachable:3834 case ZigTypeIdUnreachable:
3831 case ZigTypeIdUndefined:3835 case ZigTypeIdUndefined:
3832 case ZigTypeIdNull:3836 case ZigTypeIdNull:
3833 case ZigTypeIdOpaque:3837 add_node_error(g, source_node->type, buf_sprintf("variable of type '%s' not allowed",
3834 add_node_error(g, source_node, buf_sprintf("variable of type '%s' not allowed",
3835 buf_ptr(&type_entry->name)));3838 buf_ptr(&type_entry->name)));
3836 return g->builtin_types.entry_invalid;3839 return g->builtin_types.entry_invalid;
3837 case ZigTypeIdComptimeFloat:3840 case ZigTypeIdComptimeFloat:
...@@ -3973,7 +3976,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {...@@ -3973,7 +3976,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
3973 } else {3976 } else {
3974 tld_var->analyzing_type = true;3977 tld_var->analyzing_type = true;
3975 ZigType *proposed_type = analyze_type_expr(g, tld_var->base.parent_scope, var_decl->type);3978 ZigType *proposed_type = analyze_type_expr(g, tld_var->base.parent_scope, var_decl->type);
3976 explicit_type = validate_var_type(g, var_decl->type, proposed_type);3979 explicit_type = validate_var_type(g, var_decl, proposed_type);
3977 }3980 }
3978 }3981 }
39793982
src/analyze.hpp+1-1
...@@ -77,7 +77,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all...@@ -77,7 +77,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all
77ZigType *get_src_ptr_type(ZigType *type);77ZigType *get_src_ptr_type(ZigType *type);
78uint32_t get_ptr_align(CodeGen *g, ZigType *type);78uint32_t get_ptr_align(CodeGen *g, ZigType *type);
79bool get_ptr_const(CodeGen *g, ZigType *type);79bool get_ptr_const(CodeGen *g, ZigType *type);
80ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry);80ZigType *validate_var_type(CodeGen *g, AstNodeVariableDeclaration *source_node, ZigType *type_entry);
81ZigType *container_ref_type(ZigType *type_entry);81ZigType *container_ref_type(ZigType *type_entry);
82bool type_is_complete(ZigType *type_entry);82bool type_is_complete(ZigType *type_entry);
83bool type_is_resolved(ZigType *type_entry, ResolveStatus status);83bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
src/ir.cpp+1-1
...@@ -18505,7 +18505,7 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV...@@ -18505,7 +18505,7 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
18505 if (decl_var_instruction->var_type != nullptr) {18505 if (decl_var_instruction->var_type != nullptr) {
18506 var_type = decl_var_instruction->var_type->child;18506 var_type = decl_var_instruction->var_type->child;
18507 ZigType *proposed_type = ir_resolve_type(ira, var_type);18507 ZigType *proposed_type = ir_resolve_type(ira, var_type);
18508 explicit_type = validate_var_type(ira->codegen, var_type->base.source_node, proposed_type);18508 explicit_type = validate_var_type(ira->codegen, &var->decl_node->data.variable_declaration, proposed_type);
18509 if (type_is_invalid(explicit_type)) {18509 if (type_is_invalid(explicit_type)) {
18510 var->var_type = ira->codegen->builtin_types.entry_invalid;18510 var->var_type = ira->codegen->builtin_types.entry_invalid;
18511 return ira->codegen->invalid_inst_gen;18511 return ira->codegen->invalid_inst_gen;
test/stage1/behavior/misc.zig+7
...@@ -713,3 +713,10 @@ test "auto created variables have correct alignment" {...@@ -713,3 +713,10 @@ test "auto created variables have correct alignment" {
713 expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);713 expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
714 comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);714 comptime expect(S.foo("\x7a\x7a\x7a\x7a") == 0x7a7a7a7a);
715}715}
716
717extern var opaque_extern_var: @Type(.Opaque);
718var var_to_export: u32 = 42;
719test "extern variable with non-pointer opaque type" {
720 @export(var_to_export, .{ .name = "opaque_extern_var" });
721 expect(@ptrCast(*align(1) u32, &opaque_extern_var).* == 42);
722}