authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-14 15:20:52-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-14 15:28:52-04:00
log42ea2d0d1c3b8cafdfc9a383cbb1bab274eb0140
treeb4cda1880f432c47c228747c356200b01604bafb
parent9e8db5b7505a6c2e601c8a94b9d8a4282b5df184
signaturelock-open Commit is signed but in an unrecognized format.

fix `@export` for arrays and allow sections on extern variables

previously `@export` for an array would panic with a TODO message. now it will do the export. However, it uses the variable's name rather than the name passed to `@export`. Issue #2679 remains open for that problem.

3 files changed, 11 insertions(+), 31 deletions(-)

src/analyze.cpp+2-10
......@@ -2780,12 +2780,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
27802780 fn_table_entry->type_entry = analyze_fn_type(g, source_node, child_scope, fn_table_entry);
27812781
27822782 if (fn_proto->section_expr != nullptr) {
2783 if (fn_table_entry->body_node == nullptr) {
2784 add_node_error(g, fn_proto->section_expr,
2785 buf_sprintf("cannot set section of external function '%s'", buf_ptr(&fn_table_entry->symbol_name)));
2786 } else {
2787 analyze_const_string(g, child_scope, fn_proto->section_expr, &fn_table_entry->section_name);
2788 }
2783 analyze_const_string(g, child_scope, fn_proto->section_expr, &fn_table_entry->section_name);
27892784 }
27902785
27912786 if (fn_table_entry->type_entry->id == ZigTypeIdInvalid) {
......@@ -3258,10 +3253,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32583253 }
32593254
32603255 if (var_decl->section_expr != nullptr) {
3261 if (var_decl->is_extern) {
3262 add_node_error(g, var_decl->section_expr,
3263 buf_sprintf("cannot set section of external variable '%s'", buf_ptr(var_decl->symbol)));
3264 } else if (!analyze_const_string(g, tld_var->base.parent_scope, var_decl->section_expr, &tld_var->section_name)) {
3256 if (!analyze_const_string(g, tld_var->base.parent_scope, var_decl->section_expr, &tld_var->section_name)) {
32653257 tld_var->section_name = nullptr;
32663258 }
32673259 }
src/ir.cpp+9-1
......@@ -13901,6 +13901,15 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1390113901 want_var_export = true;
1390213902 }
1390313903 break;
13904 case ZigTypeIdArray:
13905 if (!type_allowed_in_extern(ira->codegen, target->value.type->data.array.child_type)) {
13906 ir_add_error(ira, target,
13907 buf_sprintf("array element type '%s' not extern-compatible",
13908 buf_ptr(&target->value.type->data.array.child_type->name)));
13909 } else {
13910 want_var_export = true;
13911 }
13912 break;
1390413913 case ZigTypeIdMetaType: {
1390513914 ZigType *type_value = target->value.data.x_type;
1390613915 switch (type_value->id) {
......@@ -13968,7 +13977,6 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1396813977 case ZigTypeIdInt:
1396913978 case ZigTypeIdFloat:
1397013979 case ZigTypeIdPointer:
13971 case ZigTypeIdArray:
1397213980 case ZigTypeIdComptimeFloat:
1397313981 case ZigTypeIdComptimeInt:
1397413982 case ZigTypeIdUndefined:
test/compile_errors.zig-20
......@@ -4645,16 +4645,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46454645 "tmp.zig:1:1: note: declared here",
46464646 );
46474647
4648 cases.add(
4649 "setting a section on an extern variable",
4650 \\extern var foo: i32 linksection(".text2");
4651 \\export fn entry() i32 {
4652 \\ return foo;
4653 \\}
4654 ,
4655 "tmp.zig:1:33: error: cannot set section of external variable 'foo'",
4656 );
4657
46584648 cases.add(
46594649 "setting a section on a local variable",
46604650 \\export fn entry() i32 {
......@@ -4665,16 +4655,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46654655 "tmp.zig:2:30: error: cannot set section of local variable 'foo'",
46664656 );
46674657
4668 cases.add(
4669 "setting a section on an extern fn",
4670 \\extern fn foo() linksection(".text2") void;
4671 \\export fn entry() void {
4672 \\ foo();
4673 \\}
4674 ,
4675 "tmp.zig:1:29: error: cannot set section of external function 'foo'",
4676 );
4677
46784658 cases.add(
46794659 "returning address of local variable - simple",
46804660 \\export fn foo() *i32 {