-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Using @TypeOf
for peer type resolution can trigger false dependency cycles
#12000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I wanted to mention this comes from a real world use case (despite the exotic type situation): I'm currently struggling with this in a parser combinator library I'm writing. Recursive parse trees lead to recursive types (#6211 is another issue I've had to work around), and I use peer resolution in some cases to combine multiple function results for different alternatives in the grammar. |
dependency is true. |
It's a pointer, so the size of T doesn't need to be known afaict. For reference, this does work: const T = struct {
next: ?*const T,
}; |
Tried to workaround this by re-implementing peer type resolution (poorly) in userspace. This seems possible, but it has to be manually inlined to avoid dependency cycles caused by constructing new types in a function call: fn Ident(comptime U: type) type {
return U;
}
fn Optional(comptime U: type) type {
return ?U;
}
fn PeerResolve(comptime T: type, comptime U: type) type {
if (T == @TypeOf(null) and U != void) {
return ?U;
} else if (T != void and U == @TypeOf(null)) {
return ?T;
} else unreachable;
}
const Foo = struct {
const MaybePtrFoo1 = @TypeOf(null, @as(*const Foo, undefined));
const MaybePtrFoo2 = PeerResolve(@TypeOf(null), *const Foo);
// next1: MaybePtrFoo1, // error: struct 'Foo' depends on itself
// next2: MaybePtrFoo2, // error: struct 'Foo' depends on itself
// next3: Optional(*const Foo), // error: struct 'Foo' depends on itself
next4: ?*const Foo,
next5: Ident(*const Foo),
next6: *const Ident(Foo),
};
test {
_ = Foo;
} |
Not true in stage1 #6706. The example is special cased. |
Ah, that explains it - Thanks for the info @Vexu. Glad to see this is already resolved in stage2 🙂 |
Closes ziglang#12169 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12057 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12116 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11986 Closes ziglang#11995 Closes ziglang#12000
Closes ziglang#12169 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12057 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12116 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11986 Closes ziglang#11995 Closes ziglang#12000
Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12116 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11995 Closes ziglang#12000
Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12116 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11995 Closes ziglang#12000
Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12116 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11995 Closes ziglang#12000
Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11995 Closes ziglang#12000
Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11995 Closes ziglang#12000
Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#12051 Closes ziglang#12092 Closes ziglang#12119 Closes ziglang#12142 Closes ziglang#12450 Closes ziglang#13113 Closes ziglang#11995 Closes ziglang#12000
Zig Version
0.10.0-dev.2840+32fb86821
Steps to Reproduce
Using
@TypeOf()
for peer type resolution requires using@as(T, undefined)
or similar.The problem is that this will trigger resolution of T:
Expected Behavior
This should resolve to a type:
Actual Behavior
The text was updated successfully, but these errors were encountered: