Skip to content

Commit 623d5cc

Browse files
mluggandrewrk
authored andcommitted
Sema: fix handling of @This() on opaques
Resolves: #22869
1 parent ba97b1a commit 623d5cc

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/Sema.zig

+12-6
Original file line numberDiff line numberDiff line change
@@ -17841,11 +17841,18 @@ fn zirThis(
1784117841
const zcu = pt.zcu;
1784217842
const namespace = pt.zcu.namespacePtr(block.namespace);
1784317843

17844-
const new_ty = try pt.ensureTypeUpToDate(namespace.owner_type);
17845-
17846-
switch (pt.zcu.intern_pool.indexToKey(new_ty)) {
17847-
.struct_type, .union_type => try sema.declareDependency(.{ .interned = new_ty }),
17844+
switch (pt.zcu.intern_pool.indexToKey(namespace.owner_type)) {
17845+
.opaque_type => {
17846+
// Opaque types are never outdated since they don't undergo type resolution, so nothing to do!
17847+
return Air.internedToRef(namespace.owner_type);
17848+
},
17849+
.struct_type, .union_type => {
17850+
const new_ty = try pt.ensureTypeUpToDate(namespace.owner_type);
17851+
try sema.declareDependency(.{ .interned = new_ty });
17852+
return Air.internedToRef(new_ty);
17853+
},
1784817854
.enum_type => {
17855+
const new_ty = try pt.ensureTypeUpToDate(namespace.owner_type);
1784917856
try sema.declareDependency(.{ .interned = new_ty });
1785017857
// Since this is an enum, it has to be resolved immediately.
1785117858
// `ensureTypeUpToDate` has resolved the new type if necessary.
@@ -17854,11 +17861,10 @@ fn zirThis(
1785417861
if (zcu.failed_analysis.contains(ty_unit) or zcu.transitive_failed_analysis.contains(ty_unit)) {
1785517862
return error.AnalysisFail;
1785617863
}
17864+
return Air.internedToRef(new_ty);
1785717865
},
17858-
.opaque_type => {},
1785917866
else => unreachable,
1786017867
}
17861-
return Air.internedToRef(new_ty);
1786217868
}
1786317869

1786417870
fn zirClosureGet(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {

test/behavior/this.zig

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const expect = @import("std").testing.expect;
1+
const std = @import("std");
2+
const expect = std.testing.expect;
23
const builtin = @import("builtin");
34

45
const module = @This();
@@ -55,3 +56,10 @@ test "this used as optional function parameter" {
5556
global.enter = prev;
5657
global.enter(null);
5758
}
59+
60+
test "@This() in opaque" {
61+
const T = opaque {
62+
const Self = @This();
63+
};
64+
comptime std.debug.assert(T.Self == T);
65+
}

0 commit comments

Comments
 (0)