rustpython_codegen/
error.rs1use std::fmt;
2
3pub type CodegenError = rustpython_parser_core::source_code::LocatedError<CodegenErrorType>;
4
5#[derive(Debug)]
6#[non_exhaustive]
7pub enum CodegenErrorType {
8 Assign(&'static str),
10 Delete(&'static str),
12 SyntaxError(String),
13 MultipleStarArgs,
15 InvalidStarExpr,
17 InvalidBreak,
19 InvalidContinue,
21 InvalidReturn,
22 InvalidYield,
23 InvalidYieldFrom,
24 InvalidAwait,
25 AsyncYieldFrom,
26 AsyncReturnValue,
27 InvalidFuturePlacement,
28 InvalidFutureFeature(String),
29 FunctionImportStar,
30 TooManyStarUnpack,
31 EmptyWithItems,
32 EmptyWithBody,
33 NotImplementedYet, }
35
36impl std::error::Error for CodegenErrorType {}
37
38impl fmt::Display for CodegenErrorType {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 use CodegenErrorType::*;
41 match self {
42 Assign(target) => write!(f, "cannot assign to {target}"),
43 Delete(target) => write!(f, "cannot delete {target}"),
44 SyntaxError(err) => write!(f, "{}", err.as_str()),
45 MultipleStarArgs => {
46 write!(f, "two starred expressions in assignment")
47 }
48 InvalidStarExpr => write!(f, "cannot use starred expression here"),
49 InvalidBreak => write!(f, "'break' outside loop"),
50 InvalidContinue => write!(f, "'continue' outside loop"),
51 InvalidReturn => write!(f, "'return' outside function"),
52 InvalidYield => write!(f, "'yield' outside function"),
53 InvalidYieldFrom => write!(f, "'yield from' outside function"),
54 InvalidAwait => write!(f, "'await' outside async function"),
55 AsyncYieldFrom => write!(f, "'yield from' inside async function"),
56 AsyncReturnValue => {
57 write!(f, "'return' with value inside async generator")
58 }
59 InvalidFuturePlacement => write!(
60 f,
61 "from __future__ imports must occur at the beginning of the file"
62 ),
63 InvalidFutureFeature(feat) => {
64 write!(f, "future feature {feat} is not defined")
65 }
66 FunctionImportStar => {
67 write!(f, "import * only allowed at module level")
68 }
69 TooManyStarUnpack => {
70 write!(f, "too many expressions in star-unpacking assignment")
71 }
72 EmptyWithItems => {
73 write!(f, "empty items on With")
74 }
75 EmptyWithBody => {
76 write!(f, "empty body on With")
77 }
78 NotImplementedYet => {
79 write!(f, "RustPython does not implement this feature yet")
80 }
81 }
82 }
83}