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

Attempt to set a default value for a lifetime generic parameter produces confusing diagnostic #107492

Closed
obi1kenobi opened this issue Jan 30, 2023 · 2 comments · Fixed by #107580
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@obi1kenobi
Copy link
Member

obi1kenobi commented Jan 30, 2023

Code

pub struct DefaultLifetime<'a, 'b = 'static> {
    _marker: std::marker::PhantomData<&'a &'b ()>,
}

Current output

error: expected one of `,`, `:`, or `>`, found `=`
  --> src/lib.rs:20:39
   |
20 |     pub struct DefaultLifetime<'a, 'b = 'static> {
   |                                    -- ^ expected one of `,`, `:`, or `>`
   |                                    |
   |                                    maybe try to close unmatched angle bracket

Desired output

error: expected one of `,`, `:`, or `>`, found `=`
  --> src/lib.rs:20:39
   |
20 |     pub struct DefaultLifetime<'a, 'b = 'static> {
   |                                       ^^^^^^^^^ lifetime parameters cannot have default values

Rationale and extra context

AFAIK, lifetime generic parameters are not currently allowed to have default values. Attempting to use the "obvious" syntax for setting a default lifetime is an opportunity to teach the user that this is not a supported feature, and certainly shouldn't claim that there's an unmatched angle bracket just because it didn't expect the = during parsing.

This is happening on rustc 1.69.0-nightly (d7948c843 2023-01-26) with the 2021 edition.

Other cases

No response

Anything else?

No response

@obi1kenobi obi1kenobi added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 30, 2023
@compiler-errors
Copy link
Member

compiler-errors commented Jan 31, 2023

This shouldn't be too hard to do. Happy to mentor anyone working on this.


Implementor of this fix should look here:

let param = if this.check_lifetime() {
let lifetime = this.expect_lifetime();
// Parse lifetime parameter.
let (colon_span, bounds) = if this.eat(&token::Colon) {
(Some(this.prev_token.span), this.parse_lt_param_bounds())
} else {

The recovery should be only done if the thing after the = is a lifetime.

@compiler-errors compiler-errors added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Jan 31, 2023
@lenko-d
Copy link
Contributor

lenko-d commented Jan 31, 2023

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 6, 2023
…me_generic_parameter_produces_confusing_diagnostic, r=compiler-errors

Recover from lifetimes with default lifetimes in generic args

Fixes [rust-lang#107492](rust-lang#107492)
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 E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants