Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error message for misplaced lifetime annotation in reference #75025

Closed
Storyyeller opened this issue Aug 1, 2020 · 2 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Storyyeller
Copy link
Contributor

Storyyeller commented Aug 1, 2020

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

@Storyyeller Storyyeller added the C-bug Category: This is a bug. label Aug 1, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Aug 1, 2020
@SNCPlay42
Copy link
Contributor

Duplicate of #73568

@JohnTitor
Copy link
Member

Current output:

error: lifetime must precede `mut`
 --> src/lib.rs:2:13
  |
2 |     parent: &mut 'a SpanManager,
  |             ^^^^^^^ help: place the lifetime before `mut`: `&'a mut`

error[E0412]: cannot find type `SpanManager` in this scope
 --> src/lib.rs:2:21
  |
2 |     parent: &mut 'a SpanManager,
  |                     ^^^^^^^^^^^ not found in this scope

error: aborting due to 2 previous errors

Indeed #73595 improved the diagnostics and it has a regression test for this case so I'm going to just close this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants