-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Fix couple of bugs in handling returns in if blocks #2029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
f0966bc
7586b4e
f0a910b
05408aa
0793e33
024209f
92c3bd5
b63baba
4a4c6e3
e5559a5
84852d0
1a9a16b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /// If this expression deterministically_aborts 100% of the time, this function returns | ||
| /// `true`. Used in dead-code and control-flow analysis. | ||
| pub(crate) trait DeterministicallyAborts { | ||
| pub trait DeterministicallyAborts { | ||
| fn deterministically_aborts(&self) -> bool; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| mod deterministically_aborts; | ||
| pub mod deterministically_aborts; | ||
| mod json_abi_string; | ||
| mod to_json_abi; | ||
|
|
||
| pub(crate) use deterministically_aborts::*; | ||
| pub use deterministically_aborts::*; | ||
| pub(crate) use json_abi_string::*; | ||
| pub use to_json_abi::*; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,4 +171,21 @@ impl Constant { | |
| )); | ||
| Value::new_constant(context, value, span_md_idx) | ||
| } | ||
|
|
||
| pub fn eq(&self, context: &Context, other: &Constant) -> bool { | ||
|
||
| self.ty.eq(context, &other.ty) | ||
| && match (&self.value, &other.value) { | ||
| (ConstantValue::Undef, ConstantValue::Undef) | ||
| | (ConstantValue::Unit, ConstantValue::Unit) => true, | ||
| (ConstantValue::Bool(v1), ConstantValue::Bool(v2)) => v1 == v2, | ||
| (ConstantValue::Uint(v1), ConstantValue::Uint(v2)) => v1 == v2, | ||
| (ConstantValue::B256(a1), ConstantValue::B256(a2)) => a1 == a2, | ||
| (ConstantValue::String(s1), ConstantValue::String(s2)) => s1 == s2, | ||
| (ConstantValue::Array(a1), ConstantValue::Array(a2)) | ||
| | (ConstantValue::Struct(a1), ConstantValue::Struct(a2)) => { | ||
| a1.iter().zip(a2.iter()).all(|(c1, c2)| c1.eq(context, c2)) | ||
| } | ||
| _ => false, | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [[package]] | ||
| name = 'const_inits' | ||
| source = 'root' | ||
| dependencies = ['std'] | ||
|
|
||
| [[package]] | ||
| name = 'core' | ||
| source = 'path+from-root-76FD265A93458D61' | ||
| dependencies = [] | ||
|
|
||
| [[package]] | ||
| name = 'std' | ||
| source = 'path+from-root-76FD265A93458D61' | ||
| dependencies = ['core'] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [project] | ||
| authors = ["Fuel Labs <[email protected]>"] | ||
| entry = "main.sw" | ||
| license = "Apache-2.0" | ||
| name = "const_inits" | ||
|
|
||
| [dependencies] | ||
| std = { path = "../../../../../../../sway-lib-std" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [ | ||
| { | ||
| "inputs": [], | ||
| "name": "main", | ||
| "outputs": [ | ||
| { | ||
| "components": null, | ||
| "name": "", | ||
| "type": "u64" | ||
| } | ||
| ], | ||
| "type": "function" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| script; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice change! Thanks. Do you mind also adding a test case with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Done. But there's a failure in one of the vec tests. Let me try and debug that. |
||
|
|
||
| use std::{assert::assert, logging::log}; | ||
|
|
||
| fn foo(b: bool) -> u64 { | ||
| if b { | ||
| 101 | ||
| } else { | ||
| 102 | ||
| } | ||
| } | ||
|
|
||
| fn bar(b: bool) -> u64 { | ||
| if b { | ||
| return 101; | ||
| } else { | ||
| return 102; | ||
| } | ||
| } | ||
|
|
||
| fn bell(b: bool) -> u64 { | ||
| if b { | ||
| return 101; | ||
| } else { | ||
| 102 | ||
| } | ||
| } | ||
|
|
||
| fn moo(b: bool) -> u64 { | ||
| if b { | ||
| 101 | ||
| } else { | ||
| return 102; | ||
| } | ||
| } | ||
|
|
||
| fn main() -> u64 { | ||
| assert(foo(true) == bar(true)); | ||
| assert(foo(false) == bar(false)); | ||
| assert(foo(true) == bell(true)); | ||
| assert(foo(false) == bell(false)); | ||
| assert(foo(true) == moo(true)); | ||
| assert(foo(false) == moo(false)); | ||
|
|
||
| 2 | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is somehow covering up the problem rather than solving it, though I'm not sure why, and I don't immediately have a better idea. I think somehow the blocks themselves should handle whether they terminate, and maybe return
Option<Value>which we would pass directly onto thephi. But that might not work or be too hard... I'm just thinking out loud. This is OK for now, but I feel like the 'aborts' problem is more general than this -- what about other control flow like loops?I think we can go with this for now but add a new issue to look at blocks which abort more generally.