Skip to content

Commit

Permalink
Don't type error on variant type check fail
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Feb 7, 2025
1 parent 30e74a7 commit 037947e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 7 additions & 2 deletions crates/rune/src/runtime/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2628,7 +2628,10 @@ impl Vm {
}
Repr::Any(any) => match enum_hash {
Result::<Value, Value>::HASH => {
let result = vm_try!(any.borrow_ref::<Result<Value, Value>>());
let Some(result) = vm_try!(any.try_borrow_ref::<Result<Value, Value>>())
else {
break 'out false;
};

break 'out match (&*result, variant_hash) {
(Ok(..), hash!(::std::result::Result::Ok)) => true,
Expand All @@ -2637,7 +2640,9 @@ impl Vm {
};
}
Option::<Value>::HASH => {
let option = vm_try!(any.borrow_ref::<Option<Value>>());
let Some(option) = vm_try!(any.try_borrow_ref::<Option<Value>>()) else {
break 'out false;
};

break 'out match (&*option, variant_hash) {
(None, hash!(::std::option::Option::None)) => true,
Expand Down
37 changes: 37 additions & 0 deletions crates/rune/tests/matching.rn
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,40 @@ fn match_extraction_struct() {

assert_eq!(inner(v), 42);
}

/// This tests that matching against a built-in type with a value which is not
/// the same type does not error due to a type mismatch.
#[test]
fn match_builtin_non_err() {
let outcome = match 42 {
Some(42) => false,
42 => true,
_ => false,
};

assert!(outcome, "built-in Option::Some should match");

let outcome = match 42 {
None => false,
42 => true,
_ => false,
};

assert!(outcome, "built-in Option::None should match");

let outcome = match 42 {
Ok(42) => false,
42 => true,
_ => false,
};

assert!(outcome, "built-in Result::Ok should match");

let outcome = match 42 {
Err(42) => false,
42 => true,
_ => false,
};

assert!(outcome, "built-in Result::Err should match");
}

0 comments on commit 037947e

Please sign in to comment.