Skip to content

Commit

Permalink
fix: assert de-optimization bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Jan 21, 2024
1 parent cfc2214 commit 20a94b8
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/erg_common/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub const fn get_ver_from_magic_num(magic_num: u32) -> PythonVersion {
3420..=3425 => PythonVersion::new(3, Some(9), Some(0)),
3430..=3439 => PythonVersion::new(3, Some(10), Some(0)), // main: 3439
3495 => PythonVersion::new(3, Some(11), Some(0)),
3531 => PythonVersion::new(3, Some(12), Some(0)),
_ => panic!("unknown magic number"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ impl PyCodeGenerator {
let kind = RegisterNameKind::from_ident(&ident);
let escaped = escape_ident(ident);
match &escaped[..] {
"if__" | "for__" | "while__" | "with__" | "discard__" => {
"if__" | "for__" | "while__" | "with__" | "discard__" | "assert__" => {
self.load_control();
}
"int__" | "nat__" | "str__" | "float__" => {
Expand Down
3 changes: 2 additions & 1 deletion crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ impl Context {
self.register_py_builtin(FUNC_ARRAY, t_array, Some(FUNC_LIST), 215);
self.register_py_builtin(FUNC_ASCII, t_ascii, Some(FUNC_ASCII), 53);
// Leave as `Const`, as it may negatively affect assert casting.
self.register_builtin_erg_impl(FUNC_ASSERT, t_assert, Const, vis.clone());
let name = if PYTHON_MODE { "assert" } else { "assert__" };
self.register_builtin_py_impl(FUNC_ASSERT, t_assert, Const, vis.clone(), Some(name));
self.register_builtin_py_impl(FUNC_BIN, t_bin, Immutable, vis.clone(), Some(FUNC_BIN));
self.register_builtin_py_impl(
FUNC_BYTES,
Expand Down
7 changes: 4 additions & 3 deletions crates/erg_compiler/lib/std/exception.er
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ unsound = import "unsound"
# TODO: exception: Exception
unsound.pyexec("""
def try_(p, exc=lambda _: None, els=lambda: None, fin=lambda: None):
__result = None
try:
res = p()
__result = p()
except Exception as e:
res = exc(e)
__result = exc(e)
else:
els()
finally:
fin()
return res
return __result
""")
.try! = unsound.pyeval("try_")
assert .try! in |T, U|(
Expand Down

0 comments on commit 20a94b8

Please sign in to comment.