Skip to content

Commit df426cf

Browse files
Uplift find_const_ty_from_env
1 parent 00f369a commit df426cf

File tree

5 files changed

+42
-52
lines changed

5 files changed

+42
-52
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -691,14 +691,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
691691
self.unsizing_params_for_adt(adt_def_id)
692692
}
693693

694-
fn find_const_ty_from_env(
695-
self,
696-
param_env: ty::ParamEnv<'tcx>,
697-
placeholder: Self::PlaceholderConst,
698-
) -> Ty<'tcx> {
699-
placeholder.find_const_ty_from_env(param_env)
700-
}
701-
702694
fn anonymize_bound_vars<T: TypeFoldable<TyCtxt<'tcx>>>(
703695
self,
704696
binder: ty::Binder<'tcx, T>,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -907,41 +907,6 @@ pub struct Placeholder<T> {
907907
pub universe: UniverseIndex,
908908
pub bound: T,
909909
}
910-
impl Placeholder<BoundVar> {
911-
pub fn find_const_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
912-
let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
913-
// `ConstArgHasType` are never desugared to be higher ranked.
914-
match clause.kind().skip_binder() {
915-
ty::ClauseKind::ConstArgHasType(placeholder_ct, ty) => {
916-
assert!(!(placeholder_ct, ty).has_escaping_bound_vars());
917-
918-
match placeholder_ct.kind() {
919-
ty::ConstKind::Placeholder(placeholder_ct) if placeholder_ct == self => {
920-
Some(ty)
921-
}
922-
_ => None,
923-
}
924-
}
925-
_ => None,
926-
}
927-
});
928-
929-
// N.B. it may be tempting to fix ICEs by making this function return
930-
// `Option<Ty<'tcx>>` instead of `Ty<'tcx>`; however, this is generally
931-
// considered to be a bandaid solution, since it hides more important
932-
// underlying issues with how we construct generics and predicates of
933-
// items. It's advised to fix the underlying issue rather than trying
934-
// to modify this function.
935-
let ty = candidates.next().unwrap_or_else(|| {
936-
bug!("cannot find `{self:?}` in param-env: {env:#?}");
937-
});
938-
assert!(
939-
candidates.next().is_none(),
940-
"did not expect duplicate `ConstParamHasTy` for `{self:?}` in param-env: {env:#?}"
941-
);assert!(candidates.next().is_none());
942-
ty
943-
}
944-
}
945910

946911
pub type PlaceholderRegion = Placeholder<BoundRegion>;
947912

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ where
211211
ty::ConstKind::Bound(_, _) => panic!("escaping bound vars in {:?}", ct),
212212
ty::ConstKind::Value(cv) => cv.ty(),
213213
ty::ConstKind::Placeholder(placeholder) => {
214-
self.cx().find_const_ty_from_env(goal.param_env, placeholder)
214+
placeholder.find_const_ty_from_env(goal.param_env)
215215
}
216216
};
217217

compiler/rustc_type_ir/src/inherent.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::elaborate::Elaboratable;
1212
use crate::fold::{TypeFoldable, TypeSuperFoldable};
1313
use crate::relate::Relate;
1414
use crate::solve::{AdtDestructorKind, SizedTraitKind};
15-
use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
15+
use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable, TypeVisitableExt};
1616
use crate::{self as ty, CollectAndApply, Interner, UpcastFrom};
1717

1818
pub trait Ty<I: Interner<Ty = Self>>:
@@ -538,6 +538,45 @@ pub trait PlaceholderLike<I: Interner>: Copy + Debug + Hash + Eq {
538538
fn with_updated_universe(self, ui: ty::UniverseIndex) -> Self;
539539
}
540540

541+
pub trait PlaceholderConst<I: Interner>: PlaceholderLike<I, Bound = I::BoundConst> {
542+
fn find_const_ty_from_env(self, env: I::ParamEnv) -> I::Ty;
543+
}
544+
impl<I: Interner> PlaceholderConst<I> for I::PlaceholderConst {
545+
fn find_const_ty_from_env(self, env: I::ParamEnv) -> I::Ty {
546+
let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
547+
// `ConstArgHasType` are never desugared to be higher ranked.
548+
match clause.kind().skip_binder() {
549+
ty::ClauseKind::ConstArgHasType(placeholder_ct, ty) => {
550+
assert!(!(placeholder_ct, ty).has_escaping_bound_vars());
551+
552+
match placeholder_ct.kind() {
553+
ty::ConstKind::Placeholder(placeholder_ct) if placeholder_ct == self => {
554+
Some(ty)
555+
}
556+
_ => None,
557+
}
558+
}
559+
_ => None,
560+
}
561+
});
562+
563+
// N.B. it may be tempting to fix ICEs by making this function return
564+
// `Option<Ty<'tcx>>` instead of `Ty<'tcx>`; however, this is generally
565+
// considered to be a bandaid solution, since it hides more important
566+
// underlying issues with how we construct generics and predicates of
567+
// items. It's advised to fix the underlying issue rather than trying
568+
// to modify this function.
569+
let ty = candidates.next().unwrap_or_else(|| {
570+
panic!("cannot find `{self:?}` in param-env: {env:#?}");
571+
});
572+
assert!(
573+
candidates.next().is_none(),
574+
"did not expect duplicate `ConstParamHasTy` for `{self:?}` in param-env: {env:#?}"
575+
);
576+
ty
577+
}
578+
}
579+
541580
pub trait IntoKind {
542581
type Kind;
543582

compiler/rustc_type_ir/src/interner.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub trait Interner:
133133
type Const: Const<Self>;
134134
type ParamConst: Copy + Debug + Hash + Eq + ParamLike;
135135
type BoundConst: BoundVarLike<Self>;
136-
type PlaceholderConst: PlaceholderLike<Self, Bound = Self::BoundConst>;
136+
type PlaceholderConst: PlaceholderConst<Self>;
137137
type ValueConst: ValueConst<Self>;
138138
type ExprConst: ExprConst<Self>;
139139
type ValTree: Copy + Debug + Hash + Eq;
@@ -341,12 +341,6 @@ pub trait Interner:
341341
type UnsizingParams: Deref<Target = DenseBitSet<u32>>;
342342
fn unsizing_params_for_adt(self, adt_def_id: Self::DefId) -> Self::UnsizingParams;
343343

344-
fn find_const_ty_from_env(
345-
self,
346-
param_env: Self::ParamEnv,
347-
placeholder: Self::PlaceholderConst,
348-
) -> Self::Ty;
349-
350344
fn anonymize_bound_vars<T: TypeFoldable<Self>>(
351345
self,
352346
binder: ty::Binder<Self, T>,

0 commit comments

Comments
 (0)