Skip to content

Commit ff70557

Browse files
authored
Update parser to specialize ConversionFlag (RustPython#4976)
1 parent a1d9c64 commit ff70557

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

Cargo.lock

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ rustpython-pylib = { path = "pylib" }
2929
rustpython-stdlib = { path = "stdlib" }
3030
rustpython-doc = { git = "https://github.com/RustPython/__doc__", branch = "main" }
3131

32-
rustpython-literal = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "dd4cc25227452178cce357b49677842efe533711" }
33-
rustpython-parser-core = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "dd4cc25227452178cce357b49677842efe533711" }
34-
rustpython-parser = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "dd4cc25227452178cce357b49677842efe533711" }
35-
rustpython-ast = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "dd4cc25227452178cce357b49677842efe533711" }
36-
rustpython-format = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "dd4cc25227452178cce357b49677842efe533711" }
32+
rustpython-literal = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "fc301ab1b0f5483f2b2d3e28d8d92a2845b99fec" }
33+
rustpython-parser-core = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "fc301ab1b0f5483f2b2d3e28d8d92a2845b99fec" }
34+
rustpython-parser = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "fc301ab1b0f5483f2b2d3e28d8d92a2845b99fec" }
35+
rustpython-ast = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "fc301ab1b0f5483f2b2d3e28d8d92a2845b99fec" }
36+
rustpython-format = { git = "https://github.com/youknowone/RustPython-Parser.git", rev = "fc301ab1b0f5483f2b2d3e28d8d92a2845b99fec" }
3737
# rustpython-literal = { path = "../RustPython-parser/literal" }
3838
# rustpython-parser-core = { path = "../RustPython-parser/core" }
3939
# rustpython-parser = { path = "../RustPython-parser/parser" }

compiler/codegen/src/compile.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ use rustpython_compiler_core::{
2121
bytecode::{self, Arg as OpArgMarker, CodeObject, ConstantData, Instruction, OpArg, OpArgType},
2222
Mode,
2323
};
24-
use rustpython_parser_core::{
25-
source_code::{LineNumber, SourceLocation},
26-
ConversionFlag,
27-
};
24+
use rustpython_parser_core::source_code::{LineNumber, SourceLocation};
2825
use std::borrow::Cow;
2926

3027
type CompileResult<T> = Result<T, CodegenError>;
@@ -2258,8 +2255,7 @@ impl Compiler {
22582255
emit!(
22592256
self,
22602257
Instruction::FormatValue {
2261-
conversion: ConversionFlag::from_op_arg(conversion.to_u32())
2262-
.expect("invalid conversion flag"),
2258+
conversion: *conversion,
22632259
},
22642260
);
22652261
}

compiler/core/src/bytecode.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -336,18 +336,22 @@ impl fmt::Display for Label {
336336
}
337337
}
338338

339-
op_arg_enum_impl!(
340-
enum ConversionFlag {
341-
/// No conversion
342-
None = 0, // CPython uses -1 but not pleasure for us
343-
/// Converts by calling `str(<value>)`.
344-
Str = b's',
345-
/// Converts by calling `ascii(<value>)`.
346-
Ascii = b'a',
347-
/// Converts by calling `repr(<value>)`.
348-
Repr = b'r',
339+
impl OpArgType for ConversionFlag {
340+
#[inline]
341+
fn from_op_arg(x: u32) -> Option<Self> {
342+
match x as u8 {
343+
b's' => Some(ConversionFlag::Str),
344+
b'a' => Some(ConversionFlag::Ascii),
345+
b'r' => Some(ConversionFlag::Repr),
346+
std::u8::MAX => Some(ConversionFlag::None),
347+
_ => None,
348+
}
349349
}
350-
);
350+
#[inline]
351+
fn to_op_arg(self) -> u32 {
352+
self as i8 as u8 as u32
353+
}
354+
}
351355

352356
op_arg_enum!(
353357
/// The kind of Raise that occurred.

0 commit comments

Comments
 (0)