Skip to content

Commit 6fa9f51

Browse files
committed
Suggest assoc ty bound on lifetime in eq constraint
1 parent 7a08447 commit 6fa9f51

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
@@ -450,6 +446,12 @@ parse_lifetime_in_borrow_expression = borrow expressions cannot be annotated wit
450446
.suggestion = remove the lifetime annotation
451447
.label = annotated with lifetime here
452448
449+
parse_lifetime_in_eq_constraint = lifetimes are not permitted in this context
450+
.label = lifetime is not allowed here
451+
.context_label = this introduces an associated item binding
452+
.help = if you meant to specify a trait object, write `dyn /*Trait*/ + {$lifetime}`
453+
.colon_sugg = you might have meant to write a bound here
454+
453455
parse_lone_slash = invalid trailing slash in literal
454456
.label = {parse_lone_slash}
455457

compiler/rustc_parse/src/errors.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -2618,13 +2618,22 @@ pub(crate) struct GenericsInPath {
26182618
}
26192619

26202620
#[derive(Diagnostic)]
2621-
#[diag(parse_assoc_lifetime)]
2621+
#[diag(parse_lifetime_in_eq_constraint)]
26222622
#[help]
2623-
pub(crate) struct AssocLifetime {
2623+
pub(crate) struct LifetimeInEqConstraint {
26242624
#[primary_span]
2625-
pub span: Span,
26262625
#[label]
2627-
pub lifetime: Span,
2626+
pub span: Span,
2627+
pub lifetime: Ident,
2628+
#[label(parse_context_label)]
2629+
pub binding_label: Span,
2630+
#[suggestion(
2631+
parse_colon_sugg,
2632+
style = "verbose",
2633+
applicability = "maybe-incorrect",
2634+
code = ": "
2635+
)]
2636+
pub colon_sugg: Span,
26282637
}
26292638

26302639
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/path.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,11 @@ impl<'a> Parser<'a> {
626626
let bounds = self.parse_generic_bounds()?;
627627
AssocConstraintKind::Bound { bounds }
628628
} else if self.eat(&token::Eq) {
629-
self.parse_assoc_equality_term(ident, self.prev_token.span)?
629+
self.parse_assoc_equality_term(
630+
ident,
631+
gen_args.as_ref(),
632+
self.prev_token.span,
633+
)?
630634
} else {
631635
unreachable!();
632636
};
@@ -663,11 +667,13 @@ impl<'a> Parser<'a> {
663667
}
664668

665669
/// Parse the term to the right of an associated item equality constraint.
666-
/// That is, parse `<term>` in `Item = <term>`.
667-
/// Right now, this only admits types in `<term>`.
670+
///
671+
/// That is, parse `$term` in `Item = $term` where `$term` is a type or
672+
/// a const expression (wrapped in curly braces if complex).
668673
fn parse_assoc_equality_term(
669674
&mut self,
670675
ident: Ident,
676+
gen_args: Option<&GenericArgs>,
671677
eq: Span,
672678
) -> PResult<'a, AssocConstraintKind> {
673679
let arg = self.parse_generic_arg(None)?;
@@ -679,9 +685,15 @@ impl<'a> Parser<'a> {
679685
c.into()
680686
}
681687
Some(GenericArg::Lifetime(lt)) => {
682-
let guar =
683-
self.dcx().emit_err(errors::AssocLifetime { span, lifetime: lt.ident.span });
684-
self.mk_ty(span, ast::TyKind::Err(guar)).into()
688+
let guar = self.dcx().emit_err(errors::LifetimeInEqConstraint {
689+
span: lt.ident.span,
690+
lifetime: lt.ident,
691+
binding_label: span,
692+
colon_sugg: gen_args
693+
.map_or(ident.span, |args| args.span())
694+
.between(lt.ident.span),
695+
});
696+
self.mk_ty(lt.ident.span, ast::TyKind::Err(guar)).into()
685697
}
686698
None => {
687699
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)