Skip to content
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

Fix type inference related to upvars in closures #21353

Merged
merged 1 commit into from
Feb 10, 2015
Merged

Conversation

sanxiyn
Copy link
Member

@sanxiyn sanxiyn commented Jan 18, 2015

Fix #20558.

@rust-highfive
Copy link
Contributor

r? @Aatch

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -1471,6 +1471,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
return Ok(ParameterBuiltin);
}

// Whether upvars are sized is checked elsewhere
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind say where they're checked? "Elsewhere" tends to be kind of frustrating when looking at these comments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are checked in numerous places... @nikomatsakis, do you have better ideas how to word this?

@nikomatsakis
Copy link
Contributor

I'm not sure what to make of this patch. Some thoughts:

  1. @sanxiyn is correct I think that this is harmless. More specifically, upvars are always (atm) local variables, and local variables must always (atm) have sized types.
  2. That said, I'm not entirely sure what error is being prevented. That is, I think that the upvar types are known, just known to be an integer literal type, but we consider integer literal types to be sized, so that doesn't seem right.
  3. I thought at first that the problem was that the requirement to fulfill all obligations comes before i32 fallback. If that's true, more general support for fallback (which I'll probably be implementing soon) would also address the problem, I guess, but at the same time I can't see that this change does harm.

@sanxiyn have you traced out just where the error is cropping up that you are preventing?

@sanxiyn
Copy link
Member Author

sanxiyn commented Jan 20, 2015

As far as I understand, 3 is correct. The error comes from checking IntVar: Sized before i32 fallback.

@nikomatsakis
Copy link
Contributor

OK, so, I started digging into the problem. I think it is a bug in the caching algorithm. I am double-checking, but I think the problem is that when we ask to get the list of upvar types for a closure, in some cases we do not yet know the result. This is because we haven't run all the inference we are going to need to figure everything out. This yields an ambiguous result -- which is generally the correct thing to do, and should not (typically) be harmful. However, in this case, what happens is that the ambiguous result gets cached -- again, not (typically) harmful, because normally the cache key is careful to note what parts needed more inference, so when more inference has been done, that cache entry no longer matches. (Example: Vec<_> : Foo would be a cache key; later, after inference has resolved it to Vec<i32> : Foo, the cache entry no longer matches.) What makes it bad is that it has been made ambiguous because the upvars were not available, but that is not part of the "cache key", which is just "closure : Foo"). I think the fix is prob just not to cache these results for now, since I don't see any obvious way to get that info into the cache key.

@nikomatsakis
Copy link
Contributor

That said, this patch may still be a good optimization. But I'd prefer to land the full fix first. I'll prepare a PR now.

@nikomatsakis nikomatsakis assigned nikomatsakis and unassigned Aatch Jan 21, 2015
@nikomatsakis
Copy link
Contributor

taking liberty of re-assigning responsibility for this patch to me

@sanxiyn
Copy link
Member Author

sanxiyn commented Jan 27, 2015

This is waiting on #21523.

@nikomatsakis
Copy link
Contributor

So the crucial underlying problem should be fixed now. Now the question is whether this patch on its own makes sense as a kind of optimization.

@sanxiyn
Copy link
Member Author

sanxiyn commented Feb 6, 2015

I have updated a comment as suggested.

@nikomatsakis
Copy link
Contributor

@bors r+ 4ef7551 rollup

Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 10, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Feb 10, 2015
@bors bors merged commit 4ef7551 into rust-lang:master Feb 10, 2015
@sanxiyn sanxiyn deleted the upvar branch February 11, 2015 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inference weirdness with a closure
5 participants