-
Notifications
You must be signed in to change notification settings - Fork 1.4k
More Pattern matching implementation mapping + class #6110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
50c5574
8ae2dc7
a109a59
4b63801
21fb4aa
b807bc7
be54bc0
f4543f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,7 +170,7 @@ impl PyByteArray { | |
| } | ||
|
|
||
| #[pyclass( | ||
| flags(BASETYPE), | ||
| flags(BASETYPE, _MATCH_SELF), | ||
| with( | ||
| Py, | ||
| PyRef, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -702,19 +702,17 @@ impl ExecutingFrame<'_> { | |
| } | ||
| bytecode::Instruction::Swap { index } => { | ||
| let len = self.state.stack.len(); | ||
| if len == 0 { | ||
| return Err(vm.new_runtime_error("stack underflow in SWAP".to_string())); | ||
| } | ||
| debug_assert!(len > 0, "stack underflow in SWAP"); | ||
| let i = len - 1; // TOS index | ||
| let index_val = index.get(arg) as usize; | ||
| // CPython: SWAP(n) swaps TOS with PEEK(n) where PEEK(n) = stack_pointer[-n] | ||
| // This means swap TOS with the element at index (len - n) | ||
| if index_val > len { | ||
| return Err(vm.new_runtime_error(format!( | ||
| "SWAP index {} exceeds stack size {}", | ||
| index_val, len | ||
| ))); | ||
| } | ||
| debug_assert!( | ||
| index_val <= len, | ||
| "SWAP index {} exceeds stack size {}", | ||
| index_val, | ||
| len | ||
| ); | ||
| let j = len - index_val; | ||
|
Comment on lines
+703
to
714
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion SWAP index guard is incomplete; add lower-bound check and a release-mode safety path. SWAP(0) would compute j = len - 0 and panic in release. Compiler should not emit 0, but add a debug lower-bound assertion and a release-mode guard to prevent hard-to-diagnose panics. Apply this diff: let len = self.state.stack.len();
- debug_assert!(len > 0, "stack underflow in SWAP");
+ debug_assert!(len > 0, "stack underflow in SWAP");
let i = len - 1; // TOS index
let index_val = index.get(arg) as usize;
// CPython: SWAP(n) swaps TOS with PEEK(n) where PEEK(n) = stack_pointer[-n]
// This means swap TOS with the element at index (len - n)
- debug_assert!(
- index_val <= len,
- "SWAP index {} exceeds stack size {}",
- index_val,
- len
- );
+ debug_assert!(index_val >= 1, "SWAP index must be >= 1");
+ debug_assert!(
+ index_val <= len,
+ "SWAP index {} exceeds stack size {}",
+ index_val,
+ len
+ );
+ if index_val == 0 || index_val > len {
+ self.fatal("SWAP index out of range");
+ }
let j = len - index_val;
self.state.stack.swap(i, j);🤖 Prompt for AI Agents |
||
| self.state.stack.swap(i, j); | ||
| Ok(None) | ||
|
|
@@ -1433,17 +1431,9 @@ impl ExecutingFrame<'_> { | |
| // No __match_args__, check if this is a type with MATCH_SELF behavior | ||
| // For built-in types like bool, int, str, list, tuple, dict, etc. | ||
| // they match the subject itself as the single positional argument | ||
| let is_match_self_type = cls.is(vm.ctx.types.bool_type) | ||
| || cls.is(vm.ctx.types.int_type) | ||
| || cls.is(vm.ctx.types.float_type) | ||
| || cls.is(vm.ctx.types.str_type) | ||
| || cls.is(vm.ctx.types.bytes_type) | ||
| || cls.is(vm.ctx.types.bytearray_type) | ||
| || cls.is(vm.ctx.types.list_type) | ||
| || cls.is(vm.ctx.types.tuple_type) | ||
| || cls.is(vm.ctx.types.dict_type) | ||
| || cls.is(vm.ctx.types.set_type) | ||
| || cls.is(vm.ctx.types.frozenset_type); | ||
| let is_match_self_type = cls | ||
| .downcast::<PyType>() | ||
| .is_ok_and(|t| t.slots.flags.contains(PyTypeFlags::_MATCH_SELF)); | ||
|
|
||
| if is_match_self_type { | ||
| if nargs_val == 1 { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.