-
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
Handle associated types in const
context
#70056
Conversation
When in `const` context (like array lengths) we erase all substs. Because of this, we used to emit spurious suggestions to restrict type parameters when using associated types on them, even though that would never work. Instead, we now signal `const` contexts when evaluating traits and emit a more accurate error.
r? @davidtwco (rust_highfive has picked a reviewer for you, use r? to override) |
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
@estebank This seems overkill when lazy normalization is on the horizon. |
@@ -158,6 +158,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { | |||
ty::Projection(projection) => (false, Some(projection)), | |||
_ => return, | |||
}; | |||
if self.in_const_context.get() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weirdly specific given that anything involving type parameters doesn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very common thing people encounter when trying to be too clever by half. I'm looking for other cases that are likely to be hit so that I can also handle them. One case I'm looking at now is the following, which ICEs
fn test<T: ?Sized>() {
[0u8; std::mem::size_of::<&T>()];
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fix generics_of
if the parent of AnonConst
is an Expr
(and probably any Ty
nested in a body, but that's harder to check), I think I've suggested that before. It should just work, the query cycles only come from types outside bodies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And by "fix" I mean a trivial whitelist, much smaller than this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CC #70452
I understand that, but having a stop gap in place until then is in my eyes worthwhile, given how often people hit this limitation with a very misleading error. If lazy normalization won't be hitting stable in the next couple of releases (which I don't think will happen), then we need something avoid sowing further confusion. I would be more than happy to accomplish the same with an alternative approach if one is workable. |
After reassessing the approach the second commit makes the change much smaller and way more reasonable. |
@estebank Hmm, if you want a "let's unconfuse users" stopgap, maybe we can do this instead: in (thankfully Note that I'm suggesting a warning and not an error. We don't want to break anything that already compiles, however useless it may be. |
src/librustc/hir/map/mod.rs
Outdated
| Node::AnonConst(_) | ||
| Node::Item(&Item { kind: ItemKind::Static(..), .. }) => true, | ||
Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. }) => { | ||
match self.find(parent_id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weird, why could find
ever return None
? We should fix that if it is the case (cc @Zoxc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was encountered on enum variants like enum E { A = 1f64 }
, but I didn't dig too deep.
r? @eddyb |
@eddyb I think the current diff should be palatable to you as a stopgap until lazy eval. |
I still would prefer doing this: #70056 (comment). |
I've opened #70452 for my whitelist approach. |
☔ The latest upstream changes (presumably #70009) made this pull request unmergeable. Please resolve the merge conflicts. |
When in
const
context (like array lengths) we erase all substs.Because of this, we used to emit spurious suggestions to restrict type
parameters when using associated types on them, even though that would
never work. Instead, we now signal
const
contexts when evaluatingtraits and emit a more accurate error.
Address the diagnostics part of #43408 and #61730.