Skip to content

Commit 4d51a98

Browse files
authored
Merge pull request #316 from nikomatsakis/cleanup-chalk-ir-no-vec-parameter
intern "substitutions"
2 parents 005c483 + 68cec9c commit 4d51a98

File tree

21 files changed

+332
-147
lines changed

21 files changed

+332
-147
lines changed

chalk-integration/src/lowering.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ impl LowerProjectionTy for ProjectionTy {
904904
} = *self;
905905
let chalk_ir::TraitRef {
906906
trait_id,
907-
parameters: trait_parameters,
907+
substitution: trait_substitution,
908908
} = trait_ref.lower(env)?;
909909
let lookup = match env.associated_ty_lookups.get(&(trait_id.into(), name.str)) {
910910
Some(lookup) => lookup,
@@ -933,11 +933,11 @@ impl LowerProjectionTy for ProjectionTy {
933933
}
934934
}
935935

936-
args.extend(trait_parameters);
936+
args.extend(trait_substitution.iter().cloned());
937937

938938
Ok(chalk_ir::ProjectionTy {
939939
associated_ty_id: lookup.id,
940-
parameters: args,
940+
substitution: chalk_ir::Substitution::from(args),
941941
})
942942
}
943943
}
@@ -961,7 +961,7 @@ impl LowerTy for Ty {
961961
} else {
962962
Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
963963
name: chalk_ir::TypeName::Struct(id),
964-
parameters: vec![],
964+
substitution: chalk_ir::Substitution::empty(),
965965
})
966966
.intern())
967967
}
@@ -1001,10 +1001,8 @@ impl LowerTy for Ty {
10011001
})?;
10021002
}
10031003

1004-
let parameters = args
1005-
.iter()
1006-
.map(|t| Ok(t.lower(env)?))
1007-
.collect::<LowerResult<Vec<_>>>()?;
1004+
let substitution =
1005+
chalk_ir::Substitution::from_fallible(args.iter().map(|t| Ok(t.lower(env)?)))?;
10081006

10091007
for (param, arg) in k.binders.binders.iter().zip(args.iter()) {
10101008
if param.kind() != arg.kind() {
@@ -1018,7 +1016,7 @@ impl LowerTy for Ty {
10181016

10191017
Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
10201018
name: chalk_ir::TypeName::Struct(id),
1021-
parameters: parameters,
1019+
substitution: substitution,
10221020
})
10231021
.intern())
10241022
}

chalk-integration/src/program.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ impl RustIrDatabase<ChalkIr> for Program {
163163
.filter(|(_, impl_datum)| {
164164
let trait_ref = &impl_datum.binders.value.trait_ref;
165165
trait_id == trait_ref.trait_id && {
166-
assert_eq!(trait_ref.parameters.len(), parameters.len());
167-
<[_] as CouldMatch<[_]>>::could_match(&parameters, &trait_ref.parameters)
166+
assert_eq!(trait_ref.substitution.len(), parameters.len());
167+
<[_] as CouldMatch<[_]>>::could_match(
168+
&parameters,
169+
&trait_ref.substitution.parameters(),
170+
)
168171
}
169172
})
170173
.map(|(&impl_id, _)| impl_id)
@@ -191,7 +194,7 @@ impl RustIrDatabase<ChalkIr> for Program {
191194
self.impl_data.values().any(|impl_datum| {
192195
let impl_trait_ref = &impl_datum.binders.value.trait_ref;
193196
impl_trait_ref.trait_id == auto_trait_id
194-
&& match impl_trait_ref.parameters[0].assert_ty_ref().data() {
197+
&& match impl_trait_ref.self_type_parameter().data() {
195198
TyData::Apply(apply) => match apply.name {
196199
TypeName::Struct(id) => id == struct_id,
197200
_ => false,

chalk-ir/src/cast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ impl<TF: TypeFamily> CastTo<Parameter<TF>> for Lifetime<TF> {
169169
}
170170
}
171171

172+
impl<TF: TypeFamily> CastTo<Parameter<TF>> for Parameter<TF> {
173+
fn cast_to(self) -> Parameter<TF> {
174+
self
175+
}
176+
}
177+
172178
impl<T, TF> CastTo<ProgramClause<TF>> for T
173179
where
174180
T: CastTo<DomainGoal<TF>>,

chalk-ir/src/could_match.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ where
2424
let names_could_match = a.name == b.name;
2525

2626
names_could_match
27-
&& a.parameters
27+
&& a.substitution
2828
.iter()
29-
.zip(&b.parameters)
30-
.all(|(p_a, p_b)| p_a.could_match(p_b))
29+
.zip(&b.substitution)
30+
.all(|(p_a, p_b)| p_a.could_match(&p_b))
3131
}
3232

3333
_ => true,

chalk-ir/src/debug.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ impl Debug for PlaceholderIndex {
118118

119119
impl<TF: TypeFamily> Debug for ApplicationTy<TF> {
120120
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
121-
write!(fmt, "{:?}{:?}", self.name, Angle(&self.parameters))
121+
let ApplicationTy { name, substitution } = self;
122+
write!(fmt, "{:?}{:?}", name, substitution.with_angle())
122123
}
123124
}
124125

@@ -153,13 +154,14 @@ struct SeparatorTraitRef<'me, TF: TypeFamily> {
153154

154155
impl<TF: TypeFamily> Debug for SeparatorTraitRef<'_, TF> {
155156
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
157+
let parameters = self.trait_ref.substitution.parameters();
156158
write!(
157159
fmt,
158160
"{:?}{}{:?}{:?}",
159-
self.trait_ref.parameters[0],
161+
parameters[0],
160162
self.separator,
161163
self.trait_ref.trait_id,
162-
Angle(&self.trait_ref.parameters[1..])
164+
Angle(&parameters[1..])
163165
)
164166
}
165167
}
@@ -171,7 +173,7 @@ impl<TF: TypeFamily> Debug for ProjectionTy<TF> {
171173
fmt,
172174
"({:?}){:?}",
173175
self.associated_ty_id,
174-
Angle(&self.parameters)
176+
self.substitution.with_angle()
175177
)
176178
})
177179
}
@@ -245,13 +247,9 @@ impl<TF: TypeFamily> Debug for DomainGoal<TF> {
245247
DomainGoal::IsLocal(n) => write!(fmt, "IsLocal({:?})", n),
246248
DomainGoal::IsUpstream(n) => write!(fmt, "IsUpstream({:?})", n),
247249
DomainGoal::IsFullyVisible(n) => write!(fmt, "IsFullyVisible({:?})", n),
248-
DomainGoal::LocalImplAllowed(tr) => write!(
249-
fmt,
250-
"LocalImplAllowed({:?}: {:?}{:?})",
251-
tr.parameters[0],
252-
tr.trait_id,
253-
Angle(&tr.parameters[1..])
254-
),
250+
DomainGoal::LocalImplAllowed(tr) => {
251+
write!(fmt, "LocalImplAllowed({:?})", tr.with_colon(),)
252+
}
255253
DomainGoal::Compatible(_) => write!(fmt, "Compatible"),
256254
DomainGoal::DownstreamType(n) => write!(fmt, "DownstreamType({:?})", n),
257255
}
@@ -416,9 +414,17 @@ impl<TF: TypeFamily> Display for ConstrainedSubst<TF> {
416414
}
417415
}
418416

417+
impl<TF: TypeFamily> Substitution<TF> {
418+
/// Displays the substitution in the form `< P0, .. Pn >`, or (if
419+
/// the substitution is empty) as an empty string.
420+
pub fn with_angle(&self) -> Angle<'_, Parameter<TF>> {
421+
Angle(self.parameters())
422+
}
423+
}
424+
419425
impl<TF: TypeFamily> Debug for Substitution<TF> {
420-
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
421-
Display::fmt(self, f)
426+
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
427+
Display::fmt(self, fmt)
422428
}
423429
}
424430

@@ -428,7 +434,7 @@ impl<TF: TypeFamily> Display for Substitution<TF> {
428434

429435
write!(f, "[")?;
430436

431-
for (index, value) in self.parameters.iter().enumerate() {
437+
for (index, value) in self.iter().enumerate() {
432438
if first {
433439
first = false;
434440
} else {

chalk-ir/src/family.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::tls;
22
use crate::AssocTypeId;
33
use crate::GoalData;
44
use crate::LifetimeData;
5+
use crate::Parameter;
56
use crate::ParameterData;
67
use crate::ProjectionTy;
78
use crate::RawId;
@@ -68,6 +69,14 @@ pub trait TypeFamily: Debug + Copy + Eq + Ord + Hash {
6869
/// converted back to its underlying data via `goal_data`.
6970
type InternedGoal: Debug + Clone + Eq + Ord + Hash;
7071

72+
/// "Interned" representation of a "substitution". In normal user code,
73+
/// `Self::InternedSubstitution` is not referenced. Instead, we refer to
74+
/// `Substitution<Self>`, which wraps this type.
75+
///
76+
/// An `InternedSubstitution` is created by `intern_substitution` and can be
77+
/// converted back to its underlying data via `substitution_data`.
78+
type InternedSubstitution: Debug + Clone + Eq + Ord + Hash;
79+
7180
/// The core "id" type used for struct-ids and the like.
7281
type DefId: Debug + Copy + Eq + Ord + Hash;
7382

@@ -147,7 +156,18 @@ pub trait TypeFamily: Debug + Copy + Eq + Ord + Hash {
147156
fn intern_goal(data: GoalData<Self>) -> Self::InternedGoal;
148157

149158
/// Lookup the `GoalData` that was interned to create a `InternedGoal`.
150-
fn goal_data(lifetime: &Self::InternedGoal) -> &GoalData<Self>;
159+
fn goal_data(goal: &Self::InternedGoal) -> &GoalData<Self>;
160+
161+
/// Create an "interned" substitution from `data`. This is not
162+
/// normally invoked directly; instead, you invoke
163+
/// `SubstitutionData::intern` (which will ultimately call this
164+
/// method).
165+
fn intern_substitution<E>(
166+
data: impl IntoIterator<Item = Result<Parameter<Self>, E>>,
167+
) -> Result<Self::InternedSubstitution, E>;
168+
169+
/// Lookup the `SubstitutionData` that was interned to create a `InternedSubstitution`.
170+
fn substitution_data(substitution: &Self::InternedSubstitution) -> &[Parameter<Self>];
151171
}
152172

153173
pub trait TargetTypeFamily<TF: TypeFamily>: TypeFamily {
@@ -181,6 +201,7 @@ impl TypeFamily for ChalkIr {
181201
type InternedLifetime = LifetimeData<ChalkIr>;
182202
type InternedParameter = ParameterData<ChalkIr>;
183203
type InternedGoal = Arc<GoalData<ChalkIr>>;
204+
type InternedSubstitution = Vec<Parameter<ChalkIr>>;
184205
type DefId = RawId;
185206

186207
fn debug_struct_id(
@@ -242,6 +263,16 @@ impl TypeFamily for ChalkIr {
242263
fn goal_data(goal: &Arc<GoalData<ChalkIr>>) -> &GoalData<ChalkIr> {
243264
goal
244265
}
266+
267+
fn intern_substitution<E>(
268+
data: impl IntoIterator<Item = Result<Parameter<ChalkIr>, E>>,
269+
) -> Result<Vec<Parameter<ChalkIr>>, E> {
270+
data.into_iter().collect()
271+
}
272+
273+
fn substitution_data(substitution: &Vec<Parameter<ChalkIr>>) -> &[Parameter<ChalkIr>] {
274+
substitution
275+
}
245276
}
246277

247278
impl HasTypeFamily for ChalkIr {

chalk-ir/src/fold/boring_impls.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl<T: Fold<TF, TTF>, TF: TypeFamily, TTF: TargetTypeFamily<TF>> Fold<TF, TTF>
8686
}
8787
}
8888
}
89+
8990
impl<TF: TypeFamily, TTF: TargetTypeFamily<TF>> Fold<TF, TTF> for Parameter<TF> {
9091
type Result = Parameter<TTF>;
9192
fn fold_with(
@@ -110,6 +111,19 @@ impl<TF: TypeFamily, TTF: TargetTypeFamily<TF>> Fold<TF, TTF> for Goal<TF> {
110111
}
111112
}
112113

114+
impl<TF: TypeFamily, TTF: TargetTypeFamily<TF>> Fold<TF, TTF> for Substitution<TF> {
115+
type Result = Substitution<TTF>;
116+
fn fold_with(
117+
&self,
118+
folder: &mut dyn Folder<TF, TTF>,
119+
binders: usize,
120+
) -> Fallible<Self::Result> {
121+
Ok(Substitution::from_fallible(
122+
self.iter().map(|p| p.fold_with(folder, binders)),
123+
)?)
124+
}
125+
}
126+
113127
#[macro_export]
114128
macro_rules! copy_fold {
115129
($t:ty) => {

0 commit comments

Comments
 (0)