-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Order-dependent type inference failure with match #25165
Comments
I think #22165 may be related too (in particular #22165 (comment)). |
Looks identical to #19855. @nikomatsakis Could you clarify what would be required to fix this? It sort of feels like a natural candidate for a bidirectional typechecking approach. I strongly disagree that this is expected behavior from the user side of things, even if it falls out naturally from our current inference system. I've run into this many times before and it's always felt like a pretty substantial wart. |
We would probably have to run inference in a loop of some sort. |
I ran into this in #29834. It definitely is a wart. In my case it prevents me from making wrappers around closures without breaking the inference logic: struct Wrapper<F>(F);
struct Foo;
impl Foo {
fn stuff(self) { }
}
fn api<F: Fn(Foo)>(f: Wrapper<F>) { }
fn main() {
api(Wrapper(|b| {
b.stuff(); //~ ERROR the type of this value must be known in this context
}))
} ... which makes my API really ugly. I too would like to know what's involved with changing this behavior. Perhaps the new MIR stuff can help? |
MIR runs only after typechecking, so it can't help. Your example should be able to be made to work, though. |
The reason that this doesn't work is that the type checker's current design includes various points where it cannot make progress unless it has enough type information. I think the fix would be to reimplement the type checker so that everything is a generic constraint that can be deferred; this way, we would just postpone processing |
This may be somewhat problematic because of coercions and autoderefs. Anyway, I feel like regionck is separated enough from typeck. |
@arielb1 hmm, yes, I agree there are big challenges around coercion. I was hoping we could work around those by being capturing the source order somewhat in the graph, but it may be that it prevents the whole idea from really panning out. I strongly disagree about regionck and typeck though. But we can discuss at another time, it's not really that relevant to this bug. |
(switched this from C-bug to C-enhancement, because I do not think this represent anything incorrect in our existing type inference; it just is one of many annoying short-comings that we might attempt to address in the future.) |
Not sure if this has been reported yet, but it really seems like Rust should be able to do better here.
This also works if we replace the
match
with anif let
and move theres
assignment below it.Note that this is a reduced testcase and therefore contrived, but the original issue can be seen here.
The text was updated successfully, but these errors were encountered: