Skip to content

Commit

Permalink
fix: recursive type bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Aug 17, 2023
1 parent efcf23b commit 5ef1288
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 9 deletions.
8 changes: 8 additions & 0 deletions crates/erg_common/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,14 @@ macro_rules! log {
}
}};

(backtrace) => {{
if cfg!(feature = "debug") {
use $crate::style::*;
$crate::debug_info!();
println!("\n{}", std::backtrace::Backtrace::capture());
}
}};

($($arg: tt)*) => {{
if cfg!(feature = "debug") {
use $crate::style::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ impl Context {
readable_name(name.inspect()),
&expect,
&found,
e.core.get_hint().map(|s| s.to_string()),
// e.core.get_hint().map(|s| s.to_string()),
)
})
.collect(),
Expand Down
24 changes: 19 additions & 5 deletions crates/erg_compiler/context/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ impl<'c, 'l, L: Locational> Unifier<'c, 'l, L> {
self.occur_inner(lhs, l)?;
self.occur_inner(lhs, r)
}
/*(Or(l, r), rhs) | (And(l, r), rhs) => {
self.occur_inner(l, rhs, loc)?;
self.occur_inner(r, rhs, loc)
}*/
(Or(l, r), rhs) | (And(l, r), rhs) => {
self.occur_inner(l, rhs)?;
self.occur_inner(r, rhs)
}
_ => Ok(()),
}
}
Expand All @@ -158,7 +158,7 @@ impl<'c, 'l, L: Locational> Unifier<'c, 'l, L> {
(FreeVar(fv), _) if fv.is_linked() => self.occur_inner(&fv.crack(), maybe_sup),
(_, FreeVar(fv)) if fv.is_linked() => self.occur_inner(maybe_sub, &fv.crack()),
(FreeVar(sub), FreeVar(sup)) => {
if sub.is_unbound() && sup.is_unbound() && sub == sup {
if sub.is_unbound() && sup.is_unbound() && sub.addr_eq(sup) {
Err(TyCheckErrors::from(TyCheckError::subtyping_error(
self.ctx.cfg.input.clone(),
line!() as usize,
Expand All @@ -168,6 +168,20 @@ impl<'c, 'l, L: Locational> Unifier<'c, 'l, L> {
self.ctx.caused_by(),
)))
} else {
if sub.constraint_is_sandwiched() {
let (sub_t, sup_t) = sub.get_subsup().unwrap();
sub.do_avoiding_recursion(|| {
self.occur_inner(&sub_t, maybe_sup)?;
self.occur_inner(&sup_t, maybe_sup)
})?;
}
if sup.constraint_is_sandwiched() {
let (sub_t, sup_t) = sup.get_subsup().unwrap();
sup.do_avoiding_recursion(|| {
self.occur_inner(maybe_sub, &sub_t)?;
self.occur_inner(maybe_sub, &sup_t)
})?;
}
Ok(())
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/erg_compiler/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@ mod test {
name,
&expect,
&found,
None,
);
errors.push(err);

Expand Down
10 changes: 8 additions & 2 deletions crates/erg_compiler/error/tycheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl TyCheckError {
name: &str,
expect: &Type,
found: &Type,
hint: Option<String>,
// hint: Option<String>,
) -> Self {
let name = name.with_color(Color::Yellow);
let mut expct = StyledStrings::default();
Expand All @@ -198,13 +198,19 @@ impl TyCheckError {
"english" =>fnd.push_str("but found: "),
);
fnd.push_str_with_color_and_attr(format!("{found}"), ERR, ATTR);
let hint = switch_lang!(
"japanese" => "自身を返す関数は定義できません",
"simplified_chinese" => "不能定义返回自身的函数",
"traditional_chinese" => "不能定義返回自身的函數",
"english" => "cannot define a function that returns itself",
);

Self::new(
ErrorCore::new(
vec![SubMessage::ambiguous_new(
loc,
vec![expct.to_string(), fnd.to_string()],
hint,
Some(hint.into()),
)],
switch_lang!(
"japanese" => format!("{name}の戻り値の型が違います"),
Expand Down
7 changes: 7 additions & 0 deletions tests/should_err/recursive_fn.er
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
f() = f # ERR

g 0 = 0
g _: Int = g # ERR

# left() = right() # ERR
# right() = left
5 changes: 5 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ fn exec_quantified_err() -> Result<(), ()> {
expect_failure("tests/should_err/quantified.er", 0, 3)
}

#[test]
fn exec_recursive_fn_err() -> Result<(), ()> {
expect_failure("tests/should_err/recursive_fn.er", 0, 2)
}

#[test]
fn exec_refinement_err() -> Result<(), ()> {
expect_failure("tests/should_err/refinement.er", 0, 8)
Expand Down

0 comments on commit 5ef1288

Please sign in to comment.