Skip to content

Commit 0f38041

Browse files
committed
Remove _ast conversion context wrappers
1 parent ca82736 commit 0f38041

24 files changed

Lines changed: 1291 additions & 2164 deletions

crates/codegen/src/compile.rs

Lines changed: 61 additions & 104 deletions
Large diffs are not rendered by default.

crates/codegen/src/ir.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,21 @@ impl ConstantPool {
165165
.expect("constant key canonicalization only fails on allocation error")
166166
}
167167

168+
/// Index of an already-stored constant equal to `constant`, if any.
169+
/// _PyCode_ConstantKey() keeps NaN-bearing constants distinct because
170+
/// Python-level NaN keys do not compare equal.
171+
fn find_existing(&self, constant: &ConstantData) -> Option<usize> {
172+
if Self::constant_contains_nan(constant) {
173+
return None;
174+
}
175+
self.constants
176+
.iter()
177+
.position(|existing| Self::constant_key_eq(existing, constant))
178+
}
179+
168180
pub fn insert_full(&mut self, constant: ConstantData) -> (usize, bool) {
169181
let constant = Self::canonicalize_constant_key_infallible(constant);
170-
// CPython's _PyCode_ConstantKey() keeps NaN-bearing constants distinct
171-
// because Python-level NaN keys do not compare equal.
172-
if !Self::constant_contains_nan(&constant)
173-
&& let Some(idx) = self
174-
.constants
175-
.iter()
176-
.position(|existing| Self::constant_key_eq(existing, &constant))
177-
{
182+
if let Some(idx) = self.find_existing(&constant) {
178183
return (idx, false);
179184
}
180185
let idx = self.constants.len();
@@ -184,14 +189,7 @@ impl ConstantPool {
184189

185190
fn try_insert_full(&mut self, constant: ConstantData) -> crate::InternalResult<(usize, bool)> {
186191
let constant = Self::canonicalize_constant_key(constant)?;
187-
// CPython's _PyCode_ConstantKey() keeps NaN-bearing constants distinct
188-
// because Python-level NaN keys do not compare equal.
189-
if !Self::constant_contains_nan(&constant)
190-
&& let Some(idx) = self
191-
.constants
192-
.iter()
193-
.position(|existing| Self::constant_key_eq(existing, &constant))
194-
{
192+
if let Some(idx) = self.find_existing(&constant) {
195193
return Ok((idx, false));
196194
}
197195
self.constants
@@ -6623,21 +6621,19 @@ fn get_max_label(blocks: &Blocks) -> i32 {
66236621
}
66246622

66256623
/// flowgraph.c make_except_stack
6626-
#[allow(clippy::unnecessary_wraps)]
6627-
fn make_except_stack() -> crate::InternalResult<CfgExceptStack> {
6624+
fn make_except_stack() -> CfgExceptStack {
66286625
let handlers = [BlockIdx::NULL; CO_MAXBLOCKS + 2];
66296626
debug_assert_eq!(handlers[0], BlockIdx::NULL);
6630-
Ok(CfgExceptStack { handlers, depth: 0 })
6627+
CfgExceptStack { handlers, depth: 0 }
66316628
}
66326629

66336630
/// flowgraph.c copy_except_stack
6634-
#[allow(clippy::unnecessary_wraps)]
6635-
fn copy_except_stack(stack: &CfgExceptStack) -> crate::InternalResult<CfgExceptStack> {
6631+
fn copy_except_stack(stack: &CfgExceptStack) -> CfgExceptStack {
66366632
debug_assert!(stack.depth <= CO_MAXBLOCKS + 1);
6637-
Ok(CfgExceptStack {
6633+
CfgExceptStack {
66386634
handlers: stack.handlers,
66396635
depth: stack.depth,
6640-
})
6636+
}
66416637
}
66426638

66436639
/// flowgraph.c except_stack_top
@@ -6689,7 +6685,7 @@ pub(crate) fn label_exception_targets(blocks: &mut Blocks) -> crate::InternalRes
66896685

66906686
todo.push(BlockIdx(0));
66916687
blocks[0].visited = true;
6692-
blocks[0].except_stack = Some(make_except_stack()?);
6688+
blocks[0].except_stack = Some(make_except_stack());
66936689

66946690
while let Some(block_idx) = todo.pop() {
66956691
let bi = block_idx.idx();
@@ -6716,7 +6712,7 @@ pub(crate) fn label_exception_targets(blocks: &mut Blocks) -> crate::InternalRes
67166712
if !blocks[target].visited {
67176713
blocks[target].except_stack = Some(copy_except_stack(
67186714
stack.as_ref().expect("active exception stack"),
6719-
)?);
6715+
));
67206716
todo.push(target);
67216717
blocks[target].visited = true;
67226718
}
@@ -6740,7 +6736,7 @@ pub(crate) fn label_exception_targets(blocks: &mut Blocks) -> crate::InternalRes
67406736
if bb_has_fallthrough(&blocks[bi]) {
67416737
blocks[target].except_stack = Some(copy_except_stack(
67426738
stack.as_ref().expect("active exception stack"),
6743-
)?);
6739+
));
67446740
} else {
67456741
blocks[target].except_stack = stack.take();
67466742
stack_transferred = true;
@@ -7095,7 +7091,7 @@ mod tests {
70957091

70967092
#[test]
70977093
fn except_stack_tracks_cpython_depth_and_handler_slots() {
7098-
let mut stack = make_except_stack().unwrap();
7094+
let mut stack = make_except_stack();
70997095
assert_eq!(stack.depth, 0);
71007096
assert_eq!(stack.handlers.len(), CO_MAXBLOCKS + 2);
71017097
assert_eq!(stack.handlers[0], BlockIdx::NULL);
@@ -7119,7 +7115,7 @@ mod tests {
71197115
assert!(handler.preserve_lasti);
71207116
assert!(blocks[1].preserve_lasti);
71217117

7122-
let copy = copy_except_stack(&stack).unwrap();
7118+
let copy = copy_except_stack(&stack);
71237119
assert_eq!(copy.depth, stack.depth);
71247120
assert_eq!(copy.handlers, stack.handlers);
71257121

crates/codegen/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn constant_data_to_ast_constant_value(value: ConstantData) -> ast::C
5353
},
5454
ConstantData::Ellipsis => ast::ConstantValue::Ellipsis,
5555
ConstantData::Code { .. } | ConstantData::Slice { .. } => {
56-
unreachable!("public AST constants cannot contain code objects or slices")
56+
unreachable!("ast.Constant values cannot contain code objects or slices")
5757
}
5858
}
5959
}
@@ -71,7 +71,7 @@ pub(crate) fn ast_constant_value_to_constant_data(value: ast::ConstantValue) ->
7171
ast::ConstantValue::Integer(value) => ConstantData::Integer {
7272
value: value
7373
.parse()
74-
.expect("RustPython public AST integer constants are decimal integers"),
74+
.expect("RustPython ast.Constant integer values are decimal integers"),
7575
},
7676
ast::ConstantValue::Tuple(elements) => ConstantData::Tuple {
7777
elements: elements

crates/codegen/src/symboltable.rs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,31 +1243,6 @@ impl SymbolTableBuilder {
12431243
false
12441244
}
12451245

1246-
fn runtime_interpolation_override_by_element(
1247-
&self,
1248-
element: &ast::InterpolatedElement,
1249-
) -> Option<(ast::ConstantValue, Option<Box<ast::Expr>>)> {
1250-
Some((
1251-
element.runtime_str.clone()?,
1252-
element.runtime_interpolation_format_spec.clone(),
1253-
))
1254-
}
1255-
1256-
fn runtime_formatted_value_format_spec_by_element(
1257-
&self,
1258-
element: &ast::InterpolatedElement,
1259-
) -> Option<Box<ast::Expr>> {
1260-
element.runtime_formatted_value_format_spec.clone()
1261-
}
1262-
1263-
fn runtime_joined_str_override(&self, fstring: &ast::ExprFString) -> Option<Vec<ast::Expr>> {
1264-
fstring.runtime_joined_str.clone()
1265-
}
1266-
1267-
fn runtime_template_str_override(&self, tstring: &ast::ExprTString) -> Option<Vec<ast::Expr>> {
1268-
tstring.runtime_template_str.clone()
1269-
}
1270-
12711246
fn finish(mut self) -> Result<SymbolTable, SymbolTableError> {
12721247
assert_eq!(self.tables.len(), 1);
12731248
let mut symbol_table = self.tables.pop().unwrap();
@@ -2597,8 +2572,8 @@ impl SymbolTableBuilder {
25972572
self.leave_scope();
25982573
}
25992574
Expr::FString(fstring) => {
2600-
if let Some(joined_str) = self.runtime_joined_str_override(fstring) {
2601-
for expr in &joined_str {
2575+
if let Some(joined_str) = &fstring.runtime_joined_str {
2576+
for expr in joined_str {
26022577
self.scan_expression(expr, ExpressionContext::Load)?;
26032578
}
26042579
return Ok(());
@@ -2609,10 +2584,8 @@ impl SymbolTableBuilder {
26092584
.filter_map(|x| x.as_interpolation())
26102585
{
26112586
self.scan_expression(&expr.expression, ExpressionContext::Load)?;
2612-
if let Some(format_spec) =
2613-
self.runtime_formatted_value_format_spec_by_element(expr)
2614-
{
2615-
self.scan_expression(&format_spec, ExpressionContext::Load)?;
2587+
if let Some(format_spec) = &expr.runtime_formatted_value_format_spec {
2588+
self.scan_expression(format_spec, ExpressionContext::Load)?;
26162589
} else if let Some(format_spec) = &expr.format_spec {
26172590
for element in format_spec.elements.interpolations() {
26182591
self.scan_expression(&element.expression, ExpressionContext::Load)?
@@ -2621,8 +2594,8 @@ impl SymbolTableBuilder {
26212594
}
26222595
}
26232596
Expr::TString(tstring) => {
2624-
if let Some(template_str) = self.runtime_template_str_override(tstring) {
2625-
for expr in &template_str {
2597+
if let Some(template_str) = &tstring.runtime_template_str {
2598+
for expr in template_str {
26262599
self.scan_expression(expr, ExpressionContext::Load)?;
26272600
}
26282601
return Ok(());
@@ -2634,10 +2607,8 @@ impl SymbolTableBuilder {
26342607
.filter_map(|x| x.as_interpolation())
26352608
{
26362609
self.scan_expression(&expr.expression, ExpressionContext::Load)?;
2637-
if let Some(interpolation) =
2638-
self.runtime_interpolation_override_by_element(expr)
2639-
{
2640-
if let Some(format_spec) = &interpolation.1 {
2610+
if expr.runtime_str.is_some() {
2611+
if let Some(format_spec) = &expr.runtime_interpolation_format_spec {
26412612
self.scan_expression(format_spec, ExpressionContext::Load)?;
26422613
}
26432614
} else if let Some(format_spec) = &expr.format_spec {

0 commit comments

Comments
 (0)