Skip to content

Commit 11bb80a

Browse files
committed
Auto merge of #101901 - lcnr:early-binder-type-foldable, r=compiler-errors
`EarlyBinder` prevent misuse folding a type before substituting is pretty much always wrong and could happen by accident, e.g. see #99798 (comment) this PR removes the `TypeFoldable` and `TypeVisitable` impl from `EarlyBinder`. r? types cc `@jackh726`
2 parents 503e19d + 0c3e01d commit 11bb80a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+138
-182
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::mir::{
1616
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
1717
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
1818
};
19-
use rustc_middle::ty::{self, subst::Subst, suggest_constraining_type_params, PredicateKind, Ty};
19+
use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty};
2020
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
2121
use rustc_span::def_id::LocalDefId;
2222
use rustc_span::hygiene::DesugaringKind;

compiler/rustc_borrowck/src/universal_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use rustc_hir::{BodyOwnerKind, HirId};
2222
use rustc_index::vec::{Idx, IndexVec};
2323
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
2424
use rustc_middle::ty::fold::TypeFoldable;
25-
use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef};
2625
use rustc_middle::ty::{self, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt};
26+
use rustc_middle::ty::{InternalSubsts, SubstsRef};
2727
use std::iter;
2828

2929
use crate::nll::ToRegionVid;

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::mir::pretty::display_allocation;
1313
use rustc_middle::traits::Reveal;
1414
use rustc_middle::ty::layout::LayoutOf;
1515
use rustc_middle::ty::print::with_no_trimmed_paths;
16-
use rustc_middle::ty::{self, subst::Subst, TyCtxt};
16+
use rustc_middle::ty::{self, TyCtxt};
1717
use rustc_span::source_map::Span;
1818
use rustc_target::abi::{self, Abi};
1919
use std::borrow::Cow;

compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_ast::Mutability;
44
use rustc_hir::lang_items::LangItem;
55
use rustc_middle::mir::TerminatorKind;
66
use rustc_middle::ty::layout::LayoutOf;
7-
use rustc_middle::ty::subst::Subst;
87
use rustc_span::{Span, Symbol};
98

109
use crate::interpret::{

compiler/rustc_const_eval/src/transform/validate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_middle::mir::{
1313
TerminatorKind, UnOp, START_BLOCK,
1414
};
1515
use rustc_middle::ty::fold::BottomUpFolder;
16-
use rustc_middle::ty::subst::Subst;
1716
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeFoldable, TypeVisitable};
1817
use rustc_mir_dataflow::impls::MaybeStorageLive;
1918
use rustc_mir_dataflow::storage::always_storage_live_locals;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ use rustc_middle::dep_graph::DepContext;
6969
use rustc_middle::ty::print::with_no_trimmed_paths;
7070
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7171
use rustc_middle::ty::{
72-
self, error::TypeError, Binder, List, Region, Subst, Ty, TyCtxt, TypeFoldable,
73-
TypeSuperVisitable, TypeVisitable,
72+
self, error::TypeError, Binder, List, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
73+
TypeVisitable,
7474
};
7575
use rustc_span::{sym, symbol::kw, BytePos, DesugaringKind, Pos, Span};
7676
use rustc_target::spec::abi;

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use rustc_middle::hir::nested_filter;
1515
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
1616
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
1717
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
18-
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};
1918
use rustc_middle::ty::{self, DefIdTree, InferConst};
19+
use rustc_middle::ty::{GenericArg, GenericArgKind, SubstsRef};
2020
use rustc_middle::ty::{IsSuggestable, Ty, TyCtxt, TypeckResults};
2121
use rustc_session::SessionDiagnostic;
2222
use rustc_span::symbol::{kw, Ident};

compiler/rustc_infer/src/infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::vec_map::VecMap;
88
use rustc_hir as hir;
99
use rustc_middle::traits::ObligationCause;
1010
use rustc_middle::ty::fold::BottomUpFolder;
11-
use rustc_middle::ty::subst::{GenericArgKind, Subst};
11+
use rustc_middle::ty::GenericArgKind;
1212
use rustc_middle::ty::{
1313
self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
1414
TypeVisitable, TypeVisitor,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::infer::{GenericKind, VerifyBound};
55
use rustc_data_structures::captures::Captures;
66
use rustc_data_structures::sso::SsoHashSet;
77
use rustc_hir::def_id::DefId;
8-
use rustc_middle::ty::subst::{GenericArg, Subst};
8+
use rustc_middle::ty::GenericArg;
99
use rustc_middle::ty::{self, EarlyBinder, OutlivesPredicate, Ty, TyCtxt};
1010

1111
use smallvec::smallvec;

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use crate::mir::visit::MirVisitable;
99
use crate::ty::codec::{TyDecoder, TyEncoder};
1010
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
1111
use crate::ty::print::{FmtPrinter, Printer};
12-
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
1312
use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
1413
use crate::ty::{self, List, Ty, TyCtxt};
1514
use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex};
15+
use crate::ty::{GenericArg, InternalSubsts, SubstsRef};
1616

1717
use rustc_data_structures::captures::Captures;
1818
use rustc_errors::ErrorGuaranteed;

compiler/rustc_middle/src/mir/tcx.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
use crate::mir::*;
7-
use crate::ty::subst::Subst;
87
use crate::ty::{self, Ty, TyCtxt};
98
use rustc_hir as hir;
109
use rustc_target::abi::VariantIdx;

compiler/rustc_middle/src/ty/abstract_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A subset of a mir body used for const evaluatability checking.
22
use crate::mir;
33
use crate::ty::visit::TypeVisitable;
4-
use crate::ty::{self, subst::Subst, DelaySpanBugEmitted, EarlyBinder, SubstsRef, Ty, TyCtxt};
4+
use crate::ty::{self, DelaySpanBugEmitted, EarlyBinder, SubstsRef, Ty, TyCtxt};
55
use rustc_errors::ErrorGuaranteed;
66
use rustc_hir::def_id::DefId;
77
use std::cmp;

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::mir::{
1515
use crate::thir::Thir;
1616
use crate::traits;
1717
use crate::ty::query::{self, TyCtxtAt};
18-
use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts};
1918
use crate::ty::{
2019
self, AdtDef, AdtDefData, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
2120
ClosureSizeProfileData, Const, ConstS, ConstVid, DefIdTree, ExistentialPredicate, FloatTy,
@@ -24,6 +23,7 @@ use crate::ty::{
2423
RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy,
2524
Visibility,
2625
};
26+
use crate::ty::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef, UserSubsts};
2727
use rustc_ast as ast;
2828
use rustc_data_structures::fingerprint::Fingerprint;
2929
use rustc_data_structures::fx::{FxHashMap, FxHashSet};

compiler/rustc_middle/src/ty/generics.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::ty;
2-
use crate::ty::subst::{Subst, SubstsRef};
3-
use crate::ty::EarlyBinder;
2+
use crate::ty::{EarlyBinder, SubstsRef};
43
use rustc_ast as ast;
54
use rustc_data_structures::fx::FxHashMap;
65
use rustc_hir::def_id::DefId;

compiler/rustc_middle/src/ty/instance.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
22
use crate::ty::print::{FmtPrinter, Printer};
3-
use crate::ty::subst::{InternalSubsts, Subst};
4-
use crate::ty::{
5-
self, EarlyBinder, SubstsRef, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitable,
6-
};
3+
use crate::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitable};
4+
use crate::ty::{EarlyBinder, InternalSubsts, SubstsRef};
75
use rustc_errors::ErrorGuaranteed;
86
use rustc_hir::def::Namespace;
97
use rustc_hir::def_id::{CrateNum, DefId};

compiler/rustc_middle/src/ty/layout.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
22
use crate::mir::{GeneratorLayout, GeneratorSavedLocal};
33
use crate::ty::normalize_erasing_regions::NormalizationError;
4-
use crate::ty::subst::Subst;
54
use crate::ty::{
65
self, layout_sanity_check::sanity_check_layout, subst::SubstsRef, EarlyBinder, ReprOptions, Ty,
76
TyCtxt, TypeVisitable,
@@ -2768,9 +2767,14 @@ impl<'tcx> ty::Instance<'tcx> {
27682767
// (i.e. due to being inside a projection that got normalized, see
27692768
// `src/test/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
27702769
// track of a polymorphization `ParamEnv` to allow normalizing later.
2770+
//
2771+
// We normalize the `fn_sig` again after substituting at a later point.
27712772
let mut sig = match *ty.kind() {
27722773
ty::FnDef(def_id, substs) => tcx
2773-
.normalize_erasing_regions(tcx.param_env(def_id), tcx.bound_fn_sig(def_id))
2774+
.bound_fn_sig(def_id)
2775+
.map_bound(|fn_sig| {
2776+
tcx.normalize_erasing_regions(tcx.param_env(def_id), fn_sig)
2777+
})
27742778
.subst(tcx, substs),
27752779
_ => unreachable!(),
27762780
};

compiler/rustc_middle/src/ty/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ pub use self::sty::BoundRegionKind::*;
8989
pub use self::sty::{
9090
Article, Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar,
9191
BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid,
92-
EarlyBinder, EarlyBoundRegion, ExistentialPredicate, ExistentialProjection,
93-
ExistentialTraitRef, FnSig, FreeRegion, GenSig, GeneratorSubsts, GeneratorSubstsParts,
94-
InlineConstSubsts, InlineConstSubstsParts, ParamConst, ParamTy, PolyExistentialProjection,
92+
EarlyBoundRegion, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FnSig,
93+
FreeRegion, GenSig, GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts,
94+
InlineConstSubstsParts, ParamConst, ParamTy, PolyExistentialProjection,
9595
PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, ProjectionTy, Region, RegionKind,
9696
RegionVid, TraitRef, TyKind, TypeAndMut, UpvarSubsts, VarianceDiagInfo,
9797
};

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
use crate::mir;
1111
use crate::traits::query::NoSolution;
1212
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder};
13-
use crate::ty::subst::{Subst, SubstsRef};
14-
use crate::ty::{self, EarlyBinder, Ty, TyCtxt};
13+
use crate::ty::{self, EarlyBinder, SubstsRef, Ty, TyCtxt};
1514

1615
#[derive(Debug, Copy, Clone, HashStable, TyEncodable, TyDecodable)]
1716
pub enum NormalizationError<'tcx> {

compiler/rustc_middle/src/ty/print/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ty::subst::{GenericArg, Subst};
1+
use crate::ty::GenericArg;
22
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
33

44
use rustc_data_structures::fx::FxHashSet;

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
2-
use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
32
use crate::ty::{
43
self, ConstInt, DefIdTree, ParamConst, ScalarInt, Term, TermKind, Ty, TyCtxt, TypeFoldable,
54
TypeSuperFoldable, TypeSuperVisitable, TypeVisitable,
65
};
6+
use crate::ty::{GenericArg, GenericArgKind};
77
use rustc_apfloat::ieee::{Double, Single};
88
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
99
use rustc_data_structures::sso::SsoHashSet;

compiler/rustc_middle/src/ty/relate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//! subtyping, type equality, etc.
66
77
use crate::ty::error::{ExpectedFound, TypeError};
8-
use crate::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};
98
use crate::ty::{self, ImplSubject, Term, TermKind, Ty, TyCtxt, TypeFoldable};
9+
use crate::ty::{GenericArg, GenericArgKind, SubstsRef};
1010
use rustc_hir as ast;
1111
use rustc_hir::def_id::DefId;
1212
use rustc_span::DUMMY_SP;

compiler/rustc_middle/src/ty/structural_impls.rs

-12
Original file line numberDiff line numberDiff line change
@@ -557,18 +557,6 @@ impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for Box<[T]> {
557557
}
558558
}
559559

560-
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::EarlyBinder<T> {
561-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
562-
self.try_map_bound(|ty| ty.try_fold_with(folder))
563-
}
564-
}
565-
566-
impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for ty::EarlyBinder<T> {
567-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
568-
self.as_ref().0.visit_with(visitor)
569-
}
570-
}
571-
572560
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
573561
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
574562
folder.try_fold_binder(self)

compiler/rustc_middle/src/ty/sty.rs

+2-69
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(rustc::usage_of_ty_tykind)]
44

55
use crate::infer::canonical::Canonical;
6-
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
6+
use crate::ty::subst::{GenericArg, InternalSubsts, SubstsRef};
77
use crate::ty::visit::ValidateBoundVars;
88
use crate::ty::InferTy::*;
99
use crate::ty::{
@@ -551,7 +551,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
551551
layout.variant_fields.iter().map(move |variant| {
552552
variant
553553
.iter()
554-
.map(move |field| EarlyBinder(layout.field_tys[*field]).subst(tcx, self.substs))
554+
.map(move |field| ty::EarlyBinder(layout.field_tys[*field]).subst(tcx, self.substs))
555555
})
556556
}
557557

@@ -915,73 +915,6 @@ impl<'tcx> PolyExistentialTraitRef<'tcx> {
915915
}
916916
}
917917

918-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
919-
#[derive(Encodable, Decodable, HashStable)]
920-
pub struct EarlyBinder<T>(pub T);
921-
922-
impl<T> EarlyBinder<T> {
923-
pub fn as_ref(&self) -> EarlyBinder<&T> {
924-
EarlyBinder(&self.0)
925-
}
926-
927-
pub fn map_bound_ref<F, U>(&self, f: F) -> EarlyBinder<U>
928-
where
929-
F: FnOnce(&T) -> U,
930-
{
931-
self.as_ref().map_bound(f)
932-
}
933-
934-
pub fn map_bound<F, U>(self, f: F) -> EarlyBinder<U>
935-
where
936-
F: FnOnce(T) -> U,
937-
{
938-
let value = f(self.0);
939-
EarlyBinder(value)
940-
}
941-
942-
pub fn try_map_bound<F, U, E>(self, f: F) -> Result<EarlyBinder<U>, E>
943-
where
944-
F: FnOnce(T) -> Result<U, E>,
945-
{
946-
let value = f(self.0)?;
947-
Ok(EarlyBinder(value))
948-
}
949-
950-
pub fn rebind<U>(&self, value: U) -> EarlyBinder<U> {
951-
EarlyBinder(value)
952-
}
953-
}
954-
955-
impl<T> EarlyBinder<Option<T>> {
956-
pub fn transpose(self) -> Option<EarlyBinder<T>> {
957-
self.0.map(|v| EarlyBinder(v))
958-
}
959-
}
960-
961-
impl<T, U> EarlyBinder<(T, U)> {
962-
pub fn transpose_tuple2(self) -> (EarlyBinder<T>, EarlyBinder<U>) {
963-
(EarlyBinder(self.0.0), EarlyBinder(self.0.1))
964-
}
965-
}
966-
967-
pub struct EarlyBinderIter<T> {
968-
t: T,
969-
}
970-
971-
impl<T: IntoIterator> EarlyBinder<T> {
972-
pub fn transpose_iter(self) -> EarlyBinderIter<T::IntoIter> {
973-
EarlyBinderIter { t: self.0.into_iter() }
974-
}
975-
}
976-
977-
impl<T: Iterator> Iterator for EarlyBinderIter<T> {
978-
type Item = EarlyBinder<T::Item>;
979-
980-
fn next(&mut self) -> Option<Self::Item> {
981-
self.t.next().map(|i| EarlyBinder(i))
982-
}
983-
}
984-
985918
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
986919
#[derive(HashStable)]
987920
pub enum BoundVariableKind {

0 commit comments

Comments
 (0)