Skip to content

Confusing error message for misplaced lifetime annotation in reference #75025

Closed
@Storyyeller

Description

@Storyyeller

I recently forgot where in a mutable reference the lifetime goes, and got a confusing wall of error messages. Out of the six error messages produced, only the fifth hints at the actual problem, with the rest being misleading.

I think the error messages in this case could be improved in case a newcomer runs into this issue in order to avoid leading them down a false path of trying to figure out trait objects.

Specifically, with the following code

pub struct SpanMaker<'a> {
    parent: &mut 'a SpanManager,
    source: usize,
}

I got the following compiler errors:

error: lifetime in trait object type must be followed by `+`
  --> src/spans.rs:20:18
   |
20 |     parent: &mut 'a SpanManager,
   |                  ^^

error: expected `,`, or `}`, found `SpanManager`
  --> src/spans.rs:20:20
   |
20 |     parent: &mut 'a SpanManager,
   |                    ^ help: try adding a comma: `,`

error: expected `:`, found `,`
  --> src/spans.rs:20:32
   |
20 |     parent: &mut 'a SpanManager,
   |                                ^ expected `:`

warning: trait objects without an explicit `dyn` are deprecated
  --> src/spans.rs:20:18
   |
20 |     parent: &mut 'a SpanManager,
   |                  ^^ help: use `dyn`: `dyn 'a`
   |
   = note: `#[warn(bare_trait_objects)]` on by default

error[E0106]: missing lifetime specifier
  --> src/spans.rs:20:13
   |
20 |     parent: &mut 'a SpanManager,
   |             ^ help: consider using the named lifetime: `&'a`

error[E0224]: at least one trait is required for an object type
  --> src/spans.rs:20:18
   |
20 |     parent: &mut 'a SpanManager,
   |                  ^^

error: aborting due to 5 previous errors; 1 warning emitted

I am using stable Rust.

rustc --version --verbose
rustc 1.44.1 (c7087fe00 2020-06-17)
binary: rustc
commit-hash: c7087fe00d2ba919df1d813c040a5d47e43b0fe7
commit-date: 2020-06-17
host: x86_64-unknown-linux-gnu
release: 1.44.1
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-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