Skip to content

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

Closed
@Noratrieb

Description

@Noratrieb

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.

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

No branches or pull requests

Issue actions