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

Recover from lifetimes with default lifetimes in generic args #107580

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/parse.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ parse_unexpected_token_after_struct_name_found_other = expected `where`, `{"{"}`
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items

parse_unexpected_default_value_for_lifetime_in_generic_parameters = unexpected default lifetime parameter
.label = lifetime parameters cannot have default values

parse_multiple_where_clauses = cannot define duplicate `where` clauses on an item
.label = previous `where` clause starts here
.suggestion = consider joining the two `where` clauses into one
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,14 @@ pub(crate) struct UnexpectedSelfInGenericParameters {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_unexpected_default_value_for_lifetime_in_generic_parameters)]
pub(crate) struct UnexpectedDefaultValueForLifetimeInGenericParameters {
#[primary_span]
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_multiple_where_clauses)]
pub(crate) struct MultipleWhereClauses {
Expand Down
17 changes: 16 additions & 1 deletion compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::errors::{
MultipleWhereClauses, UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody,
MultipleWhereClauses, UnexpectedDefaultValueForLifetimeInGenericParameters,
UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody,
WhereClauseBeforeTupleStructBodySugg,
};

Expand Down Expand Up @@ -145,6 +146,20 @@ impl<'a> Parser<'a> {
} else {
(None, Vec::new())
};

if this.check_noexpect(&token::Eq)
&& this.look_ahead(1, |t| t.is_lifetime())
{
let lo = this.token.span;
// Parse `= 'lifetime`.
this.bump(); // `=`
this.bump(); // `'lifetime`
let span = lo.to(this.prev_token.span);
this.sess.emit_err(
UnexpectedDefaultValueForLifetimeInGenericParameters { span },
);
}

Some(ast::GenericParam {
ident: lifetime.ident,
id: lifetime.id,
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/lifetimes/issue-107492-default-value-for-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub struct DefaultLifetime<'a, 'b = 'static> {
//~^ ERROR unexpected default lifetime parameter
_marker: std::marker::PhantomData<&'a &'b ()>,
}

fn main(){}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected default lifetime parameter
--> $DIR/issue-107492-default-value-for-lifetime.rs:1:35
|
LL | pub struct DefaultLifetime<'a, 'b = 'static> {
| ^^^^^^^^^ lifetime parameters cannot have default values

error: aborting due to previous error