Skip to content

Commit f6a37a1

Browse files
committed
Fix most of the cases of missing "implied 'static" warnings
1 parent a69dbd7 commit f6a37a1

File tree

5 files changed

+65
-29
lines changed

5 files changed

+65
-29
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,36 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
203203
fn suggest_static_lifetime_for_gat_from_hrtb(
204204
&self,
205205
diag: &mut Diag<'_>,
206-
_lower_bound: RegionVid,
207-
) {
206+
lower_bound: RegionVid,
207+
) -> Option<()> {
208208
let mut suggestions = vec![];
209209
let hir = self.infcx.tcx.hir();
210210

211211
// find higher-ranked trait bounds bounded to the generic associated types
212-
let hrtb_bounds = vec![];
213-
/*
214-
// FIXME: the best we can do is look at the representative, using something like:
215212
let scc = self.regioncx.constraint_sccs().scc(lower_bound);
216-
let Some(representative) =
217-
self.regioncx.constraint_sccs().annotation(scc).placeholder_representative()
218-
else {
219-
return;
220-
};
221-
*/
213+
let placeholder: ty::PlaceholderRegion = self.regioncx.placeholder_representative(scc)?;
214+
let placeholder_id = placeholder.bound.kind.get_id()?.as_local()?;
215+
let gat_hir_id = self.infcx.tcx.local_def_id_to_hir_id(placeholder_id);
216+
let generics_impl =
217+
self.infcx.tcx.parent_hir_node(self.infcx.tcx.parent_hir_id(gat_hir_id)).generics()?;
218+
219+
let mut hrtb_bounds = vec![];
220+
221+
for pred in generics_impl.predicates {
222+
let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) = pred
223+
else {
224+
continue;
225+
};
226+
if bound_generic_params
227+
.iter()
228+
.rfind(|bgp| self.infcx.tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
229+
.is_some()
230+
{
231+
for bound in *bounds {
232+
hrtb_bounds.push(bound);
233+
}
234+
}
235+
}
222236

223237
hrtb_bounds.iter().for_each(|bound| {
224238
let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }, _) = bound else {
@@ -267,6 +281,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
267281
Applicability::MaybeIncorrect,
268282
);
269283
}
284+
Some(())
270285
}
271286

272287
/// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.

compiler/rustc_borrowck/src/nll.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,12 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
119119
// base constraints generated by the type-check.
120120
let var_origins = infcx.get_region_var_origins();
121121
let MirTypeckRegionConstraints {
122-
placeholder_indices: _,
123-
placeholder_index_to_region: _,
124122
liveness_constraints,
125123
mut outlives_constraints,
126124
mut member_constraints,
127125
universe_causes,
128126
type_tests,
127+
..
129128
} = constraints;
130129

131130
// If requested, emit legacy polonius facts.

compiler/rustc_borrowck/src/region_infer/mod.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc_middle::mir::{
1818
TerminatorKind,
1919
};
2020
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
21-
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex};
21+
use rustc_middle::ty::{
22+
self, PlaceholderRegion, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex,
23+
};
2224
use rustc_mir_dataflow::points::DenseLocationMap;
2325
use rustc_span::Span;
2426

@@ -2226,6 +2228,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22262228
fn is_static(&self, r: RegionVid) -> bool {
22272229
r == self.universal_regions.fr_static
22282230
}
2231+
2232+
pub(crate) fn placeholder_representative(
2233+
&self,
2234+
scc: ConstraintSccIndex,
2235+
) -> Option<PlaceholderRegion> {
2236+
if let Some(representative) =
2237+
self.constraint_sccs.annotation(scc).placeholder_representative()
2238+
&& let NllRegionVariableOrigin::Placeholder(p) = self.definitions[representative].origin
2239+
{
2240+
Some(p)
2241+
} else {
2242+
None
2243+
}
2244+
}
22292245
}
22302246

22312247
impl<'tcx> RegionDefinition<'tcx> {

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,26 @@ impl<'tcx> RegionInferenceContext<'tcx> {
212212

213213
// Special handling of higher-ranked regions.
214214
if !self.scc_universe(scc).is_root() {
215-
if self
216-
.constraint_sccs
217-
.annotation(scc)
218-
.placeholder_representative()
219-
.is_some_and(|representative| representative == vid)
215+
let higher_ranked_region = if let Some(representative) =
216+
self.placeholder_representative(scc)
220217
{
221-
if let NllRegionVariableOrigin::Placeholder(p) =
222-
self.definitions[vid].origin
223-
{
224-
return ty::Region::new_placeholder(tcx, p);
225-
}
226-
}
227-
debug!("Cannot get a nice name for higher-ranked region {vid:?} as {region:?}");
228-
return region;
218+
// FIXME: remove this assertion once the tests pass!
219+
assert!(
220+
self.constraint_sccs
221+
.annotation(scc)
222+
.placeholder_representative()
223+
.is_some_and(|representative| representative == vid)
224+
);
225+
226+
ty::Region::new_placeholder(tcx, representative)
227+
} else {
228+
debug!(
229+
"Cannot get a nice name for higher-ranked region {vid:?} as {region:?}"
230+
);
231+
232+
region
233+
};
234+
return higher_ranked_region;
229235
}
230236

231237
// Find something that we can name

compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -890,14 +890,14 @@ pub(crate) struct MirTypeckRegionConstraints<'tcx> {
890890
///
891891
/// To keep everything in sync, do not insert this set
892892
/// directly. Instead, use the `placeholder_region` helper.
893-
pub(crate) placeholder_indices: PlaceholderIndices,
893+
placeholder_indices: PlaceholderIndices,
894894

895895
/// Each time we add a placeholder to `placeholder_indices`, we
896896
/// also create a corresponding "representative" region vid for
897897
/// that wraps it. This vector tracks those. This way, when we
898898
/// convert the same `ty::RePlaceholder(p)` twice, we can map to
899899
/// the same underlying `RegionVid`.
900-
pub(crate) placeholder_index_to_region: IndexVec<PlaceholderIndex, ty::Region<'tcx>>,
900+
placeholder_index_to_region: IndexVec<PlaceholderIndex, ty::Region<'tcx>>,
901901

902902
/// In general, the type-checker is not responsible for enforcing
903903
/// liveness constraints; this job falls to the region inferencer,

0 commit comments

Comments
 (0)