implement when on multiple string patterns

This commit is contained in:
Folkert 2022-07-29 12:13:21 +02:00
parent d409e5f93c
commit d0f1500dad
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C
3 changed files with 103 additions and 6 deletions

View File

@ -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,16 +2022,31 @@ 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();
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
}
}
fn fanout_decider_help<'a>(

View File

@ -3651,3 +3651,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
);
}

View File

@ -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
);
}