Skip to content

Commit 63e792b

Browse files
committed
have parse_variant apply an upcast
It's silly that it did not.
1 parent efb8dd1 commit 63e792b

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

crates/formality-core/src/cast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ macro_rules! cast_impl {
387387

388388
cast_impl!(usize);
389389
cast_impl!(u32);
390+
cast_impl!(u64);
390391
cast_impl!(String);
391392

392393
impl UpcastFrom<&str> for String {

crates/formality-core/src/parse.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use crate::{
44
binder::CoreBinder,
5+
cast_impl,
56
collections::Set,
67
language::{CoreKind, Language},
78
set,
@@ -14,7 +15,7 @@ use std::fmt::Debug;
1415
/// Trait for parsing a [`Term<L>`](`crate::term::Term`) as input.
1516
/// Typically this is auto-generated with the `#[term]` procedural macro,
1617
/// but you can implement it by hand if you want a very customized parse.
17-
pub trait CoreParse<L: Language>: Sized + Debug + Clone + Eq + 'static {
18+
pub trait CoreParse<L: Language>: Sized + Debug + Clone + Eq + 'static + Upcast<Self> {
1819
/// Parse a single instance of this type, returning an error if no such
1920
/// instance is present.
2021
///
@@ -211,6 +212,8 @@ pub struct Binding<L: Language> {
211212
pub bound_var: CoreBoundVar<L>,
212213
}
213214

215+
cast_impl!(impl(L: Language) Binding<L>);
216+
214217
impl<L, T> CoreParse<L> for Vec<T>
215218
where
216219
L: Language,

crates/formality-core/src/parse/parser.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158
impl<'s, 't, T, L> Parser<'s, 't, T, L>
159159
where
160160
L: Language,
161-
T: Debug + Clone + Eq + 'static,
161+
T: Debug + Clone + Eq + 'static + Upcast<T>,
162162
{
163163
/// Shorthand to create a parser for a nonterminal with a single variant,
164164
/// parsed by the function `op`.
@@ -225,7 +225,7 @@ where
225225
Self::parse_variant(self, variant_name, variant_precedence, |p| {
226226
p.mark_as_cast_variant();
227227
let v: V = p.nonterminal()?;
228-
Ok(v.upcast())
228+
Ok(v)
229229
})
230230
}
231231

@@ -234,12 +234,14 @@ where
234234
/// The precedence is part of how we resolve conflicts: if there are two successful parses with distinct precedence, higher precedence wins.
235235
/// The `op` is a closure that defines how the variant itself is parsed.
236236
/// The closure `op` will be invoked with an [`ActiveVariant`][] struct that has methods for consuming identifiers, numbers, keywords, etc.
237-
pub fn parse_variant(
237+
pub fn parse_variant<U>(
238238
&mut self,
239239
variant_name: &'static str,
240240
variant_precedence: Precedence,
241-
op: impl FnOnce(&mut ActiveVariant<'s, 't, L>) -> Result<T, Set<ParseError<'t>>>,
242-
) {
241+
op: impl FnOnce(&mut ActiveVariant<'s, 't, L>) -> Result<U, Set<ParseError<'t>>>,
242+
) where
243+
U: Upcast<T>,
244+
{
243245
let span = tracing::span!(
244246
tracing::Level::TRACE,
245247
"variant",
@@ -277,7 +279,7 @@ where
277279
text: active_variant.current_text,
278280
reductions: active_variant.reductions,
279281
precedence: variant_precedence,
280-
value,
282+
value: value.upcast(),
281283
});
282284
tracing::trace!("success: {:?}", self.successes.last().unwrap());
283285
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ impl CoreParse<Rust> for RigidTy {
4545
Ok(RigidTy {
4646
name: RigidName::Ref(RefKind::Shared),
4747
parameters: seq![lt.upcast(), ty.upcast()],
48-
}
49-
.upcast())
48+
})
5049
});
5150

5251
parser.parse_variant("RefMut", Precedence::default(), |p| {
@@ -125,7 +124,7 @@ impl CoreParse<Rust> for ConstData {
125124
fn parse<'t>(scope: &Scope<Rust>, text: &'t str) -> ParseResult<'t, Self> {
126125
Parser::multi_variant(scope, text, "ConstData", |parser| {
127126
parser.parse_variant("Variable", Precedence::default(), |p| {
128-
p.variable_of_kind(ParameterKind::Const).upcast()
127+
p.variable_of_kind(ParameterKind::Const)
129128
});
130129

131130
parser.parse_variant_cast::<Bool>(Precedence::default());

0 commit comments

Comments
 (0)