rework add_placeholder_from_predicate_note

This commit is contained in:
lcnr
2025-08-07 11:30:24 +02:00
parent d62e8578c5
commit a95a2ac476
12 changed files with 84 additions and 34 deletions
+49 -9
View File
@@ -6,7 +6,9 @@
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan, listify};
use rustc_hir::def::{CtorKind, Namespace};
use rustc_hir::{self as hir, CoroutineKind, LangItem};
use rustc_hir::{
self as hir, CoroutineKind, GenericBound, LangItem, WhereBoundPredicate, WherePredicateKind,
};
use rustc_index::{IndexSlice, IndexVec};
use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};
use rustc_infer::traits::SelectionError;
@@ -658,25 +660,63 @@ pub(super) fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> Str
/// Add a note to region errors and borrow explanations when higher-ranked regions in predicates
/// implicitly introduce an "outlives `'static`" constraint.
///
/// This is very similar to `fn suggest_static_lifetime_for_gat_from_hrtb` which handles this
/// note for failed type tests instead of outlives errors.
fn add_placeholder_from_predicate_note<G: EmissionGuarantee>(
&self,
err: &mut Diag<'_, G>,
diag: &mut Diag<'_, G>,
path: &[OutlivesConstraint<'tcx>],
) {
let predicate_span = path.iter().find_map(|constraint| {
let tcx = self.infcx.tcx;
let Some((gat_hir_id, generics)) = path.iter().find_map(|constraint| {
let outlived = constraint.sub;
if let Some(origin) = self.regioncx.definitions.get(outlived)
&& let NllRegionVariableOrigin::Placeholder(_) = origin.origin
&& let ConstraintCategory::Predicate(span) = constraint.category
&& let NllRegionVariableOrigin::Placeholder(placeholder) = origin.origin
&& let Some(id) = placeholder.bound.kind.get_id()
&& let Some(placeholder_id) = id.as_local()
&& let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
&& let Some(generics_impl) =
tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
{
Some(span)
Some((gat_hir_id, generics_impl))
} else {
None
}
});
}) else {
return;
};
if let Some(span) = predicate_span {
err.span_note(span, fluent::borrowck_limitations_implies_static);
for pred in generics.predicates {
let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
bound_generic_params,
bounds,
..
}) = pred.kind
else {
continue;
};
if bound_generic_params
.iter()
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
.is_some()
{
diag.span_note(pred.span, fluent::borrowck_limitations_implies_static);
return;
}
for bound in bounds.iter() {
if let GenericBound::Trait(bound) = bound {
if bound
.bound_generic_params
.iter()
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
.is_some()
{
diag.span_note(bound.span, fluent::borrowck_limitations_implies_static);
return;
}
}
}
}
}
@@ -22,12 +22,6 @@ LL | force_send(async_load(&not_static));
...
LL | }
| - `not_static` dropped here while still borrowed
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/implementation-not-general-enough-ice-133252.rs:16:18
|
LL | fn force_send<T: Send>(_: T) {}
| ^^^^
error: aborting due to 2 previous errors
@@ -10,10 +10,10 @@ LL | }
| - temporary value is freed at the end of this statement
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/hrtb-implied-1.rs:26:26
--> $DIR/hrtb-implied-1.rs:26:5
|
LL | for<'a> I::Item<'a>: Debug,
| ^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -15,7 +15,11 @@ LL | let _next = iter2.next();
= note: requirement occurs because of a mutable reference to `Eat<&mut I, F>`
= note: mutable references are invariant over their type parameter
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
= note: due to current limitations in the borrow checker, this implies a `'static` lifetime
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/hrtb-implied-2.rs:31:8
|
LL | F: FnMut(I::Item<'_>),
| ^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -12,10 +12,10 @@ LL | trivial_bound(iter);
| argument requires that `'1` must outlive `'static`
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/hrtb-implied-3.rs:14:26
--> $DIR/hrtb-implied-3.rs:14:5
|
LL | for<'a> I::Item<'a>: Sized,
| ^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -30,6 +30,12 @@ LL | fn test_lifetime<'lt, T: Trait>(_: Foo<&'lt u8>) {}
| | |
| | lifetime `'lt` defined here
| requires that `'lt` must outlive `'static`
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/normalization-placeholder-leak.rs:19:5
|
LL | for<'x> T::Ty<'x>: Sized;
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: lifetime may not live long enough
--> $DIR/normalization-placeholder-leak.rs:38:5
@@ -39,6 +45,12 @@ LL | fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {}
| | |
| | lifetime `'lt` defined here
| requires that `'lt` must outlive `'static`
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/normalization-placeholder-leak.rs:19:5
|
LL | for<'x> T::Ty<'x>: Sized;
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 6 previous errors
+2 -2
View File
@@ -7,10 +7,10 @@ LL | foo::<&'a i32>();
| ^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/issue-26217.rs:1:30
--> $DIR/issue-26217.rs:1:19
|
LL | fn foo<T>() where for<'a> T: 'a {}
| ^^
| ^^^^^^^^^^^^^
error: aborting due to 1 previous error
@@ -13,10 +13,10 @@ LL | }
| - `local` dropped here while still borrowed
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/local-outlives-static-via-hrtb.rs:15:53
--> $DIR/local-outlives-static-via-hrtb.rs:15:42
|
LL | fn assert_static_via_hrtb<G>(_: G) where for<'a> G: Outlives<'a> {}
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^
error[E0597]: `local` does not live long enough
--> $DIR/local-outlives-static-via-hrtb.rs:25:45
@@ -33,10 +33,10 @@ LL | }
| - `local` dropped here while still borrowed
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/local-outlives-static-via-hrtb.rs:19:20
--> $DIR/local-outlives-static-via-hrtb.rs:19:5
|
LL | for<'a> &'a T: Reference<AssociatedType = &'a ()>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
@@ -14,10 +14,10 @@ LL | }
| - `a` dropped here while still borrowed
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:11
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^
| ^^^^^^^^^^^^^^
error: implementation of `Fn` is not general enough
--> $DIR/location-insensitive-scopes-issue-117146.rs:13:5
@@ -14,10 +14,10 @@ LL | }
| - `a` dropped here while still borrowed
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:22
--> $DIR/location-insensitive-scopes-issue-117146.rs:20:11
|
LL | fn bad<F: Fn(&()) -> &()>(_: F) {}
| ^^^
| ^^^^^^^^^^^^^^
error: implementation of `Fn` is not general enough
--> $DIR/location-insensitive-scopes-issue-117146.rs:13:5
+2 -2
View File
@@ -13,10 +13,10 @@ LL | outlives_forall::<Value<'a>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/type-test-universe.rs:6:16
--> $DIR/type-test-universe.rs:6:5
|
LL | for<'u> T: 'u,
| ^^
| ^^^^^^^^^^^^^
error: aborting due to 2 previous errors
@@ -68,10 +68,10 @@ LL | unsafe { extend_hrtb(src) }
| argument requires that `'a` must outlive `'static`
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> $DIR/reject_lifetime_extension.rs:85:25
--> $DIR/reject_lifetime_extension.rs:85:9
|
LL | for<'b> &'b u8: TransmuteFrom<&'a u8>,
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 8 previous errors