Skip to content
Prev Previous commit
Next Next commit
Fix continue error message
  • Loading branch information
ShaharNaveh committed May 19, 2026
commit 3b61bde47aab9bf8342386fcdd0b27e2ee4a7c71
3 changes: 1 addition & 2 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@

A continue outside loop should not be allowed.

>>> def foo(): # TODO: RUSTPYTHON; Wrong error message # doctest: +EXPECTED_FAILURE
>>> def foo():
... try:
... continue
... finally:
Expand Down Expand Up @@ -2912,7 +2912,6 @@ def test_break_outside_loop(self):
self._check_error("with object() as obj:\n break",
msg, lineno=2)

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_continue_outside_loop(self):
msg = "not properly in loop"
self._check_error("if 0: continue", msg, lineno=1)
Expand Down
59 changes: 29 additions & 30 deletions crates/codegen/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,74 +101,73 @@ impl core::error::Error for CodegenErrorType {}

impl fmt::Display for CodegenErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use CodegenErrorType::*;
match self {
Assign(target) => write!(f, "cannot assign to {target}"),
Delete(target) => write!(f, "cannot delete {target}"),
SyntaxError(err) => write!(f, "{}", err.as_str()),
MultipleStarArgs => {
Self::Assign(target) => write!(f, "cannot assign to {target}"),
Self::Delete(target) => write!(f, "cannot delete {target}"),
Self::SyntaxError(err) => write!(f, "{}", err.as_str()),
Self::MultipleStarArgs => {
write!(f, "multiple starred expressions in assignment")
}
InvalidStarExpr => write!(f, "can't use starred expression here"),
InvalidBreak => write!(f, "'break' outside loop"),
InvalidContinue => write!(f, "'continue' outside loop"),
InvalidReturn => write!(f, "'return' outside function"),
InvalidYield => write!(f, "'yield' outside function"),
InvalidYieldFrom => write!(f, "'yield from' outside function"),
InvalidAwait => write!(f, "'await' outside async function"),
InvalidAsyncFor => write!(f, "'async for' outside async function"),
InvalidAsyncWith => write!(f, "'async with' outside async function"),
InvalidAsyncComprehension => {
Self::InvalidStarExpr => write!(f, "can't use starred expression here"),
Self::InvalidBreak => write!(f, "'break' outside loop"),
Self::InvalidContinue => write!(f, "'continue' not properly in loop"),
Self::InvalidReturn => write!(f, "'return' outside function"),
Self::InvalidYield => write!(f, "'yield' outside function"),
Self::InvalidYieldFrom => write!(f, "'yield from' outside function"),
Self::InvalidAwait => write!(f, "'await' outside async function"),
Self::InvalidAsyncFor => write!(f, "'async for' outside async function"),
Self::InvalidAsyncWith => write!(f, "'async with' outside async function"),
Self::InvalidAsyncComprehension => {
write!(
f,
"asynchronous comprehension outside of an asynchronous function"
)
}
AsyncYieldFrom => write!(f, "'yield from' inside async function"),
AsyncReturnValue => {
Self::AsyncYieldFrom => write!(f, "'yield from' inside async function"),
Self::AsyncReturnValue => {
write!(f, "'return' with value inside async generator")
}
InvalidFuturePlacement => write!(
Self::InvalidFuturePlacement => write!(
f,
"from __future__ imports must occur at the beginning of the file"
),
InvalidFutureFeature(feat) => {
Self::InvalidFutureFeature(feat) => {
write!(f, "future feature {feat} is not defined")
}
FunctionImportStar => {
Self::FunctionImportStar => {
write!(f, "import * only allowed at module level")
}
TooManyStarUnpack => {
Self::TooManyStarUnpack => {
write!(f, "too many expressions in star-unpacking assignment")
}
EmptyWithItems => {
Self::EmptyWithItems => {
write!(f, "empty items on With")
}
EmptyWithBody => {
Self::EmptyWithBody => {
write!(f, "empty body on With")
}
ForbiddenName => {
Self::ForbiddenName => {
write!(f, "forbidden attribute name")
}
DuplicateStore(s) => {
Self::DuplicateStore(s) => {
write!(f, "duplicate store {s}")
}
UnreachablePattern(reason) => {
Self::UnreachablePattern(reason) => {
write!(f, "{reason} makes remaining patterns unreachable")
}
RepeatedAttributePattern => {
Self::RepeatedAttributePattern => {
write!(f, "attribute name repeated in class pattern")
}
ConflictingNameBindPattern => {
Self::ConflictingNameBindPattern => {
write!(f, "alternative patterns bind different names")
}
BreakContinueReturnInExceptStar => {
Self::BreakContinueReturnInExceptStar => {
write!(
f,
"'break', 'continue' and 'return' cannot appear in an except* block"
)
}
NotImplementedYet => {
Self::NotImplementedYet => {
write!(f, "RustPython does not implement this feature yet")
}
}
Expand Down