Skip to content

Commit 394ba0c

Browse files
committed
use #[term] for Const
1 parent 1d94403 commit 394ba0c

File tree

6 files changed

+34
-58
lines changed

6 files changed

+34
-58
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
mod valtree;
22

33
use super::{Parameter, Ty, Variable};
4-
use formality_core::{term, DowncastTo, Upcast, UpcastFrom, Visit};
4+
use formality_core::{term, DowncastTo, Upcast, UpcastFrom};
55
use std::sync::Arc;
66
pub use valtree::*;
77

8-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Visit)]
8+
#[term]
9+
#[cast]
910
pub struct Const {
1011
data: Arc<ConstData>,
1112
}
@@ -39,16 +40,14 @@ impl Const {
3940
}
4041
}
4142

42-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Visit)]
43+
#[term]
44+
#[customize(parse)]
4345
pub enum ConstData {
4446
Value(ValTree, Ty),
45-
Variable(Variable),
46-
}
4747

48-
impl UpcastFrom<Self> for ConstData {
49-
fn upcast_from(term: Self) -> Self {
50-
term
51-
}
48+
#[variable]
49+
#[precedence(1)]
50+
Variable(Variable),
5251
}
5352

5453
impl DowncastTo<ConstData> for Const {
@@ -81,15 +80,16 @@ pub enum Bool {
8180
False,
8281
}
8382

84-
impl UpcastFrom<Bool> for Const {
83+
impl UpcastFrom<Bool> for ConstData {
8584
fn upcast_from(term: Bool) -> Self {
86-
Self::new(ConstData::Value(term.upcast(), Ty::bool()))
85+
ConstData::Value(term.upcast(), Ty::bool())
8786
}
8887
}
8988

90-
impl UpcastFrom<Variable> for ConstData {
91-
fn upcast_from(v: Variable) -> Self {
92-
Self::Variable(v)
89+
impl UpcastFrom<Bool> for Const {
90+
fn upcast_from(term: Bool) -> Self {
91+
let c: ConstData = term.upcast();
92+
Const::new(c)
9393
}
9494
}
9595

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
use crate::grammar::Const;
21

3-
impl std::fmt::Debug for Const {
4-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5-
match self.data() {
6-
crate::grammar::ConstData::Value(valtree, ty) => write!(f, "{valtree:?}_{ty:?}"),
7-
crate::grammar::ConstData::Variable(r) => write!(f, "{r:?}"),
8-
}
9-
}
10-
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use formality_core::parse::{ActiveVariant, CoreParse, ParseError, ParseResult, P
44
use formality_core::Upcast;
55
use formality_core::{seq, Set};
66

7-
use crate::grammar::{AdtId, AssociatedItemId, Bool, Const, RefKind, RigidName, Scalar, TraitId};
7+
use crate::grammar::{
8+
AdtId, AssociatedItemId, Bool, ConstData, RefKind, RigidName, Scalar, TraitId, ValTree,
9+
};
810

911
use super::{AliasTy, AssociatedTyName, Lt, Parameter, RigidTy, ScalarId, Ty};
1012

@@ -106,22 +108,21 @@ fn parse_parameters<'t>(
106108

107109
// For consts, we invest some effort into parsing them decently because it makes
108110
// writing tests so much more pleasant.
109-
impl CoreParse<Rust> for Const {
111+
impl CoreParse<Rust> for ConstData {
110112
fn parse<'t>(scope: &Scope<Rust>, text: &'t str) -> ParseResult<'t, Self> {
111-
let mut parser: Parser<'_, '_, Const, Rust> = Parser::new(scope, text, "Ty");
112-
parser.parse_variant_cast::<Bool>(1);
113+
let mut parser: Parser<'_, '_, ConstData, Rust> = Parser::new(scope, text, "Ty");
114+
113115
parser.parse_variant("Variable", 1, |p| p.variable());
114-
parser.parse_variant("Int", 0, |p| p.nonterminal_with(parse_int));
116+
117+
parser.parse_variant_cast::<Bool>(1);
118+
119+
parser.parse_variant("Int", 0, |p| {
120+
let n: u128 = p.number()?;
121+
p.expect_char('_')?;
122+
let ty: Ty = p.nonterminal()?;
123+
Ok(ConstData::Value(Scalar::new(n).upcast(), ty))
124+
});
125+
115126
parser.finish()
116127
}
117128
}
118-
119-
#[tracing::instrument(level = "trace", ret)]
120-
fn parse_int<'t>(scope: &Scope<Rust>, text: &'t str) -> ParseResult<'t, Const> {
121-
Parser::single_variant(scope, text, "Ty", |p| {
122-
let n: u128 = p.number()?;
123-
p.expect_char('_')?;
124-
let ty: Ty = p.nonterminal()?;
125-
Ok(Const::valtree(Scalar::new(n), ty))
126-
})
127-
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@ impl formality_core::language::HasKind<FormalityLang> for Parameter {
1717
}
1818
}
1919

20-
impl CoreFold<FormalityLang> for Const {
21-
fn substitute(&self, substitution_fn: SubstitutionFn<'_, FormalityLang>) -> Self {
22-
match self.data() {
23-
ConstData::Value(v, ty) => Self::valtree(
24-
v.substitute(substitution_fn),
25-
ty.substitute(substitution_fn),
26-
),
27-
ConstData::Variable(v) => match substitution_fn(*v) {
28-
None => self.clone(),
29-
Some(Parameter::Const(c)) => c,
30-
Some(param) => panic!("ill-kinded substitute: expected const, got {param:?}"),
31-
},
32-
}
33-
}
34-
}
35-
3620
impl CoreFold<FormalityLang> for ValTree {
3721
fn substitute(&self, _substitution_fn: SubstitutionFn<'_, FormalityLang>) -> Self {
3822
self.clone()

tests/ui/consts/mismatch.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Error: check_trait_impl(impl <> Foo < const 42_(rigid (scalar u32)) > for (rigid (scalar u32)) where [] { })
1+
Error: check_trait_impl(impl <> Foo < const value(42, (rigid (scalar u32))) > for (rigid (scalar u32)) where [] { })
22

33
Caused by:
4-
failed to prove {Foo((rigid (scalar u32)), const 42_(rigid (scalar u32)))} given {}, got {}
4+
failed to prove {Foo((rigid (scalar u32)), const value(42, (rigid (scalar u32))))} given {}, got {}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Error: check_trait(Foo)
22

33
Caused by:
4-
0: prove_where_clause_well_formed(type_of_const 0_(rigid (scalar bool)) is (rigid (scalar u32)))
5-
1: failed to prove {(rigid (scalar u32)) = (rigid (scalar bool))} given {@ ConstHasType(0_(rigid (scalar bool)) , (rigid (scalar u32)))}, got {}
4+
0: prove_where_clause_well_formed(type_of_const value(0, (rigid (scalar bool))) is (rigid (scalar u32)))
5+
1: failed to prove {(rigid (scalar u32)) = (rigid (scalar bool))} given {@ ConstHasType(value(0, (rigid (scalar bool))) , (rigid (scalar u32)))}, got {}

0 commit comments

Comments
 (0)