Allow int/float to coerce in type checker (#679)

This commit is contained in:
JT 2022-01-06 07:58:58 +11:00 committed by GitHub
parent 3c2a336ef9
commit cc1ae969fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View file

@ -8,6 +8,8 @@ use nu_protocol::{
pub fn type_compatible(lhs: &Type, rhs: &Type) -> bool { pub fn type_compatible(lhs: &Type, rhs: &Type) -> bool {
match (lhs, rhs) { match (lhs, rhs) {
(Type::List(c), Type::List(d)) => type_compatible(c, d), (Type::List(c), Type::List(d)) => type_compatible(c, d),
(Type::Number, Type::Int) => true,
(Type::Number, Type::Float) => true,
(Type::Unknown, _) => true, (Type::Unknown, _) => true,
(_, Type::Unknown) => true, (_, Type::Unknown) => true,
(lhs, rhs) => lhs == rhs, (lhs, rhs) => lhs == rhs,

View file

@ -14,3 +14,13 @@ fn type_in_list_of_this_type() -> TestResult {
fn type_in_list_of_non_this_type() -> TestResult { fn type_in_list_of_non_this_type() -> TestResult {
fail_test(r#"'hello' in [41 42 43]"#, "mismatched for operation") fail_test(r#"'hello' in [41 42 43]"#, "mismatched for operation")
} }
#[test]
fn number_int() -> TestResult {
run_test(r#"def foo [x:number] { $x }; foo 1"#, "1")
}
#[test]
fn number_float() -> TestResult {
run_test(r#"def foo [x:number] { $x }; foo 1.4"#, "1.4")
}