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

Various changes to name resolution of anon consts #111215

Merged
merged 2 commits into from
May 9, 2023

Conversation

BoxyUwU
Copy link
Member

@BoxyUwU BoxyUwU commented May 4, 2023

Sorry this PR is kind of all over the place ^^'

Fixes #111012

  • Rewrites anon const nameres to all go through fn resolve_anon_const explicitly instead of visit_anon_const to ensure that we do not accidentally resolve anon consts as if they are allowed to use generics when they aren't. Also means that we dont have bits of code for resolving anon consts that will get out of sync (i.e. legacy const generics and resolving path consts that were parsed as type arguments)
  • Renames two of the LifetimeRibKind, AnonConst -> ConcreteAnonConst and ConstGeneric -> ConstParamTy
  • Noticed while doing this that under generic_const_exprs all lifetimes currently get resolved to errors without any error being emitted which was causing a bunch of tests to pass without their bugs having been fixed, incidentally fixed that in this PR and marked those tests as // known-bug:. I'm fine to break those since generic_const_exprs is a very unstable incomplete feature and this PR does make generic_const_exprs "less broken" as a whole, also I can't be assed to figure out what the underlying causes of all of them are. This PR reopens ICE: cannot relate bound region: ReLateBound #77357 ICE: index out of bounds: the len is 0 but the index is 18446744073709551615 #83993
  • Changed generics_of to stop providing generics and predicates to enum variant discriminant anon consts since those are not allowed to use generic parameters
  • Updated the error for non 'static lifetime in const arguments and the error for non 'static lifetime in const param tys to use derive(Diagnostic)

I have a vague idea why const-arg-in-const-arg.rs, in-closure.rs and simple.rs have started failing which is unfortunate since these were deliberately made to work, I think lifetime resolution being broken just means this regressed at some point and nobody noticed because the tests were not testing anything :( I'm fine breaking these too for the same reason as the tests for #77357 #83993. I couldn't get // known-bug to work for these ICEs and just kept getting different stderr between CI and local --bless so I just removed them and will create an issue to track re-adding (and fixing) the bugs if this PR lands.

r? @cjgillot cc @compiler-errors

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 4, 2023
@BoxyUwU BoxyUwU changed the title im so sorry? Various changes to name resolution of anon consts May 4, 2023
@BoxyUwU BoxyUwU force-pushed the resolve_anon_consts_differently branch from d66e493 to 316a493 Compare May 4, 2023 19:21
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the resolve_anon_consts_differently branch from 316a493 to 83aee5f Compare May 4, 2023 19:29
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the resolve_anon_consts_differently branch from e6c7a7d to 79c8ee9 Compare May 5, 2023 12:31
@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

Would it be possible to separate the changes to error types in their own commit, so they don't pollute semantic changes?

@@ -326,6 +326,18 @@ pub(crate) struct ParamInTyOfConstParam {
#[label]
pub(crate) span: Span,
pub(crate) name: Symbol,
#[subdiagnostic]
pub(crate) sub_is_type: Option<ParamInTyOfConstParamIsType>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this terminology. Is this the kind of the param that appears in type of const param?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not completely sure what the naming scheme is tbh I just copied it. I think the sub is short for "subdiagnostic", the is_type is to determine what kind of generic parameter was found. I've renamed all this to be more clear since I found it rather confusing too and so did others I spoke to.

It's now pub(crate) param_kind: Option<ParamKindInTyOfConstParam>.

As far as I know none of these ParamKindIn<blank> can be merged into a single type as they all have different diagnostic messages with their #[note]/#[help]. It's definitely unfortunate because there's a lot of them 😅

compiler/rustc_resolve/src/errors.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/errors.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late.rs Show resolved Hide resolved
compiler/rustc_resolve/src/late.rs Show resolved Hide resolved
tests/ui/const-generics/late-bound-vars/in_closure.stderr Outdated Show resolved Hide resolved
@@ -3,12 +3,16 @@ error[E0770]: the type of const parameters must not depend on other generic para
|
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
| ^ the type must not depend on the parameter `N`
|
= note: const parameters may not be used in the type of const parameters
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
= note: const parameters may not be used in the type of const parameters
= note: const parameters may not be used in the type of other const parameters

?

Copy link
Member Author

@BoxyUwU BoxyUwU May 5, 2023

Choose a reason for hiding this comment

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

I think technically no because const parameters cannot be used in the type of themselves either- const N: [(); N] would error. That would be a pretty nonsensical type though, its not really possible to construct a valid one even if we lifted the restriction that const parameter type's cant be generic lol

@BoxyUwU BoxyUwU force-pushed the resolve_anon_consts_differently branch 2 times, most recently from c606b62 to daef782 Compare May 5, 2023 20:02
@BoxyUwU
Copy link
Member Author

BoxyUwU commented May 5, 2023

I think I've split the diagnostic changes out from the other changes, sorry I should have done that in the first place. I had thought it was going to be a lot harder to do retroactively, its not a perfect split but it should hopefully be much better now.

@BoxyUwU
Copy link
Member Author

BoxyUwU commented May 5, 2023

I tried to explain a bit in the PR description what is going on with the test/revision removals and things turning into ICEs but I think I didn't give enough information.

So for context (as i discovered while making this PR) right now on nightly lifetime resolution for anon consts under feature(generic_const_exprs) is really broken. Every lifetime you write that isnt '_ or 'static gets resolved to an error without an error actually having been emitted, this goes for when you do actually have a lifetime in scope (i.e. fn foo<'a>() -> [(); { let _: &'a (); 10 }] {}) or when you are writing lifetimes that haven't been declared (i.e. fn foo() -> [(); { let _: &'a (); 10 }] {}). I have added tests/ui/const-generics/generic_const_exprs/unresolved_lifetimes_error.rs to test that we emit errors when lifetimes are unresolved inside const arguments when generic_const_exprs is enabled (if you try that code on playground on nightly you'll see it doesnt error)

It hasn't always been like this and at some point it broke and started acting this way, this PR incidentally fixes this bug by not having the LifetimeRibKind::ConcreteAnonConst around anon consts. What was happening previously was ConcreteAnonConst was around every anon const, and when resolving a lifetime if we hit that rib kind we emit an error and store that the lifetime resolved to an error. With feature(generic_const_exprs) enabled we would silence the error caused by ConcreteAnonConst but still resolve the lifetime to an error.

As far as I can tell this is why 5 of the tests have started ICEing instead of passing, but this PR hasn't actually introduced any broken code. Generally all 5 of the tests started ICEing because the compiler cannot handle lifetimes in const args for one reason or another but it was being masked due to them always being resolved to errors.

  • const-arg-in-const-arg.rs's full revision, late-bound-vars/simple.rs and late-bound-vars/in-closure.rs are all ICEing because the logic to convert all regions to variables for borrowck doesn't work correctly for anon consts under generic_const_exprs. This definitely used to function correctly at one point and the tests were deliberately added because bugs were fixed. Then when lifetime resolution broke for generic_const_exprs these tests stopped actually testing anything because none of the lifetimes were resolved, and some PR (not entirely sure which) regressed these tests but it wasnt caught (until this PR made them start testing things again).
    • I removed const-arg-in-const-arg.rs's full revision because I couldn't get stderr normalization to work correctly and give consistent results between CI and local for the ICE message 😅
  • issue-77357.rs and issue-83993.rs I do not believe have ever functioned correctly, the tests were added because glacier stuff started passing. This was solely because lifetime resolution broke and masked the ICEs.
    • I removed these tests because I couldn't get stderr normalization to work correctly and give consistent results between CI and local for the ICE message 😅

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the resolve_anon_consts_differently branch from daef782 to 531d410 Compare May 5, 2023 20:31
@BoxyUwU BoxyUwU force-pushed the resolve_anon_consts_differently branch from 531d410 to 73b3ce2 Compare May 5, 2023 20:43
self.record_lifetime_res(
lifetime.id,
LifetimeRes::Error,
LifetimeElisionCandidate::Ignore,
);
return;
}
LifetimeRibKind::AnonConst => {
self.maybe_emit_forbidden_non_static_lifetime_error(lifetime);
LifetimeRibKind::ConcreteAnonConst(cause) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Every lifetime you write that isnt '_ or 'static gets resolved to an error without an error actually having been emitted, this goes for when you do actually have a lifetime in scope (i.e. fn foo<'a>() -> [(); { let _: &'a (); 10 }] {}) or when you are writing lifetimes that haven't been declared (i.e. fn foo() -> [(); { let _: &'a (); 10 }] {}).

The bug is here. We need the feature check for generic_const_exprs be done on this whole block, instead of only doing it in emit_forbidden_non_static_lifetime_error. This way, lifetime resolution will continue walking ribs, instead of returning a LifetimeRes::Error straight.

Copy link
Member Author

@BoxyUwU BoxyUwU May 6, 2023

Choose a reason for hiding this comment

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

yeah, this PR accomplishes that by just not adding the ConcreteAnonConst rib if generic_const_exprs is enabled, so the only time we encounter LifetimeRibKind::ConcreteAnonConst should be when we need to error. Super subtle 😅

@cjgillot
Copy link
Contributor

cjgillot commented May 7, 2023

Thanks for the explanations.
@bors r+

@bors
Copy link
Contributor

bors commented May 7, 2023

📌 Commit 73b3ce2 has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 7, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request May 9, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#97320 (Stabilize const_ptr_read)
 - rust-lang#110770 (Limit lifetime of format_args!() with inlined args.)
 - rust-lang#111021 (Move some tests)
 - rust-lang#111215 (Various changes to name resolution of anon consts)
 - rust-lang#111242 (support set `rpath` option  for each target independently)
 - rust-lang#111282 (Remove some `assume`s from slice iterators that don't do anything)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE for enum discriminant being a const generic
6 participants