Skip to content

Misleading Suggestion when Returning a Reference to a Bare Trait from a Function  #127689

Closed
@veera-sivarajan

Description

@veera-sivarajan
Contributor

Code

// edition: 2021
trait Trait {}

fn fun() -> &Trait {
    todo!()
}

fn main() {}

Current output

error[E0106]: missing lifetime specifier
 --> <source>:5:13
  |
5 | fn fun() -> &Trait {
  |             ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
  |
5 | fn fun() -> &'static Trait {
  |              +++++++

error[E0782]: trait objects must include the `dyn` keyword
 --> <source>:5:14
  |
5 | fn fun() -> &Trait {
  |              ^^^^^
  |
help: add `dyn` keyword before this trait
  |
5 | fn fun() -> &dyn Trait {
  |              +++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0106, E0782.
For more information about an error, try `rustc --explain E0106`.

Desired output

error[E0782]: trait objects must include the `dyn` keyword
 --> <source>:5:14
  |
5 | fn fun() -> &Trait {
  |              ^^^^^
  |
help: add `dyn` keyword before this trait
  |
5 | fn fun() -> &dyn Trait {
  |              +++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0106, E0782.
For more information about an error, try `rustc --explain E0106`.

Rationale and extra context

The error E0106 suggests to add a static lifetime but this is incorrect. If and when #127692 is merged, the error E0782 can provide accurate and helpful messages and the former should be suppressed in favor of E0782.

But this can be tricky as E0106 is emitted at rustc_resolve which doesn't have any context about type of values.

Rust Version

rustc 1.79.0

Anything else?

No response

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 Jul 13, 2024
veera-sivarajan

veera-sivarajan commented on Jul 13, 2024

@veera-sivarajan
ContributorAuthor

@rustbot label +D-verbose +D-incorrect +A-trait-objects

added
A-dyn-traitArea: trait objects, vtable layout
D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
on Jul 13, 2024
added 3 commits that reference this issue on Sep 2, 2024

Rollup merge of rust-lang#127692 - veera-sivarajan:bugfix-125139, r=e…

b409db4

Rollup merge of rust-lang#127692 - veera-sivarajan:bugfix-125139, r=e…

4b38ef6

Rollup merge of rust-lang#127692 - veera-sivarajan:bugfix-125139, r=e…

f75a195
added a commit that references this issue on Sep 5, 2024
GrigorenkoPV

GrigorenkoPV commented on Oct 13, 2024

@GrigorenkoPV
Contributor

The message has changed a bit, maybe due to #131239.

The 'static suggestion is still here.

But now it also suggests &impl Trait, which is cool.

The &dyn Trait suggestion got changed to Box<dyn Trait>, which probably makes more sense, considering the function has no input lifetimes.

error[E0106]: missing lifetime specifier
 --> src/main.rs:4:13
  |
4 | fn fun() -> &Trait {
  |             ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
  |
4 | fn fun() -> &'static Trait {
  |              +++++++

error[E0782]: expected a type, found a trait
 --> src/main.rs:4:14
  |
4 | fn fun() -> &Trait {
  |              ^^^^^
  |
help: use `impl Trait` to return an opaque type, as long as you return a single underlying type
  |
4 | fn fun() -> &impl Trait {
  |              ++++
help: alternatively, you can return an owned trait object
  |
4 | fn fun() -> Box<dyn Trait> {
  |             ~~~~~~~~~~~~~~
veera-sivarajan

veera-sivarajan commented on Oct 13, 2024

@veera-sivarajan
ContributorAuthor

Yeah, it's hard to prevent the 'static suggestion because its from the resolver. Anyways, this looks good to me. Thank you for updating.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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

        Participants

        @veera-sivarajan@rustbot@GrigorenkoPV

        Issue actions

          Misleading Suggestion when Returning a Reference to a Bare Trait from a Function · Issue #127689 · rust-lang/rust