Skip to content

Commit 9bd84b2

Browse files
authored
Handle full-width operands in the None-return scanner (#9347)
Use decoded logical instructions rather than the preceding raw operand byte. Check referenced constants by None identity and decode common constants without panicking. Assisted-by: OpenAI Codex:model unavailable
1 parent 2efa217 commit 9bd84b2

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

‎crates/vm/src/vm/crossinterp.rs‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ use crate::{
1414
PyMemoryView, PyStr, PyStrInterned, PyTuple,
1515
},
1616
bytecode::{
17-
BorrowedConstant, CodeFlags, Constant, Instruction,
17+
CodeFlags, Instruction,
1818
oparg::{CommonConstant, OpArg, OpArgState},
1919
},
2020
protocol::PyBuffer,
2121
};
22-
use core::sync::atomic::{AtomicBool, Ordering};
22+
use core::{
23+
borrow::Borrow,
24+
sync::atomic::{AtomicBool, Ordering},
25+
};
2326
use num_traits::ToPrimitive;
2427
use rustpython_common::wtf8::Wtf8Buf;
2528

@@ -828,31 +831,27 @@ pub(crate) fn code_returns_only_none(code: &Py<PyCode>) -> bool {
828831
if !is_pure_function(code) {
829832
return false;
830833
}
831-
let units = &code.instructions;
832-
// Look up None in co_consts.
833-
let none_index = code
834-
.constants
835-
.iter()
836-
.position(|c| matches!(c.borrow_constant(), BorrowedConstant::None));
837-
// We don't worry about EXTENDED_ARG for now.
838-
for (at, op, _) in walk_instructions(code) {
839-
if !matches!(op, Instruction::ReturnValue) {
840-
continue;
841-
}
842-
debug_assert_ne!(at, 0);
843-
let prev_op = units.read_op(at - 1).deoptimize();
844-
let prev_arg = u32::from(u8::from(units.read_arg(at - 1)));
845-
if matches!(prev_op, Instruction::LoadCommonConstant { .. })
846-
&& prev_arg == u32::from(CommonConstant::None)
847-
{
834+
let mut previous_loads_none = false;
835+
for (_, instruction, arg) in walk_instructions(code) {
836+
if matches!(instruction, Instruction::ExtendedArg) {
848837
continue;
849838
}
850-
if matches!(prev_op, Instruction::LoadConst { .. })
851-
&& none_index.is_some_and(|none_index| prev_arg as usize == none_index)
852-
{
853-
continue;
839+
if matches!(instruction, Instruction::ReturnValue) && !previous_loads_none {
840+
return false;
854841
}
855-
return false;
842+
previous_loads_none = match instruction {
843+
Instruction::LoadCommonConstant { idx } => {
844+
matches!(idx.try_get(arg), Ok(CommonConstant::None))
845+
}
846+
Instruction::LoadConst { consti } => code
847+
.constants
848+
.get(consti.get(arg).as_usize())
849+
.is_some_and(|constant| {
850+
let object: &PyObject = constant.borrow();
851+
object.is(&crate::Context::genesis().none)
852+
}),
853+
_ => false,
854+
};
856855
}
857856
true
858857
}

0 commit comments

Comments
 (0)