Skip to content

Commit 02e5577

Browse files
committed
Make interned type wrappers derive Copy and make interning list-like types fallible
1 parent eeaa60c commit 02e5577

File tree

2 files changed

+97
-82
lines changed

2 files changed

+97
-82
lines changed

chalk-ir/src/interner.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,10 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
506506
/// normally invoked directly; instead, you invoke
507507
/// `GoalsData::intern` (which will ultimately call this
508508
/// method).
509-
fn intern_goals(&self, data: impl IntoIterator<Item = Goal<Self>>) -> Self::InternedGoals;
509+
fn intern_goals<E>(
510+
&self,
511+
data: impl IntoIterator<Item = Result<Goal<Self>, E>>,
512+
) -> Result<Self::InternedGoals, E>;
510513

511514
/// Lookup the `GoalsData` that was interned to create a `InternedGoals`.
512515
fn goals_data<'a>(&self, goals: &'a Self::InternedGoals) -> &'a [Goal<Self>];
@@ -542,10 +545,10 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
542545
/// normally invoked directly; instead, you invoke
543546
/// `ProgramClauses::from` (which will ultimately call this
544547
/// method).
545-
fn intern_program_clauses(
548+
fn intern_program_clauses<E>(
546549
&self,
547-
data: impl IntoIterator<Item = ProgramClause<Self>>,
548-
) -> Self::InternedProgramClauses;
550+
data: impl IntoIterator<Item = Result<ProgramClause<Self>, E>>,
551+
) -> Result<Self::InternedProgramClauses, E>;
549552

550553
/// Lookup the `ProgramClauseData` that was interned to create a `ProgramClause`.
551554
fn program_clauses_data<'a>(
@@ -557,10 +560,10 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
557560
/// normally invoked directly; instead, you invoke
558561
/// `QuantifiedWhereClauses::from` (which will ultimately call this
559562
/// method).
560-
fn intern_quantified_where_clauses(
563+
fn intern_quantified_where_clauses<E>(
561564
&self,
562-
data: impl IntoIterator<Item = QuantifiedWhereClause<Self>>,
563-
) -> Self::InternedQuantifiedWhereClauses;
565+
data: impl IntoIterator<Item = Result<QuantifiedWhereClause<Self>, E>>,
566+
) -> Result<Self::InternedQuantifiedWhereClauses, E>;
564567

565568
/// Lookup the slice of `QuantifiedWhereClause` that was interned to
566569
/// create a `QuantifiedWhereClauses`.
@@ -573,10 +576,10 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
573576
/// normally invoked directly; instead, you invoke
574577
/// `ParameterKinds::from` (which will ultimately call this
575578
/// method).
576-
fn intern_parameter_kinds(
579+
fn intern_parameter_kinds<E>(
577580
&self,
578-
data: impl IntoIterator<Item = ParameterKind<()>>,
579-
) -> Self::InternedParameterKinds;
581+
data: impl IntoIterator<Item = Result<ParameterKind<()>, E>>,
582+
) -> Result<Self::InternedParameterKinds, E>;
580583

581584
/// Lookup the slice of `ParameterKind` that was interned to
582585
/// create a `ParameterKinds`.
@@ -589,10 +592,10 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
589592
/// normally invoked directly; instead, you invoke
590593
/// `CanonicalVarKinds::from` (which will ultimately call this
591594
/// method).
592-
fn intern_canonical_var_kinds(
595+
fn intern_canonical_var_kinds<E>(
593596
&self,
594-
data: impl IntoIterator<Item = ParameterKind<UniverseIndex>>,
595-
) -> Self::InternedCanonicalVarKinds;
597+
data: impl IntoIterator<Item = Result<ParameterKind<UniverseIndex>, E>>,
598+
) -> Result<Self::InternedCanonicalVarKinds, E>;
596599

597600
/// Lookup the slice of `ParameterKind` that was interned to
598601
/// create a `ParameterKinds`.
@@ -879,10 +882,10 @@ mod default {
879882
goal
880883
}
881884

882-
fn intern_goals(
885+
fn intern_goals<E>(
883886
&self,
884-
data: impl IntoIterator<Item = Goal<ChalkIr>>,
885-
) -> Vec<Goal<ChalkIr>> {
887+
data: impl IntoIterator<Item = Result<Goal<ChalkIr>, E>>,
888+
) -> Result<Vec<Goal<ChalkIr>>, E> {
886889
data.into_iter().collect()
887890
}
888891

@@ -915,10 +918,10 @@ mod default {
915918
clause
916919
}
917920

918-
fn intern_program_clauses(
921+
fn intern_program_clauses<E>(
919922
&self,
920-
data: impl IntoIterator<Item = ProgramClause<Self>>,
921-
) -> Vec<ProgramClause<Self>> {
923+
data: impl IntoIterator<Item = Result<ProgramClause<Self>, E>>,
924+
) -> Result<Vec<ProgramClause<Self>>, E> {
922925
data.into_iter().collect()
923926
}
924927

@@ -929,10 +932,10 @@ mod default {
929932
clauses
930933
}
931934

932-
fn intern_quantified_where_clauses(
935+
fn intern_quantified_where_clauses<E>(
933936
&self,
934-
data: impl IntoIterator<Item = QuantifiedWhereClause<Self>>,
935-
) -> Self::InternedQuantifiedWhereClauses {
937+
data: impl IntoIterator<Item = Result<QuantifiedWhereClause<Self>, E>>,
938+
) -> Result<Self::InternedQuantifiedWhereClauses, E> {
936939
data.into_iter().collect()
937940
}
938941

@@ -942,24 +945,27 @@ mod default {
942945
) -> &'a [QuantifiedWhereClause<Self>] {
943946
clauses
944947
}
945-
fn intern_parameter_kinds(
948+
fn intern_parameter_kinds<E>(
946949
&self,
947-
data: impl IntoIterator<Item = ParameterKind<()>>,
948-
) -> Self::InternedParameterKinds {
950+
data: impl IntoIterator<Item = Result<ParameterKind<()>, E>>,
951+
) -> Result<Self::InternedParameterKinds, E> {
949952
data.into_iter().collect()
950953
}
954+
951955
fn parameter_kinds_data<'a>(
952956
&self,
953957
parameter_kinds: &'a Self::InternedParameterKinds,
954958
) -> &'a [ParameterKind<()>] {
955959
parameter_kinds
956960
}
957-
fn intern_canonical_var_kinds(
961+
962+
fn intern_canonical_var_kinds<E>(
958963
&self,
959-
data: impl IntoIterator<Item = ParameterKind<UniverseIndex>>,
960-
) -> Self::InternedCanonicalVarKinds {
964+
data: impl IntoIterator<Item = Result<ParameterKind<UniverseIndex>, E>>,
965+
) -> Result<Self::InternedCanonicalVarKinds, E> {
961966
data.into_iter().collect()
962967
}
968+
963969
fn canonical_var_kinds_data<'a>(
964970
&self,
965971
canonical_var_kinds: &'a Self::InternedCanonicalVarKinds,

chalk-ir/src/lib.rs

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ pub struct OpaqueTyId<I: Interner>(pub I::DefId);
225225

226226
impl_debugs!(ImplId, ClauseId);
227227

228-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
228+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
229229
pub struct Ty<I: Interner> {
230230
interned: I::InternedType,
231231
}
@@ -819,7 +819,7 @@ impl<T, L> ParameterKind<T, L> {
819819
}
820820
}
821821

822-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
822+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
823823
pub struct Parameter<I: Interner> {
824824
interned: I::InternedParameter,
825825
}
@@ -1124,7 +1124,7 @@ impl<I: Interner> QuantifiedWhereClause<I> {
11241124
}
11251125
}
11261126

1127-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
1127+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
11281128
pub struct QuantifiedWhereClauses<I: Interner> {
11291129
interned: I::InternedQuantifiedWhereClauses,
11301130
}
@@ -1142,25 +1142,26 @@ impl<I: Interner> QuantifiedWhereClauses<I> {
11421142
interner: &I,
11431143
clauses: impl IntoIterator<Item = impl CastTo<QuantifiedWhereClause<I>>>,
11441144
) -> Self {
1145-
use crate::cast::Caster;
1146-
QuantifiedWhereClauses {
1147-
interned: I::intern_quantified_where_clauses(
1148-
interner,
1149-
clauses.into_iter().casted(interner),
1150-
),
1151-
}
1145+
Self::from_fallible(
1146+
interner,
1147+
clauses
1148+
.into_iter()
1149+
.map(|p| -> Result<QuantifiedWhereClause<I>, ()> { Ok(p.cast(interner)) }),
1150+
)
1151+
.unwrap()
11521152
}
11531153

11541154
pub fn from_fallible<E>(
11551155
interner: &I,
11561156
clauses: impl IntoIterator<Item = Result<impl CastTo<QuantifiedWhereClause<I>>, E>>,
11571157
) -> Result<Self, E> {
11581158
use crate::cast::Caster;
1159-
let clauses = clauses
1160-
.into_iter()
1161-
.casted(interner)
1162-
.collect::<Result<Vec<QuantifiedWhereClause<I>>, _>>()?;
1163-
Ok(Self::from(interner, clauses))
1159+
Ok(QuantifiedWhereClauses {
1160+
interned: I::intern_quantified_where_clauses(
1161+
interner,
1162+
clauses.into_iter().casted(interner),
1163+
)?,
1164+
})
11641165
}
11651166

11661167
pub fn iter(&self, interner: &I) -> std::slice::Iter<'_, QuantifiedWhereClause<I>> {
@@ -1459,7 +1460,7 @@ impl<I: Interner> ProgramClauseData<I> {
14591460
}
14601461
}
14611462

1462-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
1463+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
14631464
pub struct ProgramClause<I: Interner> {
14641465
interned: I::InternedProgramClause,
14651466
}
@@ -1485,7 +1486,7 @@ impl<I: Interner> ProgramClause<I> {
14851486
}
14861487
}
14871488

1488-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
1489+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
14891490
pub struct ProgramClauses<I: Interner> {
14901491
interned: I::InternedProgramClauses,
14911492
}
@@ -1503,22 +1504,23 @@ impl<I: Interner> ProgramClauses<I> {
15031504
interner: &I,
15041505
clauses: impl IntoIterator<Item = impl CastTo<ProgramClause<I>>>,
15051506
) -> Self {
1506-
use crate::cast::Caster;
1507-
ProgramClauses {
1508-
interned: I::intern_program_clauses(interner, clauses.into_iter().casted(interner)),
1509-
}
1507+
Self::from_fallible(
1508+
interner,
1509+
clauses
1510+
.into_iter()
1511+
.map(|p| -> Result<ProgramClause<I>, ()> { Ok(p.cast(interner)) }),
1512+
)
1513+
.unwrap()
15101514
}
15111515

15121516
pub fn from_fallible<E>(
15131517
interner: &I,
15141518
clauses: impl IntoIterator<Item = Result<impl CastTo<ProgramClause<I>>, E>>,
15151519
) -> Result<Self, E> {
15161520
use crate::cast::Caster;
1517-
let clauses = clauses
1518-
.into_iter()
1519-
.casted(interner)
1520-
.collect::<Result<Vec<ProgramClause<I>>, _>>()?;
1521-
Ok(Self::from(interner, clauses))
1521+
Ok(ProgramClauses {
1522+
interned: I::intern_program_clauses(interner, clauses.into_iter().casted(interner))?,
1523+
})
15221524
}
15231525

15241526
pub fn iter(&self, interner: &I) -> std::slice::Iter<'_, ProgramClause<I>> {
@@ -1538,7 +1540,7 @@ impl<I: Interner> ProgramClauses<I> {
15381540
}
15391541
}
15401542

1541-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
1543+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
15421544
pub struct ParameterKinds<I: Interner> {
15431545
interned: I::InternedParameterKinds,
15441546
}
@@ -1556,19 +1558,22 @@ impl<I: Interner> ParameterKinds<I> {
15561558
interner: &I,
15571559
parameter_kinds: impl IntoIterator<Item = ParameterKind<()>>,
15581560
) -> Self {
1559-
ParameterKinds {
1560-
interned: I::intern_parameter_kinds(interner, parameter_kinds.into_iter()),
1561-
}
1561+
Self::from_fallible(
1562+
interner,
1563+
parameter_kinds
1564+
.into_iter()
1565+
.map(|p| -> Result<ParameterKind<()>, ()> { Ok(p) }),
1566+
)
1567+
.unwrap()
15621568
}
15631569

15641570
pub fn from_fallible<E>(
15651571
interner: &I,
15661572
parameter_kinds: impl IntoIterator<Item = Result<ParameterKind<()>, E>>,
15671573
) -> Result<Self, E> {
1568-
let parameter_kinds = parameter_kinds
1569-
.into_iter()
1570-
.collect::<Result<Vec<ParameterKind<()>>, _>>()?;
1571-
Ok(Self::from(interner, parameter_kinds))
1574+
Ok(ParameterKinds {
1575+
interned: I::intern_parameter_kinds(interner, parameter_kinds.into_iter())?,
1576+
})
15721577
}
15731578

15741579
pub fn iter(&self, interner: &I) -> std::slice::Iter<'_, ParameterKind<()>> {
@@ -1588,7 +1593,7 @@ impl<I: Interner> ParameterKinds<I> {
15881593
}
15891594
}
15901595

1591-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
1596+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
15921597
pub struct CanonicalVarKinds<I: Interner> {
15931598
interned: I::InternedCanonicalVarKinds,
15941599
}
@@ -1606,19 +1611,22 @@ impl<I: Interner> CanonicalVarKinds<I> {
16061611
interner: &I,
16071612
parameter_kinds: impl IntoIterator<Item = ParameterKind<UniverseIndex>>,
16081613
) -> Self {
1609-
CanonicalVarKinds {
1610-
interned: I::intern_canonical_var_kinds(interner, parameter_kinds.into_iter()),
1611-
}
1614+
Self::from_fallible(
1615+
interner,
1616+
parameter_kinds
1617+
.into_iter()
1618+
.map(|p| -> Result<ParameterKind<UniverseIndex>, ()> { Ok(p) }),
1619+
)
1620+
.unwrap()
16121621
}
16131622

16141623
pub fn from_fallible<E>(
16151624
interner: &I,
16161625
parameter_kinds: impl IntoIterator<Item = Result<ParameterKind<UniverseIndex>, E>>,
16171626
) -> Result<Self, E> {
1618-
let parameter_kinds = parameter_kinds
1619-
.into_iter()
1620-
.collect::<Result<Vec<ParameterKind<UniverseIndex>>, _>>()?;
1621-
Ok(Self::from(interner, parameter_kinds))
1627+
Ok(CanonicalVarKinds {
1628+
interned: I::intern_canonical_var_kinds(interner, parameter_kinds.into_iter())?,
1629+
})
16221630
}
16231631

16241632
pub fn iter(&self, interner: &I) -> std::slice::Iter<'_, ParameterKind<UniverseIndex>> {
@@ -1704,7 +1712,7 @@ impl<T: HasInterner> UCanonical<T> {
17041712
}
17051713
}
17061714

1707-
#[derive(Clone, PartialEq, Eq, Hash, HasInterner)]
1715+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HasInterner)]
17081716
/// A list of goals.
17091717
pub struct Goals<I: Interner> {
17101718
interned: I::InternedGoals,
@@ -1720,22 +1728,23 @@ impl<I: Interner> Goals<I> {
17201728
}
17211729

17221730
pub fn from(interner: &I, goals: impl IntoIterator<Item = impl CastTo<Goal<I>>>) -> Self {
1723-
use crate::cast::Caster;
1724-
Goals {
1725-
interned: I::intern_goals(interner, goals.into_iter().casted(interner)),
1726-
}
1731+
Self::from_fallible(
1732+
interner,
1733+
goals
1734+
.into_iter()
1735+
.map(|p| -> Result<Goal<I>, ()> { Ok(p.cast(interner)) }),
1736+
)
1737+
.unwrap()
17271738
}
17281739

17291740
pub fn from_fallible<E>(
17301741
interner: &I,
17311742
goals: impl IntoIterator<Item = Result<impl CastTo<Goal<I>>, E>>,
17321743
) -> Result<Self, E> {
17331744
use crate::cast::Caster;
1734-
let goals = goals
1735-
.into_iter()
1736-
.casted(interner)
1737-
.collect::<Result<Vec<Goal<I>>, _>>()?;
1738-
Ok(Goals::from(interner, goals))
1745+
Ok(Goals {
1746+
interned: I::intern_goals(interner, goals.into_iter().casted(interner))?,
1747+
})
17391748
}
17401749

17411750
pub fn iter(&self, interner: &I) -> std::slice::Iter<'_, Goal<I>> {
@@ -1755,7 +1764,7 @@ impl<I: Interner> Goals<I> {
17551764
}
17561765
}
17571766

1758-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
1767+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
17591768
/// A general goal; this is the full range of questions you can pose to Chalk.
17601769
pub struct Goal<I: Interner> {
17611770
interned: I::InternedGoal,
@@ -1904,7 +1913,7 @@ pub enum Constraint<I: Interner> {
19041913
}
19051914

19061915
/// A mapping of inference variables to instantiations thereof.
1907-
#[derive(Clone, PartialEq, Eq, Hash, HasInterner)]
1916+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HasInterner)]
19081917
pub struct Substitution<I: Interner> {
19091918
/// Map free variable with given index to the value with the same
19101919
/// index. Naturally, the kind of the variable must agree with

0 commit comments

Comments
 (0)