Files
ruff/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__raise.py.snap
Charlie Marsh 7b13f01ba1 [ty] Reduce retained AST memory by shrinking expressions (#28335)
## Summary

We reduce `Expr` from 64 to 56 bytes on 64-bit targets. The largest
variants determine the enum's size, so shrinking those variants saves
eight bytes in every `Expr` value, including expressions in the parsed
modules retained by ty.

The changes are:

- Store positional call arguments in `ThinVec`, reducing `Arguments` and
`ExprCall` by eight bytes.
- Store all comparison operands in one boxed slice. This removes the
separate left-operand allocation and reduces `ExprCompare` by eight
bytes while keeping operators in a boxed slice.
- Store dictionary-comprehension generators in a boxed slice, reducing
`ExprDictComp` by eight bytes.
- Store f-string and t-string elements in boxed slices, and represent a
single f-string directly as `FString`. This keeps single parts inline
and reduces `ExprFString` and `ExprTString` by eight bytes.

The parser, AST visitors, formatter, lint rules, and type inference use
the updated layouts. Size assertions cover the 56-byte enum and its
affected variants.
2026-09-10 15:39:14 -04:00

558 lines
23 KiB
Plaintext

---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/valid/statement/raise.py
---
## AST
```
Module(
ModModule {
node_index: NodeIndex(None),
range: 0..289,
body: [
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 8..13,
exc: None,
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 14..21,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 20..21,
id: Name("a"),
ctx: Load,
},
),
),
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 22..34,
exc: Some(
Tuple(
ExprTuple {
node_index: NodeIndex(None),
range: 28..34,
elts: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 29..30,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 32..33,
id: Name("b"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
),
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 35..46,
exc: Some(
Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 41..46,
ops: [
Lt,
],
operands: [
NumberLiteral(
ExprNumberLiteral {
node_index: NodeIndex(None),
range: 41..42,
value: Int(
1,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: NodeIndex(None),
range: 45..46,
value: Int(
2,
),
},
),
],
},
),
),
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 47..60,
exc: Some(
BoolOp(
ExprBoolOp {
node_index: NodeIndex(None),
range: 53..60,
op: And,
values: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 53..54,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 59..60,
id: Name("b"),
ctx: Load,
},
),
],
},
),
),
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 61..78,
exc: Some(
Lambda(
ExprLambda {
node_index: NodeIndex(None),
range: 67..78,
parameters: Some(
Parameters {
range: 74..75,
node_index: NodeIndex(None),
posonlyargs: [],
args: [
ParameterWithDefault {
range: 74..75,
node_index: NodeIndex(None),
parameter: Parameter {
range: 74..75,
node_index: NodeIndex(None),
name: Identifier {
id: Name("x"),
range: 74..75,
node_index: NodeIndex(None),
},
annotation: None,
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
),
body: Name(
ExprName {
node_index: NodeIndex(None),
range: 77..78,
id: Name("y"),
ctx: Load,
},
),
},
),
),
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 79..92,
exc: Some(
Await(
ExprAwait {
node_index: NodeIndex(None),
range: 85..92,
value: Name(
ExprName {
node_index: NodeIndex(None),
range: 91..92,
id: Name("x"),
ctx: Load,
},
),
},
),
),
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 93..115,
exc: Some(
If(
ExprIf {
node_index: NodeIndex(None),
range: 99..115,
test: BooleanLiteral(
ExprBooleanLiteral {
node_index: NodeIndex(None),
range: 104..108,
value: true,
},
),
body: Name(
ExprName {
node_index: NodeIndex(None),
range: 99..100,
id: Name("x"),
ctx: Load,
},
),
orelse: Name(
ExprName {
node_index: NodeIndex(None),
range: 114..115,
id: Name("y"),
ctx: Load,
},
),
},
),
),
cause: None,
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 138..152,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 144..145,
id: Name("x"),
ctx: Load,
},
),
),
cause: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 151..152,
id: Name("a"),
ctx: Load,
},
),
),
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 153..172,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 159..160,
id: Name("x"),
ctx: Load,
},
),
),
cause: Some(
Tuple(
ExprTuple {
node_index: NodeIndex(None),
range: 166..172,
elts: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 167..168,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 170..171,
id: Name("b"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: true,
},
),
),
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 173..191,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 179..180,
id: Name("x"),
ctx: Load,
},
),
),
cause: Some(
Compare(
ExprCompare {
node_index: NodeIndex(None),
range: 186..191,
ops: [
Lt,
],
operands: [
NumberLiteral(
ExprNumberLiteral {
node_index: NodeIndex(None),
range: 186..187,
value: Int(
1,
),
},
),
NumberLiteral(
ExprNumberLiteral {
node_index: NodeIndex(None),
range: 190..191,
value: Int(
2,
),
},
),
],
},
),
),
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 192..212,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 198..199,
id: Name("x"),
ctx: Load,
},
),
),
cause: Some(
BoolOp(
ExprBoolOp {
node_index: NodeIndex(None),
range: 205..212,
op: And,
values: [
Name(
ExprName {
node_index: NodeIndex(None),
range: 205..206,
id: Name("a"),
ctx: Load,
},
),
Name(
ExprName {
node_index: NodeIndex(None),
range: 211..212,
id: Name("b"),
ctx: Load,
},
),
],
},
),
),
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 213..237,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 219..220,
id: Name("x"),
ctx: Load,
},
),
),
cause: Some(
Lambda(
ExprLambda {
node_index: NodeIndex(None),
range: 226..237,
parameters: Some(
Parameters {
range: 233..234,
node_index: NodeIndex(None),
posonlyargs: [],
args: [
ParameterWithDefault {
range: 233..234,
node_index: NodeIndex(None),
parameter: Parameter {
range: 233..234,
node_index: NodeIndex(None),
name: Identifier {
id: Name("x"),
range: 233..234,
node_index: NodeIndex(None),
},
annotation: None,
},
default: None,
},
],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
),
body: Name(
ExprName {
node_index: NodeIndex(None),
range: 236..237,
id: Name("y"),
ctx: Load,
},
),
},
),
),
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 238..258,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 244..245,
id: Name("x"),
ctx: Load,
},
),
),
cause: Some(
Await(
ExprAwait {
node_index: NodeIndex(None),
range: 251..258,
value: Name(
ExprName {
node_index: NodeIndex(None),
range: 257..258,
id: Name("x"),
ctx: Load,
},
),
},
),
),
},
),
Raise(
StmtRaise {
node_index: NodeIndex(None),
range: 259..288,
exc: Some(
Name(
ExprName {
node_index: NodeIndex(None),
range: 265..266,
id: Name("x"),
ctx: Load,
},
),
),
cause: Some(
If(
ExprIf {
node_index: NodeIndex(None),
range: 272..288,
test: BooleanLiteral(
ExprBooleanLiteral {
node_index: NodeIndex(None),
range: 277..281,
value: true,
},
),
body: Name(
ExprName {
node_index: NodeIndex(None),
range: 272..273,
id: Name("x"),
ctx: Load,
},
),
orelse: Name(
ExprName {
node_index: NodeIndex(None),
range: 287..288,
id: Name("y"),
ctx: Load,
},
),
},
),
),
},
),
],
},
)
```