Skip to content

Commit

Permalink
Add static lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
jackh726 committed Oct 3, 2020
1 parent c74da51 commit b276130
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 72 deletions.
13 changes: 2 additions & 11 deletions chalk-engine/src/slg/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,23 +422,14 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
fn aggregate_lifetimes(&mut self, l1: &Lifetime<I>, l2: &Lifetime<I>) -> Lifetime<I> {
let interner = self.interner;
match (l1.data(interner), l2.data(interner)) {
(LifetimeData::InferenceVar(_), _) | (_, LifetimeData::InferenceVar(_)) => {
self.new_lifetime_variable()
}

(LifetimeData::BoundVar(_), _) | (_, LifetimeData::BoundVar(_)) => {
self.new_lifetime_variable()
}

(LifetimeData::Placeholder(_), LifetimeData::Placeholder(_)) => {
(LifetimeData::Phantom(..), _) | (_, LifetimeData::Phantom(..)) => unreachable!(),
_ => {
if l1 == l2 {
l1.clone()
} else {
self.new_lifetime_variable()
}
}

(LifetimeData::Phantom(..), _) | (_, LifetimeData::Phantom(..)) => unreachable!(),
}
}

Expand Down
7 changes: 5 additions & 2 deletions chalk-engine/src/slg/resolvent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ impl<'i, I: Interner> Zipper<'i, I> for AnswerSubstitutor<'i, I> {
self.assert_matching_vars(*answer_depth, *pending_depth)
}

(LifetimeData::Placeholder(_), LifetimeData::Placeholder(_)) => {
(LifetimeData::Static, LifetimeData::Static)
| (LifetimeData::Placeholder(_), LifetimeData::Placeholder(_)) => {
assert_eq!(answer, pending);
Ok(())
}
Expand All @@ -455,7 +456,9 @@ impl<'i, I: Interner> Zipper<'i, I> for AnswerSubstitutor<'i, I> {
answer, pending,
),

(LifetimeData::BoundVar(_), _) | (LifetimeData::Placeholder(_), _) => panic!(
(LifetimeData::Static, _)
| (LifetimeData::BoundVar(_), _)
| (LifetimeData::Placeholder(_), _) => panic!(
"structural mismatch between answer `{:?}` and pending goal `{:?}`",
answer, pending,
),
Expand Down
4 changes: 4 additions & 0 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@ impl LowerWithEnv for Lifetime {
}
})
}
Lifetime::Static => Ok(chalk_ir::Lifetime::new(
interner,
chalk_ir::LifetimeData::Static,
)),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ impl<I: Interner> Debug for LifetimeData<I> {
LifetimeData::BoundVar(db) => write!(fmt, "'{:?}", db),
LifetimeData::InferenceVar(var) => write!(fmt, "'{:?}", var),
LifetimeData::Placeholder(index) => write!(fmt, "'{:?}", index),
LifetimeData::Static => write!(fmt, "'static"),
LifetimeData::Phantom(..) => unreachable!(),
}
}
Expand Down
1 change: 1 addition & 0 deletions chalk-ir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ where
LifetimeData::Placeholder(universe) => {
folder.fold_free_placeholder_lifetime(*universe, outer_binder)
}
LifetimeData::Static => Ok(LifetimeData::<TI>::Static.intern(folder.target_interner())),
LifetimeData::Phantom(..) => unreachable!(),
}
}
Expand Down
3 changes: 3 additions & 0 deletions chalk-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ impl<I: Interner> Lifetime<I> {
LifetimeData::BoundVar(_) => true,
LifetimeData::InferenceVar(_) => false,
LifetimeData::Placeholder(_) => false,
LifetimeData::Static => false,
LifetimeData::Phantom(..) => unreachable!(),
}
}
Expand All @@ -1078,6 +1079,8 @@ pub enum LifetimeData<I: Interner> {
InferenceVar(InferenceVar),
/// Lifetime on some yet-unknown placeholder.
Placeholder(PlaceholderIndex),
/// Static lifetime
Static,
/// Lifetime on phantom data.
Phantom(Void, PhantomData<I>),
}
Expand Down
1 change: 1 addition & 0 deletions chalk-ir/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ impl<I: Interner> SuperVisit<I> for Lifetime<I> {
LifetimeData::Placeholder(universe) => {
visitor.visit_free_placeholder(*universe, outer_binder)
}
LifetimeData::Static => R::new(),
LifetimeData::Phantom(..) => unreachable!(),
}
}
Expand Down
1 change: 1 addition & 0 deletions chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ impl Default for Safety {
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Lifetime {
Id { name: Identifier },
Static,
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down
1 change: 1 addition & 0 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ RawMutability: Mutability = {

Lifetime: Lifetime = {
<n:LifetimeId> => Lifetime::Id { name: n },
"'static" => Lifetime::Static,
};

ConstWithoutId: Const = {
Expand Down
1 change: 1 addition & 0 deletions chalk-solve/src/display/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ impl<I: Interner> RenderAsRust<I> for LifetimeData<I> {
LifetimeData::Placeholder(ix) => {
write!(f, "'_placeholder_{}_{}", ix.ui.counter, ix.idx)
}
LifetimeData::Static => write!(f, "'static"),
// Matching the void ensures at compile time that this code is
// unreachable
LifetimeData::Phantom(void, _) => match *void {},
Expand Down
14 changes: 13 additions & 1 deletion chalk-solve/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,19 @@ impl<'t, I: Interner> Unifier<'t, I> {
self.unify_lifetime_var(a, b, b_var, a, a_idx.ui)
}

(&LifetimeData::Placeholder(_), &LifetimeData::Placeholder(_)) => {
(&LifetimeData::InferenceVar(a_var), &LifetimeData::Static) => {
self.unify_lifetime_var(a, b, a_var, b, UniverseIndex::root())
}

(&LifetimeData::Static, &LifetimeData::InferenceVar(b_var)) => {
self.unify_lifetime_var(a, b, b_var, a, UniverseIndex::root())
}

(&LifetimeData::Static, &LifetimeData::Static) => Ok(()),

(&LifetimeData::Static, &LifetimeData::Placeholder(_))
| (&LifetimeData::Placeholder(_), &LifetimeData::Static)
| (&LifetimeData::Placeholder(_), &LifetimeData::Placeholder(_)) => {
if a != b {
Ok(self.push_lifetime_eq_goals(a.clone(), b.clone()))
} else {
Expand Down
Loading

0 comments on commit b276130

Please sign in to comment.