Skip to content

Commit 56229b3

Browse files
committed
slight fn relate_item_args refactor
1 parent 2570c23 commit 56229b3

File tree

11 files changed

+133
-40
lines changed

11 files changed

+133
-40
lines changed

compiler/rustc_borrowck/src/polonius/liveness_constraints.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::collections::BTreeMap;
22

33
use rustc_index::bit_set::SparseBitMatrix;
44
use rustc_middle::mir::{Body, Location};
5-
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
5+
use rustc_middle::ty::relate::{
6+
self, Relate, RelateResult, TypeRelation, relate_args_with_variances,
7+
};
68
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeVisitable};
79
use rustc_mir_dataflow::points::PointIndex;
810

@@ -256,6 +258,18 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for VarianceExtractor<'_, 'tcx> {
256258
self.tcx
257259
}
258260

261+
fn relate_item_args(
262+
&mut self,
263+
item_def_id: rustc_hir::def_id::DefId,
264+
a_arg: ty::GenericArgsRef<'tcx>,
265+
b_arg: ty::GenericArgsRef<'tcx>,
266+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
267+
) -> RelateResult<'tcx, Ty<'tcx>> {
268+
let variances = self.cx().variances_of(item_def_id);
269+
let args = relate_args_with_variances(self, item_def_id, variances, a_arg, b_arg, false)?;
270+
Ok(f(args))
271+
}
272+
259273
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
260274
&mut self,
261275
variance: ty::Variance,

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_middle::mir::ConstraintCategory;
1010
use rustc_middle::traits::ObligationCause;
1111
use rustc_middle::traits::query::NoSolution;
1212
use rustc_middle::ty::relate::combine::{super_combine_consts, super_combine_tys};
13+
use rustc_middle::ty::relate::relate_args_with_variances;
1314
use rustc_middle::ty::{self, FnMutDelegate, Ty, TyCtxt, TypeVisitableExt};
1415
use rustc_middle::{bug, span_bug};
1516
use rustc_span::{Span, Symbol, sym};
@@ -303,6 +304,20 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
303304
self.type_checker.infcx.tcx
304305
}
305306

307+
fn relate_item_args(
308+
&mut self,
309+
item_def_id: rustc_hir::def_id::DefId,
310+
a_arg: ty::GenericArgsRef<'tcx>,
311+
b_arg: ty::GenericArgsRef<'tcx>,
312+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
313+
) -> RelateResult<'tcx, Ty<'tcx>> {
314+
// FIXME: We could avoid fetching the variances if we're currently in an
315+
// invariant context.
316+
let variances = self.cx().variances_of(item_def_id);
317+
let args = relate_args_with_variances(self, item_def_id, variances, a_arg, b_arg, true)?;
318+
Ok(f(args))
319+
}
320+
306321
#[instrument(skip(self, info), level = "trace", ret)]
307322
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
308323
&mut self,

compiler/rustc_infer/src/infer/outlives/test_type_match.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::hash_map::Entry;
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_middle::ty::error::TypeError;
55
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
6+
use rustc_type_ir::relate::relate_args_with_variances;
67
use tracing::instrument;
78

89
use crate::infer::region_constraints::VerifyIfEq;
@@ -137,6 +138,22 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstHigherRankedOutlives<'tcx>
137138
self.tcx
138139
}
139140

141+
fn relate_item_args(
142+
&mut self,
143+
item_def_id: rustc_hir::def_id::DefId,
144+
a_arg: ty::GenericArgsRef<'tcx>,
145+
b_arg: ty::GenericArgsRef<'tcx>,
146+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
147+
) -> RelateResult<'tcx, Ty<'tcx>> {
148+
// FIXME(@lcnr): This is weird. We are ignoring variance
149+
// here, effectively treating everything as either covariant
150+
// or contravariant, as we only ever check whether the context is
151+
// bivariant.
152+
let variances = self.cx().variances_of(item_def_id);
153+
let args = relate_args_with_variances(self, item_def_id, variances, a_arg, b_arg, false)?;
154+
Ok(f(args))
155+
}
156+
140157
#[instrument(level = "trace", skip(self))]
141158
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
142159
&mut self,

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,24 +466,19 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
466466
item_def_id: DefId,
467467
a_arg: ty::GenericArgsRef<'tcx>,
468468
b_arg: ty::GenericArgsRef<'tcx>,
469-
) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
470-
if self.ambient_variance == ty::Invariant {
469+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
470+
) -> RelateResult<'tcx, Ty<'tcx>> {
471+
let args = if self.ambient_variance == ty::Invariant {
471472
// Avoid fetching the variance if we are in an invariant
472473
// context; no need, and it can induce dependency cycles
473474
// (e.g., #41849).
474475
relate::relate_args_invariantly(self, a_arg, b_arg)
475476
} else {
476477
let tcx = self.cx();
477-
let opt_variances = tcx.variances_of(item_def_id);
478-
relate::relate_args_with_variances(
479-
self,
480-
item_def_id,
481-
opt_variances,
482-
a_arg,
483-
b_arg,
484-
false,
485-
)
486-
}
478+
let variances = tcx.variances_of(item_def_id);
479+
relate::relate_args_with_variances(self, item_def_id, variances, a_arg, b_arg, false)
480+
}?;
481+
Ok(f(args))
487482
}
488483

489484
#[instrument(level = "debug", skip(self, variance, b), ret)]

compiler/rustc_infer/src/infer/relate/lattice.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_middle::ty::relate::combine::{super_combine_consts, super_combine_tys}
2222
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
2323
use rustc_middle::ty::{self, Ty, TyCtxt, TyVar, TypeVisitableExt};
2424
use rustc_span::Span;
25+
use rustc_type_ir::relate::relate_args_with_variances;
2526
use tracing::{debug, instrument};
2627

2728
use super::StructurallyRelateAliases;
@@ -75,6 +76,18 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for LatticeOp<'_, 'tcx> {
7576
self.infcx.tcx
7677
}
7778

79+
fn relate_item_args(
80+
&mut self,
81+
item_def_id: rustc_hir::def_id::DefId,
82+
a_arg: ty::GenericArgsRef<'tcx>,
83+
b_arg: ty::GenericArgsRef<'tcx>,
84+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
85+
) -> RelateResult<'tcx, Ty<'tcx>> {
86+
let variances = self.cx().variances_of(item_def_id);
87+
let args = relate_args_with_variances(self, item_def_id, variances, a_arg, b_arg, false)?;
88+
Ok(f(args))
89+
}
90+
7891
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
7992
&mut self,
8093
variance: ty::Variance,

compiler/rustc_infer/src/infer/relate/type_relating.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, 'tcx> {
8484
item_def_id: rustc_hir::def_id::DefId,
8585
a_arg: ty::GenericArgsRef<'tcx>,
8686
b_arg: ty::GenericArgsRef<'tcx>,
87-
) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
88-
if self.ambient_variance == ty::Invariant {
87+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
88+
) -> RelateResult<'tcx, Ty<'tcx>> {
89+
let args = if self.ambient_variance == ty::Invariant {
8990
// Avoid fetching the variance if we are in an invariant
9091
// context; no need, and it can induce dependency cycles
9192
// (e.g., #41849).
9293
relate_args_invariantly(self, a_arg, b_arg)
9394
} else {
9495
let tcx = self.cx();
95-
let opt_variances = tcx.variances_of(item_def_id);
96-
relate_args_with_variances(self, item_def_id, opt_variances, a_arg, b_arg, false)
97-
}
96+
let variances = tcx.variances_of(item_def_id);
97+
relate_args_with_variances(self, item_def_id, variances, a_arg, b_arg, false)
98+
}?;
99+
100+
Ok(f(args))
98101
}
99102

100103
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(

compiler/rustc_lint/src/impl_trait_overcaptures.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1212
use rustc_macros::LintDiagnostic;
1313
use rustc_middle::middle::resolve_bound_vars::ResolvedArg;
1414
use rustc_middle::ty::relate::{
15-
Relate, RelateResult, TypeRelation, structurally_relate_consts, structurally_relate_tys,
15+
Relate, RelateResult, TypeRelation, relate_args_with_variances, structurally_relate_consts,
16+
structurally_relate_tys,
1617
};
1718
use rustc_middle::ty::{
1819
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
@@ -502,6 +503,18 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
502503
self.tcx
503504
}
504505

506+
fn relate_item_args(
507+
&mut self,
508+
item_def_id: rustc_hir::def_id::DefId,
509+
a_arg: ty::GenericArgsRef<'tcx>,
510+
b_arg: ty::GenericArgsRef<'tcx>,
511+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
512+
) -> RelateResult<'tcx, Ty<'tcx>> {
513+
let variances = self.cx().variances_of(item_def_id);
514+
let args = relate_args_with_variances(self, item_def_id, variances, a_arg, b_arg, false)?;
515+
Ok(f(args))
516+
}
517+
505518
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
506519
&mut self,
507520
variance: ty::Variance,

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,17 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
22582258
self.0.tcx
22592259
}
22602260

2261+
fn relate_item_args(
2262+
&mut self,
2263+
_: rustc_hir::def_id::DefId,
2264+
a_arg: ty::GenericArgsRef<'tcx>,
2265+
b_arg: ty::GenericArgsRef<'tcx>,
2266+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
2267+
) -> RelateResult<'tcx, Ty<'tcx>> {
2268+
let args = relate::relate_args_invariantly(self, a_arg, b_arg)?;
2269+
Ok(f(args))
2270+
}
2271+
22612272
fn relate_with_variance<T: relate::Relate<TyCtxt<'tcx>>>(
22622273
&mut self,
22632274
_variance: ty::Variance,

compiler/rustc_trait_selection/src/traits/select/_match.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstFreshVars<'tcx> {
3636
self.tcx
3737
}
3838

39+
fn relate_item_args(
40+
&mut self,
41+
_: rustc_hir::def_id::DefId,
42+
a_arg: ty::GenericArgsRef<'tcx>,
43+
b_arg: ty::GenericArgsRef<'tcx>,
44+
f: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
45+
) -> RelateResult<'tcx, Ty<'tcx>> {
46+
let args = relate::relate_args_invariantly(self, a_arg, b_arg)?;
47+
Ok(f(args))
48+
}
49+
3950
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
4051
&mut self,
4152
_: ty::Variance,

compiler/rustc_type_ir/src/relate.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,13 @@ pub trait TypeRelation<I: Interner>: Sized {
7676
/// Relate the two args for the given item. The default
7777
/// is to look up the variance for the item and proceed
7878
/// accordingly.
79-
#[instrument(skip(self), level = "trace")]
8079
fn relate_item_args(
8180
&mut self,
8281
item_def_id: I::DefId,
8382
a_arg: I::GenericArgs,
8483
b_arg: I::GenericArgs,
85-
) -> RelateResult<I, I::GenericArgs> {
86-
let cx = self.cx();
87-
let opt_variances = cx.variances_of(item_def_id);
88-
relate_args_with_variances(self, item_def_id, opt_variances, a_arg, b_arg, true)
89-
}
84+
f: impl FnOnce(I::GenericArgs) -> I::Ty,
85+
) -> RelateResult<I, I::Ty>;
9086

9187
/// Switch variance for the purpose of relating `a` and `b`.
9288
fn relate_with_variance<T: Relate<I>>(
@@ -402,12 +398,13 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
402398
(ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => Ok(a),
403399

404400
(ty::Adt(a_def, a_args), ty::Adt(b_def, b_args)) if a_def == b_def => {
405-
Ok(if a_args.is_empty() {
406-
a
401+
if a_args.is_empty() {
402+
Ok(a)
407403
} else {
408-
let args = relation.relate_item_args(a_def.def_id().into(), a_args, b_args)?;
409-
if args == a_args { a } else { Ty::new_adt(cx, a_def, args) }
410-
})
404+
relation.relate_item_args(a_def.def_id().into(), a_args, b_args, |args| {
405+
if args == a_args { a } else { Ty::new_adt(cx, a_def, args) }
406+
})
407+
}
411408
}
412409

413410
(ty::Foreign(a_id), ty::Foreign(b_id)) if a_id == b_id => Ok(Ty::new_foreign(cx, a_id)),
@@ -516,12 +513,13 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
516513
}
517514

518515
(ty::FnDef(a_def_id, a_args), ty::FnDef(b_def_id, b_args)) if a_def_id == b_def_id => {
519-
Ok(if a_args.is_empty() {
520-
a
516+
if a_args.is_empty() {
517+
Ok(a)
521518
} else {
522-
let args = relation.relate_item_args(a_def_id.into(), a_args, b_args)?;
523-
if args == a_args { a } else { Ty::new_fn_def(cx, a_def_id, args) }
524-
})
519+
relation.relate_item_args(a_def_id.into(), a_args, b_args, |args| {
520+
if args == a_args { a } else { Ty::new_fn_def(cx, a_def_id, args) }
521+
})
522+
}
525523
}
526524

527525
(ty::FnPtr(a_sig_tys, a_hdr), ty::FnPtr(b_sig_tys, b_hdr)) => {

0 commit comments

Comments
 (0)