Skip to content

Commit 2941db4

Browse files
committed
Fix incorrect suggestion for undeclared hrtb lifetimes in where clauses.
fixes #122714
1 parent 6e1f7b5 commit 2941db4

5 files changed

+134
-8
lines changed

compiler/rustc_resolve/src/late.rs

+45
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,13 @@ struct DiagMetadata<'ast> {
653653
current_elision_failures: Vec<MissingLifetime>,
654654
}
655655

656+
#[derive(Debug, Default)]
657+
struct RedundantPolyTraitRef {
658+
poly_trait: Span,
659+
trait_ref: Span,
660+
generic_param_idents: Vec<Ident>,
661+
}
662+
656663
struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
657664
r: &'b mut Resolver<'a, 'tcx>,
658665

@@ -693,6 +700,9 @@ struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
693700

694701
/// Count the number of places a lifetime is used.
695702
lifetime_uses: FxHashMap<LocalDefId, LifetimeUseSet>,
703+
704+
/// Record poly-trait-ref, only used for diagnostic.
705+
with_poly_trait_ref: FxHashMap<NodeId, RedundantPolyTraitRef>,
696706
}
697707

698708
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
@@ -1197,6 +1207,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
11971207

11981208
fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
11991209
debug!("visit_where_predicate {:?}", p);
1210+
self.record_where_bound_predicate_with_poly_trait(p);
12001211
let previous_value = replace(&mut self.diag_metadata.current_where_predicate, Some(p));
12011212
self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
12021213
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
@@ -1306,6 +1317,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
13061317
// errors at module scope should always be reported
13071318
in_func_body: false,
13081319
lifetime_uses: Default::default(),
1320+
with_poly_trait_ref: Default::default(),
13091321
}
13101322
}
13111323

@@ -4702,6 +4714,39 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
47024714
});
47034715
}
47044716
}
4717+
4718+
fn record_where_bound_predicate_with_poly_trait(&mut self, p: &WherePredicate) {
4719+
if let ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
4720+
bounded_ty,
4721+
bounds,
4722+
..
4723+
}) = p
4724+
{
4725+
for bound in bounds {
4726+
if let ast::GenericBound::Trait(
4727+
ast::PolyTraitRef {
4728+
span: poly_span,
4729+
trait_ref: ast::TraitRef { path: ast::Path { span, .. }, .. },
4730+
bound_generic_params,
4731+
..
4732+
},
4733+
_,
4734+
) = bound
4735+
{
4736+
let names = bound_generic_params.iter().map(|v| v.ident).collect();
4737+
self.with_poly_trait_ref.insert(
4738+
bounded_ty.node_id(),
4739+
RedundantPolyTraitRef {
4740+
poly_trait: *poly_span,
4741+
trait_ref: *span,
4742+
generic_param_idents: names,
4743+
},
4744+
);
4745+
break;
4746+
}
4747+
}
4748+
}
4749+
}
47054750
}
47064751

47074752
/// Walks the whole crate in DFS order, visiting each item, counting the declared number of

compiler/rustc_resolve/src/late/diagnostics.rs

+57-8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
3131
use rustc_span::edition::Edition;
3232
use rustc_span::hygiene::MacroKind;
3333
use rustc_span::symbol::{kw, sym, Ident, Symbol};
34-
use rustc_span::Span;
34+
use rustc_span::{Span, DUMMY_SP};
3535

3636
use rustc_middle::ty;
3737

@@ -2718,10 +2718,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
27182718
suggest: impl Fn(&mut Diag<'_>, bool, Span, Cow<'static, str>, String) -> bool,
27192719
) {
27202720
let mut suggest_note = true;
2721+
27212722
for rib in self.lifetime_ribs.iter().rev() {
27222723
let mut should_continue = true;
27232724
match rib.kind {
2724-
LifetimeRibKind::Generics { binder: _, span, kind } => {
2725+
LifetimeRibKind::Generics { binder: node_id, span, kind } => {
27252726
// Avoid suggesting placing lifetime parameters on constant items unless the relevant
27262727
// feature is enabled. Suggest the parent item as a possible location if applicable.
27272728
if let LifetimeBinderKind::ConstItem = kind
@@ -2750,14 +2751,48 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
27502751
| LifetimeBinderKind::PolyTrait
27512752
| LifetimeBinderKind::WhereBound
27522753
);
2753-
let (span, sugg) = if span.is_empty() {
2754+
2755+
let (span, sugg, rm_poly_trait_span) = if span.is_empty() {
2756+
let (generic_params, poly_trait_span, trait_ref_span) =
2757+
if let Some(with_poly_trait_ref) =
2758+
self.with_poly_trait_ref.get(&node_id)
2759+
&& higher_ranked
2760+
{
2761+
let generic_params = with_poly_trait_ref
2762+
.generic_param_idents
2763+
.iter()
2764+
.fold("".to_string(), |mut generic_params, x| {
2765+
generic_params += x.as_str();
2766+
generic_params += ", ";
2767+
generic_params
2768+
});
2769+
(
2770+
generic_params,
2771+
with_poly_trait_ref.poly_trait,
2772+
with_poly_trait_ref.trait_ref,
2773+
)
2774+
} else {
2775+
("".to_string(), DUMMY_SP, DUMMY_SP)
2776+
};
2777+
2778+
let rm_poly_trait_span = if generic_params.is_empty() {
2779+
DUMMY_SP
2780+
} else {
2781+
poly_trait_span.with_hi(trait_ref_span.lo())
2782+
};
2783+
2784+
let lifetime_list = format!("{}{}", generic_params, name.unwrap_or("'a"));
27542785
let sugg = format!(
27552786
"{}<{}>{}",
27562787
if higher_ranked { "for" } else { "" },
2757-
name.unwrap_or("'a"),
2788+
if generic_params.is_empty() {
2789+
name.unwrap_or("'a")
2790+
} else {
2791+
&lifetime_list
2792+
},
27582793
if higher_ranked { " " } else { "" },
27592794
);
2760-
(span, sugg)
2795+
(span, sugg, rm_poly_trait_span)
27612796
} else {
27622797
let span = self
27632798
.r
@@ -2767,15 +2802,30 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
27672802
.span_through_char(span, '<')
27682803
.shrink_to_hi();
27692804
let sugg = format!("{}, ", name.unwrap_or("'a"));
2770-
(span, sugg)
2805+
(span, sugg, DUMMY_SP)
27712806
};
2807+
27722808
if higher_ranked {
27732809
let message = Cow::from(format!(
27742810
"consider making the {} lifetime-generic with a new `{}` lifetime",
27752811
kind.descr(),
27762812
name.unwrap_or("'a"),
27772813
));
2778-
should_continue = suggest(err, true, span, message, sugg);
2814+
should_continue = if !rm_poly_trait_span.is_dummy() {
2815+
// For poly-trait-ref like `for<'a> Trait<T>` in
2816+
// `T: for<'a> Trait<T> + 'b { }`.
2817+
// We should merge the higher-ranked lifetimes: existed `for<'a>` and suggestion `for<'b>`
2818+
// or will get err:
2819+
// `[E0316] nested quantification of lifetimes`.
2820+
err.multipart_suggestion_verbose(
2821+
message,
2822+
vec![(span, sugg), (rm_poly_trait_span, "".to_string())],
2823+
Applicability::MaybeIncorrect,
2824+
);
2825+
false
2826+
} else {
2827+
suggest(err, true, span, message.clone(), sugg.clone())
2828+
};
27792829
err.note_once(
27802830
"for more information on higher-ranked polymorphism, visit \
27812831
https://doc.rust-lang.org/nomicon/hrtb.html",
@@ -3298,7 +3348,6 @@ fn mk_where_bound_predicate(
32983348
poly_trait_ref: &ast::PolyTraitRef,
32993349
ty: &Ty,
33003350
) -> Option<ast::WhereBoundPredicate> {
3301-
use rustc_span::DUMMY_SP;
33023351
let modified_segments = {
33033352
let mut segments = path.segments.clone();
33043353
let [preceding @ .., second_last, last] = segments.as_mut_slice() else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ run-rustfix
2+
3+
#![allow(dead_code)]
4+
5+
trait Trait<T>
6+
where for<'a, 'b> T: Trait<T> + 'b { } //~ ERROR use of undeclared lifetime name `'b`
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ run-rustfix
2+
3+
#![allow(dead_code)]
4+
5+
trait Trait<T>
6+
where T: for<'a> Trait<T> + 'b { } //~ ERROR use of undeclared lifetime name `'b`
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0261]: use of undeclared lifetime name `'b`
2+
--> $DIR/generic-higher-ranked-lifetime-issue-122714.rs:6:31
3+
|
4+
LL | where T: for<'a> Trait<T> + 'b { }
5+
| ^^ undeclared lifetime
6+
|
7+
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
8+
help: consider making the bound lifetime-generic with a new `'b` lifetime
9+
|
10+
LL - where T: for<'a> Trait<T> + 'b { }
11+
LL + where for<'a, 'b> T: Trait<T> + 'b { }
12+
|
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)