Skip to content

Commit

Permalink
Fix unnecessary check inside of the match exhaustivity algorithm (#2009)
Browse files Browse the repository at this point in the history
* Allow for nested patterns.

* Remove the comment in code.
  • Loading branch information
emilyaherbert authored Jun 16, 2022
1 parent 458724e commit 9791173
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -629,16 +629,11 @@ impl ConstructorFactory {
}

fn resolve_possible_types(&self, pattern: &Pattern, span: &Span) -> CompileResult<&TypeInfo> {
let mut warnings = vec![];
let warnings = vec![];
let mut errors = vec![];
let mut type_info = None;
for possible_type in self.possible_types.iter() {
let matches = check!(
pattern.matches_type_info(possible_type, span),
continue,
warnings,
errors
);
let matches = pattern.matches_type_info(possible_type, span);
if matches {
type_info = Some(possible_type);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,13 +657,7 @@ impl Pattern {
}
}

pub(crate) fn matches_type_info(
&self,
type_info: &TypeInfo,
span: &Span,
) -> CompileResult<bool> {
let warnings = vec![];
let mut errors = vec![];
pub(crate) fn matches_type_info(&self, type_info: &TypeInfo, span: &Span) -> bool {
match (self, type_info) {
(pattern, TypeInfo::Ref(type_id, _)) => {
pattern.matches_type_info(&look_up_type_id(*type_id), span)
Expand All @@ -680,20 +674,13 @@ impl Pattern {
..
},
) => {
let res = l_enum_name.as_str() == r_enum_name.as_str()
l_enum_name.as_str() == r_enum_name.as_str()
&& variant_types
.iter()
.map(|x| x.name.clone())
.any(|x| x.as_str() == variant_name.as_str());
ok(res, warnings, errors)
}
_ => {
errors.push(CompileError::Unimplemented(
"cannot yet compare this pattern with this type",
span.clone(),
));
err(warnings, errors)
.map(|variant_type| variant_type.name.clone())
.any(|name| name.as_str() == variant_name.as_str())
}
_ => false, // NOTE: We may need to expand this in the future
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ pub fn run(locked: bool, filter_regex: Option<regex::Regex>) {
0xdc, 0xe7, 0x7f, 0x70,
])), // "ReturnData":{"data":"0000000000000002", .. }
),
(
"should_pass/language/match_expressions_with_self",
ProgramState::Return(1),
),
(
"should_pass/test_contracts/auth_testing_contract",
ProgramState::Revert(0),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'abort_control_flow_good'
source = 'root'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-CB060A17C0778A7E'
dependencies = []

[[package]]
name = 'std'
source = 'path+from-root-CB060A17C0778A7E'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "abort_control_flow_good"

[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,28 @@
script;

use core::ops::Eq;
use std::assert::*;

enum Initialized {
True: (),
False: (),
}

impl Eq for Initialized {
fn eq(self, other: Self) -> bool {
match (self, other) {
(Initialized::True, Initialized::True) => true,
(Initialized::False, Initialized::False) => true,
_ => false,
}
}
}

fn main() -> u64 {
let a = Initialized::True;
let b = Initialized::False;
let c = a == b;
assert(c == false);

1
}

0 comments on commit 9791173

Please sign in to comment.