Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type checker not substituting type var when unifying match arms #735

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project does not currently adhere to a particular versioning scheme.

## [Unreleased]

### Fixed
- Fix type checker not properly unifying all the arms of a match expression.

## [0.2.37] - 2024-10-18

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/fun/check/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ fn infer_match_cases(
// Recurse and unify with the other arms.
let s = s2.compose(s1);
let (s_rest, t_rest) = infer_match_cases(env.subst(&s), book, types, adt, rest, adt_s, var_gen)?;
let (t_final, s_final) = unify_term(&t1, &t_rest, bod)?;
let (t_final, s_final) = unify_term(&t1.subst(&s), &t_rest, bod)?;

Ok((s_final.compose(s_rest).compose(s), t_final))
} else {
Expand Down
17 changes: 16 additions & 1 deletion tests/golden_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! CLI tool. Then, run `cargo insta review` to review these changes.

use bend::{
compile_book, desugar_book,
check_book, compile_book, desugar_book,
diagnostics::{Diagnostics, DiagnosticsConfig, Severity},
fun::{
load_book::do_parse_book, net_to_term::net_to_term, parser::ParseBook, term_to_net::Labels, Book, Ctx,
Expand Down Expand Up @@ -334,6 +334,21 @@ fn parse_file() {
})
}

/// Runs the check command on a file.
#[test]
fn check_file() {
run_golden_test_dir(function_name!(), &|code, path| {
let compile_opts = CompileOpts::default();
let diagnostics_cfg = DiagnosticsConfig {
unused_definition: Severity::Allow,
..DiagnosticsConfig::new(Severity::Error, true)
};
let mut book = parse_book_single_file(code, path)?;
check_book(&mut book, diagnostics_cfg, compile_opts)?;
Ok(book.to_string())
})
}

/// Runs compilation up to the last term-level pass (`bend desugar` command).
#[test]
fn desugar_file() {
Expand Down
14 changes: 14 additions & 0 deletions tests/golden_tests/check_file/type_err_match_arm.bend
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Nat_:
Zero
Succ {x: Unit, pred: Nat_}

type Unit:
Unit

Test1: Nat_ -> (Nat_, Nat_)
Test1 = λx match x {
Nat_/Zero: (Nat_/Zero, Nat_/Zero)
Nat_/Succ: (x.x, x.pred)
}

main = *
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/desugar_file/fail_type_bad_rec_fn_adt.bend
input_file: tests/golden_tests/check_file/fail_type_bad_rec_fn_adt.bend
---
Errors:
In λa match a { Box/Box b: (Erase b); }:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/desugar_file/non_exaustive_limit.bend
input_file: tests/golden_tests/check_file/non_exaustive_limit.bend
---
Errors:
In tests/golden_tests/desugar_file/non_exaustive_limit.bend :
In tests/golden_tests/check_file/non_exaustive_limit.bend :
In definition 'Bar':
Non-exhaustive pattern matching rule. Constructor 'Foo/B' of type 'Foo' not covered
9 changes: 9 additions & 0 deletions tests/snapshots/check_file__type_err_match_arm.bend.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/check_file/type_err_match_arm.bend
---
Errors:
In tests/golden_tests/check_file/type_err_match_arm.bend :
In definition 'Test1':
In (Nat_/Zero, Nat_/Zero):
Can't unify '(Nat_, Nat_)' and '(Unit, Nat_)'.
Loading