Skip to content

Commit d9f6005

Browse files
committed
Recover from default value for a lifetime in generic parameters.
1 parent 3de7d7f commit d9f6005

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
@@ -472,6 +472,9 @@ parse_unexpected_token_after_struct_name_found_other = expected `where`, `{"{"}`
472472
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
473473
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
474474
475+
parse_unexpected_default_value_for_lifetime_in_generic_parameters = unexpected default lifetime parameter
476+
.label = lifetime parameters cannot have default values
477+
475478
parse_multiple_where_clauses = cannot define duplicate `where` clauses on an item
476479
.label = previous `where` clause starts here
477480
.suggestion = consider joining the two `where` clauses into one

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,14 @@ pub(crate) struct UnexpectedSelfInGenericParameters {
15891589
pub span: Span,
15901590
}
15911591

1592+
#[derive(Diagnostic)]
1593+
#[diag(parse_unexpected_default_value_for_lifetime_in_generic_parameters)]
1594+
pub(crate) struct UnexpectedDefaultValueForLifetimeInGenericParameters {
1595+
#[primary_span]
1596+
#[label]
1597+
pub span: Span,
1598+
}
1599+
15921600
#[derive(Diagnostic)]
15931601
#[diag(parse_multiple_where_clauses)]
15941602
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)