From 43c2ea13fe3db72814ef1723e8d478e7b14ef27e Mon Sep 17 00:00:00 2001 From: Sean Billig Date: Mon, 3 Jan 2022 11:45:59 -0800 Subject: [PATCH] Lower `revert` argument (#619) --- crates/lowering/src/mappers/functions.rs | 4 +++- .../tests/snapshots/lowering__module_const.snap | 2 +- crates/test-files/fixtures/crashes/revert_const.fe | 7 +++++++ crates/tests/src/crashes.rs | 1 + newsfragments/619.bugfix.md | 13 +++++++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 crates/test-files/fixtures/crashes/revert_const.fe create mode 100644 newsfragments/619.bugfix.md diff --git a/crates/lowering/src/mappers/functions.rs b/crates/lowering/src/mappers/functions.rs index 8d49acdf7e..e8049d6230 100644 --- a/crates/lowering/src/mappers/functions.rs +++ b/crates/lowering/src/mappers/functions.rs @@ -221,7 +221,9 @@ fn func_stmt(context: &mut FnContext, stmt: Node) -> Vec vec![stmt.kind], fe::FuncStmt::Break => vec![stmt.kind], fe::FuncStmt::Continue => vec![stmt.kind], - fe::FuncStmt::Revert { .. } => vec![stmt.kind], + fe::FuncStmt::Revert { error } => vec![fe::FuncStmt::Revert { + error: error.map(|expr| expressions::expr(context, expr)), + }], }; let span = stmt.span; diff --git a/crates/lowering/tests/snapshots/lowering__module_const.snap b/crates/lowering/tests/snapshots/lowering__module_const.snap index 93a0c12650..4363034a87 100644 --- a/crates/lowering/tests/snapshots/lowering__module_const.snap +++ b/crates/lowering/tests/snapshots/lowering__module_const.snap @@ -42,7 +42,7 @@ contract Foo: return 3 * 10 else: if true: - revert Bar(val=THREE) + revert Bar(val=3) diff --git a/crates/test-files/fixtures/crashes/revert_const.fe b/crates/test-files/fixtures/crashes/revert_const.fe new file mode 100644 index 0000000000..b2ec81b4ea --- /dev/null +++ b/crates/test-files/fixtures/crashes/revert_const.fe @@ -0,0 +1,7 @@ +const FOO: u256 = 0x101 + +struct Error: + code: u256 + +fn exit(): + revert Error(code = FOO) diff --git a/crates/tests/src/crashes.rs b/crates/tests/src/crashes.rs index 906afec8dd..31f88c6c80 100644 --- a/crates/tests/src/crashes.rs +++ b/crates/tests/src/crashes.rs @@ -18,3 +18,4 @@ macro_rules! test_file { test_file! { agroce531 } test_file! { agroce550 } +test_file! { revert_const } diff --git a/newsfragments/619.bugfix.md b/newsfragments/619.bugfix.md new file mode 100644 index 0000000000..69c9468037 --- /dev/null +++ b/newsfragments/619.bugfix.md @@ -0,0 +1,13 @@ +The argument to `revert` wasn't being lowered by the compiler, +meaning that some `revert` calls would cause a compiler panic +in later stages. For example: + +``` +const BAD_MOJO: u256 = 0xdeaddead + +struct Error: + code: u256 + +fn fail(): + revert Error(code = BAD_MOJO) +```