mirror of
https://github.com/astral-sh/ruff.git
synced 2025-12-10 03:59:33 +00:00
New Singleton enum for PatternMatchSingleton node (#8063)
## Summary This PR adds a new `Singleton` enum for the `PatternMatchSingleton` node. Earlier the node was using the `Constant` enum but the value for this pattern can only be either `None`, `True` or `False`. With the coming PR to remove the `Constant`, this node required a new type to fill in. This also has the benefit of narrowing the type down to only the possible values for the node as evident by the removal of `unreachable`. ## Test Plan Update the AST snapshots and run `cargo test`.
This commit is contained in:
parent
ee7d445ef5
commit
78bbf6d403
10 changed files with 70 additions and 28 deletions
|
|
@ -185,8 +185,8 @@ pub struct PatternMatchValue<'a> {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct PatternMatchSingleton<'a> {
|
||||
value: ComparableConstant<'a>,
|
||||
pub struct PatternMatchSingleton {
|
||||
value: ComparableSingleton,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
|
|
@ -227,7 +227,7 @@ pub struct PatternMatchOr<'a> {
|
|||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparablePattern<'a> {
|
||||
MatchValue(PatternMatchValue<'a>),
|
||||
MatchSingleton(PatternMatchSingleton<'a>),
|
||||
MatchSingleton(PatternMatchSingleton),
|
||||
MatchSequence(PatternMatchSequence<'a>),
|
||||
MatchMapping(PatternMatchMapping<'a>),
|
||||
MatchClass(PatternMatchClass<'a>),
|
||||
|
|
@ -326,6 +326,23 @@ impl<'a> From<&'a ast::Decorator> for ComparableDecorator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableSingleton {
|
||||
None,
|
||||
True,
|
||||
False,
|
||||
}
|
||||
|
||||
impl From<&ast::Singleton> for ComparableSingleton {
|
||||
fn from(singleton: &ast::Singleton) -> Self {
|
||||
match singleton {
|
||||
ast::Singleton::None => Self::None,
|
||||
ast::Singleton::True => Self::True,
|
||||
ast::Singleton::False => Self::False,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableConstant<'a> {
|
||||
None,
|
||||
|
|
|
|||
|
|
@ -3139,7 +3139,7 @@ impl AstNode for ast::PatternMatchSingleton {
|
|||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
let ast::PatternMatchSingleton { value, range: _ } = self;
|
||||
visitor.visit_constant(value);
|
||||
visitor.visit_singleton(value);
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::PatternMatchSequence {
|
||||
|
|
|
|||
|
|
@ -1923,7 +1923,7 @@ impl From<PatternMatchValue> for Pattern {
|
|||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PatternMatchSingleton {
|
||||
pub range: TextRange,
|
||||
pub value: Constant,
|
||||
pub value: Singleton,
|
||||
}
|
||||
|
||||
impl From<PatternMatchSingleton> for Pattern {
|
||||
|
|
@ -2578,6 +2578,23 @@ impl Ranged for Identifier {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Singleton {
|
||||
None,
|
||||
True,
|
||||
False,
|
||||
}
|
||||
|
||||
impl From<bool> for Singleton {
|
||||
fn from(value: bool) -> Self {
|
||||
if value {
|
||||
Singleton::True
|
||||
} else {
|
||||
Singleton::False
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum Constant {
|
||||
None,
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use crate::{
|
||||
Alias, Arguments, BoolOp, CmpOp, Comprehension, Constant, Decorator, ElifElseClause,
|
||||
ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Parameter, ParameterWithDefault,
|
||||
Parameters, Pattern, PatternArguments, PatternKeyword, Stmt, TypeParam, TypeParams, UnaryOp,
|
||||
WithItem,
|
||||
Parameters, Pattern, PatternArguments, PatternKeyword, Singleton, Stmt, TypeParam, TypeParams,
|
||||
UnaryOp, WithItem,
|
||||
};
|
||||
use crate::{AnyNodeRef, AstNode};
|
||||
|
||||
|
|
@ -44,6 +44,9 @@ pub trait PreorderVisitor<'a> {
|
|||
#[inline]
|
||||
fn visit_constant(&mut self, _constant: &'a Constant) {}
|
||||
|
||||
#[inline]
|
||||
fn visit_singleton(&mut self, _singleton: &'a Singleton) {}
|
||||
|
||||
#[inline]
|
||||
fn visit_bool_op(&mut self, bool_op: &'a BoolOp) {
|
||||
walk_bool_op(self, bool_op);
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use ruff_python_ast::visitor::preorder::{
|
|||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{
|
||||
Alias, BoolOp, CmpOp, Comprehension, Constant, ExceptHandler, Expr, Keyword, MatchCase, Mod,
|
||||
Operator, Parameter, Parameters, Pattern, Stmt, TypeParam, UnaryOp, WithItem,
|
||||
Operator, Parameter, Parameters, Pattern, Singleton, Stmt, TypeParam, UnaryOp, WithItem,
|
||||
};
|
||||
use ruff_python_parser::lexer::lex;
|
||||
use ruff_python_parser::{parse_tokens, Mode};
|
||||
|
|
@ -197,6 +197,10 @@ impl PreorderVisitor<'_> for RecordVisitor {
|
|||
self.emit(&constant);
|
||||
}
|
||||
|
||||
fn visit_singleton(&mut self, singleton: &Singleton) {
|
||||
self.emit(&singleton);
|
||||
}
|
||||
|
||||
fn visit_bool_op(&mut self, bool_op: &BoolOp) {
|
||||
self.emit(&bool_op);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue