Skip to content

Commit 3879acb

Browse files
committed
Suggest assoc ty bound on lifetime in eq constraint
1 parent 22b9e96 commit 3879acb

File tree

5 files changed

+49
-21
lines changed

5 files changed

+49
-21
lines changed

compiler/rustc_parse/messages.ftl

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ parse_array_index_offset_of = array indexing not supported in offset_of
1414
1515
parse_assignment_else_not_allowed = <assignment> ... else {"{"} ... {"}"} is not allowed
1616
17-
parse_assoc_lifetime = associated lifetimes are not supported
18-
.label = the lifetime is given here
19-
.help = if you meant to specify a trait object, write `dyn Trait + 'lifetime`
20-
2117
parse_associated_static_item_not_allowed = associated `static` items are not allowed
2218
2319
parse_async_block_in_2015 = `async` blocks are only allowed in Rust 2018 or later
@@ -445,6 +441,12 @@ parse_lifetime_in_borrow_expression = borrow expressions cannot be annotated wit
445441
.suggestion = remove the lifetime annotation
446442
.label = annotated with lifetime here
447443
444+
parse_lifetime_in_eq_constraint = lifetimes are not permitted in this context
445+
.label = lifetime is not allowed here
446+
.context_label = this introduces an associated item binding
447+
.help = if you meant to specify a trait object, write `dyn /* Trait */ + {$lifetime}`
448+
.colon_sugg = you might have meant to write a bound here
449+
448450
parse_lone_slash = invalid trailing slash in literal
449451
.label = {parse_lone_slash}
450452

compiler/rustc_parse/src/errors.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -2611,13 +2611,22 @@ pub(crate) struct GenericsInPath {
26112611
}
26122612

26132613
#[derive(Diagnostic)]
2614-
#[diag(parse_assoc_lifetime)]
2614+
#[diag(parse_lifetime_in_eq_constraint)]
26152615
#[help]
2616-
pub(crate) struct AssocLifetime {
2616+
pub(crate) struct LifetimeInEqConstraint {
26172617
#[primary_span]
2618-
pub span: Span,
26192618
#[label]
2620-
pub lifetime: Span,
2619+
pub span: Span,
2620+
pub lifetime: Ident,
2621+
#[label(parse_context_label)]
2622+
pub binding_label: Span,
2623+
#[suggestion(
2624+
parse_colon_sugg,
2625+
style = "verbose",
2626+
applicability = "maybe-incorrect",
2627+
code = ": "
2628+
)]
2629+
pub colon_sugg: Span,
26212630
}
26222631

26232632
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/path.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,11 @@ impl<'a> Parser<'a> {
718718
let bounds = self.parse_generic_bounds()?;
719719
AssocConstraintKind::Bound { bounds }
720720
} else if self.eat(&token::Eq) {
721-
self.parse_assoc_equality_term(ident, self.prev_token.span)?
721+
self.parse_assoc_equality_term(
722+
ident,
723+
gen_args.as_ref(),
724+
self.prev_token.span,
725+
)?
722726
} else {
723727
unreachable!();
724728
};
@@ -753,11 +757,13 @@ impl<'a> Parser<'a> {
753757
}
754758

755759
/// Parse the term to the right of an associated item equality constraint.
756-
/// That is, parse `<term>` in `Item = <term>`.
757-
/// Right now, this only admits types in `<term>`.
760+
///
761+
/// That is, parse `$term` in `Item = $term` where `$term` is a type or
762+
/// a const expression (wrapped in curly braces if complex).
758763
fn parse_assoc_equality_term(
759764
&mut self,
760765
ident: Ident,
766+
gen_args: Option<&GenericArgs>,
761767
eq: Span,
762768
) -> PResult<'a, AssocConstraintKind> {
763769
let arg = self.parse_generic_arg(None)?;
@@ -769,9 +775,15 @@ impl<'a> Parser<'a> {
769775
c.into()
770776
}
771777
Some(GenericArg::Lifetime(lt)) => {
772-
let guar =
773-
self.dcx().emit_err(errors::AssocLifetime { span, lifetime: lt.ident.span });
774-
self.mk_ty(span, ast::TyKind::Err(guar)).into()
778+
let guar = self.dcx().emit_err(errors::LifetimeInEqConstraint {
779+
span: lt.ident.span,
780+
lifetime: lt.ident,
781+
binding_label: span,
782+
colon_sugg: gen_args
783+
.map_or(ident.span, |args| args.span())
784+
.between(lt.ident.span),
785+
});
786+
self.mk_ty(lt.ident.span, ast::TyKind::Err(guar)).into()
775787
}
776788
None => {
777789
let after_eq = eq.shrink_to_hi();
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(FALSE)]
22
fn syntax() {
3-
bar::<Item = 'a>(); //~ ERROR associated lifetimes are not supported
3+
bar::<Item = 'a>(); //~ ERROR lifetimes are not permitted in this context
44
}
55

66
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
error: associated lifetimes are not supported
2-
--> $DIR/recover-assoc-lifetime-constraint.rs:3:11
1+
error: lifetimes are not permitted in this context
2+
--> $DIR/recover-assoc-lifetime-constraint.rs:3:18
33
|
44
LL | bar::<Item = 'a>();
5-
| ^^^^^^^--
6-
| |
7-
| the lifetime is given here
5+
| -------^^
6+
| | |
7+
| | lifetime is not allowed here
8+
| this introduces an associated item binding
89
|
9-
= help: if you meant to specify a trait object, write `dyn Trait + 'lifetime`
10+
= help: if you meant to specify a trait object, write `dyn /* Trait */ + 'a`
11+
help: you might have meant to write a bound here
12+
|
13+
LL | bar::<Item: 'a>();
14+
| ~
1015

1116
error: aborting due to 1 previous error
1217

0 commit comments

Comments
 (0)