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

Handle associated types in const context #70056

Closed
wants to merge 5 commits into from

Conversation

estebank
Copy link
Contributor

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.

Address the diagnostics part of #43408 and #61730.

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.
@rust-highfive
Copy link
Collaborator

r? @davidtwco

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 16, 2020
@estebank
Copy link
Contributor Author

estebank commented Mar 16, 2020

This is a bit of a blunt approach and we should verify this is a reasonable way to do this.

CC @rust-lang/compiler @varkor @oli-obk

@eddyb
Copy link
Member

eddyb commented Mar 16, 2020

@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() {
Copy link
Member

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.

Copy link
Contributor Author

@estebank estebank Mar 16, 2020

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>()];
}

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CC #70452

@estebank
Copy link
Contributor Author

This seems overkill when lazy normalization is on the horizon.

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.

@estebank
Copy link
Contributor Author

After reassessing the approach the second commit makes the change much smaller and way more reasonable.

@eddyb
Copy link
Member

eddyb commented Mar 17, 2020

@estebank Hmm, if you want a "let's unconfuse users" stopgap, maybe we can do this instead: in astconv, whereever DefKind::TyParam is converted into ty::Param, we could emit a warning if it's not in tcx.generics_of(self.item_def_id) (you probably need a way to get the generics in scope from AstConv and they could also be cached in the implementers of AstConv).

(thankfully ty::ConstKind::Param doesn't need this as #![feature(const_generics)] doesn't ever use the wrong generics, at the cost of breaking a few things pre-lazy-normalization)

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.
The warning message could say something along the lines of "this type parameter is not yet correctly supported here, misleading errors may occur".

| Node::AnonConst(_)
| Node::Item(&Item { kind: ItemKind::Static(..), .. }) => true,
Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. }) => {
match self.find(parent_id) {
Copy link
Member

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)

Copy link
Contributor Author

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.

src/librustc/hir/map/mod.rs Outdated Show resolved Hide resolved
@davidtwco
Copy link
Member

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned davidtwco Mar 18, 2020
@estebank
Copy link
Contributor Author

@eddyb I think the current diff should be palatable to you as a stopgap until lazy eval.

@eddyb
Copy link
Member

eddyb commented Mar 26, 2020

I still would prefer doing this: #70056 (comment).

@eddyb
Copy link
Member

eddyb commented Mar 27, 2020

I've opened #70452 for my whitelist approach.

@bors
Copy link
Contributor

bors commented Mar 30, 2020

☔ The latest upstream changes (presumably #70009) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon JohnCSimon removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 6, 2020
@JohnCSimon JohnCSimon added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 6, 2020
@estebank estebank closed this Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants