Generify CST/Cursor/Query#930
Generify CST/Cursor/Query#930AntonyBlakey wants to merge 1 commit intoNomicFoundation:mainfrom AntonyBlakey:AntonyBlakey/generify-cst-cursor-query
Conversation
|
|
|
||
| mod metaslang_cst { | ||
| #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)] // But why? | ||
| pub struct KindTypes; |
There was a problem hiding this comment.
Whenever we want to use types as markers, it's slightly better to use an uninhabitable enum pub enum KindTypes {} as this makes it impossible to construct the value (by mistake), but can still be used in the generic argument position.
crates/metaslang/cst/src/cst.rs
Outdated
| let text_len = children.iter().map(|node| node.text_len()).sum(); | ||
|
|
||
| Self::Rule(Rc::new(RuleNode { | ||
| Self::Rule(Rc::new(NonTerminalNode::<T> { |
There was a problem hiding this comment.
Almost all of the types can be easily inferred in the return position; specifying turbofish ::<T> here is, I feel, adding unnecessary visual noise that distract slightly from the actual logic.
There was a problem hiding this comment.
The issue is that 'almost all' comment. I prefer a more regular, obvious form that doesn't require the reader to be aware of just when the type can be inferred. WDYT?
There was a problem hiding this comment.
but done after f2f
crates/metaslang/cst/src/cst.rs
Outdated
| pub fn token(kind: TokenKind, text: String) -> Self { | ||
| Self::Token(Rc::new(TokenNode { kind, text })) | ||
| pub fn token(kind: T::TerminalKind, text: String) -> Self { | ||
| Self::Token(Rc::new(TerminalNode::<T> { kind, text })) |
There was a problem hiding this comment.
ditto here and in all of the similar places
There was a problem hiding this comment.
as above, so below
| + std::fmt::Display | ||
| + std::fmt::Debug | ||
| + serde::Serialize | ||
| + for<'a> std::convert::TryFrom<&'a str, Error = strum::ParseError> |
There was a problem hiding this comment.
Instead of relying on strum in our public API, and forcing users to use it, I wonder if it is possible to use the standard std::str::FromStr instead, and keep strum as an internal implementation detail.
There was a problem hiding this comment.
It has to be TryFrom because it is a partial function.
There was a problem hiding this comment.
FromStr also returns a Result<>:
https://doc.rust-lang.org/std/str/trait.FromStr.html
pub trait FromStr: Sized {
type Err;
fn from_str(s: &str) -> Result<Self, Self::Err>;
}There was a problem hiding this comment.
and is also typically used for many other rust types via "value".parse().
For us, we can use #[derive(strum::EnumString)] which auto-derives std::str::FromStr on the enum, but that is an implementation detail.
There was a problem hiding this comment.
the problem is that the Err associated type in FromStr needs to implement std::fmt::Debug, but it isn't possible to constrain the associated type using stable Rust.
There was a problem hiding this comment.
Either you or @Xanewok may have a solution - the only one I have propagates the constraint on the associated type all through the codebase.
There was a problem hiding this comment.
I see. What do you think about simply using String as the error type for FromStr? this also has the advantage of providing an actual error message, instead of the (only) Unit variant strum::ParseError::VariantNotFound. The API will then be idiomatic, without exposing/depending on the highly-specific strum.
Not blocking though if it is not feasible. Just a minor suggestion.
There was a problem hiding this comment.
I tried that, but no joy, because the specification of the Err type still causes difficulties. My preference is to use an associated type constraint, as soon as it is stabilised. It is a significant improvement to the type system.
There was a problem hiding this comment.
I'll take quick look if we can simplify this.
crates/codegen/language/tests/src/fail/parsing/duplicate_map_key/test.stderr
Outdated
Show resolved
Hide resolved
OmarTawfik
left a comment
There was a problem hiding this comment.
Looking great. I will rebase this on the latest main and send you an update!
Thanks.
This rebases NomicFoundation#930 after applying recent infra changes.
This rebases NomicFoundation#930 after applying recent infra changes.
This rebases NomicFoundation#930 after applying recent infra changes.
This rebases #930 after applying recent infra changes.
Pull request was closed
No description provided.