Skip to content

Commit 02d1033

Browse files
committed
Switch Vec<ParameterKind<UniverseIndex>> to ParameterKindsWithUniverseIndex<I>
1 parent 2b0f5bb commit 02d1033

File tree

21 files changed

+247
-69
lines changed

21 files changed

+247
-69
lines changed

chalk-engine/src/forest.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ pub enum SubstitutionResult<S> {
113113
Floundered,
114114
}
115115

116+
impl<S> SubstitutionResult<S> {
117+
pub fn as_ref(&self) -> SubstitutionResult<&S> {
118+
match self {
119+
SubstitutionResult::Definite(subst) => SubstitutionResult::Definite(subst),
120+
SubstitutionResult::Ambiguous(subst) => SubstitutionResult::Ambiguous(subst),
121+
SubstitutionResult::Floundered => SubstitutionResult::Floundered,
122+
}
123+
}
124+
pub fn map<U, F: FnOnce(S) -> U>(self, f: F) -> SubstitutionResult<U> {
125+
match self {
126+
SubstitutionResult::Definite(subst) => SubstitutionResult::Definite(f(subst)),
127+
SubstitutionResult::Ambiguous(subst) => SubstitutionResult::Ambiguous(f(subst)),
128+
SubstitutionResult::Floundered => SubstitutionResult::Floundered,
129+
}
130+
}
131+
}
132+
116133
impl<S: Display> Display for SubstitutionResult<S> {
117134
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118135
match self {

chalk-engine/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,5 @@ impl Minimums {
278278
min(self.positive, self.negative)
279279
}
280280
}
281+
282+

chalk-integration/src/program.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ impl tls::DebugContext for Program {
188188
write!(fmt, "{:?}", parameter.data(interner).inner_debug())
189189
}
190190

191+
fn debug_parameter_kinds(
192+
&self,
193+
parameter_kinds: &chalk_ir::ParameterKinds<ChalkIr>,
194+
fmt: &mut fmt::Formatter<'_>,
195+
) -> Result<(), fmt::Error> {
196+
let interner = self.interner();
197+
write!(fmt, "{:?}", parameter_kinds.as_slice(interner))
198+
}
199+
200+
fn debug_parameter_kinds_with_universe_index(
201+
&self,
202+
parameter_kinds: &chalk_ir::ParameterKindsWithUniverseIndex<ChalkIr>,
203+
fmt: &mut fmt::Formatter<'_>,
204+
) -> Result<(), fmt::Error> {
205+
let interner = self.interner();
206+
write!(fmt, "{:?}", parameter_kinds.as_slice(interner))
207+
}
208+
191209
fn debug_goal(
192210
&self,
193211
goal: &Goal<ChalkIr>,

chalk-ir/src/cast.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ reflexive_impl!(for(I: Interner) Goal<I>);
8282
reflexive_impl!(for(I: Interner) WhereClause<I>);
8383
reflexive_impl!(for(I: Interner) ProgramClause<I>);
8484
reflexive_impl!(for(I: Interner) QuantifiedWhereClause<I>);
85+
reflexive_impl!(for(I: Interner) ParameterKinds<I>);
86+
reflexive_impl!(for(I: Interner) ParameterKindsWithUniverseIndex<I>);
8587

8688
impl<I: Interner> CastTo<WhereClause<I>> for TraitRef<I> {
8789
fn cast_to(self, _interner: &I) -> WhereClause<I> {
@@ -268,16 +270,16 @@ where
268270
impl<T, U> CastTo<Canonical<U>> for Canonical<T>
269271
where
270272
T: CastTo<U> + HasInterner,
271-
U: HasInterner,
273+
U: HasInterner<Interner = T::Interner>,
272274
{
273-
fn cast_to(self, interner: &U::Interner) -> Canonical<U> {
275+
fn cast_to(self, interner: &T::Interner) -> Canonical<U> {
274276
// Subtle point: It should be ok to re-use the binders here,
275277
// because `cast()` never introduces new inference variables,
276278
// nor changes the "substance" of the type we are working
277279
// with. It just introduces new wrapper types.
278280
Canonical {
279281
value: self.value.cast(interner),
280-
binders: self.binders,
282+
binders: self.binders.cast(interner),
281283
}
282284
}
283285
}

chalk-ir/src/debug.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,32 @@ impl<I: Interner> Debug for Environment<I> {
636636
}
637637
}
638638

639-
impl<T: Display> Display for Canonical<T> {
640-
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
641-
let Canonical { binders, value } = self;
639+
impl<I: Interner> Debug for ParameterKindsWithUniverseIndex<I> {
640+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
641+
I::debug_parameter_kinds_with_universe_index(self, fmt)
642+
.unwrap_or_else(|| write!(fmt, "{:?}", self.interned))
643+
}
644+
}
645+
646+
impl<T: HasInterner + Display> Canonical<T> {
647+
pub fn display<'a>(&'a self, interner: &'a T::Interner) -> CanonicalDisplay<'a, T> {
648+
CanonicalDisplay {
649+
canonical: self,
650+
interner,
651+
}
652+
}
653+
}
642654

655+
pub struct CanonicalDisplay<'a, T: HasInterner> {
656+
canonical: &'a Canonical<T>,
657+
interner: &'a T::Interner,
658+
}
659+
660+
impl<'a, T: HasInterner + Display> Display for CanonicalDisplay<'a, T> {
661+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
662+
let Canonical { binders, value } = self.canonical;
663+
let interner = self.interner;
664+
let binders = binders.as_slice(interner);
643665
if binders.is_empty() {
644666
// Ordinarily, we try to print all binder levels, if they
645667
// are empty, but we can skip in this *particular* case
@@ -665,6 +687,17 @@ impl<T: Display> Display for Canonical<T> {
665687
}
666688
}
667689

690+
pub struct CanonicalDisplay<'a, I: Interner, T> {
691+
canonical: &'a Canonical<I, T>,
692+
interner: &'a I,
693+
}
694+
695+
impl<'a, I: Interner, T: Display> Display for CanonicalDisplay<'a, I, T> {
696+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
697+
DisplayWithInterner::fmt_with_interner(self.canonical, self.interner, f)
698+
}
699+
}
700+
668701
impl<T: Debug, L: Debug> Debug for ParameterKind<T, L> {
669702
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
670703
match *self {

chalk-ir/src/fold/binder_impls.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ where
5555
}
5656
}
5757

58-
impl<T, I, TI> Fold<I, TI> for Canonical<T>
58+
impl<I, T, TI> Fold<I, TI> for Canonical<T>
5959
where
60-
T: Fold<I, TI>,
6160
I: Interner,
61+
T: HasInterner<Interner = I> + Fold<I, TI>,
62+
<T as Fold<I, TI>>::Result: HasInterner<Interner = TI>,
6263
TI: TargetInterner<I>,
6364
{
6465
type Result = Canonical<T::Result>;
@@ -76,8 +77,13 @@ where
7677
value: self_value,
7778
} = self;
7879
let value = self_value.fold_with(folder, outer_binder.shifted_in())?;
80+
let binders = ParameterKindsWithUniverseIndex {
81+
interned: TI::transfer_parameter_kinds_with_universe_index(
82+
self_binders.interned().clone(),
83+
),
84+
};
7985
Ok(Canonical {
80-
binders: self_binders.clone(),
86+
binders: binders,
8187
value: value,
8288
})
8389
}

chalk-ir/src/interner.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::OpaqueTyId;
1111
use crate::Parameter;
1212
use crate::ParameterData;
1313
use crate::ParameterKind;
14+
use crate::ParameterKinds;
15+
use crate::ParameterKindsWithUniverseIndex;
1416
use crate::ProgramClause;
1517
use crate::ProgramClauseData;
1618
use crate::ProgramClauseImplication;
@@ -282,6 +284,36 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
282284
None
283285
}
284286

287+
/// Prints the debug representation of a parameter kinds list. To get good
288+
/// results, this requires inspecting TLS, and is difficult to
289+
/// code without reference to a specific interner (and hence
290+
/// fully known types).
291+
///
292+
/// Returns `None` to fallback to the default debug output (e.g.,
293+
/// if no info about current program is available from TLS).
294+
#[allow(unused_variables)]
295+
fn debug_parameter_kinds(
296+
parameter_kinds: &ParameterKinds<Self>,
297+
fmt: &mut fmt::Formatter<'_>,
298+
) -> Option<fmt::Result> {
299+
None
300+
}
301+
302+
/// Prints the debug representation of an parameter kinds list with universe index.
303+
/// To get good results, this requires inspecting TLS, and is difficult to
304+
/// code without reference to a specific interner (and hence
305+
/// fully known types).
306+
///
307+
/// Returns `None` to fallback to the default debug output (e.g.,
308+
/// if no info about current program is available from TLS).
309+
#[allow(unused_variables)]
310+
fn debug_parameter_kinds_with_universe_index(
311+
parameter_kinds_with_universe_index: &ParameterKindsWithUniverseIndex<Self>,
312+
fmt: &mut fmt::Formatter<'_>,
313+
) -> Option<fmt::Result> {
314+
None
315+
}
316+
285317
/// Prints the debug representation of an goal. To get good
286318
/// results, this requires inspecting TLS, and is difficult to
287319
/// code without reference to a specific interner (and hence
@@ -548,12 +580,22 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
548580

549581
pub trait TargetInterner<I: Interner>: Interner {
550582
fn transfer_def_id(def_id: I::DefId) -> Self::DefId;
583+
584+
fn transfer_parameter_kinds_with_universe_index(
585+
parameter_kinds: I::InternedParameterKindsWithUniverseIndex,
586+
) -> Self::InternedParameterKindsWithUniverseIndex;
551587
}
552588

553589
impl<I: Interner> TargetInterner<I> for I {
554590
fn transfer_def_id(def_id: I::DefId) -> Self::DefId {
555591
def_id
556592
}
593+
594+
fn transfer_parameter_kinds_with_universe_index(
595+
parameter_kinds: I::InternedParameterKindsWithUniverseIndex,
596+
) -> Self::InternedParameterKindsWithUniverseIndex {
597+
parameter_kinds
598+
}
557599
}
558600

559601
/// Implemented by types that have an associated interner (which
@@ -674,6 +716,27 @@ mod default {
674716
tls::with_current_program(|prog| Some(prog?.debug_parameter(parameter, fmt)))
675717
}
676718

719+
fn debug_parameter_kinds(
720+
parameter_kinds: &ParameterKinds<Self>,
721+
fmt: &mut fmt::Formatter<'_>,
722+
) -> Option<fmt::Result> {
723+
tls::with_current_program(|prog| {
724+
Some(prog?.debug_parameter_kinds(parameter_kinds, fmt))
725+
})
726+
}
727+
728+
fn debug_parameter_kinds_with_universe_index(
729+
parameter_kinds_with_universe_index: &ParameterKindsWithUniverseIndex<Self>,
730+
fmt: &mut fmt::Formatter<'_>,
731+
) -> Option<fmt::Result> {
732+
tls::with_current_program(|prog| {
733+
Some(prog?.debug_parameter_kinds_with_universe_index(
734+
parameter_kinds_with_universe_index,
735+
fmt,
736+
))
737+
})
738+
}
739+
677740
fn debug_goal(goal: &Goal<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
678741
tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt)))
679742
}

chalk-ir/src/lib.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,16 +1596,13 @@ impl<I: Interner> ParameterKindsWithUniverseIndex<I> {
15961596
/// All unresolved existential variables are "renumbered" according to their
15971597
/// first appearance; the kind/universe of the variable is recorded in the
15981598
/// `binders` field.
1599-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1600-
pub struct Canonical<T> {
1599+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1600+
pub struct Canonical<T: HasInterner> {
16011601
pub value: T,
1602-
pub binders: Vec<ParameterKind<UniverseIndex>>,
1602+
pub binders: ParameterKindsWithUniverseIndex<T::Interner>,
16031603
}
16041604

1605-
impl<T> HasInterner for Canonical<T>
1606-
where
1607-
T: HasInterner,
1608-
{
1605+
impl<T: HasInterner> HasInterner for Canonical<T> {
16091606
type Interner = T::Interner;
16101607
}
16111608

@@ -1615,21 +1612,21 @@ where
16151612
/// distinctions.
16161613
///
16171614
/// To produce one of these values, use the `u_canonicalize` method.
1618-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1619-
pub struct UCanonical<T> {
1615+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1616+
pub struct UCanonical<T: HasInterner> {
16201617
pub canonical: Canonical<T>,
16211618
pub universes: usize,
16221619
}
16231620

1624-
impl<T> UCanonical<T> {
1625-
pub fn is_trivial_substitution<I: Interner>(
1621+
impl<T: HasInterner> UCanonical<T> {
1622+
pub fn is_trivial_substitution(
16261623
&self,
1627-
interner: &I,
1628-
canonical_subst: &Canonical<AnswerSubst<I>>,
1624+
interner: &T::Interner,
1625+
canonical_subst: &Canonical<AnswerSubst<T::Interner>>,
16291626
) -> bool {
16301627
let subst = &canonical_subst.value.subst;
16311628
assert_eq!(
1632-
self.canonical.binders.len(),
1629+
self.canonical.binders.len(interner),
16331630
subst.parameters(interner).len()
16341631
);
16351632
subst.is_identity_subst(interner)

chalk-ir/src/tls.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::interner::ChalkIr;
22
use crate::{
33
debug::SeparatorTraitRef, AliasTy, ApplicationTy, AssocTypeId, Goal, Goals, Lifetime, OpaqueTy,
4-
OpaqueTyId, Parameter, ProgramClause, ProgramClauseImplication, ProgramClauses, ProjectionTy,
5-
QuantifiedWhereClauses, StructId, Substitution, TraitId, Ty,
4+
OpaqueTyId, Parameter, ParameterKinds, ParameterKindsWithUniverseIndex, ProgramClause,
5+
ProgramClauseImplication, ProjectionTy, ProgramClauses, QuantifiedWhereClauses, StructId, Substitution,
6+
TraitId, Ty,
67
};
78
use std::cell::RefCell;
89
use std::fmt;
@@ -69,6 +70,18 @@ pub trait DebugContext {
6970
fmt: &mut fmt::Formatter<'_>,
7071
) -> Result<(), fmt::Error>;
7172

73+
fn debug_parameter_kinds(
74+
&self,
75+
parameter_kinds: &ParameterKinds<ChalkIr>,
76+
fmt: &mut fmt::Formatter<'_>,
77+
) -> Result<(), fmt::Error>;
78+
79+
fn debug_parameter_kinds_with_universe_index(
80+
&self,
81+
parameter_kinds: &ParameterKindsWithUniverseIndex<ChalkIr>,
82+
fmt: &mut fmt::Formatter<'_>,
83+
) -> Result<(), fmt::Error>;
84+
7285
fn debug_goal(
7386
&self,
7487
goal: &Goal<ChalkIr>,

chalk-ir/src/visit/binder_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ where
3737
}
3838
}
3939

40-
impl<T, I> Visit<I> for Canonical<T>
40+
impl<I, T> Visit<I> for Canonical<T>
4141
where
42-
T: Visit<I>,
4342
I: Interner,
43+
T: HasInterner<Interner = I> + Visit<I>,
4444
{
4545
fn visit_with<'i, R: VisitResult>(
4646
&self,

0 commit comments

Comments
 (0)