Skip to content

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

Closed
@Noratrieb

Description

@Noratrieb
Member

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.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Nov 30, 2021
added
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Nov 30, 2021
compiler-errors

compiler-errors commented on Dec 1, 2021

@compiler-errors
Member

Gave this a shot in the attached PR, so

@rustbot claim

added a commit that references this issue on Mar 31, 2022

Rollup merge of rust-lang#91416 - compiler-errors:infinite-ty-option-…

521c590
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @compiler-errors@camelid@Noratrieb

    Issue actions

      rustc suggests `Box<Option<T>>`, but could specialize to suggest `Option<Box<T>>` instead · Issue #91402 · rust-lang/rust