Skip to content

rustc suggests Box<Option<T>>, but could specialize to suggest Option<Box<T>> instead #91402

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

Closed
Noratrieb opened this issue Nov 30, 2021 · 1 comment · Fixed by #91416
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Noratrieb
Copy link
Member

Noratrieb commented Nov 30, 2021

Given the following code: playground link

struct X {
    x: Option<X>,
}

The current output is:

error[E0072]: recursive type `X` has infinite size
 --> src/lib.rs:1:1
  |
1 | struct X {
  | ^^^^^^^^ recursive type has infinite size
2 |     x: Option<X>,
  |        --------- recursive without indirection
  |
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `X` representable
  |
2 |     x: Box<Option<X>>,
  |        ++++         +

For more information about this error, try `rustc --explain E0072`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

error[E0072]: recursive type `X` has infinite size
 --> src/lib.rs:1:1
  |
1 | struct X {
  | ^^^^^^^^ recursive type has infinite size
2 |     x: Option<X>,
  |        --------- recursive without indirection
  |
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `X` representable
  |
2 |     x: Option<Box<X>>,
  |               ++++ +

For more information about this error, try `rustc --explain E0072`.
error: could not compile `playground` due to previous error

This allows rustc to use the nullpointer niche optimization, and is also easier to handle, and what the user probably wants in general.

@Noratrieb Noratrieb added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 30, 2021
@camelid camelid added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 30, 2021
@compiler-errors
Copy link
Member

Gave this a shot in the attached PR, so

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants