Use CompactString for Identifier (#12101)

This commit is contained in:
Micha Reiser 2024-07-01 10:06:02 +02:00 committed by GitHub
parent db6ee74cbe
commit 5109b50bb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
474 changed files with 4953 additions and 4776 deletions

View file

@ -4,6 +4,7 @@ use std::ops::Deref;
use bitflags::bitflags;
use rustc_hash::{FxBuildHasher, FxHashSet};
use ruff_python_ast::name::Name;
use ruff_python_ast::{
self as ast, BoolOp, CmpOp, ConversionFlag, Expr, ExprContext, FStringElement, FStringElements,
IpyEscapeKind, Number, Operator, UnaryOp,
@ -477,14 +478,11 @@ impl<'src> Parser<'src> {
let TokenValue::Name(name) = self.bump_value(TokenKind::Name) else {
unreachable!();
};
return ast::Identifier {
id: name.into_string(),
range,
};
return ast::Identifier { id: name, range };
}
if self.current_token_kind().is_soft_keyword() {
let id = self.src_text(range).to_string();
let id = Name::new(self.src_text(range));
self.bump_soft_keyword_as_name();
return ast::Identifier { id, range };
}
@ -499,7 +497,7 @@ impl<'src> Parser<'src> {
range,
);
let id = self.src_text(range).to_string();
let id = Name::new(self.src_text(range));
self.bump_any();
ast::Identifier { id, range }
} else {
@ -509,7 +507,7 @@ impl<'src> Parser<'src> {
);
ast::Identifier {
id: String::new(),
id: Name::empty(),
range: self.missing_node_range(),
}
}
@ -597,7 +595,7 @@ impl<'src> Parser<'src> {
);
Expr::Name(ast::ExprName {
range: self.missing_node_range(),
id: String::new(),
id: Name::empty(),
ctx: ExprContext::Invalid,
})
}
@ -719,7 +717,7 @@ impl<'src> Parser<'src> {
&parsed_expr,
);
ast::Identifier {
id: String::new(),
id: Name::empty(),
range: parsed_expr.range(),
}
};
@ -793,7 +791,7 @@ impl<'src> Parser<'src> {
value: Box::new(value),
slice: Box::new(Expr::Name(ast::ExprName {
range: slice_range,
id: String::new(),
id: Name::empty(),
ctx: ExprContext::Invalid,
})),
ctx: ExprContext::Load,

View file

@ -1,3 +1,4 @@
use ruff_python_ast::name::Name;
use ruff_python_ast::{self as ast, Expr, ExprContext, Number, Operator, Pattern, Singleton};
use ruff_text_size::{Ranged, TextSize};
@ -510,7 +511,7 @@ impl<'src> Parser<'src> {
);
let invalid_node = Expr::Name(ast::ExprName {
range: self.missing_node_range(),
id: String::new(),
id: Name::empty(),
ctx: ExprContext::Invalid,
});
Pattern::MatchValue(ast::PatternMatchValue {
@ -616,7 +617,7 @@ impl<'src> Parser<'src> {
} else {
Box::new(Expr::Name(ast::ExprName {
range: ident.range(),
id: String::new(),
id: Name::empty(),
ctx: ExprContext::Invalid,
}))
}
@ -667,7 +668,7 @@ impl<'src> Parser<'src> {
&pattern,
);
ast::Identifier {
id: String::new(),
id: Name::empty(),
range: parser.missing_node_range(),
}
};

View file

@ -1,3 +1,4 @@
use ruff_python_ast::name::Name;
use ruff_python_ast::{self as ast, Expr, ExprContext, Pattern};
use ruff_text_size::{Ranged, TextLen, TextRange};
@ -110,7 +111,7 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr {
range,
value: Box::new(Expr::Name(ast::ExprName {
range: TextRange::new(range.end() - "_".text_len(), range.end()),
id: "_".to_string(),
id: Name::new_static("_"),
ctx: ExprContext::Store,
})),
ctx: ExprContext::Store,
@ -124,7 +125,7 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr {
}) => match (pattern, name) {
(Some(_), Some(_)) => Expr::Name(ast::ExprName {
range,
id: String::new(),
id: Name::empty(),
ctx: ExprContext::Invalid,
}),
(Some(pattern), None) => pattern_to_expr(*pattern),
@ -135,7 +136,7 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr {
}),
(None, None) => Expr::Name(ast::ExprName {
range,
id: "_".to_string(),
id: Name::new_static("_"),
ctx: ExprContext::Store,
}),
},

View file

@ -1,11 +1,11 @@
---
source: crates/ruff_python_parser/src/parser/tests.rs
expression: expr
expression: parsed.expr()
---
Name(
ExprName {
range: 0..5,
id: "first",
id: Name("first"),
ctx: Load,
},
)

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser/tests.rs
expression: parse_ast
expression: parsed.syntax()
---
Module(
ModModule {
@ -15,7 +15,7 @@ Module(
left: Name(
ExprName {
range: 27..28,
id: "a",
id: Name("a"),
ctx: Load,
},
),
@ -23,7 +23,7 @@ Module(
right: Name(
ExprName {
range: 39..40,
id: "b",
id: Name("b"),
ctx: Load,
},
),
@ -128,7 +128,7 @@ Module(
is_async: false,
decorator_list: [],
name: Identifier {
id: "foo",
id: Name("foo"),
range: 570..573,
},
type_params: None,
@ -152,7 +152,7 @@ Module(
left: Name(
ExprName {
range: 598..599,
id: "a",
id: Name("a"),
ctx: Load,
},
),
@ -163,7 +163,7 @@ Module(
Name(
ExprName {
range: 619..620,
id: "b",
id: Name("b"),
ctx: Load,
},
),
@ -204,7 +204,7 @@ Module(
target: Name(
ExprName {
range: 715..716,
id: "a",
id: Name("a"),
ctx: Store,
},
),
@ -214,7 +214,7 @@ Module(
func: Name(
ExprName {
range: 720..725,
id: "range",
id: Name("range"),
ctx: Load,
},
),
@ -253,7 +253,7 @@ Module(
Name(
ExprName {
range: 739..741,
id: "p1",
id: Name("p1"),
ctx: Store,
},
),
@ -273,14 +273,14 @@ Module(
target: Name(
ExprName {
range: 749..751,
id: "p2",
id: Name("p2"),
ctx: Store,
},
),
annotation: Name(
ExprName {
range: 753..756,
id: "str",
id: Name("str"),
ctx: Load,
},
),
@ -303,7 +303,7 @@ Module(
Name(
ExprName {
range: 764..767,
id: "foo",
id: Name("foo"),
ctx: Store,
},
),
@ -331,7 +331,7 @@ Module(
Name(
ExprName {
range: 792..795,
id: "foo",
id: Name("foo"),
ctx: Store,
},
),

View file

@ -1,6 +1,6 @@
---
source: crates/ruff_python_parser/src/parser/tests.rs
expression: parse_ast
expression: suite
---
[
Assign(
@ -10,7 +10,7 @@ expression: parse_ast
Name(
ExprName {
range: 0..1,
id: "x",
id: Name("x"),
ctx: Store,
},
),

View file

@ -1,7 +1,9 @@
use compact_str::CompactString;
use std::fmt::Display;
use rustc_hash::{FxBuildHasher, FxHashSet};
use ruff_python_ast::name::Name;
use ruff_python_ast::{
self as ast, ExceptHandler, Expr, ExprContext, IpyEscapeKind, Operator, Stmt, WithItem,
};
@ -623,7 +625,7 @@ impl<'src> Parser<'src> {
let range = self.node_range(start);
return ast::Alias {
name: ast::Identifier {
id: "*".into(),
id: Name::new_static("*"),
range,
},
asname: None,
@ -669,7 +671,7 @@ impl<'src> Parser<'src> {
fn parse_dotted_name(&mut self) -> ast::Identifier {
let start = self.node_start();
let mut dotted_name = self.parse_identifier().id;
let mut dotted_name: CompactString = self.parse_identifier().id.into();
let mut progress = ParserProgress::default();
while self.eat(TokenKind::Dot) {
@ -686,7 +688,7 @@ impl<'src> Parser<'src> {
// import a.b.c
// import a . b . c
ast::Identifier {
id: dotted_name,
id: Name::from(dotted_name),
range: self.node_range(start),
}
}