Merge pull request #1055 from rtfeldman/err-void

Err with void type
This commit is contained in:
Richard Feldman 2021-03-07 23:07:43 -05:00 committed by GitHub
commit eee4cb1456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 3 deletions

View File

@ -4139,12 +4139,13 @@ fn sorted_field_symbols<'a>(
let mut field_symbols_temp = Vec::with_capacity_in(args.len(), env.arena);
for (var, mut arg) in args.drain(..) {
// Layout will unpack this unwrapped tack if it only has one (non-zero-sized) field
// Layout will unpack this unwrapped tag if it only has one (non-zero-sized) field
let layout = match layout_cache.from_var(env.arena, var, env.subs) {
Ok(cached) => cached,
Err(LayoutProblem::UnresolvedTypeVar(_)) => {
// this argument has type `forall a. a`, which is isomorphic to
// the empty type (Void, Never, the empty tag union `[]`)
// Note it does not catch the use of `[]` currently.
use roc_can::expr::Expr;
arg.value = Expr::RuntimeError(RuntimeError::VoidValue);
Layout::Struct(&[])

View File

@ -1539,7 +1539,9 @@ pub fn union_sorted_tags_help<'a>(
}
Err(LayoutProblem::UnresolvedTypeVar(_)) => {
// If we encounter an unbound type var (e.g. `Ok *`)
// then it's zero-sized; drop the argument.
// then it's zero-sized; In the future we may drop this argument
// completely, but for now we represent it with the empty struct
layouts.push(Layout::Struct(&[]))
}
Err(LayoutProblem::Erroneous) => {
// An erroneous type var will code gen to a runtime
@ -1616,7 +1618,9 @@ pub fn union_sorted_tags_help<'a>(
}
Err(LayoutProblem::UnresolvedTypeVar(_)) => {
// If we encounter an unbound type var (e.g. `Ok *`)
// then it's zero-sized; drop the argument.
// then it's zero-sized; In the future we may drop this argument
// completely, but for now we represent it with the empty struct
arg_layouts.push(Layout::Struct(&[]));
}
Err(LayoutProblem::Erroneous) => {
// An erroneous type var will code gen to a runtime

View File

@ -98,3 +98,51 @@ fn result_map_err() {
i64
);
}
#[test]
fn err_type_var() {
assert_evals_to!(
indoc!(
r#"
Result.map (Ok 3) (\x -> x + 1)
|> Result.withDefault -1
"#
),
4,
i64
);
}
#[test]
fn err_type_var_annotation() {
assert_evals_to!(
indoc!(
r#"
ok : Result I64 *
ok = Ok 3
Result.map ok (\x -> x + 1)
|> Result.withDefault -1
"#
),
4,
i64
);
}
#[test]
fn err_empty_tag_union() {
assert_evals_to!(
indoc!(
r#"
ok : Result I64 []
ok = Ok 3
Result.map ok (\x -> x + 1)
|> Result.withDefault -1
"#
),
4,
i64
);
}