Skip to content

Commit 8ddbfad

Browse files
authored
Rollup merge of #107580 - lenko-d:default_value_for_a_lifetime_generic_parameter_produces_confusing_diagnostic, r=compiler-errors
Recover from lifetimes with default lifetimes in generic args Fixes [#107492](#107492)
2 parents 496adf8 + d9f6005 commit 8ddbfad

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

compiler/rustc_error_messages/locales/en-US/parse.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ parse_unexpected_token_after_struct_name_found_other = expected `where`, `{"{"}`
475475
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
476476
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
477477
478+
parse_unexpected_default_value_for_lifetime_in_generic_parameters = unexpected default lifetime parameter
479+
.label = lifetime parameters cannot have default values
480+
478481
parse_multiple_where_clauses = cannot define duplicate `where` clauses on an item
479482
.label = previous `where` clause starts here
480483
.suggestion = consider joining the two `where` clauses into one

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,14 @@ pub(crate) struct UnexpectedSelfInGenericParameters {
16011601
pub span: Span,
16021602
}
16031603

1604+
#[derive(Diagnostic)]
1605+
#[diag(parse_unexpected_default_value_for_lifetime_in_generic_parameters)]
1606+
pub(crate) struct UnexpectedDefaultValueForLifetimeInGenericParameters {
1607+
#[primary_span]
1608+
#[label]
1609+
pub span: Span,
1610+
}
1611+
16041612
#[derive(Diagnostic)]
16051613
#[diag(parse_multiple_where_clauses)]
16061614
pub(crate) struct MultipleWhereClauses {

compiler/rustc_parse/src/parser/generics.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::errors::{
2-
MultipleWhereClauses, UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody,
2+
MultipleWhereClauses, UnexpectedDefaultValueForLifetimeInGenericParameters,
3+
UnexpectedSelfInGenericParameters, WhereClauseBeforeTupleStructBody,
34
WhereClauseBeforeTupleStructBodySugg,
45
};
56

@@ -145,6 +146,20 @@ impl<'a> Parser<'a> {
145146
} else {
146147
(None, Vec::new())
147148
};
149+
150+
if this.check_noexpect(&token::Eq)
151+
&& this.look_ahead(1, |t| t.is_lifetime())
152+
{
153+
let lo = this.token.span;
154+
// Parse `= 'lifetime`.
155+
this.bump(); // `=`
156+
this.bump(); // `'lifetime`
157+
let span = lo.to(this.prev_token.span);
158+
this.sess.emit_err(
159+
UnexpectedDefaultValueForLifetimeInGenericParameters { span },
160+
);
161+
}
162+
148163
Some(ast::GenericParam {
149164
ident: lifetime.ident,
150165
id: lifetime.id,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub struct DefaultLifetime<'a, 'b = 'static> {
2+
//~^ ERROR unexpected default lifetime parameter
3+
_marker: std::marker::PhantomData<&'a &'b ()>,
4+
}
5+
6+
fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected default lifetime parameter
2+
--> $DIR/issue-107492-default-value-for-lifetime.rs:1:35
3+
|
4+
LL | pub struct DefaultLifetime<'a, 'b = 'static> {
5+
| ^^^^^^^^^ lifetime parameters cannot have default values
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)