Skip to content

Commit 2f225f5

Browse files
authored
Rollup merge of rust-lang#98311 - eggyal:reverse-folder-hierarchy, r=jackh726
Reverse folder hierarchy rust-lang#91318 introduced a trait for infallible folders distinct from the fallible version. For some reason (completely unfathomable to me now that I look at it with fresh eyes), the infallible trait was a supertrait of the fallible one: that is, all fallible folders were required to also be infallible. Moreover the `Error` associated type was defined on the infallible trait! It's so absurd that it has me questioning whether I was entirely sane. This trait reverses the hierarchy, so that the fallible trait is a supertrait of the infallible one: all infallible folders are required to also be fallible (which is a trivial blanket implementation). This of course makes much more sense! It also enables the `Error` associated type to sit on the fallible trait, where it sensibly belongs. There is one downside however: folders expose a `tcx` accessor method. Since the blanket fallible implementation for infallible folders only has access to a generic `F: TypeFolder`, we need that trait to expose such an accessor to which we can delegate. Alternatively it's possible to extract that accessor into a separate `HasTcx` trait (or similar) that would then be a supertrait of both the fallible and infallible folder traits: this would ensure that there's only one unambiguous `tcx` method, at the cost of a little additional boilerplate. If desired, I can submit that as a separate PR. r? ``@jackh726``
2 parents 7354c04 + 2526b08 commit 2f225f5

File tree

5 files changed

+25
-42
lines changed

5 files changed

+25
-42
lines changed

compiler/rustc_infer/src/infer/resolve.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
9292
.borrow_mut()
9393
.unwrap_region_constraints()
9494
.opportunistic_resolve_var(rid);
95-
self.tcx().reuse_or_mk_region(r, ty::ReVar(resolved))
95+
TypeFolder::tcx(self).reuse_or_mk_region(r, ty::ReVar(resolved))
9696
}
9797
_ => r,
9898
}
@@ -179,15 +179,13 @@ struct FullTypeResolver<'a, 'tcx> {
179179
infcx: &'a InferCtxt<'a, 'tcx>,
180180
}
181181

182-
impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
182+
impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
183183
type Error = FixupError<'tcx>;
184184

185185
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
186186
self.infcx.tcx
187187
}
188-
}
189188

190-
impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
191189
fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
192190
if !t.needs_infer() {
193191
Ok(t) // micro-optimize -- if there is nothing in this type that this fold affects...

compiler/rustc_middle/src/ty/fold.rs

+19-30
Original file line numberDiff line numberDiff line change
@@ -241,58 +241,37 @@ pub trait TypeSuperFoldable<'tcx>: TypeFoldable<'tcx> {
241241
/// a blanket implementation of [`FallibleTypeFolder`] will defer to
242242
/// the infallible methods of this trait to ensure that the two APIs
243243
/// are coherent.
244-
pub trait TypeFolder<'tcx>: Sized {
245-
type Error = !;
246-
244+
pub trait TypeFolder<'tcx>: FallibleTypeFolder<'tcx, Error = !> {
247245
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
248246

249247
fn fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T>
250248
where
251249
T: TypeFoldable<'tcx>,
252-
Self: TypeFolder<'tcx, Error = !>,
253250
{
254251
t.super_fold_with(self)
255252
}
256253

257-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx>
258-
where
259-
Self: TypeFolder<'tcx, Error = !>,
260-
{
254+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
261255
t.super_fold_with(self)
262256
}
263257

264-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx>
265-
where
266-
Self: TypeFolder<'tcx, Error = !>,
267-
{
258+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
268259
r.super_fold_with(self)
269260
}
270261

271-
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx>
272-
where
273-
Self: TypeFolder<'tcx, Error = !>,
274-
{
262+
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
275263
c.super_fold_with(self)
276264
}
277265

278-
fn fold_unevaluated(&mut self, uv: ty::Unevaluated<'tcx>) -> ty::Unevaluated<'tcx>
279-
where
280-
Self: TypeFolder<'tcx, Error = !>,
281-
{
266+
fn fold_unevaluated(&mut self, uv: ty::Unevaluated<'tcx>) -> ty::Unevaluated<'tcx> {
282267
uv.super_fold_with(self)
283268
}
284269

285-
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx>
286-
where
287-
Self: TypeFolder<'tcx, Error = !>,
288-
{
270+
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
289271
p.super_fold_with(self)
290272
}
291273

292-
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx>
293-
where
294-
Self: TypeFolder<'tcx, Error = !>,
295-
{
274+
fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
296275
bug!("most type folders should not be folding MIR datastructures: {:?}", c)
297276
}
298277
}
@@ -304,7 +283,11 @@ pub trait TypeFolder<'tcx>: Sized {
304283
/// A blanket implementation of this trait (that defers to the relevant
305284
/// method of [`TypeFolder`]) is provided for all infallible folders in
306285
/// order to ensure the two APIs are coherent.
307-
pub trait FallibleTypeFolder<'tcx>: TypeFolder<'tcx> {
286+
pub trait FallibleTypeFolder<'tcx>: Sized {
287+
type Error;
288+
289+
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;
290+
308291
fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, Self::Error>
309292
where
310293
T: TypeFoldable<'tcx>,
@@ -350,8 +333,14 @@ pub trait FallibleTypeFolder<'tcx>: TypeFolder<'tcx> {
350333
// delegates to infallible methods to ensure coherence.
351334
impl<'tcx, F> FallibleTypeFolder<'tcx> for F
352335
where
353-
F: TypeFolder<'tcx, Error = !>,
336+
F: TypeFolder<'tcx>,
354337
{
338+
type Error = !;
339+
340+
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
341+
TypeFolder::tcx(self)
342+
}
343+
355344
fn try_fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, Self::Error>
356345
where
357346
T: TypeFoldable<'tcx>,

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,13 @@ impl<'tcx> TryNormalizeAfterErasingRegionsFolder<'tcx> {
228228
}
229229
}
230230

231-
impl<'tcx> TypeFolder<'tcx> for TryNormalizeAfterErasingRegionsFolder<'tcx> {
231+
impl<'tcx> FallibleTypeFolder<'tcx> for TryNormalizeAfterErasingRegionsFolder<'tcx> {
232232
type Error = NormalizationError<'tcx>;
233233

234234
fn tcx(&self) -> TyCtxt<'tcx> {
235235
self.tcx
236236
}
237-
}
238237

239-
impl<'tcx> FallibleTypeFolder<'tcx> for TryNormalizeAfterErasingRegionsFolder<'tcx> {
240238
fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
241239
match self.try_normalize_generic_arg_after_erasing_regions(ty.into()) {
242240
Ok(t) => Ok(t.expect_ty()),

compiler/rustc_middle/src/ty/subst.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
705705
return val;
706706
}
707707

708-
let result = ty::fold::shift_vars(self.tcx(), val, self.binders_passed);
708+
let result = ty::fold::shift_vars(TypeFolder::tcx(self), val, self.binders_passed);
709709
debug!("shift_vars: shifted result = {:?}", result);
710710

711711
result

compiler/rustc_trait_selection/src/traits/query/normalize.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::sso::SsoHashMap;
1212
use rustc_data_structures::stack::ensure_sufficient_stack;
1313
use rustc_infer::traits::Normalized;
1414
use rustc_middle::mir;
15-
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
15+
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
1616
use rustc_middle::ty::subst::Subst;
1717
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor};
1818

@@ -162,15 +162,13 @@ struct QueryNormalizer<'cx, 'tcx> {
162162
universes: Vec<Option<ty::UniverseIndex>>,
163163
}
164164

165-
impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
165+
impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
166166
type Error = NoSolution;
167167

168168
fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
169169
self.infcx.tcx
170170
}
171-
}
172171

173-
impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
174172
fn try_fold_binder<T: TypeFoldable<'tcx>>(
175173
&mut self,
176174
t: ty::Binder<'tcx, T>,

0 commit comments

Comments
 (0)