implement AstNode
for Either
This commit is contained in:
parent
32be158630
commit
cfc01150bf
@ -1472,14 +1472,7 @@ fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_inside_unsafe(&self, expr: &ast::Expr) -> bool {
|
fn is_inside_unsafe(&self, expr: &ast::Expr) -> bool {
|
||||||
let item_or_variant = |ancestor: SyntaxNode| {
|
let Some(enclosing_item) = expr.syntax().ancestors().find_map(Either::<ast::Item, ast::Variant>::cast) else { return false };
|
||||||
if ast::Item::can_cast(ancestor.kind()) {
|
|
||||||
ast::Item::cast(ancestor).map(Either::Left)
|
|
||||||
} else {
|
|
||||||
ast::Variant::cast(ancestor).map(Either::Right)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let Some(enclosing_item) = expr.syntax().ancestors().find_map(item_or_variant) else { return false };
|
|
||||||
|
|
||||||
let def = match &enclosing_item {
|
let def = match &enclosing_item {
|
||||||
Either::Left(ast::Item::Fn(it)) if it.unsafe_token().is_some() => return true,
|
Either::Left(ast::Item::Fn(it)) if it.unsafe_token().is_some() => return true,
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use itertools::Either;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
|
syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
|
||||||
SyntaxKind,
|
SyntaxKind,
|
||||||
@ -98,6 +100,34 @@ fn next(&mut self) -> Option<N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<L, R> AstNode for Either<L, R>
|
||||||
|
where
|
||||||
|
L: AstNode,
|
||||||
|
R: AstNode,
|
||||||
|
{
|
||||||
|
fn can_cast(kind: SyntaxKind) -> bool
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
L::can_cast(kind) || R::can_cast(kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cast(syntax: SyntaxNode) -> Option<Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
if L::can_cast(syntax.kind()) {
|
||||||
|
L::cast(syntax).map(Either::Left)
|
||||||
|
} else {
|
||||||
|
R::cast(syntax).map(Either::Right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn syntax(&self) -> &SyntaxNode {
|
||||||
|
self.as_ref().either(L::syntax, R::syntax)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod support {
|
mod support {
|
||||||
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
|
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user