mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-20 19:52:48 +00:00
cleanup casts
This commit is contained in:
parent
8718a47088
commit
5c594bcb48
@ -25,6 +25,12 @@ impl<A, B> Either<A, B> {
|
||||
Either::B(b) => Either::B(f2(b)),
|
||||
}
|
||||
}
|
||||
pub fn map_a<U, F>(self, f: F) -> Either<U, B>
|
||||
where
|
||||
F: FnOnce(A) -> U,
|
||||
{
|
||||
self.map(f, |it| it)
|
||||
}
|
||||
pub fn a(self) -> Option<A> {
|
||||
match self {
|
||||
Either::A(it) => Some(it),
|
||||
|
@ -309,15 +309,11 @@ impl SourceAnalyzer {
|
||||
crate::Resolution::LocalBinding(it) => {
|
||||
// We get a `PatId` from resolver, but it actually can only
|
||||
// point at `BindPat`, and not at the arbitrary pattern.
|
||||
let pat_ptr = self.body_source_map.as_ref()?.pat_syntax(it)?;
|
||||
let pat_ptr = match pat_ptr {
|
||||
Either::A(pat) => {
|
||||
let pat: AstPtr<ast::BindPat> =
|
||||
pat.cast_checking_kind(|kind| kind == BIND_PAT).unwrap();
|
||||
Either::A(pat)
|
||||
}
|
||||
Either::B(self_param) => Either::B(self_param),
|
||||
};
|
||||
let pat_ptr = self
|
||||
.body_source_map
|
||||
.as_ref()?
|
||||
.pat_syntax(it)?
|
||||
.map_a(|ptr| ptr.cast::<ast::BindPat>().unwrap());
|
||||
PathResolution::LocalBinding(pat_ptr)
|
||||
}
|
||||
crate::Resolution::GenericParam(it) => PathResolution::GenericParam(it),
|
||||
|
@ -10,7 +10,7 @@ use std::marker::PhantomData;
|
||||
|
||||
use crate::{
|
||||
syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
|
||||
SmolStr,
|
||||
SmolStr, SyntaxKind,
|
||||
};
|
||||
|
||||
pub use self::{
|
||||
@ -26,6 +26,8 @@ pub use self::{
|
||||
/// the same representation: a pointer to the tree root and a pointer to the
|
||||
/// node itself.
|
||||
pub trait AstNode: Clone {
|
||||
fn can_cast(kind: SyntaxKind) -> bool;
|
||||
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -12,18 +12,34 @@ the below applies to the result of this template
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
use crate::{
|
||||
SyntaxNode, SyntaxKind::*,
|
||||
SyntaxNode, SyntaxKind::{self, *},
|
||||
ast::{self, AstNode},
|
||||
};
|
||||
{% for node, methods in ast %}
|
||||
// {{ node }}
|
||||
|
||||
{%- if methods.enum %}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct {{ node }} {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
|
||||
impl AstNode for {{ node }} {
|
||||
fn can_cast(kind: SyntaxKind) -> bool {
|
||||
match kind {
|
||||
{%- if methods.enum %}
|
||||
{% for kind in methods.enum %} | {{ kind | SCREAM }} {%- endfor -%}
|
||||
{% else %}
|
||||
{{ node | SCREAM }}
|
||||
{%- endif %} => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
if Self::can_cast(syntax.kind()) { Some({{ node }} { syntax }) } else { None }
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
|
||||
{% if methods.enum %}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum {{ node }}Kind {
|
||||
{%- for kind in methods.enum %}
|
||||
@ -33,25 +49,9 @@ pub enum {{ node }}Kind {
|
||||
|
||||
{%- for kind in methods.enum %}
|
||||
impl From<{{ kind }}> for {{ node }} {
|
||||
fn from(n: {{ kind }}) -> {{ node }} {
|
||||
{{ node }}::cast(n.syntax).unwrap()
|
||||
}
|
||||
fn from(n: {{ kind }}) -> {{ node }} { {{ node }} { syntax: n.syntax } }
|
||||
}
|
||||
{%- endfor %}
|
||||
|
||||
|
||||
impl AstNode for {{ node }} {
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
{%- for kind in methods.enum %}
|
||||
| {{ kind | SCREAM }}
|
||||
{%- endfor %} => Some({{ node }} { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
|
||||
impl {{ node }} {
|
||||
pub fn kind(&self) -> {{ node }}Kind {
|
||||
match self.syntax.kind() {
|
||||
@ -62,22 +62,6 @@ impl {{ node }} {
|
||||
}
|
||||
}
|
||||
}
|
||||
{% else %}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct {{ node }} {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
|
||||
impl AstNode for {{ node }} {
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
{{ node | SCREAM }} => Some({{ node }} { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
|
||||
{% endif %}
|
||||
{% if methods.traits -%}
|
||||
|
||||
|
@ -61,12 +61,8 @@ impl<N: AstNode> AstPtr<N> {
|
||||
self.raw
|
||||
}
|
||||
|
||||
// FIXME: extend AstNode to do this safely
|
||||
pub fn cast_checking_kind<U: AstNode>(
|
||||
self,
|
||||
cond: impl FnOnce(SyntaxKind) -> bool,
|
||||
) -> Option<AstPtr<U>> {
|
||||
if !cond(self.raw.kind()) {
|
||||
pub fn cast<U: AstNode>(self) -> Option<AstPtr<U>> {
|
||||
if !U::can_cast(self.raw.kind()) {
|
||||
return None;
|
||||
}
|
||||
Some(AstPtr { raw: self.raw, _ty: PhantomData })
|
||||
|
Loading…
Reference in New Issue
Block a user