Skip to content

Commit 94dabb8

Browse files
authored
fix(semantic): missing comptime scope flags on comptime { ... } (#179)
1 parent 3e65d86 commit 94dabb8

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/semantic/SemanticBuilder.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ inline fn visitRecursiveSlice(self: *SemanticBuilder, node_id: NodeIndex) !void
538538
fn visitBlock(self: *SemanticBuilder, statements: []const NodeIndex) !void {
539539
const NON_COMPTIME_BLOCKS: Scope.Flags = .{ .s_test = true, .s_block = true, .s_function = true };
540540
const is_root = self.currentScope() == ROOT_SCOPE;
541-
const is_comptime = is_root and !self._curr_scope_flags.intersects(NON_COMPTIME_BLOCKS);
542541
const was_comptime = self._curr_scope_flags.s_comptime;
542+
const is_comptime = was_comptime or (is_root and !self._curr_scope_flags.intersects(NON_COMPTIME_BLOCKS));
543543

544544
self._curr_scope_flags.s_comptime = is_comptime;
545545
defer self._curr_scope_flags.s_comptime = was_comptime;

src/semantic/test/scope_flags_test.zig

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,33 @@ test "function signatures and body scopes" {
109109
// try testXDeclScope(cases);
110110
// }
111111

112-
test "comptime scopes" {
112+
test "block scopes" {
113113
const cases = &[_]TestCase{
114+
// .{
115+
// "const y = { const x = 1; };",
116+
// Scope.Flags{ .s_block = true, .s_comptime = true },
117+
// },
118+
// .{
119+
// \\fn foo() u32 {
120+
// \\ const y = blk: {
121+
// \\ const x: u32 = 1;
122+
// \\ break :blk x;
123+
// \\ };
124+
// \\
125+
// \\ return y;
126+
// \\}
127+
// ,
128+
// .{ .s_block = true },
129+
// },
114130
.{
115-
"const y = { const x = 1; };",
116-
Scope.Flags{ .s_block = true, .s_comptime = true },
131+
\\fn foo() void {
132+
\\ comptime {
133+
\\ const x: u32 = 1;
134+
\\ }
135+
\\ return y;
136+
\\}
137+
,
138+
.{ .s_block = true, .s_comptime = true },
117139
},
118140
};
119141

test/snapshots/snapshot-coverage/simple/pass/cond_if.zig.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322
"children": [
323323
{
324324
"id": util.id.NominalId(u32)(15),
325-
"flags": ["block"],
325+
"flags": ["block", "comptime"],
326326
"bindings": {
327327

328328
},

test/snapshots/snapshot-coverage/simple/pass/fn_comptime.zig.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,14 @@
259259
"children": [
260260
{
261261
"id": util.id.NominalId(u32)(2),
262-
"flags": ["function", "block"],
262+
"flags": ["function", "block", "comptime"],
263263
"bindings": {
264264

265265
},
266266
"children": [
267267
{
268268
"id": util.id.NominalId(u32)(3),
269-
"flags": ["struct", "block"],
269+
"flags": ["struct", "block", "comptime"],
270270
"bindings": {
271271
"inner": 3,
272272
"Self": 4,
@@ -312,22 +312,22 @@
312312
"children": [
313313
{
314314
"id": util.id.NominalId(u32)(7),
315-
"flags": ["function", "block"],
315+
"flags": ["function", "block", "comptime"],
316316
"bindings": {
317317
"l": 9,
318318

319319
},
320320
"children": [
321321
{
322322
"id": util.id.NominalId(u32)(8),
323-
"flags": [],
323+
"flags": ["comptime"],
324324
"bindings": {
325325

326326
},
327327
"children": [
328328
{
329329
"id": util.id.NominalId(u32)(9),
330-
"flags": ["block"],
330+
"flags": ["block", "comptime"],
331331
"bindings": {
332332

333333
},
@@ -338,15 +338,15 @@
338338

339339
}, {
340340
"id": util.id.NominalId(u32)(10),
341-
"flags": [],
341+
"flags": ["comptime"],
342342
"bindings": {
343343

344344
},
345345
"children": [],
346346

347347
}, {
348348
"id": util.id.NominalId(u32)(11),
349-
"flags": ["struct", "block"],
349+
"flags": ["struct", "block", "comptime"],
350350
"bindings": {
351351
"inner": 10,
352352

@@ -378,22 +378,22 @@
378378
"children": [
379379
{
380380
"id": util.id.NominalId(u32)(14),
381-
"flags": ["block"],
381+
"flags": ["block", "comptime"],
382382
"bindings": {
383383
"c": 14,
384384

385385
},
386386
"children": [
387387
{
388388
"id": util.id.NominalId(u32)(15),
389-
"flags": [],
389+
"flags": ["comptime"],
390390
"bindings": {
391391

392392
},
393393
"children": [
394394
{
395395
"id": util.id.NominalId(u32)(16),
396-
"flags": ["block"],
396+
"flags": ["block", "comptime"],
397397
"bindings": {
398398

399399
},
@@ -404,7 +404,7 @@
404404

405405
}, {
406406
"id": util.id.NominalId(u32)(17),
407-
"flags": [],
407+
"flags": ["comptime"],
408408
"bindings": {
409409

410410
},

0 commit comments

Comments
 (0)