Commit fe38898246

Veikka Tuominen <git@vexu.eu>
2022-11-25 22:28:50
Sema: pass c_import_buf to child block in more places
Closes #13651
1 parent 587ef60
Changed files (3)
src
test
cases
compile_errors
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;
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,
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