Skip to content

Commit a9d1caf

Browse files
committed
Auto merge of #102355 - lcnr:bye-bye-type-traversal, r=oli-obk
remove type traversal for mir constants r? `@oli-obk` cc `@b-naber`
2 parents abd7744 + d04bff6 commit a9d1caf

File tree

15 files changed

+37
-251
lines changed

15 files changed

+37
-251
lines changed

compiler/rustc_borrowck/src/renumber.rs

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_index::vec::IndexVec;
22
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
33
use rustc_middle::mir::visit::{MutVisitor, TyContext};
4+
use rustc_middle::mir::Constant;
45
use rustc_middle::mir::{Body, Location, Promoted};
5-
use rustc_middle::mir::{Constant, ConstantKind};
66
use rustc_middle::ty::subst::SubstsRef;
77
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
88

@@ -38,21 +38,6 @@ where
3838
})
3939
}
4040

41-
// FIXME(valtrees): This function is necessary because `fold_regions`
42-
// panics for mir constants in the visitor.
43-
//
44-
// Once `visit_mir_constant` is removed we can also remove this function
45-
// and just use `renumber_regions`.
46-
fn renumber_regions_in_mir_constant<'tcx>(
47-
infcx: &InferCtxt<'tcx>,
48-
value: ConstantKind<'tcx>,
49-
) -> ConstantKind<'tcx> {
50-
infcx.tcx.super_fold_regions(value, |_region, _depth| {
51-
let origin = NllRegionVariableOrigin::Existential { from_forall: false };
52-
infcx.next_nll_region_var(origin)
53-
})
54-
}
55-
5641
struct NllVisitor<'a, 'tcx> {
5742
infcx: &'a InferCtxt<'tcx>,
5843
}
@@ -64,13 +49,6 @@ impl<'a, 'tcx> NllVisitor<'a, 'tcx> {
6449
{
6550
renumber_regions(self.infcx, value)
6651
}
67-
68-
fn renumber_regions_in_mir_constant(
69-
&mut self,
70-
value: ConstantKind<'tcx>,
71-
) -> ConstantKind<'tcx> {
72-
renumber_regions_in_mir_constant(self.infcx, value)
73-
}
7452
}
7553

7654
impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
@@ -103,7 +81,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
10381
#[instrument(skip(self), level = "debug")]
10482
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) {
10583
let literal = constant.literal;
106-
constant.literal = self.renumber_regions_in_mir_constant(literal);
84+
constant.literal = self.renumber_regions(literal);
10785
debug!("constant: {:#?}", constant);
10886
}
10987
}

compiler/rustc_infer/src/infer/resolve.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
22
use super::{FixupError, FixupResult, InferCtxt, Span};
3-
use rustc_middle::mir;
43
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
54
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor};
65
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable, TypeVisitable};
@@ -48,10 +47,6 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
4847
ct.super_fold_with(self)
4948
}
5049
}
51-
52-
fn fold_mir_const(&mut self, constant: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
53-
constant.super_fold_with(self)
54-
}
5550
}
5651

5752
/// The opportunistic region resolver opportunistically resolves regions

compiler/rustc_middle/src/macros.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,22 @@ macro_rules! TrivialTypeTraversalImpls {
5454
impl<$tcx> $crate::ty::fold::TypeFoldable<$tcx> for $ty {
5555
fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$tcx>>(
5656
self,
57-
_: &mut F
58-
) -> ::std::result::Result<$ty, F::Error> {
57+
_: &mut F,
58+
) -> ::std::result::Result<Self, F::Error> {
5959
Ok(self)
6060
}
61+
62+
#[inline]
63+
fn fold_with<F: $crate::ty::fold::TypeFolder<$tcx>>(
64+
self,
65+
_: &mut F,
66+
) -> Self {
67+
self
68+
}
6169
}
6270

6371
impl<$tcx> $crate::ty::visit::TypeVisitable<$tcx> for $ty {
72+
#[inline]
6473
fn visit_with<F: $crate::ty::visit::TypeVisitor<$tcx>>(
6574
&self,
6675
_: &mut F)

compiler/rustc_middle/src/mir/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use crate::mir::interpret::{
77
};
88
use crate::mir::visit::MirVisitable;
99
use crate::ty::codec::{TyDecoder, TyEncoder};
10-
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
10+
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable};
1111
use crate::ty::print::{FmtPrinter, Printer};
12-
use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
12+
use crate::ty::visit::{TypeVisitable, TypeVisitor};
1313
use crate::ty::{self, List, Ty, TyCtxt};
1414
use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex};
1515
use crate::ty::{GenericArg, InternalSubsts, SubstsRef};
@@ -2056,7 +2056,7 @@ pub struct Constant<'tcx> {
20562056
}
20572057

20582058
#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
2059-
#[derive(Lift)]
2059+
#[derive(Lift, TypeFoldable, TypeVisitable)]
20602060
pub enum ConstantKind<'tcx> {
20612061
/// This constant came from the type system
20622062
Ty(ty::Const<'tcx>),
@@ -2448,7 +2448,7 @@ impl<'tcx> ConstantKind<'tcx> {
24482448

24492449
/// An unevaluated (potentially generic) constant used in MIR.
24502450
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)]
2451-
#[derive(Hash, HashStable)]
2451+
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
24522452
pub struct UnevaluatedConst<'tcx> {
24532453
pub def: ty::WithOptConstParam<DefId>,
24542454
pub substs: SubstsRef<'tcx>,

compiler/rustc_middle/src/mir/type_foldable.rs

+6-42
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use rustc_ast::InlineAsmTemplatePiece;
44

55
use super::*;
6-
use crate::mir;
76
use crate::ty;
87

98
TrivialTypeTraversalAndLiftImpls! {
@@ -27,6 +26,12 @@ TrivialTypeTraversalAndLiftImpls! {
2726
GeneratorSavedLocal,
2827
}
2928

29+
TrivialTypeTraversalImpls! {
30+
for <'tcx> {
31+
ConstValue<'tcx>,
32+
}
33+
}
34+
3035
impl<'tcx> TypeFoldable<'tcx> for &'tcx [InlineAsmTemplatePiece] {
3136
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _folder: &mut F) -> Result<Self, F::Error> {
3237
Ok(self)
@@ -50,44 +55,3 @@ impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
5055
Ok(self)
5156
}
5257
}
53-
54-
impl<'tcx> TypeFoldable<'tcx> for mir::UnevaluatedConst<'tcx> {
55-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
56-
folder.try_fold_mir_unevaluated(self)
57-
}
58-
}
59-
60-
impl<'tcx> TypeSuperFoldable<'tcx> for mir::UnevaluatedConst<'tcx> {
61-
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
62-
self,
63-
folder: &mut F,
64-
) -> Result<Self, F::Error> {
65-
Ok(mir::UnevaluatedConst {
66-
def: self.def,
67-
substs: self.substs.try_fold_with(folder)?,
68-
promoted: self.promoted,
69-
})
70-
}
71-
}
72-
73-
impl<'tcx> TypeFoldable<'tcx> for ConstantKind<'tcx> {
74-
#[inline(always)]
75-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
76-
folder.try_fold_mir_const(self)
77-
}
78-
}
79-
80-
impl<'tcx> TypeSuperFoldable<'tcx> for ConstantKind<'tcx> {
81-
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
82-
self,
83-
folder: &mut F,
84-
) -> Result<Self, F::Error> {
85-
match self {
86-
ConstantKind::Ty(c) => Ok(ConstantKind::Ty(c.try_fold_with(folder)?)),
87-
ConstantKind::Val(v, t) => Ok(ConstantKind::Val(v, t.try_fold_with(folder)?)),
88-
ConstantKind::Unevaluated(uv, t) => {
89-
Ok(ConstantKind::Unevaluated(uv.try_fold_with(folder)?, t.try_fold_with(folder)?))
90-
}
91-
}
92-
}
93-
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,9 @@
11
//! `TypeVisitable` implementations for MIR types
22
33
use super::*;
4-
use crate::mir;
54

65
impl<'tcx, R: Idx, C: Idx> TypeVisitable<'tcx> for BitMatrix<R, C> {
76
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
87
ControlFlow::CONTINUE
98
}
109
}
11-
12-
impl<'tcx> TypeVisitable<'tcx> for mir::UnevaluatedConst<'tcx> {
13-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
14-
visitor.visit_mir_unevaluated(*self)
15-
}
16-
}
17-
18-
impl<'tcx> TypeSuperVisitable<'tcx> for mir::UnevaluatedConst<'tcx> {
19-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
20-
self.substs.visit_with(visitor)
21-
}
22-
}
23-
24-
impl<'tcx> TypeVisitable<'tcx> for ConstantKind<'tcx> {
25-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
26-
visitor.visit_mir_const(*self)
27-
}
28-
}
29-
30-
impl<'tcx> TypeSuperVisitable<'tcx> for ConstantKind<'tcx> {
31-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
32-
match *self {
33-
ConstantKind::Ty(c) => c.visit_with(visitor),
34-
ConstantKind::Val(_, t) => t.visit_with(visitor),
35-
ConstantKind::Unevaluated(uv, t) => {
36-
uv.visit_with(visitor)?;
37-
t.visit_with(visitor)
38-
}
39-
}
40-
}
41-
}

compiler/rustc_middle/src/query/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1871,14 +1871,6 @@ rustc_queries! {
18711871
remap_env_constness
18721872
}
18731873

1874-
/// Do not call this query directly: invoke `try_normalize_erasing_regions` instead.
1875-
query try_normalize_mir_const_after_erasing_regions(
1876-
goal: ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>
1877-
) -> Result<mir::ConstantKind<'tcx>, NoSolution> {
1878-
desc { "normalizing `{}`", goal.value }
1879-
remap_env_constness
1880-
}
1881-
18821874
query implied_outlives_bounds(
18831875
goal: CanonicalTyGoal<'tcx>
18841876
) -> Result<

compiler/rustc_middle/src/ty/erase_regions.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::mir;
21
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
32
use crate::ty::visit::TypeVisitable;
43
use crate::ty::{self, Ty, TyCtxt, TypeFlags};
@@ -67,8 +66,4 @@ impl<'tcx> TypeFolder<'tcx> for RegionEraserVisitor<'tcx> {
6766
_ => self.tcx.lifetimes.re_erased,
6867
}
6968
}
70-
71-
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
72-
c.super_fold_with(self)
73-
}
7469
}

compiler/rustc_middle/src/ty/fold.rs

-40
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
//! - ty.super_fold_with(folder)
4343
//! - u.fold_with(folder)
4444
//! ```
45-
use crate::mir;
4645
use crate::ty::{self, Binder, BoundTy, Ty, TyCtxt, TypeVisitable};
4746
use rustc_data_structures::fx::FxIndexMap;
4847
use rustc_hir::def_id::DefId;
@@ -134,20 +133,9 @@ pub trait TypeFolder<'tcx>: FallibleTypeFolder<'tcx, Error = !> {
134133
uv.super_fold_with(self)
135134
}
136135

137-
fn fold_mir_unevaluated(
138-
&mut self,
139-
uv: mir::UnevaluatedConst<'tcx>,
140-
) -> mir::UnevaluatedConst<'tcx> {
141-
uv.super_fold_with(self)
142-
}
143-
144136
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
145137
p.super_fold_with(self)
146138
}
147-
148-
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
149-
bug!("most type folders should not be folding MIR datastructures: {:?}", c)
150-
}
151139
}
152140

153141
/// This trait is implemented for every folding traversal. There is a fold
@@ -188,26 +176,12 @@ pub trait FallibleTypeFolder<'tcx>: Sized {
188176
c.try_super_fold_with(self)
189177
}
190178

191-
fn try_fold_mir_unevaluated(
192-
&mut self,
193-
c: mir::UnevaluatedConst<'tcx>,
194-
) -> Result<mir::UnevaluatedConst<'tcx>, Self::Error> {
195-
c.try_super_fold_with(self)
196-
}
197-
198179
fn try_fold_predicate(
199180
&mut self,
200181
p: ty::Predicate<'tcx>,
201182
) -> Result<ty::Predicate<'tcx>, Self::Error> {
202183
p.try_super_fold_with(self)
203184
}
204-
205-
fn try_fold_mir_const(
206-
&mut self,
207-
c: mir::ConstantKind<'tcx>,
208-
) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
209-
bug!("most type folders should not be folding MIR datastructures: {:?}", c)
210-
}
211185
}
212186

213187
// This blanket implementation of the fallible trait for infallible folders
@@ -248,23 +222,9 @@ where
248222
Ok(self.fold_ty_unevaluated(c))
249223
}
250224

251-
fn try_fold_mir_unevaluated(
252-
&mut self,
253-
c: mir::UnevaluatedConst<'tcx>,
254-
) -> Result<mir::UnevaluatedConst<'tcx>, !> {
255-
Ok(self.fold_mir_unevaluated(c))
256-
}
257-
258225
fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result<ty::Predicate<'tcx>, !> {
259226
Ok(self.fold_predicate(p))
260227
}
261-
262-
fn try_fold_mir_const(
263-
&mut self,
264-
c: mir::ConstantKind<'tcx>,
265-
) -> Result<mir::ConstantKind<'tcx>, !> {
266-
Ok(self.fold_mir_const(c))
267-
}
268228
}
269229

270230
///////////////////////////////////////////////////////////////////////////

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

-21
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,6 @@ impl<'tcx> TypeFolder<'tcx> for NormalizeAfterErasingRegionsFolder<'tcx> {
214214
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
215215
self.normalize_generic_arg_after_erasing_regions(c.into()).expect_const()
216216
}
217-
218-
#[inline]
219-
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
220-
// FIXME: This *probably* needs canonicalization too!
221-
let arg = self.param_env.and(c);
222-
self.tcx
223-
.try_normalize_mir_const_after_erasing_regions(arg)
224-
.unwrap_or_else(|_| bug!("failed to normalize {:?}", c))
225-
}
226217
}
227218

228219
struct TryNormalizeAfterErasingRegionsFolder<'tcx> {
@@ -267,16 +258,4 @@ impl<'tcx> FallibleTypeFolder<'tcx> for TryNormalizeAfterErasingRegionsFolder<'t
267258
Err(_) => Err(NormalizationError::Const(c)),
268259
}
269260
}
270-
271-
fn try_fold_mir_const(
272-
&mut self,
273-
c: mir::ConstantKind<'tcx>,
274-
) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
275-
// FIXME: This *probably* needs canonicalization too!
276-
let arg = self.param_env.and(c);
277-
match self.tcx.try_normalize_mir_const_after_erasing_regions(arg) {
278-
Ok(c) => Ok(c),
279-
Err(_) => Err(NormalizationError::ConstantKind(c)),
280-
}
281-
}
282261
}

compiler/rustc_middle/src/ty/subst.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Type substitutions.
22

3-
use crate::mir;
43
use crate::ty::codec::{TyDecoder, TyEncoder};
54
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
65
use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts};
@@ -662,11 +661,6 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
662661
c.super_fold_with(self)
663662
}
664663
}
665-
666-
#[inline]
667-
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
668-
c.super_fold_with(self)
669-
}
670664
}
671665

672666
impl<'a, 'tcx> SubstFolder<'a, 'tcx> {

0 commit comments

Comments
 (0)