Skip to content

Commit a08aa18

Browse files
Review comments
1 parent a23e0c9 commit a08aa18

File tree

2 files changed

+40
-48
lines changed

2 files changed

+40
-48
lines changed

compiler/rustc_lint/src/impl_trait_overcaptures.rs

+39-48
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::debug_assert_matches;
12
use std::cell::LazyCell;
23

34
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
@@ -181,7 +182,7 @@ fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
181182
ambient_variance: ty::Covariant,
182183
generics: tcx.generics_of(parent_def_id),
183184
};
184-
let _ = functional_variances.relate(sig, sig);
185+
functional_variances.relate(sig, sig).unwrap();
185186
functional_variances.variances
186187
}),
187188
outlives_env: LazyCell::new(|| {
@@ -211,10 +212,7 @@ where
211212
VarFn: FnOnce() -> FxHashMap<DefId, ty::Variance>,
212213
OutlivesFn: FnOnce() -> OutlivesEnvironment<'tcx>,
213214
{
214-
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(
215-
&mut self,
216-
t: &ty::Binder<'tcx, T>,
217-
) -> Self::Result {
215+
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(&mut self, t: &ty::Binder<'tcx, T>) {
218216
// When we get into a binder, we need to add its own bound vars to the scope.
219217
let mut added = vec![];
220218
for arg in t.bound_vars() {
@@ -244,7 +242,7 @@ where
244242
}
245243
}
246244

247-
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
245+
fn visit_ty(&mut self, t: Ty<'tcx>) {
248246
if !t.has_aliases() {
249247
return;
250248
}
@@ -296,50 +294,43 @@ where
296294
current_def_id = generics.parent;
297295
}
298296

299-
// Compute the set of in scope params that are not captured. Get their spans,
300-
// since that's all we really care about them for emitting the diagnostic.
297+
// Compute the set of in scope params that are not captured.
301298
let mut uncaptured_args: FxIndexSet<_> = self
302299
.in_scope_parameters
303300
.iter()
304301
.filter(|&(def_id, _)| !captured.contains(def_id))
305302
.collect();
306-
307-
// These are args that we know are likely fine to "overcapture", since they can be
308-
// contravariantly shortened to one of the already-captured lifetimes that they
309-
// outlive.
310-
let covariant_long_args: FxIndexSet<_> = uncaptured_args
311-
.iter()
312-
.copied()
313-
.filter(|&(def_id, kind)| {
314-
let Some(ty::Bivariant | ty::Contravariant) = self.variances.get(def_id)
315-
else {
316-
return false;
317-
};
318-
let DefKind::LifetimeParam = self.tcx.def_kind(def_id) else {
319-
return false;
320-
};
321-
let uncaptured = match *kind {
322-
ParamKind::Early(name, index) => ty::Region::new_early_param(
323-
self.tcx,
324-
ty::EarlyParamRegion { name, index },
325-
),
326-
ParamKind::Free(def_id, name) => ty::Region::new_late_param(
327-
self.tcx,
328-
self.parent_def_id.to_def_id(),
329-
ty::BoundRegionKind::BrNamed(def_id, name),
330-
),
331-
ParamKind::Late => return false,
332-
};
333-
// Does this region outlive any captured region?
334-
captured_regions.iter().any(|r| {
335-
self.outlives_env
336-
.free_region_map()
337-
.sub_free_regions(self.tcx, *r, uncaptured)
338-
})
303+
// Remove the set of lifetimes that are in-scope that outlive some other captured
304+
// lifetime and are contravariant (i.e. covariant in argument position).
305+
uncaptured_args.retain(|&(def_id, kind)| {
306+
let Some(ty::Bivariant | ty::Contravariant) = self.variances.get(def_id) else {
307+
// Keep all covariant/invariant args. Also if variance is `None`,
308+
// then that means it's either not a lifetime, or it didn't show up
309+
// anywhere in the signature.
310+
return true;
311+
};
312+
// We only computed variance of lifetimes...
313+
debug_assert_matches!(self.tcx.def_kind(def_id), DefKind::LifetimeParam);
314+
let uncaptured = match *kind {
315+
ParamKind::Early(name, index) => ty::Region::new_early_param(
316+
self.tcx,
317+
ty::EarlyParamRegion { name, index },
318+
),
319+
ParamKind::Free(def_id, name) => ty::Region::new_late_param(
320+
self.tcx,
321+
self.parent_def_id.to_def_id(),
322+
ty::BoundRegionKind::BrNamed(def_id, name),
323+
),
324+
// Totally ignore late bound args from binders.
325+
ParamKind::Late => return true,
326+
};
327+
// Does this region outlive any captured region?
328+
!captured_regions.iter().any(|r| {
329+
self.outlives_env
330+
.free_region_map()
331+
.sub_free_regions(self.tcx, *r, uncaptured)
339332
})
340-
.collect();
341-
// We don't care to warn on these args.
342-
uncaptured_args.retain(|arg| !covariant_long_args.contains(arg));
333+
});
343334

344335
// If we have uncaptured args, and if the opaque doesn't already have
345336
// `use<>` syntax on it, and we're < edition 2024, then warn the user.
@@ -548,13 +539,13 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
548539
) -> RelateResult<'tcx, T> {
549540
let old_variance = self.ambient_variance;
550541
self.ambient_variance = self.ambient_variance.xform(variance);
551-
self.relate(a, b)?;
542+
self.relate(a, b).unwrap();
552543
self.ambient_variance = old_variance;
553544
Ok(a)
554545
}
555546

556547
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
557-
structurally_relate_tys(self, a, b)?;
548+
structurally_relate_tys(self, a, b).unwrap();
558549
Ok(a)
559550
}
560551

@@ -592,7 +583,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
592583
a: ty::Const<'tcx>,
593584
b: ty::Const<'tcx>,
594585
) -> RelateResult<'tcx, ty::Const<'tcx>> {
595-
structurally_relate_consts(self, a, b)?;
586+
structurally_relate_consts(self, a, b).unwrap();
596587
Ok(a)
597588
}
598589

@@ -604,7 +595,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
604595
where
605596
T: Relate<TyCtxt<'tcx>>,
606597
{
607-
self.relate(a.skip_binder(), b.skip_binder())?;
598+
self.relate(a.skip_binder(), b.skip_binder()).unwrap();
608599
Ok(a)
609600
}
610601
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
3131
#![doc(rust_logo)]
3232
#![feature(array_windows)]
33+
#![feature(assert_matches)]
3334
#![feature(box_patterns)]
3435
#![feature(control_flow_enum)]
3536
#![feature(extract_if)]

0 commit comments

Comments
 (0)