diff --git a/compiler/erg_compiler/context/compare.rs b/compiler/erg_compiler/context/compare.rs index 6dfa9127..2d9fa91f 100644 --- a/compiler/erg_compiler/context/compare.rs +++ b/compiler/erg_compiler/context/compare.rs @@ -606,10 +606,10 @@ impl Context { self.poly_supertype_of(lhs, lparams, rparams) } (MonoQVar(name), r) | (PolyQVar { name, .. }, r) => { - panic!("Not instantiated type variable: {name}, r: {r}") + panic!("internal error: not instantiated type variable: '{name}, r: {r}") } (l, MonoQVar(name)) | (l, PolyQVar { name, .. }) => { - panic!("Not instantiated type variable: {name}, l: {l}") + panic!("internal error: not instantiated type variable: '{name}, l: {l}") } (MonoProj { .. }, _) => todo!(), (_, MonoProj { .. }) => todo!(), diff --git a/compiler/erg_compiler/context/instantiate.rs b/compiler/erg_compiler/context/instantiate.rs index f7645de3..6a6d9f5c 100644 --- a/compiler/erg_compiler/context/instantiate.rs +++ b/compiler/erg_compiler/context/instantiate.rs @@ -133,6 +133,7 @@ impl TyVarContext { params .into_iter() .map(|p| { + erg_common::log!(err "p: {p}, {:?}", p.tvar_name()); if let Some(name) = p.tvar_name() { let tp = self.instantiate_qtp(p); self.push_or_init_typaram(&name, &tp); @@ -243,7 +244,7 @@ impl TyVarContext { } } TyParam::Type(t) => { - if let Type::MonoQVar(n) = *t { + if let Some(n) = t.as_ref().tvar_name() { if let Some(t) = self.get_typaram(&n) { t.clone() } else if let Some(t) = self.get_tyvar(&n) { @@ -254,7 +255,7 @@ impl TyVarContext { TyParam::t(tv) } } else { - todo!("{t}") + unreachable!("{t}") } } TyParam::UnaryOp { op, val } => { @@ -336,6 +337,7 @@ impl TyVarContext { } } +/// TODO: this struct will be removed when const functions are implemented. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ConstTemplate { Obj(ValueObj), @@ -766,6 +768,9 @@ impl Context { let t = Self::instantiate_t(*t, tv_ctx, loc)?; Ok(TyParam::t(t)) } + TyParam::FreeVar(fv) if fv.is_linked() => { + Self::instantiate_tp(fv.crack().clone(), tv_ctx, loc) + } p @ (TyParam::Value(_) | TyParam::Mono(_) | TyParam::FreeVar(_)) => Ok(p), other => todo!("{other}"), } @@ -867,6 +872,7 @@ impl Context { Quantified(_) => { panic!("a quantified type should not be instantiated, instantiate the inner type") } + FreeVar(fv) if fv.is_linked() => Self::instantiate_t(fv.crack().clone(), tv_ctx, loc), other if other.is_monomorphic() => Ok(other), other => todo!("{other}"), } diff --git a/compiler/erg_type/lib.rs b/compiler/erg_type/lib.rs index 1176d89f..20c2538c 100644 --- a/compiler/erg_type/lib.rs +++ b/compiler/erg_type/lib.rs @@ -1817,6 +1817,7 @@ impl Type { pub fn tvar_name(&self) -> Option { match self { + Self::FreeVar(fv) if fv.is_linked() => fv.crack().tvar_name(), Self::FreeVar(fv) => fv.unbound_name(), Self::MonoQVar(name) => Some(name.clone()), _ => None, diff --git a/compiler/erg_type/typaram.rs b/compiler/erg_type/typaram.rs index 2be0a1bd..a96cfe37 100644 --- a/compiler/erg_type/typaram.rs +++ b/compiler/erg_type/typaram.rs @@ -509,6 +509,7 @@ impl TyParam { pub fn name(&self) -> Option { match self { Self::Type(t) => Some(t.name()), + Self::FreeVar(fv) if fv.is_linked() => fv.crack().name(), Self::Mono(name) => Some(name.clone()), Self::MonoQVar(name) => Some(name.clone()), _ => None, @@ -518,6 +519,7 @@ impl TyParam { pub fn tvar_name(&self) -> Option { match self { Self::Type(t) => t.tvar_name(), + Self::FreeVar(fv) if fv.is_linked() => fv.crack().tvar_name(), Self::FreeVar(fv) => fv.unbound_name(), Self::MonoQVar(name) => Some(name.clone()), _ => None,