-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
false positive circular dependency when a comptime-called function has function as a return type that uses generic parameters from the outer function #11367
Copy link
Copy link
Open
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone
Description
Zig Version: 0.10.0-dev.1694+ecd756834
test {
var map: HashMapUnmanaged(AutoContext(*Decl)) = .{};
_ = map;
}
const Decl = struct {
namespace: *Namespace,
};
const Namespace = struct {
decls: HashMapUnmanaged(AutoContext(*Decl)),
};
fn getFn(comptime K: type) (fn (K) u64) {
return struct {
fn hash(key: K) u64 {
_ = key;
return 1234;
}
}.hash;
}
fn AutoContext(comptime K: type) type {
return struct {
const hash = getFn(K);
};
}
fn HashMapUnmanaged(comptime Context: type) type {
comptime verifyContext(Context);
return struct {};
}
fn verifyContext(comptime Context: type) void {
const hash = Context.hash;
_ = hash;
}Expected to pass. Instead:
$ ./stage2/bin/zig test test3.zig
test3.zig:28:9: error: 'hash' depends on itself
const hash = getFn(K);
^
This happens because:
verifyContexttries to look at thehashfunction, invoking comptimegetFn.- while evaluating the return type of
getFn, zig tries to check ifKis a comptime-only type. Kis*Declso to check if it is a comptime-only type, it looks at Decl fields types, and then recursively looks at Namespace field types.HashMapUnmanaged(AutoContext(*Decl))is comptime-evaluatedverifyContextis again called at comptime for the same parameters (the previous call is still ongoing)hashis still wip but we are trying to look at it again.
A workaround is placing the comptime call to verifyContext inside the struct rather than before it:
fn HashMapUnmanaged(comptime Context: type) type {
return struct {
comptime {
verifyContext(Context);
}
};
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.Tokenization, parsing, AstGen, Sema, and Liveness.