8000 Cleanup match statement codegen by arihant2math · Pull Request #5700 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Cleanup match statement codegen #5700

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

Merged
merged 10 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve error handling
  • Loading branch information
arihant2math committed Apr 15, 2025
commit 21272025c29f17b640016c78faf640212e26a4c4
14 changes: 6 additions & 8 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2158,10 +2158,7 @@ impl Compiler<'_> {
for ident in attrs.iter().take(n_attrs).skip(i + 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't the code you changed, but if you're looking to make it more rusty, you could use .enumerate() here for the outer loop (for (i, attr) in attrs.iter().enumerate()) and then just for ident in &attrs[i + 1..] for the inner loop.

let other = ident.as_str();
if attr == other {
todo!();
// return Err(self.compiler_error(
// &format!("attribute name repeated in class pattern: {}", attr),
// ));
return Err(self.error(CodegenErrorType::RepeatedAttributePattern));
}
}
}
Expand Down Expand Up @@ -2415,15 +2412,16 @@ impl Compiler<'_> {
} else {
let control_vec = control.as_ref().unwrap();
if nstores != control_vec.len() {
todo!();
// return self.compiler_error("alternative patterns bind different names");
return Err(self.error(CodegenErrorType::ConflictingNameBindPattern));
} else if nstores > 0 {
// Check that the names occur in the same order.
for icontrol in (0..nstores).rev() {
let name = &control_vec[icontrol];
// Find the index of `name` in the current stores.
let istores = pc.stores.iter().position(|n| n == name).unwrap();
// .ok_or_else(|| self.compiler_error("alternative patterns bind different names"))?;
let istores =
pc.stores.iter().position(|n| n == name).ok_or_else(|| {
self.error(CodegenErrorType::ConflictingNameBindPattern)
})?;
if icontrol != istores {
// The orders differ; we must reorder.
assert!(istores < icontrol, "expected istores < icontrol");
Expand Down
8 changes: 8 additions & 0 deletions compiler/codegen/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum CodegenErrorType {
ForbiddenName,
DuplicateStore(String),
UnreachablePattern(PatternUnreachableReason),
RepeatedAttributePattern,
ConflictingNameBindPattern,
NotImplementedYet, // RustPython marker for unimplemented features
}

Expand Down Expand Up @@ -119,6 +121,12 @@ impl fmt::Display for CodegenErrorType {
UnreachablePattern(reason) => {
write!(f, "{reason} makes remaining patterns unreachable")
}
RepeatedAttributePattern => {
write!(f, "attribute name repeated in class pattern")
}
ConflictingNameBindPattern => {
write!(f, "alternative patterns bind different names")
}
NotImplementedYet => {
write!(f, "RustPython does not implement this feature yet")
}
Expand Down
Loading
0