Skip to content

Commit

Permalink
Fix projection type inference bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Oct 15, 2022
1 parent 1981174 commit 4e2b36b
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 309 deletions.
70 changes: 43 additions & 27 deletions compiler/erg_compiler/context/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,15 +921,10 @@ impl Context {
}
// in Methods
if self.name == sub.qual_name() {
if let Ok(obj) = self.get_const_local(&Token::symbol(&rhs), &self.name) {
if let ValueObj::Type(quant_t) = obj {
let subst_ctx = SubstContext::new(&sub, self, t_loc);
let t = subst_ctx.substitute(quant_t.typ().clone())?;
let t = self.eval_t_params(t, level, t_loc)?;
return Ok(t);
} else {
todo!()
}
if let Some(t) =
self.validate_and_project(&sub, opt_sup.as_ref(), &rhs, self, level, t_loc)
{
return Ok(t);
}
}
for ty_ctx in self.get_nominal_super_type_ctxs(&sub).ok_or_else(|| {
Expand All @@ -942,15 +937,10 @@ impl Context {
None, // TODO:
)
})? {
if let Ok(obj) = ty_ctx.get_const_local(&Token::symbol(&rhs), &self.name) {
if let ValueObj::Type(quant_t) = obj {
let subst_ctx = SubstContext::new(&sub, self, t_loc);
let t = subst_ctx.substitute(quant_t.typ().clone())?;
let t = self.eval_t_params(t, level, t_loc)?;
return Ok(t);
} else {
todo!()
}
if let Some(t) =
self.validate_and_project(&sub, opt_sup.as_ref(), &rhs, ty_ctx, level, t_loc)
{
return Ok(t);
}
for (class, methods) in ty_ctx.methods_list.iter() {
match (class, &opt_sup) {
Expand All @@ -966,15 +956,10 @@ impl Context {
}
_ => {}
}
if let Ok(obj) = methods.get_const_local(&Token::symbol(&rhs), &self.name) {
if let ValueObj::Type(quant_t) = obj {
let subst_ctx = SubstContext::new(&sub, self, t_loc);
let t = subst_ctx.substitute(quant_t.typ().clone())?;
let t = self.eval_t_params(t, level, t_loc)?;
return Ok(t);
} else {
todo!()
}
if let Some(t) =
self.validate_and_project(&sub, opt_sup.as_ref(), &rhs, methods, level, t_loc)
{
return Ok(t);
}
}
}
Expand Down Expand Up @@ -1014,6 +999,37 @@ impl Context {
}
}

fn validate_and_project(
&self,
sub: &Type,
opt_sup: Option<&Type>,
rhs: &str,
methods: &Context,
level: usize,
t_loc: Location,
) -> Option<Type> {
if let Ok(obj) = methods.get_const_local(&Token::symbol(rhs), &self.name) {
#[allow(clippy::single_match)]
match (&opt_sup, methods.impl_of()) {
(Some(sup), Some(trait_)) => {
if !self.supertype_of(&trait_, sup) {
return None;
}
}
_ => {}
}
if let ValueObj::Type(quant_t) = obj {
let subst_ctx = SubstContext::new(sub, self, t_loc);
let t = subst_ctx.substitute(quant_t.typ().clone()).ok()?;
let t = self.eval_t_params(t, level, t_loc).ok()?;
return Some(t);
} else {
todo!()
}
}
None
}

fn eval_proj_call(
&self,
lhs: TyParam,
Expand Down
Loading

0 comments on commit 4e2b36b

Please sign in to comment.