Skip to content

Commit 5a3ddbc

Browse files
committed
rework add_placeholder_from_predicate_note
1 parent bd0cde9 commit 5a3ddbc

12 files changed

+85
-34
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use rustc_abi::{FieldIdx, VariantIdx};
66
use rustc_data_structures::fx::FxIndexMap;
77
use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan, listify};
88
use rustc_hir::def::{CtorKind, Namespace};
9-
use rustc_hir::{self as hir, CoroutineKind, LangItem};
9+
use rustc_hir::{
10+
self as hir, CoroutineKind, GenericBound, LangItem, WhereBoundPredicate, WherePredicateKind,
11+
};
1012
use rustc_index::{IndexSlice, IndexVec};
1113
use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};
1214
use rustc_infer::traits::SelectionError;
@@ -658,25 +660,64 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
658660

659661
/// Add a note to region errors and borrow explanations when higher-ranked regions in predicates
660662
/// implicitly introduce an "outlives `'static`" constraint.
663+
///
664+
/// This is very similar to `fn suggest_static_lifetime_for_gat_from_hrtb` which handles this
665+
/// note for failed type tests instead of outlives errors.
661666
fn add_placeholder_from_predicate_note<G: EmissionGuarantee>(
662667
&self,
663-
err: &mut Diag<'_, G>,
668+
diag: &mut Diag<'_, G>,
664669
path: &[OutlivesConstraint<'tcx>],
665670
) {
666-
let predicate_span = path.iter().find_map(|constraint| {
671+
let tcx = self.infcx.tcx;
672+
let Some((gat_hir_id, generics)) = path.iter().find_map(|constraint| {
667673
let outlived = constraint.sub;
668674
if let Some(origin) = self.regioncx.definitions.get(outlived)
669-
&& let NllRegionVariableOrigin::Placeholder(_) = origin.origin
670-
&& let ConstraintCategory::Predicate(span) = constraint.category
675+
&& let NllRegionVariableOrigin::Placeholder(placeholder) = origin.origin
676+
&& let Some(id) = placeholder.bound.kind.get_id()
677+
&& let Some(placeholder_id) = id.as_local()
678+
&& let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
679+
&& let Some(generics_impl) =
680+
tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
671681
{
672-
Some(span)
682+
Some((gat_hir_id, generics_impl))
673683
} else {
674684
None
675685
}
676-
});
686+
}) else {
687+
return;
688+
};
677689

678-
if let Some(span) = predicate_span {
679-
err.span_note(span, fluent::borrowck_limitations_implies_static);
690+
// find higher-ranked trait bounds bounded to the generic associated types
691+
for pred in generics.predicates {
692+
let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
693+
bound_generic_params,
694+
bounds,
695+
..
696+
}) = pred.kind
697+
else {
698+
continue;
699+
};
700+
if bound_generic_params
701+
.iter()
702+
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
703+
.is_some()
704+
{
705+
diag.span_note(pred.span, fluent::borrowck_limitations_implies_static);
706+
return;
707+
}
708+
for bound in bounds.iter() {
709+
if let GenericBound::Trait(bound) = bound {
710+
if bound
711+
.bound_generic_params
712+
.iter()
713+
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
714+
.is_some()
715+
{
716+
diag.span_note(bound.span, fluent::borrowck_limitations_implies_static);
717+
return;
718+
}
719+
}
720+
}
680721
}
681722
}
682723

tests/ui/borrowck/implementation-not-general-enough-ice-133252.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ LL | force_send(async_load(&not_static));
2222
...
2323
LL | }
2424
| - `not_static` dropped here while still borrowed
25-
|
26-
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
27-
--> $DIR/implementation-not-general-enough-ice-133252.rs:16:18
28-
|
29-
LL | fn force_send<T: Send>(_: T) {}
30-
| ^^^^
3125

3226
error: aborting due to 2 previous errors
3327

tests/ui/generic-associated-types/bugs/hrtb-implied-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ LL | }
1010
| - temporary value is freed at the end of this statement
1111
|
1212
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
13-
--> $DIR/hrtb-implied-1.rs:26:26
13+
--> $DIR/hrtb-implied-1.rs:26:5
1414
|
1515
LL | for<'a> I::Item<'a>: Debug,
16-
| ^^^^^
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1717

1818
error: aborting due to 1 previous error
1919

tests/ui/generic-associated-types/bugs/hrtb-implied-2.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ LL | let _next = iter2.next();
1515
= note: requirement occurs because of a mutable reference to `Eat<&mut I, F>`
1616
= note: mutable references are invariant over their type parameter
1717
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
18-
= note: due to current limitations in the borrow checker, this implies a `'static` lifetime
18+
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
19+
--> $DIR/hrtb-implied-2.rs:31:8
20+
|
21+
LL | F: FnMut(I::Item<'_>),
22+
| ^^^^^^^^^^^^^^^^^^
1923

2024
error: aborting due to 1 previous error
2125

tests/ui/generic-associated-types/bugs/hrtb-implied-3.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ LL | trivial_bound(iter);
1212
| argument requires that `'1` must outlive `'static`
1313
|
1414
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
15-
--> $DIR/hrtb-implied-3.rs:14:26
15+
--> $DIR/hrtb-implied-3.rs:14:5
1616
|
1717
LL | for<'a> I::Item<'a>: Sized,
18-
| ^^^^^
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1919

2020
error: aborting due to 1 previous error
2121

tests/ui/implied-bounds/normalization-placeholder-leak.fail.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ LL | fn test_lifetime<'lt, T: Trait>(_: Foo<&'lt u8>) {}
3030
| | |
3131
| | lifetime `'lt` defined here
3232
| requires that `'lt` must outlive `'static`
33+
|
34+
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
35+
--> $DIR/normalization-placeholder-leak.rs:19:5
36+
|
37+
LL | for<'x> T::Ty<'x>: Sized;
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^
3339

3440
error: lifetime may not live long enough
3541
--> $DIR/normalization-placeholder-leak.rs:38:5
@@ -39,6 +45,12 @@ LL | fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {}
3945
| | |
4046
| | lifetime `'lt` defined here
4147
| requires that `'lt` must outlive `'static`
48+
|
49+
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
50+
--> $DIR/normalization-placeholder-leak.rs:19:5
51+
|
52+
LL | for<'x> T::Ty<'x>: Sized;
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^
4254

4355
error: aborting due to 6 previous errors
4456

tests/ui/issues/issue-26217.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | foo::<&'a i32>();
77
| ^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
88
|
99
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
10-
--> $DIR/issue-26217.rs:1:30
10+
--> $DIR/issue-26217.rs:1:19
1111
|
1212
LL | fn foo<T>() where for<'a> T: 'a {}
13-
| ^^
13+
| ^^^^^^^^^^^^^
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/nll/local-outlives-static-via-hrtb.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ LL | }
1313
| - `local` dropped here while still borrowed
1414
|
1515
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
16-
--> $DIR/local-outlives-static-via-hrtb.rs:15:53
16+
--> $DIR/local-outlives-static-via-hrtb.rs:15:42
1717
|
1818
LL | fn assert_static_via_hrtb<G>(_: G) where for<'a> G: Outlives<'a> {}
19-
| ^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error[E0597]: `local` does not live long enough
2222
--> $DIR/local-outlives-static-via-hrtb.rs:25:45
@@ -33,10 +33,10 @@ LL | }
3333
| - `local` dropped here while still borrowed
3434
|
3535
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
36-
--> $DIR/local-outlives-static-via-hrtb.rs:19:20
36+
--> $DIR/local-outlives-static-via-hrtb.rs:19:5
3737
|
3838
LL | for<'a> &'a T: Reference<AssociatedType = &'a ()>,
39-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040

4141
error: aborting due to 2 previous errors
4242

tests/ui/nll/polonius/location-insensitive-scopes-issue-117146.nll.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ LL | }
1414
| - `a` dropped here while still borrowed
1515
|
1616
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
17-
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
17+
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:11
1818
|
1919
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
20-
| ^^^
20+
| ^^^^^^^^^^^^^^
2121

2222
error: implementation of `Fn` is not general enough
2323
--> $DIR/location-insensitive-scopes-issue-117146.rs:13:5

tests/ui/nll/polonius/location-insensitive-scopes-issue-117146.polonius.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ LL | }
1414
| - `a` dropped here while still borrowed
1515
|
1616
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
17-
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
17+
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:11
1818
|
1919
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
20-
| ^^^
20+
| ^^^^^^^^^^^^^^
2121

2222
error: implementation of `Fn` is not general enough
2323
--> $DIR/location-insensitive-scopes-issue-117146.rs:13:5

0 commit comments

Comments
 (0)