Skip to content

Commit 1d94403

Browse files
committed
make Lt use #[term]
1 parent 449c1e3 commit 1d94403

File tree

4 files changed

+8
-97
lines changed

4 files changed

+8
-97
lines changed

crates/formality-types/src/grammar/ty.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ pub enum Variance {
281281
Invariant,
282282
}
283283

284-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
284+
#[term]
285+
#[cast]
285286
pub struct Lt {
286287
data: Arc<LtData>,
287288
}
@@ -321,22 +322,12 @@ impl DowncastTo<LtData> for Lt {
321322
}
322323
}
323324

324-
impl UpcastFrom<LtData> for Parameter {
325-
fn upcast_from(v: LtData) -> Self {
326-
Lt::new(v).upcast()
327-
}
328-
}
329-
330-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
325+
#[term]
331326
pub enum LtData {
332327
Static,
333-
Variable(Variable),
334-
}
335328

336-
impl UpcastFrom<LtData> for LtData {
337-
fn upcast_from(term: LtData) -> Self {
338-
term
339-
}
329+
#[variable]
330+
Variable(Variable),
340331
}
341332

342333
impl UpcastFrom<Variable> for Parameter {
@@ -375,11 +366,10 @@ cast_impl!((BoundVar) <: (Variable) <: (Ty));
375366
cast_impl!((UniversalVar) <: (Variable) <: (Parameter));
376367
cast_impl!((ExistentialVar) <: (Variable) <: (Parameter));
377368
cast_impl!((BoundVar) <: (Variable) <: (Parameter));
378-
cast_impl!(Lt);
379-
cast_impl!(LtData::Variable(Variable));
380369
cast_impl!((ExistentialVar) <: (Variable) <: (LtData));
381370
cast_impl!((UniversalVar) <: (Variable) <: (LtData));
382371
cast_impl!((BoundVar) <: (Variable) <: (LtData));
383372
cast_impl!((UniversalVar) <: (LtData) <: (Lt));
384373
cast_impl!((ExistentialVar) <: (LtData) <: (Lt));
385374
cast_impl!((BoundVar) <: (LtData) <: (Lt));
375+
cast_impl!((LtData) <: (Lt) <: (Parameter));

crates/formality-types/src/grammar/ty/debug_impls.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,3 @@ impl std::fmt::Debug for Const {
88
}
99
}
1010
}
11-
12-
impl std::fmt::Debug for super::Lt {
13-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14-
match self.data() {
15-
super::LtData::Static => write!(f, "static"),
16-
super::LtData::Variable(v) => write!(f, "{:?}", v),
17-
}
18-
}
19-
}

crates/formality-types/src/grammar/ty/parse_impls.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use formality_core::{seq, Set};
66

77
use crate::grammar::{AdtId, AssociatedItemId, Bool, Const, RefKind, RigidName, Scalar, TraitId};
88

9-
use super::{AliasTy, AssociatedTyName, Lt, LtData, Parameter, RigidTy, ScalarId, Ty};
9+
use super::{AliasTy, AssociatedTyName, Lt, Parameter, RigidTy, ScalarId, Ty};
1010

1111
use crate::rust::FormalityLang as Rust;
1212

@@ -104,18 +104,6 @@ fn parse_parameters<'t>(
104104
Ok(parameters)
105105
}
106106

107-
impl CoreParse<Rust> for Lt {
108-
fn parse<'t>(scope: &Scope<Rust>, text0: &'t str) -> ParseResult<'t, Self> {
109-
let mut parser = Parser::new(scope, text0, "Lt");
110-
parser.parse_variant("static", 0, |p| {
111-
p.expect_keyword("static")?;
112-
Ok(Lt::new(LtData::Static))
113-
});
114-
parser.parse_variant("variable", 0, |p| p.variable());
115-
parser.finish()
116-
}
117-
}
118-
119107
// For consts, we invest some effort into parsing them decently because it makes
120108
// writing tests so much more pleasant.
121109
impl CoreParse<Rust> for Const {
Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
1-
use crate::grammar::{Const, ConstData, Lt, LtData, Parameter, ValTree};
2-
use crate::rust::Variable;
1+
use crate::grammar::{Const, ConstData, Parameter, ValTree};
32
use crate::FormalityLang;
43
use formality_core::{
54
fold::{CoreFold, SubstitutionFn},
65
language::CoreKind,
7-
visit::CoreVisit,
86
};
97

108
use super::ParameterKind;
119

12-
impl CoreVisit<FormalityLang> for LtData {
13-
fn free_variables(&self) -> Vec<Variable> {
14-
match self {
15-
LtData::Variable(v) => {
16-
if v.is_free() {
17-
vec![*v]
18-
} else {
19-
vec![]
20-
}
21-
}
22-
LtData::Static => vec![],
23-
}
24-
}
25-
26-
fn size(&self) -> usize {
27-
match self {
28-
LtData::Variable(v) => v.size(),
29-
LtData::Static => 1,
30-
}
31-
}
32-
33-
fn assert_valid(&self) {
34-
match self {
35-
LtData::Variable(v) => v.assert_valid(),
36-
LtData::Static => (),
37-
}
38-
}
39-
}
40-
4110
impl formality_core::language::HasKind<FormalityLang> for Parameter {
4211
fn kind(&self) -> CoreKind<FormalityLang> {
4312
match self {
@@ -69,30 +38,3 @@ impl CoreFold<FormalityLang> for ValTree {
6938
self.clone()
7039
}
7140
}
72-
73-
impl CoreFold<FormalityLang> for Lt {
74-
fn substitute(&self, substitution_fn: SubstitutionFn<'_, FormalityLang>) -> Self {
75-
match self.data() {
76-
LtData::Static => self.clone(),
77-
LtData::Variable(v) => match substitution_fn(*v) {
78-
None => self.clone(),
79-
Some(Parameter::Lt(t)) => t,
80-
Some(param) => panic!("ill-kinded substitute: expected lifetime, got {param:?}"),
81-
},
82-
}
83-
}
84-
}
85-
86-
impl CoreVisit<FormalityLang> for Lt {
87-
fn free_variables(&self) -> Vec<Variable> {
88-
self.data().free_variables()
89-
}
90-
91-
fn size(&self) -> usize {
92-
self.data().size()
93-
}
94-
95-
fn assert_valid(&self) {
96-
self.data().assert_valid()
97-
}
98-
}

0 commit comments

Comments
 (0)