Skip to content

Commit 58f5122

Browse files
committed
codegen::{CompileError -> CodegenError}
1 parent 1606c26 commit 58f5122

8 files changed

Lines changed: 81 additions & 82 deletions

File tree

compiler/codegen/src/compile.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! <https://github.com/micropython/micropython/blob/master/py/compile.c>
77
88
use crate::{
9-
error::{CompileError, CompileErrorType},
9+
error::{CodegenError, CodegenErrorType},
1010
ir,
1111
symboltable::{self, SymbolScope, SymbolTable},
1212
IndexSet,
@@ -20,7 +20,7 @@ use std::borrow::Cow;
2020

2121
pub use rustpython_bytecode::Mode;
2222

23-
type CompileResult<T> = Result<T, CompileError>;
23+
type CompileResult<T> = Result<T, CodegenError>;
2424

2525
#[derive(PartialEq, Eq, Clone, Copy)]
2626
enum NameUsage {
@@ -129,7 +129,7 @@ fn compile_impl<Ast: ?Sized>(
129129
) -> CompileResult<CodeObject> {
130130
let symbol_table = match make_symbol_table(ast) {
131131
Ok(x) => x,
132-
Err(e) => return Err(e.into_compile_error(source_path)),
132+
Err(e) => return Err(e.into_codegen_error(source_path)),
133133
};
134134

135135
let mut compiler = Compiler::new(opts, source_path, "<module>".to_owned());
@@ -234,11 +234,11 @@ impl Compiler {
234234
}
235235
}
236236

237-
fn error(&self, error: CompileErrorType) -> CompileError {
237+
fn error(&self, error: CodegenErrorType) -> CodegenError {
238238
self.error_loc(error, self.current_source_location)
239239
}
240-
fn error_loc(&self, error: CompileErrorType, location: ast::Location) -> CompileError {
241-
CompileError {
240+
fn error_loc(&self, error: CodegenErrorType, location: ast::Location) -> CodegenError {
241+
CodegenError {
242242
error,
243243
location,
244244
source_path: self.source_path.clone(),
@@ -455,7 +455,7 @@ impl Compiler {
455455
NameUsage::Delete if is_forbidden_name(name) => "cannot delete",
456456
_ => return Ok(()),
457457
};
458-
Err(self.error(CompileErrorType::SyntaxError(format!("{} {}", msg, name))))
458+
Err(self.error(CodegenErrorType::SyntaxError(format!("{} {}", msg, name))))
459459
}
460460

461461
fn compile_name(&mut self, name: &str, usage: NameUsage) -> CompileResult<()> {
@@ -580,7 +580,7 @@ impl Compiler {
580580
let from_list = if import_star {
581581
if self.ctx.in_func() {
582582
return Err(self
583-
.error_loc(CompileErrorType::FunctionImportStar, statement.location));
583+
.error_loc(CodegenErrorType::FunctionImportStar, statement.location));
584584
}
585585
vec![ConstantData::Str {
586586
value: "*".to_owned(),
@@ -769,7 +769,7 @@ impl Compiler {
769769
self.emit(Instruction::Break { target: end });
770770
}
771771
None => {
772-
return Err(self.error_loc(CompileErrorType::InvalidBreak, statement.location));
772+
return Err(self.error_loc(CodegenErrorType::InvalidBreak, statement.location));
773773
}
774774
},
775775
Continue => match self.ctx.loop_data {
@@ -778,13 +778,13 @@ impl Compiler {
778778
}
779779
None => {
780780
return Err(
781-
self.error_loc(CompileErrorType::InvalidContinue, statement.location)
781+
self.error_loc(CodegenErrorType::InvalidContinue, statement.location)
782782
);
783783
}
784784
},
785785
Return { value } => {
786786
if !self.ctx.in_func() {
787-
return Err(self.error_loc(CompileErrorType::InvalidReturn, statement.location));
787+
return Err(self.error_loc(CodegenErrorType::InvalidReturn, statement.location));
788788
}
789789
match value {
790790
Some(v) => {
@@ -795,7 +795,7 @@ impl Compiler {
795795
.contains(bytecode::CodeFlags::IS_GENERATOR)
796796
{
797797
return Err(self.error_loc(
798-
CompileErrorType::AsyncReturnValue,
798+
CodegenErrorType::AsyncReturnValue,
799799
statement.location,
800800
));
801801
}
@@ -857,9 +857,9 @@ impl Compiler {
857857
}
858858
}
859859
ast::ExprKind::BinOp { .. } | ast::ExprKind::UnaryOp { .. } => {
860-
return Err(self.error(CompileErrorType::Delete("expression")))
860+
return Err(self.error(CodegenErrorType::Delete("expression")))
861861
}
862-
_ => return Err(self.error(CompileErrorType::Delete(expression.node.name()))),
862+
_ => return Err(self.error(CodegenErrorType::Delete(expression.node.name()))),
863863
}
864864
Ok(())
865865
}
@@ -922,7 +922,7 @@ impl Compiler {
922922
.chain(&args.kwonlyargs);
923923
for name in args_iter {
924924
if Compiler::is_forbidden_arg_name(&name.node.arg) {
925-
return Err(self.error(CompileErrorType::SyntaxError(format!(
925+
return Err(self.error(CodegenErrorType::SyntaxError(format!(
926926
"cannot assign to {}",
927927
&name.node.arg
928928
))));
@@ -1401,7 +1401,7 @@ impl Compiler {
14011401
let (item, items) = if let Some(parts) = items.split_first() {
14021402
parts
14031403
} else {
1404-
return Err(self.error(CompileErrorType::EmptyWithItems));
1404+
return Err(self.error(CodegenErrorType::EmptyWithItems));
14051405
};
14061406

14071407
let final_block = {
@@ -1433,7 +1433,7 @@ impl Compiler {
14331433

14341434
if items.is_empty() {
14351435
if body.is_empty() {
1436-
return Err(self.error(CompileErrorType::EmptyWithBody));
1436+
return Err(self.error(CodegenErrorType::EmptyWithBody));
14371437
}
14381438
self.compile_statements(body)?;
14391439
} else {
@@ -1529,7 +1529,7 @@ impl Compiler {
15291529
) -> CompileResult<()> {
15301530
eprintln!("match subject: {subject:?}");
15311531
eprintln!("match cases: {cases:?}");
1532-
Err(self.error(CompileErrorType::NotImplementedYet))
1532+
Err(self.error(CodegenErrorType::NotImplementedYet))
15331533
}
15341534

15351535
fn compile_chained_comparison(
@@ -1699,15 +1699,15 @@ impl Compiler {
16991699
for (i, element) in elts.iter().enumerate() {
17001700
if let ast::ExprKind::Starred { .. } = &element.node {
17011701
if seen_star {
1702-
return Err(self.error(CompileErrorType::MultipleStarArgs));
1702+
return Err(self.error(CodegenErrorType::MultipleStarArgs));
17031703
} else {
17041704
seen_star = true;
17051705
let before = i;
17061706
let after = elts.len() - i - 1;
17071707
let (before, after) = (|| Some((before.to_u8()?, after.to_u8()?)))()
17081708
.ok_or_else(|| {
17091709
self.error_loc(
1710-
CompileErrorType::TooManyStarUnpack,
1710+
CodegenErrorType::TooManyStarUnpack,
17111711
target.location,
17121712
)
17131713
})?;
@@ -1732,10 +1732,10 @@ impl Compiler {
17321732
}
17331733
_ => {
17341734
return Err(self.error(match target.node {
1735-
ast::ExprKind::Starred { .. } => CompileErrorType::SyntaxError(
1735+
ast::ExprKind::Starred { .. } => CodegenErrorType::SyntaxError(
17361736
"starred assignment target must be in a list or tuple".to_owned(),
17371737
),
1738-
_ => CompileErrorType::Assign(target.node.name()),
1738+
_ => CodegenErrorType::Assign(target.node.name()),
17391739
}));
17401740
}
17411741
}
@@ -1776,7 +1776,7 @@ impl Compiler {
17761776
AugAssignKind::Attr { idx }
17771777
}
17781778
_ => {
1779-
return Err(self.error(CompileErrorType::Assign(target.node.name())));
1779+
return Err(self.error(CodegenErrorType::Assign(target.node.name())));
17801780
}
17811781
};
17821782

@@ -2049,7 +2049,7 @@ impl Compiler {
20492049
}
20502050
Yield { value } => {
20512051
if !self.ctx.in_func() {
2052-
return Err(self.error(CompileErrorType::InvalidYield));
2052+
return Err(self.error(CodegenErrorType::InvalidYield));
20532053
}
20542054
self.mark_generator();
20552055
match value {
@@ -2060,7 +2060,7 @@ impl Compiler {
20602060
}
20612061
Await { value } => {
20622062
if self.ctx.func != FunctionContext::AsyncFunction {
2063-
return Err(self.error(CompileErrorType::InvalidAwait));
2063+
return Err(self.error(CodegenErrorType::InvalidAwait));
20642064
}
20652065
self.compile_expression(value)?;
20662066
self.emit(Instruction::GetAwaitable);
@@ -2070,10 +2070,10 @@ impl Compiler {
20702070
YieldFrom { value } => {
20712071
match self.ctx.func {
20722072
FunctionContext::NoFunction => {
2073-
return Err(self.error(CompileErrorType::InvalidYieldFrom));
2073+
return Err(self.error(CodegenErrorType::InvalidYieldFrom));
20742074
}
20752075
FunctionContext::AsyncFunction => {
2076-
return Err(self.error(CompileErrorType::AsyncYieldFrom));
2076+
return Err(self.error(CodegenErrorType::AsyncYieldFrom));
20772077
}
20782078
FunctionContext::Function => {}
20792079
}
@@ -2208,7 +2208,7 @@ impl Compiler {
22082208
})?;
22092209
}
22102210
Starred { .. } => {
2211-
return Err(self.error(CompileErrorType::InvalidStarExpr));
2211+
return Err(self.error(CodegenErrorType::InvalidStarExpr));
22122212
}
22132213
IfExp { test, body, orelse } => {
22142214
let else_block = self.new_block();
@@ -2417,8 +2417,8 @@ impl Compiler {
24172417

24182418
fn compile_comprehension_element(&mut self, element: &ast::Expr) -> CompileResult<()> {
24192419
self.compile_expression(element).map_err(|e| {
2420-
if let CompileErrorType::InvalidStarExpr = e.error {
2421-
self.error(CompileErrorType::SyntaxError(
2420+
if let CodegenErrorType::InvalidStarExpr = e.error {
2421+
self.error(CodegenErrorType::SyntaxError(
24222422
"iterable unpacking cannot be used in comprehension".to_owned(),
24232423
))
24242424
} else {
@@ -2547,9 +2547,9 @@ impl Compiler {
25472547
Ok(())
25482548
}
25492549

2550-
fn compile_future_features(&mut self, features: &[ast::Alias]) -> Result<(), CompileError> {
2550+
fn compile_future_features(&mut self, features: &[ast::Alias]) -> Result<(), CodegenError> {
25512551
if self.done_with_future_stmts {
2552-
return Err(self.error(CompileErrorType::InvalidFuturePlacement));
2552+
return Err(self.error(CodegenErrorType::InvalidFuturePlacement));
25532553
}
25542554
for feature in features {
25552555
match &*feature.node.name {
@@ -2559,7 +2559,7 @@ impl Compiler {
25592559
// "generator_stop" => {}
25602560
"annotations" => self.future_annotations = true,
25612561
other => {
2562-
return Err(self.error(CompileErrorType::InvalidFutureFeature(other.to_owned())))
2562+
return Err(self.error(CodegenErrorType::InvalidFutureFeature(other.to_owned())))
25632563
}
25642564
}
25652565
}

compiler/codegen/src/error.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use rustpython_ast::Location;
33
use std::{error::Error, fmt};
44

55
#[derive(Debug)]
6-
pub struct CompileError {
7-
pub error: CompileErrorType,
6+
pub struct CodegenError {
7+
pub error: CodegenErrorType,
88
pub location: Location,
99
pub source_path: String,
1010
}
1111

1212
#[derive(Debug)]
1313
#[non_exhaustive]
14-
pub enum CompileErrorType {
14+
pub enum CodegenErrorType {
1515
/// Invalid assignment, cannot store value in target.
1616
Assign(&'static str),
1717
/// Invalid delete
@@ -40,61 +40,62 @@ pub enum CompileErrorType {
4040
NotImplementedYet, // RustPython marker for unimplemented features
4141
}
4242

43-
impl fmt::Display for CompileErrorType {
43+
impl fmt::Display for CodegenErrorType {
4444
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45+
use CodegenErrorType::*;
4546
match self {
46-
CompileErrorType::Assign(target) => write!(f, "cannot assign to {}", target),
47-
CompileErrorType::Delete(target) => write!(f, "cannot delete {}", target),
48-
CompileErrorType::SyntaxError(err) => write!(f, "{}", err.as_str()),
49-
CompileErrorType::MultipleStarArgs => {
47+
Assign(target) => write!(f, "cannot assign to {}", target),
48+
Delete(target) => write!(f, "cannot delete {}", target),
49+
SyntaxError(err) => write!(f, "{}", err.as_str()),
50+
MultipleStarArgs => {
5051
write!(f, "two starred expressions in assignment")
5152
}
52-
CompileErrorType::InvalidStarExpr => write!(f, "cannot use starred expression here"),
53-
CompileErrorType::InvalidBreak => write!(f, "'break' outside loop"),
54-
CompileErrorType::InvalidContinue => write!(f, "'continue' outside loop"),
55-
CompileErrorType::InvalidReturn => write!(f, "'return' outside function"),
56-
CompileErrorType::InvalidYield => write!(f, "'yield' outside function"),
57-
CompileErrorType::InvalidYieldFrom => write!(f, "'yield from' outside function"),
58-
CompileErrorType::InvalidAwait => write!(f, "'await' outside async function"),
59-
CompileErrorType::AsyncYieldFrom => write!(f, "'yield from' inside async function"),
60-
CompileErrorType::AsyncReturnValue => {
53+
InvalidStarExpr => write!(f, "cannot use starred expression here"),
54+
InvalidBreak => write!(f, "'break' outside loop"),
55+
InvalidContinue => write!(f, "'continue' outside loop"),
56+
InvalidReturn => write!(f, "'return' outside function"),
57+
InvalidYield => write!(f, "'yield' outside function"),
58+
InvalidYieldFrom => write!(f, "'yield from' outside function"),
59+
InvalidAwait => write!(f, "'await' outside async function"),
60+
AsyncYieldFrom => write!(f, "'yield from' inside async function"),
61+
AsyncReturnValue => {
6162
write!(f, "'return' with value inside async generator")
6263
}
63-
CompileErrorType::InvalidFuturePlacement => write!(
64+
InvalidFuturePlacement => write!(
6465
f,
6566
"from __future__ imports must occur at the beginning of the file"
6667
),
67-
CompileErrorType::InvalidFutureFeature(feat) => {
68+
InvalidFutureFeature(feat) => {
6869
write!(f, "future feature {} is not defined", feat)
6970
}
70-
CompileErrorType::FunctionImportStar => {
71+
FunctionImportStar => {
7172
write!(f, "import * only allowed at module level")
7273
}
73-
CompileErrorType::TooManyStarUnpack => {
74+
TooManyStarUnpack => {
7475
write!(f, "too many expressions in star-unpacking assignment")
7576
}
76-
CompileErrorType::EmptyWithItems => {
77+
EmptyWithItems => {
7778
write!(f, "empty items on With")
7879
}
79-
CompileErrorType::EmptyWithBody => {
80+
EmptyWithBody => {
8081
write!(f, "empty body on With")
8182
}
82-
CompileErrorType::NotImplementedYet => {
83+
NotImplementedYet => {
8384
write!(f, "RustPython does not implement this feature yet")
8485
}
8586
}
8687
}
8788
}
8889

89-
impl Error for CompileErrorType {}
90+
impl Error for CodegenErrorType {}
9091

91-
impl fmt::Display for CompileError {
92+
impl fmt::Display for CodegenError {
9293
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9394
write!(f, "{} at {}", self.error, self.location)
9495
}
9596
}
9697

97-
impl Error for CompileError {
98+
impl Error for CodegenError {
9899
fn source(&self) -> Option<&(dyn Error + 'static)> {
99100
None
100101
}

compiler/codegen/src/symboltable.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Inspirational file: https://github.com/python/cpython/blob/main/Python/symtable.
88
*/
99

1010
use crate::{
11-
error::{CompileError, CompileErrorType},
11+
error::{CodegenError, CodegenErrorType},
1212
IndexMap,
1313
};
1414
use rustpython_ast::{self as ast, Location};
@@ -168,9 +168,9 @@ pub struct SymbolTableError {
168168
}
169169

170170
impl SymbolTableError {
171-
pub fn into_compile_error(self, source_path: String) -> CompileError {
172-
CompileError {
173-
error: CompileErrorType::SyntaxError(self.error),
171+
pub fn into_codegen_error(self, source_path: String) -> CodegenError {
172+
CodegenError {
173+
error: CodegenErrorType::SyntaxError(self.error),
174174
location: self.location,
175175
source_path,
176176
}

0 commit comments

Comments
 (0)