Allow downcasting to data in piped function calls.

We have been a bit too strict on disallowing 'allow_cast' propagations. This is really only problematic for nested elements like Tuple's elements or App's args. However, for linked and unbound var it is probably okay, and it certainly is as well for function arguments.
This commit is contained in:
KtorZ 2024-03-23 11:56:38 +01:00 committed by Kasey
parent 4e8042fd06
commit a3f7b48ec3
2 changed files with 37 additions and 3 deletions

View File

@ -2131,6 +2131,40 @@ fn can_down_cast_to_data_always() {
assert!(check(parse(source_code)).is_ok()); assert!(check(parse(source_code)).is_ok());
} }
#[test]
fn can_down_cast_to_data_on_fn_call() {
let source_code = r#"
pub type Foo { Foo }
pub fn serialise(data: Data) -> ByteArray {
""
}
test foo() {
serialise(Foo) == ""
}
"#;
assert!(check(parse(source_code)).is_ok());
}
#[test]
fn can_down_cast_to_data_on_pipe() {
let source_code = r#"
pub type Foo { Foo }
pub fn serialise(data: Data) -> ByteArray {
""
}
test foo() {
(Foo |> serialise) == ""
}
"#;
assert!(check(parse(source_code)).is_ok());
}
#[test] #[test]
fn correct_span_for_backpassing_args() { fn correct_span_for_backpassing_args() {
let source_code = r#" let source_code = r#"

View File

@ -1431,7 +1431,7 @@ impl<'a> Environment<'a> {
lhs, lhs,
Type::with_alias(tipo.clone(), alias.clone()), Type::with_alias(tipo.clone(), alias.clone()),
location, location,
false, allow_cast,
); );
} }
} }
@ -1470,7 +1470,7 @@ impl<'a> Environment<'a> {
Ok(()) Ok(())
} }
Action::Unify(t) => self.unify(t, rhs, location, false), Action::Unify(t) => self.unify(t, rhs, location, allow_cast),
Action::CouldNotUnify => Err(Error::CouldNotUnify { Action::CouldNotUnify => Err(Error::CouldNotUnify {
location, location,
@ -1550,7 +1550,7 @@ impl<'a> Environment<'a> {
}, },
) if args1.len() == args2.len() => { ) if args1.len() == args2.len() => {
for (a, b) in args1.iter().zip(args2) { for (a, b) in args1.iter().zip(args2) {
self.unify(a.clone(), b.clone(), location, false) self.unify(a.clone(), b.clone(), location, allow_cast)
.map_err(|_| Error::CouldNotUnify { .map_err(|_| Error::CouldNotUnify {
location, location,
expected: lhs.clone(), expected: lhs.clone(),