Skip to content

Commit 2d424cb

Browse files
committed
Fix most of the cases of missing "implied 'static" warnings
1 parent a555e50 commit 2d424cb

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
@@ -205,22 +205,36 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
205205
fn suggest_static_lifetime_for_gat_from_hrtb(
206206
&self,
207207
diag: &mut Diag<'_>,
208-
_lower_bound: RegionVid,
209-
) {
208+
lower_bound: RegionVid,
209+
) -> Option<()> {
210210
let mut suggestions = vec![];
211211
let hir = self.infcx.tcx.hir();
212212

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

225239
hrtb_bounds.iter().for_each(|bound| {
226240
let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }, _) = bound else {
@@ -269,6 +283,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
269283
Applicability::MaybeIncorrect,
270284
);
271285
}
286+
Some(())
272287
}
273288

274289
/// 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
@@ -122,13 +122,12 @@ pub(crate) fn compute_regions<'a, 'tcx>(
122122
// base constraints generated by the type-check.
123123
let var_origins = infcx.get_region_var_origins();
124124
let MirTypeckRegionConstraints {
125-
placeholder_indices: _,
126-
placeholder_index_to_region: _,
127125
liveness_constraints,
128126
mut outlives_constraints,
129127
mut member_constraints,
130128
universe_causes,
131129
type_tests,
130+
..
132131
} = constraints;
133132

134133
// 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
use tracing::{debug, instrument, trace};
@@ -2228,6 +2230,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22282230
fn is_static(&self, r: RegionVid) -> bool {
22292231
r == self.universal_regions.fr_static
22302232
}
2233+
2234+
pub(crate) fn placeholder_representative(
2235+
&self,
2236+
scc: ConstraintSccIndex,
2237+
) -> Option<PlaceholderRegion> {
2238+
if let Some(representative) =
2239+
self.constraint_sccs.annotation(scc).placeholder_representative()
2240+
&& let NllRegionVariableOrigin::Placeholder(p) = self.definitions[representative].origin
2241+
{
2242+
Some(p)
2243+
} else {
2244+
None
2245+
}
2246+
}
22312247
}
22322248

22332249
impl<'tcx> RegionDefinition<'tcx> {

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

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

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

232238
// Find something that we can name

compiler/rustc_borrowck/src/type_check/mod.rs

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

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

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

0 commit comments

Comments
 (0)