Skip to content

Commit cfe5a1e

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

5 files changed

+118
-12
lines changed

compiler/rustc_resolve/src/late.rs

+37
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,31 @@ 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(poly_trait_ref, _) = bound {
4727+
let names =
4728+
poly_trait_ref.bound_generic_params.iter().map(|v| v.ident).collect();
4729+
self.with_poly_trait_ref.insert(
4730+
bounded_ty.node_id(),
4731+
RedundantPolyTraitRef {
4732+
poly_trait: poly_trait_ref.span,
4733+
trait_ref: poly_trait_ref.trait_ref.path.span,
4734+
generic_param_idents: names,
4735+
},
4736+
);
4737+
break;
4738+
}
4739+
}
4740+
}
4741+
}
47054742
}
47064743

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

compiler/rustc_resolve/src/late/diagnostics.rs

+49-12
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,32 +2751,69 @@ 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-
let sugg = format!(
2754+
2755+
let mut span = span;
2756+
let mut rm_poly_trait_span = DUMMY_SP;
2757+
let mut sugg = format!("{}, ", name.unwrap_or("'a"));
2758+
2759+
if span.is_empty() {
2760+
let mut generic_params = "".to_string();
2761+
if let Some(with_poly_trait_ref) = self.with_poly_trait_ref.get(&node_id)
2762+
&& higher_ranked
2763+
{
2764+
generic_params = with_poly_trait_ref.generic_param_idents.iter().fold(
2765+
"".to_string(),
2766+
|mut generic_params, x| {
2767+
generic_params += x.as_str();
2768+
generic_params += ", ";
2769+
generic_params
2770+
},
2771+
);
2772+
if !generic_params.is_empty() {
2773+
rm_poly_trait_span = with_poly_trait_ref
2774+
.poly_trait
2775+
.with_hi(with_poly_trait_ref.trait_ref.lo());
2776+
}
2777+
}
2778+
2779+
sugg = format!(
27552780
"{}<{}>{}",
27562781
if higher_ranked { "for" } else { "" },
2757-
name.unwrap_or("'a"),
2782+
format!("{}{}", generic_params, name.unwrap_or("'a")),
27582783
if higher_ranked { " " } else { "" },
27592784
);
2760-
(span, sugg)
27612785
} else {
2762-
let span = self
2786+
span = self
27632787
.r
27642788
.tcx
27652789
.sess
27662790
.source_map()
27672791
.span_through_char(span, '<')
27682792
.shrink_to_hi();
2769-
let sugg = format!("{}, ", name.unwrap_or("'a"));
2770-
(span, sugg)
2771-
};
2793+
}
2794+
27722795
if higher_ranked {
27732796
let message = Cow::from(format!(
27742797
"consider making the {} lifetime-generic with a new `{}` lifetime",
27752798
kind.descr(),
27762799
name.unwrap_or("'a"),
27772800
));
2778-
should_continue = suggest(err, true, span, message, sugg);
2801+
should_continue = if !rm_poly_trait_span.is_dummy() {
2802+
// We should merge the higher-ranked lifetimes in following situation:
2803+
// Change `T: for<'a> Trait<T> + 'b` to `for<'a, 'b> T: Trait<T> + 'b`
2804+
// T: for<'a> Trait<T> + 'b
2805+
// ^^^^^^^ remove existed `for<'a>`
2806+
// for<'a, 'b> T: Trait<T> + 'b
2807+
// ^^^^^^^^^^^ add suggestion `for<'a, 'b>`
2808+
err.multipart_suggestion_verbose(
2809+
message,
2810+
vec![(span, sugg), (rm_poly_trait_span, "".to_string())],
2811+
Applicability::MaybeIncorrect,
2812+
);
2813+
false
2814+
} else {
2815+
suggest(err, true, span, message.clone(), sugg.clone())
2816+
};
27792817
err.note_once(
27802818
"for more information on higher-ranked polymorphism, visit \
27812819
https://doc.rust-lang.org/nomicon/hrtb.html",
@@ -3298,7 +3336,6 @@ fn mk_where_bound_predicate(
32983336
poly_trait_ref: &ast::PolyTraitRef,
32993337
ty: &Ty,
33003338
) -> Option<ast::WhereBoundPredicate> {
3301-
use rustc_span::DUMMY_SP;
33023339
let modified_segments = {
33033340
let mut segments = path.segments.clone();
33043341
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)