zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit fe388982462db067bdb697ec3e89c2c13ce2ffa8 (tree)
parent 587ef60a28ae7c1c780e02ecf60b331ead53bc7f
Author: Veikka Tuominen <git@vexu.eu>
Date:   Fri, 25 Nov 2022 23:28:50 +0200

Sema: pass c_import_buf to child block in more places

Closes  #13651

Diffstat:
Msrc/AstGen.zig | 3+++
Msrc/Sema.zig | 2++
Atest/cases/compile_errors/cimport.zig | 15+++++++++++++++
3 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/src/AstGen.zig b/src/AstGen.zig @@ -7674,6 +7674,7 @@ fn typeOf( var typeof_scope = gz.makeSubBlock(scope); typeof_scope.force_comptime = false; + typeof_scope.c_import = false; defer typeof_scope.unstack(); const ty_expr = try reachableExpr(&typeof_scope, &typeof_scope.base, .{ .rl = .none }, args[0], node); @@ -8532,6 +8533,8 @@ fn cImport( const astgen = gz.astgen; const gpa = astgen.gpa; + if (gz.c_import) return gz.astgen.failNode(node, "cannot nest @cImport", .{}); + var block_scope = gz.makeSubBlock(scope); block_scope.force_comptime = true; block_scope.c_import = true; diff --git a/src/Sema.zig b/src/Sema.zig @@ -5105,6 +5105,7 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErro .is_typeof = parent_block.is_typeof, .want_safety = parent_block.want_safety, .float_mode = parent_block.float_mode, + .c_import_buf = parent_block.c_import_buf, .runtime_cond = parent_block.runtime_cond, .runtime_loop = parent_block.runtime_loop, .runtime_index = parent_block.runtime_index, @@ -10266,6 +10267,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError .comptime_reason = block.comptime_reason, .is_typeof = block.is_typeof, .switch_else_err_ty = else_error_ty, + .c_import_buf = block.c_import_buf, .runtime_cond = block.runtime_cond, .runtime_loop = block.runtime_loop, .runtime_index = block.runtime_index, diff --git a/test/cases/compile_errors/cimport.zig b/test/cases/compile_errors/cimport.zig @@ -0,0 +1,15 @@ +const b = @cDefine("foo", "1"); +const c = @cImport({ + _ = @TypeOf(@cDefine("foo", "1")); +}); +const d = @cImport({ + _ = @cImport(@cDefine("foo", "1")); +}); + +// error +// backend=stage2 +// target=native +// +// :1:11: error: C define valid only inside C import block +// :3:17: error: C define valid only inside C import block +// :6:9: error: cannot nest @cImport