mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-21 07:49:17 +03:00
Merge pull request #3657 from rtfeldman/when-on-strings
implement when on multiple string patterns
This commit is contained in:
commit
ff0edc72c3
@ -96,6 +96,23 @@ enum Test<'a> {
|
||||
},
|
||||
}
|
||||
|
||||
impl<'a> Test<'a> {
|
||||
fn can_be_switch(&self) -> bool {
|
||||
match self {
|
||||
Test::IsCtor { .. } => true,
|
||||
Test::IsInt(_, int_width) => {
|
||||
// llvm does not like switching on 128-bit values
|
||||
!matches!(int_width, IntWidth::U128 | IntWidth::I128)
|
||||
}
|
||||
Test::IsFloat(_, _) => true,
|
||||
Test::IsDecimal(_) => false,
|
||||
Test::IsStr(_) => false,
|
||||
Test::IsBit(_) => true,
|
||||
Test::IsByte { .. } => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use std::hash::{Hash, Hasher};
|
||||
impl<'a> Hash for Test<'a> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
@ -1833,7 +1850,8 @@ fn decide_to_branching<'a>(
|
||||
Test::IsBit(v) => v as u64,
|
||||
Test::IsByte { tag_id, .. } => tag_id as u64,
|
||||
Test::IsCtor { tag_id, .. } => tag_id as u64,
|
||||
other => todo!("other {:?}", other),
|
||||
Test::IsDecimal(_) => unreachable!("decimals cannot be switched on"),
|
||||
Test::IsStr(_) => unreachable!("strings cannot be switched on"),
|
||||
};
|
||||
|
||||
// branch info is only useful for refcounted values
|
||||
@ -2004,15 +2022,30 @@ fn fanout_decider<'a>(
|
||||
edges: Vec<(GuardedTest<'a>, DecisionTree<'a>)>,
|
||||
) -> Decider<'a, u64> {
|
||||
let fallback_decider = tree_to_decider(fallback);
|
||||
let necessary_tests = edges
|
||||
let necessary_tests: Vec<_> = edges
|
||||
.into_iter()
|
||||
.map(|(test, tree)| fanout_decider_help(tree, test))
|
||||
.collect();
|
||||
|
||||
Decider::FanOut {
|
||||
path,
|
||||
tests: necessary_tests,
|
||||
fallback: Box::new(fallback_decider),
|
||||
if necessary_tests.iter().all(|(t, _)| t.can_be_switch()) {
|
||||
Decider::FanOut {
|
||||
path,
|
||||
tests: necessary_tests,
|
||||
fallback: Box::new(fallback_decider),
|
||||
}
|
||||
} else {
|
||||
// in llvm, we cannot switch on strings so must chain
|
||||
let mut decider = fallback_decider;
|
||||
|
||||
for (test, branch_decider) in necessary_tests.into_iter().rev() {
|
||||
decider = Decider::Chain {
|
||||
test_chain: vec![(path.clone(), test)],
|
||||
success: Box::new(branch_decider),
|
||||
failure: Box::new(decider),
|
||||
};
|
||||
}
|
||||
|
||||
decider
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3648,3 +3648,34 @@ fn promote_u128_number_layout() {
|
||||
u128
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn when_on_decimals() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
when 42.42dec is
|
||||
42.42 -> 42
|
||||
0.05 -> 1
|
||||
3.14 -> 2
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
42,
|
||||
i64
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
when 42.42dec is
|
||||
0.05 -> 1
|
||||
3.14 -> 2
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
4,
|
||||
i64
|
||||
);
|
||||
}
|
||||
|
@ -102,30 +102,6 @@ fn fn_record() {
|
||||
i64
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
rec = { x: 15, y: 17, z: 19 }
|
||||
|
||||
rec.y
|
||||
"#
|
||||
),
|
||||
17,
|
||||
i64
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
rec = { x: 15, y: 17, z: 19 }
|
||||
|
||||
rec.z
|
||||
"#
|
||||
),
|
||||
19,
|
||||
i64
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -1813,3 +1813,36 @@ fn llvm_wasm_str_layout_small() {
|
||||
[i32; 3]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn when_on_strings() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
when "Deyr fé, deyja frændr" is
|
||||
"Deyr fé, deyja frændr" -> 42
|
||||
"deyr sjalfr it sama" -> 1
|
||||
"en orðstírr deyr aldregi" -> 2
|
||||
"hveim er sér góðan getr" -> 3
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
42,
|
||||
i64
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
when "Deyr fé, deyja frændr" is
|
||||
"deyr sjalfr it sama" -> 1
|
||||
"en orðstírr deyr aldregi" -> 2
|
||||
"hveim er sér góðan getr" -> 3
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
4,
|
||||
i64
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user