Minor changes

This commit is contained in:
Shunsuke Shibayama 2022-12-22 09:25:11 +09:00
parent f677d23294
commit dfa119672a
3 changed files with 13 additions and 14 deletions

View file

@ -1566,11 +1566,14 @@ impl Context {
// -> K.variance() == vec![Contravariant, Covariant] // -> K.variance() == vec![Contravariant, Covariant]
// TODO: support keyword arguments // TODO: support keyword arguments
pub(crate) fn type_params_variance(&self) -> Vec<Variance> { pub(crate) fn type_params_variance(&self) -> Vec<Variance> {
let match_tp_name = |tp: &TyParam, name: &VarName| -> bool {
tp.qual_name().as_ref() == Some(name.inspect())
};
let in_inout = |t: &Type, name: &VarName| { let in_inout = |t: &Type, name: &VarName| {
(&t.qual_name()[..] == "Input" || &t.qual_name()[..] == "Output") (&t.qual_name()[..] == "Input" || &t.qual_name()[..] == "Output")
&& t.typarams() && t.typarams()
.first() .first()
.map(|inner| inner.qual_name().as_ref() == Some(name.inspect())) .map(|inner| match_tp_name(inner, name))
.unwrap_or(false) .unwrap_or(false)
}; };
self.params self.params
@ -1578,13 +1581,13 @@ impl Context {
.map(|(opt_name, _)| { .map(|(opt_name, _)| {
if let Some(name) = opt_name { if let Some(name) = opt_name {
// トレイトの変性を調べるときはsuper_classesも見る必要がある // トレイトの変性を調べるときはsuper_classesも見る必要がある
if let Some(t) = self if let Some(variance_trait) = self
.super_traits .super_traits
.iter() .iter()
.chain(self.super_classes.iter()) .chain(self.super_classes.iter())
.find(|t| in_inout(t, name)) .find(|t| in_inout(t, name))
{ {
match &t.qual_name()[..] { match &variance_trait.qual_name()[..] {
"Output" => Variance::Covariant, "Output" => Variance::Covariant,
"Input" => Variance::Contravariant, "Input" => Variance::Contravariant,
_ => unreachable!(), _ => unreachable!(),

View file

@ -18,7 +18,7 @@ use crate::ty::{HasType, Predicate, Type};
// use crate::context::eval::SubstContext; // use crate::context::eval::SubstContext;
use crate::context::{Context, Variance}; use crate::context::{Context, Variance};
use crate::error::{SingleTyCheckResult, TyCheckError, TyCheckErrors, TyCheckResult}; use crate::error::{SingleTyCheckResult, TyCheckError, TyCheckErrors, TyCheckResult};
use crate::{feature_error, hir, type_feature_error}; use crate::{feature_error, hir, type_feature_error, unreachable_error};
use Predicate as Pred; use Predicate as Pred;
use Type::*; use Type::*;
@ -1438,17 +1438,12 @@ impl Context {
// * sub_unify({0}, ?T(:> {1}, <: Nat)): (?T(:> {0, 1}, <: Nat)) // * sub_unify({0}, ?T(:> {1}, <: Nat)): (?T(:> {0, 1}, <: Nat))
// * sub_unify(Bool, ?T(<: Bool or Y)): (?T == Bool) // * sub_unify(Bool, ?T(<: Bool or Y)): (?T == Bool)
Constraint::Sandwiched { sub, sup } => { Constraint::Sandwiched { sub, sup } => {
/*if let Some(new_sub) = self.max(maybe_sub, sub) {
*constraint =
Constraint::new_sandwiched(new_sub.clone(), mem::take(sup), *cyclicity);
} else {*/
let new_sub = self.union(maybe_sub, sub); let new_sub = self.union(maybe_sub, sub);
if sup.contains_union(&new_sub) { if sup.contains_union(&new_sub) {
rfv.link(&new_sub); // Bool <: ?T <: Bool or Y ==> ?T == Bool rfv.link(&new_sub); // Bool <: ?T <: Bool or Y ==> ?T == Bool
} else { } else {
*constraint = Constraint::new_sandwiched(new_sub, mem::take(sup)); *constraint = Constraint::new_sandwiched(new_sub, mem::take(sup));
} }
// }
} }
// sub_unify(Nat, ?T(: Type)): (/* ?T(:> Nat) */) // sub_unify(Nat, ?T(: Type)): (/* ?T(:> Nat) */)
Constraint::TypeOf(ty) => { Constraint::TypeOf(ty) => {
@ -1705,7 +1700,8 @@ impl Context {
self.sub_unify_pred(sub_first, sup_first, loc)?; self.sub_unify_pred(sub_first, sup_first, loc)?;
return Ok(()); return Ok(());
} }
todo!("{sub}, {sup}") log!(err "unification error: {sub} <: {sup}");
unreachable_error!(TyCheckErrors, TyCheckError, self)
} }
// {I: Int | I >= 1} <: Nat == {I: Int | I >= 0} // {I: Int | I >= 1} <: Nat == {I: Int | I >= 0}
(Type::Refinement(_), sup) => { (Type::Refinement(_), sup) => {

View file

@ -310,10 +310,10 @@ impl LimitedDisplay for TyParam {
impl CanbeFree for TyParam { impl CanbeFree for TyParam {
fn unbound_name(&self) -> Option<Str> { fn unbound_name(&self) -> Option<Str> {
if let TyParam::FreeVar(fv) = self { match self {
fv.unbound_name() TyParam::FreeVar(fv) => fv.unbound_name(),
} else { TyParam::Type(t) => t.unbound_name(),
None _ => None,
} }
} }