Skip to content

regression: rustc suggests .as_ref() at incorrect location and other spans have regressed #90286

Closed
@camelid

Description

@camelid

Code

Playground (regressed rustc version)

fn foo(opt: &Option<Box<i32>>) -> String {
    opt.map(|x| x.to_string()).unwrap_or_else(String::new)
}

Output (stable 1.56.0, good)

error[E0507]: cannot move out of `*opt` which is behind a shared reference
 --> src/lib.rs:2:5
  |
2 |     opt.map(|x| x.to_string()).unwrap_or_else(String::new)
  |     ^^^ move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` trait
  |
help: consider borrowing the `Option`'s content
  |
2 |     opt.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new)
  |        +++++++++

For more information about this error, try `rustc --explain E0507`.

Output (beta 1.57.0-beta.2, incorrect)

error[E0507]: cannot move out of `*opt` which is behind a shared reference
 --> src/lib.rs:2:5
  |
2 |     opt.map(|x| x.to_string()).unwrap_or_else(String::new)
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` trait
  |
help: consider borrowing the `Option`'s content
  |
2 |     opt.map(|x| x.to_string()).as_ref().unwrap_or_else(String::new)
  |                               +++++++++

For more information about this error, try `rustc --explain E0507`.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions