diff --git a/src/AstGen.zig b/src/AstGen.zig index db80a2f350..945c64107c 100644 --- 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 index d6a563bd36..f056164f80 100644 --- 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 new file mode 100644 index 0000000000..855f9aeb40 --- /dev/null +++ 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