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

Mention that type parameters are used recursively on bivariance error #127871

Merged
merged 3 commits into from
Jul 19, 2024

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Jul 17, 2024

Right now when a type parameter is used recursively, even with indirection (so it has a finite size) we say that the type parameter is unused:

struct B<T>(Box<B<T>>);

This is confusing, because the type parameter is used, it just doesn't have its variance constrained. This PR tweaks that message to mention that it must be used non-recursively.

Not sure if we should actually mention "variance" here, but also I'd somewhat prefer we don't keep the power users in the dark w.r.t the real underlying issue, which is that the variance isn't constrained. That technical detail is reserved for a note, though.

cc @fee1-dead

Fixes #118976
Fixes #26283
Fixes #53191
Fixes #105740
Fixes #110466

@rustbot
Copy link
Collaborator

rustbot commented Jul 17, 2024

r? @estebank

rustbot has assigned @estebank.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@compiler-errors compiler-errors changed the title Mention that type parameters are used recursively Mention that type parameters are used recursively on bivariance error Jul 17, 2024
@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 Jul 17, 2024
Comment on lines +23 to +25
| - type parameter must be used non-recursively in the definition
LL | Cons(Box<ListCell<T>>),
| ^
Copy link
Member Author

Choose a reason for hiding this comment

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

Notably, the primary span is now the usage, not the definition. We could put that back, I guess.

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Pretty sure this fixes #118976 (edit: Well, I marked that as A-docs not A-diagnostics but doesn't matter as long as we clear up the confusion somehow). Might also fix some of the open issues linked here: #118976 (comment) (dunno, haven't looked at them lol)

compiler/rustc_hir_analysis/messages.ftl Outdated Show resolved Hide resolved
@compiler-errors
Copy link
Member Author

compiler-errors commented Jul 17, 2024

Interestingly, this actually regresses #66912...

struct Foo<T> {
    bar: Bar<T>,
}
struct Bar<T> {}

Maybe we should recurse into the structs/aliases to check for cycles 🤔

@fmease
Copy link
Member

fmease commented Jul 17, 2024

Interestingly, this actually regresses #66912.

Oh, that's a bummer. Makes sense looking at the current impl.

If you can make it work easily, I'd love to see this addressed. Note though that the cycles may have arbitrary periods.

@compiler-errors
Copy link
Member Author

Sure, but never greater than O(number of structs in the crate) if we're only doing a purely def-id based traversal to eliminate cases that are obviously not cycles. I don't really care about the substitution corner cases.

@compiler-errors
Copy link
Member Author

OK, should also "fix" #66912 by explaining that the parameter is likely unused in the containing type. Not totally certain if that's sufficient, but it should at least unregress the test.

@rust-log-analyzer

This comment has been minimized.

Comment on lines +18 to +27
error: type parameter `T` is only used recursively
--> $DIR/issue-105231.rs:2:15
|
LL | struct A<T>(B<T>);
| ^ unused type parameter
| - ^
| |
| type parameter must be used non-recursively in the definition
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
= note: all type parameters must be used in a non-recursive way in order to constrain their variance
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be ideal if we either expanded this diagnostic with some of the info from E0072, or if we silenced this one if E0072 was emitted (because it gives the "solution" already, making this one redundant).

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jul 18, 2024

📌 Commit c02d0de has been approved by estebank

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 Jul 18, 2024
@fmease
Copy link
Member

fmease commented Jul 18, 2024

Esteban, I'm not entirely sure why you r+'ed even tho I had assigned myself. Clearly, I had sth. I wanted to say. Anyway, I will post it as a PR instead.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#127418 (Wrap too long type name)
 - rust-lang#127594 (Fuchsia status code match arm)
 - rust-lang#127835 (Fix ICE in suggestion caused by `⩵` being recovered as `==`)
 - rust-lang#127858 (match lowering: Rename `MatchPair` to `MatchPairTree`)
 - rust-lang#127871 (Mention that type parameters are used recursively on bivariance error)
 - rust-lang#127913 (remove `debug-logging` default from tools profile)
 - rust-lang#127925 (Remove tag field from `Relation`s)
 - rust-lang#127929 (Use more accurate span for `addr_of!` suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#127418 (Wrap too long type name)
 - rust-lang#127594 (Fuchsia status code match arm)
 - rust-lang#127835 (Fix ICE in suggestion caused by `⩵` being recovered as `==`)
 - rust-lang#127858 (match lowering: Rename `MatchPair` to `MatchPairTree`)
 - rust-lang#127871 (Mention that type parameters are used recursively on bivariance error)
 - rust-lang#127913 (remove `debug-logging` default from tools profile)
 - rust-lang#127925 (Remove tag field from `Relation`s)
 - rust-lang#127929 (Use more accurate span for `addr_of!` suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#127418 (Wrap too long type name)
 - rust-lang#127594 (Fuchsia status code match arm)
 - rust-lang#127835 (Fix ICE in suggestion caused by `⩵` being recovered as `==`)
 - rust-lang#127858 (match lowering: Rename `MatchPair` to `MatchPairTree`)
 - rust-lang#127871 (Mention that type parameters are used recursively on bivariance error)
 - rust-lang#127913 (remove `debug-logging` default from tools profile)
 - rust-lang#127925 (Remove tag field from `Relation`s)
 - rust-lang#127929 (Use more accurate span for `addr_of!` suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 65de5d0 into rust-lang:master Jul 19, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jul 19, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2024
Rollup merge of rust-lang#127871 - compiler-errors:recursive, r=estebank

Mention that type parameters are used recursively on bivariance error

Right now when a type parameter is used recursively, even with indirection (so it has a finite size) we say that the type parameter is unused:

```
struct B<T>(Box<B<T>>);
```

This is confusing, because the type parameter is *used*, it just doesn't have its variance constrained. This PR tweaks that message to mention that it must be used *non-recursively*.

Not sure if we should actually mention "variance" here, but also I'd somewhat prefer we don't keep the power users in the dark w.r.t the real underlying issue, which is that the variance isn't constrained. That technical detail is reserved for a note, though.

cc `@fee1-dead`

Fixes rust-lang#118976
Fixes rust-lang#26283
Fixes rust-lang#53191
Fixes rust-lang#105740
Fixes rust-lang#110466
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 19, 2024
…better-err, r=compiler-errors

Lazy type aliases: Diagostics: Detect bivariant ty params that are only used recursively

Follow-up to errs's rust-lang#127871. Extends the logic to cover LTAs, too, not just ADTs.
This change only takes effect with the next-gen solver enabled as cycle errors like
the one we have here are fatal in the old solver. That's my explanation anyways.

r? compiler-errors
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2024
Rollup merge of rust-lang#127976 - fmease:lta-cyclic-bivariant-param-better-err, r=compiler-errors

Lazy type aliases: Diagostics: Detect bivariant ty params that are only used recursively

Follow-up to errs's rust-lang#127871. Extends the logic to cover LTAs, too, not just ADTs.
This change only takes effect with the next-gen solver enabled as cycle errors like
the one we have here are fatal in the old solver. That's my explanation anyways.

r? compiler-errors
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
6 participants