core: Initial implementation for const generics
This commit is contained in:
parent
af422f1785
commit
5266e9b48e
|
@ -58,6 +58,7 @@ impl TopLevelComposer {
|
|||
let mut unifier = primitives.1;
|
||||
let mut keyword_list: HashSet<StrRef> = HashSet::from_iter(vec![
|
||||
"Generic".into(),
|
||||
"Const".into(),
|
||||
"virtual".into(),
|
||||
"list".into(),
|
||||
"tuple".into(),
|
||||
|
@ -401,6 +402,7 @@ impl TopLevelComposer {
|
|||
let class_resolver = class_resolver.deref();
|
||||
|
||||
let mut is_generic = false;
|
||||
let mut is_const_generic = false;
|
||||
for b in class_bases_ast {
|
||||
match &b.node {
|
||||
// analyze typevars bounded to the class,
|
||||
|
@ -408,66 +410,77 @@ impl TopLevelComposer {
|
|||
// things like `class A(Generic[T, V, ImportedModule.T])` is not supported
|
||||
// i.e. only simple names are allowed in the subscript
|
||||
// should update the TopLevelDef::Class.typevars and the TypeEnum::TObj.params
|
||||
ast::ExprKind::Subscript { value, slice, .. }
|
||||
if {
|
||||
matches!(
|
||||
&value.node,
|
||||
ast::ExprKind::Name { id, .. } if id == &"Generic".into()
|
||||
)
|
||||
} =>
|
||||
{
|
||||
if !is_generic {
|
||||
is_generic = true;
|
||||
} else {
|
||||
return Err(format!(
|
||||
"only single Generic[...] is allowed (at {})",
|
||||
b.location
|
||||
));
|
||||
}
|
||||
|
||||
let type_var_list: Vec<&ast::Expr<()>>;
|
||||
// if `class A(Generic[T, V, G])`
|
||||
if let ast::ExprKind::Tuple { elts, .. } = &slice.node {
|
||||
type_var_list = elts.iter().collect_vec();
|
||||
// `class A(Generic[T])`
|
||||
} else {
|
||||
type_var_list = vec![slice.deref()];
|
||||
}
|
||||
|
||||
// parse the type vars
|
||||
let type_vars = type_var_list
|
||||
.into_iter()
|
||||
.map(|e| {
|
||||
class_resolver.parse_type_annotation(
|
||||
&temp_def_list,
|
||||
unifier,
|
||||
primitives_store,
|
||||
e,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
// check if all are unique type vars
|
||||
let all_unique_type_var = {
|
||||
let mut occurred_type_var_id: HashSet<u32> = HashSet::new();
|
||||
type_vars.iter().all(|x| {
|
||||
let ty = unifier.get_ty(*x);
|
||||
if let TypeEnum::TVar { id, .. } = ty.as_ref() {
|
||||
occurred_type_var_id.insert(*id)
|
||||
ast::ExprKind::Subscript { value, slice, .. } => {
|
||||
match &value.node {
|
||||
ast::ExprKind::Name { id, .. } if id == &"Generic".into() || id == &"Const".into() => {
|
||||
if id == &"Generic".into() {
|
||||
if !is_generic {
|
||||
is_generic = true;
|
||||
} else {
|
||||
return Err(format!(
|
||||
"only single Generic[...] is allowed (at {})",
|
||||
b.location
|
||||
));
|
||||
}
|
||||
} else if id == &"Const".into() {
|
||||
if !is_const_generic {
|
||||
is_const_generic = true;
|
||||
} else {
|
||||
return Err(format!(
|
||||
"only single Const[...] is allowed (at {})",
|
||||
b.location
|
||||
));
|
||||
}
|
||||
} else {
|
||||
false
|
||||
unreachable!()
|
||||
}
|
||||
})
|
||||
};
|
||||
if !all_unique_type_var {
|
||||
return Err(format!(
|
||||
"duplicate type variable occurs (at {})",
|
||||
slice.location
|
||||
));
|
||||
}
|
||||
|
||||
// add to TopLevelDef
|
||||
class_def_type_vars.extend(type_vars);
|
||||
let type_var_list: Vec<&ast::Expr<()>>;
|
||||
// if `class A(Generic[T, V, G])`
|
||||
if let ast::ExprKind::Tuple { elts, .. } = &slice.node {
|
||||
type_var_list = elts.iter().collect_vec();
|
||||
// `class A(Generic[T])`
|
||||
} else {
|
||||
type_var_list = vec![slice.deref()];
|
||||
}
|
||||
|
||||
// parse the type vars
|
||||
let type_vars = type_var_list
|
||||
.into_iter()
|
||||
.map(|e| {
|
||||
class_resolver.parse_type_annotation(
|
||||
&temp_def_list,
|
||||
unifier,
|
||||
primitives_store,
|
||||
e,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
// check if all are unique type vars
|
||||
let all_unique_type_var = {
|
||||
let mut occurred_type_var_id: HashSet<u32> = HashSet::new();
|
||||
type_vars.iter().all(|x| {
|
||||
let ty = unifier.get_ty(*x);
|
||||
if let TypeEnum::TVar { id, .. } = ty.as_ref() {
|
||||
occurred_type_var_id.insert(*id)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
};
|
||||
if !all_unique_type_var {
|
||||
return Err(format!(
|
||||
"duplicate type variable occurs (at {})",
|
||||
slice.location
|
||||
));
|
||||
}
|
||||
|
||||
// add to TopLevelDef
|
||||
class_def_type_vars.extend(type_vars);
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
// if others, do nothing in this function
|
||||
|
@ -536,7 +549,7 @@ impl TopLevelComposer {
|
|||
ast::ExprKind::Subscript { value, .. }
|
||||
if matches!(
|
||||
&value.node,
|
||||
ast::ExprKind::Name { id, .. } if id == &"Generic".into()
|
||||
ast::ExprKind::Name { id, .. } if id == &"Generic".into() || id == &"Const".into()
|
||||
)
|
||||
) {
|
||||
continue;
|
||||
|
@ -560,6 +573,7 @@ impl TopLevelComposer {
|
|||
&primitive_types,
|
||||
b,
|
||||
vec![(*class_def_id, class_type_vars.clone())].into_iter().collect(),
|
||||
None,
|
||||
)?;
|
||||
|
||||
if let TypeAnnotation::CustomClass { .. } = &base_ty {
|
||||
|
@ -894,6 +908,7 @@ impl TopLevelComposer {
|
|||
// NOTE: since only class need this, for function
|
||||
// it should be fine to be empty map
|
||||
HashMap::new(),
|
||||
None,
|
||||
)?;
|
||||
|
||||
let type_vars_within =
|
||||
|
@ -961,6 +976,7 @@ impl TopLevelComposer {
|
|||
// NOTE: since only class need this, for function
|
||||
// it should be fine to be empty map
|
||||
HashMap::new(),
|
||||
None,
|
||||
)?
|
||||
};
|
||||
|
||||
|
@ -1158,6 +1174,7 @@ impl TopLevelComposer {
|
|||
vec![(class_id, class_type_vars_def.clone())]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
None,
|
||||
)?
|
||||
};
|
||||
// find type vars within this method parameter type annotation
|
||||
|
@ -1221,6 +1238,7 @@ impl TopLevelComposer {
|
|||
primitives,
|
||||
result,
|
||||
vec![(class_id, class_type_vars_def.clone())].into_iter().collect(),
|
||||
None,
|
||||
)?;
|
||||
// find type vars within this return type annotation
|
||||
let type_vars_within =
|
||||
|
@ -1317,6 +1335,7 @@ impl TopLevelComposer {
|
|||
primitives,
|
||||
annotation.as_ref(),
|
||||
vec![(class_id, class_type_vars_def.clone())].into_iter().collect(),
|
||||
None,
|
||||
)?;
|
||||
// find type vars within this return type annotation
|
||||
let type_vars_within =
|
||||
|
@ -1735,7 +1754,7 @@ impl TopLevelComposer {
|
|||
.iter()
|
||||
.map(|(_, ty)| {
|
||||
unifier.get_instantiations(*ty).unwrap_or_else(|| {
|
||||
if let TypeEnum::TVar { name, loc, .. } = &*unifier.get_ty(*ty)
|
||||
if let TypeEnum::TVar { name, loc, is_const_generic: false, .. } = &*unifier.get_ty(*ty)
|
||||
{
|
||||
let rigid = unifier.get_fresh_rigid_var(*name, *loc).0;
|
||||
no_ranges.push(rigid);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::symbol_resolver::SymbolValue;
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
@ -12,6 +13,18 @@ pub enum TypeAnnotation {
|
|||
// can only be CustomClassKind
|
||||
Virtual(Box<TypeAnnotation>),
|
||||
TypeVar(Type),
|
||||
/// A const generic variable.
|
||||
ConstGeneric(Type),
|
||||
/// A constant used in the context of a const-generic variable.
|
||||
Constant {
|
||||
/// The non-type variable associated with this constant.
|
||||
///
|
||||
/// Invoking [Unifier::get_ty] on this type will return a [TypeEnum::TVar] representing the
|
||||
/// const generic variable of which this constant is associated with.
|
||||
ty: Type,
|
||||
/// The constant value of this constant.
|
||||
value: SymbolValue
|
||||
},
|
||||
List(Box<TypeAnnotation>),
|
||||
Tuple(Vec<TypeAnnotation>),
|
||||
}
|
||||
|
@ -20,7 +33,7 @@ impl TypeAnnotation {
|
|||
pub fn stringify(&self, unifier: &mut Unifier) -> String {
|
||||
use TypeAnnotation::*;
|
||||
match self {
|
||||
Primitive(ty) | TypeVar(ty) => unifier.stringify(*ty),
|
||||
Primitive(ty) | TypeVar(ty) | ConstGeneric(ty) => unifier.stringify(*ty),
|
||||
CustomClass { id, params } => {
|
||||
let class_name = match unifier.top_level {
|
||||
Some(ref top) => {
|
||||
|
@ -47,6 +60,7 @@ impl TypeAnnotation {
|
|||
}
|
||||
)
|
||||
}
|
||||
Constant { value, .. } => format!("Const({value})"),
|
||||
Virtual(ty) => format!("virtual[{}]", ty.stringify(unifier)),
|
||||
List(ty) => format!("list[{}]", ty.stringify(unifier)),
|
||||
Tuple(types) => {
|
||||
|
@ -56,6 +70,12 @@ impl TypeAnnotation {
|
|||
}
|
||||
}
|
||||
|
||||
/// Parses an AST expression `expr` into a [TypeAnnotation].
|
||||
///
|
||||
/// * `locked` - A [HashMap] containing the IDs of known definitions, mapped to a [Vec] of all
|
||||
/// generic variables associated with the definition.
|
||||
/// * `type_var` - The type variable associated with the type argument currently being parsed. Pass
|
||||
/// [None] when this function is invoked externally.
|
||||
pub fn parse_ast_to_type_annotation_kinds<T>(
|
||||
resolver: &(dyn SymbolResolver + Send + Sync),
|
||||
top_level_defs: &[Arc<RwLock<TopLevelDef>>],
|
||||
|
@ -64,6 +84,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
expr: &ast::Expr<T>,
|
||||
// the key stores the type_var of this topleveldef::class, we only need this field here
|
||||
locked: HashMap<DefinitionId, Vec<Type>>,
|
||||
type_var: Option<Type>,
|
||||
) -> Result<TypeAnnotation, String> {
|
||||
let name_handle = |id: &StrRef,
|
||||
unifier: &mut Unifier,
|
||||
|
@ -127,7 +148,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
slice: &ast::Expr<T>,
|
||||
unifier: &mut Unifier,
|
||||
mut locked: HashMap<DefinitionId, Vec<Type>>| {
|
||||
if vec!["virtual".into(), "Generic".into(), "list".into(), "tuple".into(), "Option".into()].contains(id)
|
||||
if vec!["virtual".into(), "Generic".into(), "Const".into(), "list".into(), "tuple".into(), "Option".into()].contains(id)
|
||||
{
|
||||
return Err(format!("keywords cannot be class name (at {})", expr.location));
|
||||
}
|
||||
|
@ -161,7 +182,8 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
}
|
||||
let result = params_ast
|
||||
.iter()
|
||||
.map(|x| {
|
||||
.enumerate()
|
||||
.map(|(idx, x)| {
|
||||
parse_ast_to_type_annotation_kinds(
|
||||
resolver,
|
||||
top_level_defs,
|
||||
|
@ -172,6 +194,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
locked.insert(obj_id, type_vars.clone());
|
||||
locked.clone()
|
||||
},
|
||||
Some(type_vars[idx]),
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
@ -190,6 +213,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
};
|
||||
Ok(TypeAnnotation::CustomClass { id: obj_id, params: param_type_infos })
|
||||
};
|
||||
|
||||
match &expr.node {
|
||||
ast::ExprKind::Name { id, .. } => name_handle(id, unifier, locked),
|
||||
// virtual
|
||||
|
@ -205,6 +229,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
primitives,
|
||||
slice.as_ref(),
|
||||
locked,
|
||||
None,
|
||||
)?;
|
||||
if !matches!(def, TypeAnnotation::CustomClass { .. }) {
|
||||
unreachable!("must be concretized custom class kind in the virtual")
|
||||
|
@ -225,6 +250,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
primitives,
|
||||
slice.as_ref(),
|
||||
locked,
|
||||
None,
|
||||
)?;
|
||||
Ok(TypeAnnotation::List(def_ann.into()))
|
||||
}
|
||||
|
@ -242,6 +268,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
primitives,
|
||||
slice.as_ref(),
|
||||
locked,
|
||||
None,
|
||||
)?;
|
||||
let id =
|
||||
if let TypeEnum::TObj { obj_id, .. } = unifier.get_ty(primitives.option).as_ref() {
|
||||
|
@ -275,6 +302,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
primitives,
|
||||
e,
|
||||
locked.clone(),
|
||||
None,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
@ -290,6 +318,23 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||
}
|
||||
}
|
||||
|
||||
ast::ExprKind::Constant { value, .. } => {
|
||||
let type_var = type_var.expect("Expect type variable to be present");
|
||||
|
||||
let ntv_ty_enum = unifier.get_ty_immutable(type_var);
|
||||
let TypeEnum::TVar { range: underlying_ty, .. } = ntv_ty_enum.as_ref() else {
|
||||
unreachable!()
|
||||
};
|
||||
let underlying_ty = underlying_ty[0];
|
||||
|
||||
let value = SymbolValue::from_constant(value, underlying_ty, primitives, unifier)?;
|
||||
|
||||
Ok(TypeAnnotation::Constant {
|
||||
ty: type_var,
|
||||
value,
|
||||
})
|
||||
}
|
||||
|
||||
_ => Err(format!("unsupported expression for type annotation (at {})", expr.location)),
|
||||
}
|
||||
}
|
||||
|
@ -308,94 +353,130 @@ pub fn get_type_from_type_annotation_kinds(
|
|||
TypeAnnotation::CustomClass { id: obj_id, params } => {
|
||||
let def_read = top_level_defs[obj_id.0].read();
|
||||
let class_def: &TopLevelDef = def_read.deref();
|
||||
if let TopLevelDef::Class { fields, methods, type_vars, .. } = class_def {
|
||||
if type_vars.len() != params.len() {
|
||||
Err(format!(
|
||||
"unexpected number of type parameters: expected {} but got {}",
|
||||
type_vars.len(),
|
||||
params.len()
|
||||
))
|
||||
} else {
|
||||
let param_ty = params
|
||||
.iter()
|
||||
.map(|x| {
|
||||
get_type_from_type_annotation_kinds(
|
||||
top_level_defs,
|
||||
unifier,
|
||||
primitives,
|
||||
x,
|
||||
subst_list
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let TopLevelDef::Class { fields, methods, type_vars, .. } = class_def else {
|
||||
unreachable!("should be class def here")
|
||||
};
|
||||
|
||||
let subst = {
|
||||
// check for compatible range
|
||||
// TODO: if allow type var to be applied(now this disallowed in the parse_to_type_annotation), need more check
|
||||
let mut result: HashMap<u32, Type> = HashMap::new();
|
||||
for (tvar, p) in type_vars.iter().zip(param_ty) {
|
||||
if let TypeEnum::TVar { id, range, fields: None, name, loc } =
|
||||
unifier.get_ty(*tvar).as_ref()
|
||||
{
|
||||
let ok: bool = {
|
||||
// create a temp type var and unify to check compatibility
|
||||
p == *tvar || {
|
||||
let temp = unifier.get_fresh_var_with_range(
|
||||
range.as_slice(),
|
||||
*name,
|
||||
*loc,
|
||||
);
|
||||
unifier.unify(temp.0, p).is_ok()
|
||||
}
|
||||
};
|
||||
if ok {
|
||||
result.insert(*id, p);
|
||||
} else {
|
||||
return Err(format!(
|
||||
"cannot apply type {} to type variable with id {:?}",
|
||||
unifier.internal_stringify(
|
||||
p,
|
||||
&mut |id| format!("class{}", id),
|
||||
&mut |id| format!("typevar{}", id),
|
||||
&mut None
|
||||
),
|
||||
*id
|
||||
));
|
||||
if type_vars.len() != params.len() {
|
||||
return Err(format!(
|
||||
"unexpected number of type parameters: expected {} but got {}",
|
||||
type_vars.len(),
|
||||
params.len()
|
||||
))
|
||||
}
|
||||
|
||||
let param_ty = params
|
||||
.iter()
|
||||
.map(|x| {
|
||||
get_type_from_type_annotation_kinds(
|
||||
top_level_defs,
|
||||
unifier,
|
||||
primitives,
|
||||
x,
|
||||
subst_list
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
let subst = {
|
||||
// check for compatible range
|
||||
// TODO: if allow type var to be applied(now this disallowed in the parse_to_type_annotation), need more check
|
||||
let mut result: HashMap<u32, Type> = HashMap::new();
|
||||
for (tvar, p) in type_vars.iter().zip(param_ty) {
|
||||
match unifier.get_ty(*tvar).as_ref() {
|
||||
TypeEnum::TVar { id, range, fields: None, name, loc, is_const_generic: false } => {
|
||||
let ok: bool = {
|
||||
// create a temp type var and unify to check compatibility
|
||||
p == *tvar || {
|
||||
let temp = unifier.get_fresh_var_with_range(
|
||||
range.as_slice(),
|
||||
*name,
|
||||
*loc,
|
||||
);
|
||||
unifier.unify(temp.0, p).is_ok()
|
||||
}
|
||||
};
|
||||
if ok {
|
||||
result.insert(*id, p);
|
||||
} else {
|
||||
unreachable!("must be generic type var")
|
||||
return Err(format!(
|
||||
"cannot apply type {} to type variable with id {:?}",
|
||||
unifier.internal_stringify(
|
||||
p,
|
||||
&mut |id| format!("class{}", id),
|
||||
&mut |id| format!("typevar{}", id),
|
||||
&mut None
|
||||
),
|
||||
*id
|
||||
));
|
||||
}
|
||||
}
|
||||
result
|
||||
};
|
||||
let mut tobj_fields = methods
|
||||
.iter()
|
||||
.map(|(name, ty, _)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
// methods are immutable
|
||||
(*name, (subst_ty, false))
|
||||
})
|
||||
.collect::<HashMap<_, _>>();
|
||||
tobj_fields.extend(fields.iter().map(|(name, ty, mutability)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
(*name, (subst_ty, *mutability))
|
||||
}));
|
||||
let need_subst = !subst.is_empty();
|
||||
let ty = unifier.add_ty(TypeEnum::TObj {
|
||||
obj_id: *obj_id,
|
||||
fields: tobj_fields,
|
||||
params: subst,
|
||||
});
|
||||
if need_subst {
|
||||
subst_list.as_mut().map(|wl| wl.push(ty));
|
||||
|
||||
TypeEnum::TVar { id, range, name, loc, is_const_generic: true, .. } => {
|
||||
let ty = range[0];
|
||||
let ok: bool = {
|
||||
// create a temp type var and unify to check compatibility
|
||||
p == *tvar || {
|
||||
let temp = unifier.get_fresh_const_generic_var(
|
||||
ty,
|
||||
*name,
|
||||
*loc,
|
||||
);
|
||||
unifier.unify(temp.0, p).is_ok()
|
||||
}
|
||||
};
|
||||
if ok {
|
||||
result.insert(*id, p);
|
||||
} else {
|
||||
return Err(format!(
|
||||
"cannot apply type {} to type variable {}",
|
||||
unifier.stringify(p),
|
||||
name.unwrap_or_else(|| format!("typevar{id}").into()),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
_ => unreachable!("must be generic type var"),
|
||||
}
|
||||
Ok(ty)
|
||||
}
|
||||
} else {
|
||||
unreachable!("should be class def here")
|
||||
result
|
||||
};
|
||||
let mut tobj_fields = methods
|
||||
.iter()
|
||||
.map(|(name, ty, _)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
// methods are immutable
|
||||
(*name, (subst_ty, false))
|
||||
})
|
||||
.collect::<HashMap<_, _>>();
|
||||
tobj_fields.extend(fields.iter().map(|(name, ty, mutability)| {
|
||||
let subst_ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||
(*name, (subst_ty, *mutability))
|
||||
}));
|
||||
let need_subst = !subst.is_empty();
|
||||
let ty = unifier.add_ty(TypeEnum::TObj {
|
||||
obj_id: *obj_id,
|
||||
fields: tobj_fields,
|
||||
params: subst,
|
||||
});
|
||||
if need_subst {
|
||||
subst_list.as_mut().map(|wl| wl.push(ty));
|
||||
}
|
||||
Ok(ty)
|
||||
}
|
||||
TypeAnnotation::Primitive(ty) | TypeAnnotation::TypeVar(ty) | TypeAnnotation::ConstGeneric(ty) => Ok(*ty),
|
||||
TypeAnnotation::Constant { ty, value, .. } => {
|
||||
let ty_enum = unifier.get_ty(*ty);
|
||||
let (ty, name, loc) = match &*ty_enum {
|
||||
TypeEnum::TVar { range: ntv_underlying_ty, name, loc, is_const_generic: true, .. } => {
|
||||
(ntv_underlying_ty[0], name, loc)
|
||||
}
|
||||
_ => unreachable!("{} ({})", unifier.stringify(*ty), ty_enum.get_type_name()),
|
||||
};
|
||||
|
||||
let var = unifier.get_fresh_constant(value.clone(), ty, *name, *loc);
|
||||
Ok(var)
|
||||
}
|
||||
TypeAnnotation::Primitive(ty) | TypeAnnotation::TypeVar(ty) => Ok(*ty),
|
||||
TypeAnnotation::Virtual(ty) => {
|
||||
let ty = get_type_from_type_annotation_kinds(
|
||||
top_level_defs,
|
||||
|
@ -470,7 +551,7 @@ pub fn get_type_var_contained_in_type_annotation(ann: &TypeAnnotation) -> Vec<Ty
|
|||
result.extend(get_type_var_contained_in_type_annotation(a));
|
||||
}
|
||||
}
|
||||
TypeAnnotation::Primitive(..) => {}
|
||||
TypeAnnotation::Primitive(..) | TypeAnnotation::ConstGeneric(..) | TypeAnnotation::Constant { .. } => {}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
|
|
@ -134,6 +134,18 @@ pub enum TypeEnum {
|
|||
range: Vec<Type>,
|
||||
name: Option<StrRef>,
|
||||
loc: Option<Location>,
|
||||
/// Whether this type variable refers to a const-generic variable.
|
||||
is_const_generic: bool,
|
||||
},
|
||||
|
||||
/// A constant for substitution into a const generic variable.
|
||||
TConstant {
|
||||
/// The value of the constant.
|
||||
value: SymbolValue,
|
||||
/// The underlying type of the value.
|
||||
ty: Type,
|
||||
name: Option<StrRef>,
|
||||
loc: Option<Location>,
|
||||
},
|
||||
|
||||
/// A tuple type.
|
||||
|
@ -178,6 +190,7 @@ impl TypeEnum {
|
|||
match self {
|
||||
TypeEnum::TRigidVar { .. } => "TRigidVar",
|
||||
TypeEnum::TVar { .. } => "TVar",
|
||||
TypeEnum::TConstant { .. } => "TConstant",
|
||||
TypeEnum::TTuple { .. } => "TTuple",
|
||||
TypeEnum::TList { .. } => "TList",
|
||||
TypeEnum::TObj { .. } => "TObj",
|
||||
|
@ -263,6 +276,7 @@ impl Unifier {
|
|||
fields: Some(fields),
|
||||
name: None,
|
||||
loc: None,
|
||||
is_const_generic: false,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -336,7 +350,34 @@ impl Unifier {
|
|||
let id = self.var_id + 1;
|
||||
self.var_id += 1;
|
||||
let range = range.to_vec();
|
||||
(self.add_ty(TypeEnum::TVar { id, range, fields: None, name, loc }), id)
|
||||
(self.add_ty(TypeEnum::TVar { id, range, fields: None, name, loc, is_const_generic: false }), id)
|
||||
}
|
||||
|
||||
/// Returns a fresh type representing a constant generic variable with the given underlying type
|
||||
/// `ty`.
|
||||
pub fn get_fresh_const_generic_var(
|
||||
&mut self,
|
||||
ty: Type,
|
||||
name: Option<StrRef>,
|
||||
loc: Option<Location>,
|
||||
) -> (Type, u32) {
|
||||
let id = self.var_id + 1;
|
||||
self.var_id += 1;
|
||||
(self.add_ty(TypeEnum::TVar { id, range: vec![ty], fields: None, name, loc, is_const_generic: true }), id)
|
||||
}
|
||||
|
||||
/// Returns a fresh type representing a [fresh constant][TypeEnum::TConstant] with the given
|
||||
/// `value` and type `ty`.
|
||||
pub fn get_fresh_constant(
|
||||
&mut self,
|
||||
value: SymbolValue,
|
||||
ty: Type,
|
||||
name: Option<StrRef>,
|
||||
loc: Option<Location>,
|
||||
) -> Type {
|
||||
assert!(matches!(self.get_ty(ty).as_ref(), TypeEnum::TObj { .. }));
|
||||
|
||||
self.add_ty(TypeEnum::TConstant { ty, value, name, loc })
|
||||
}
|
||||
|
||||
/// Unification would not unify rigid variables with other types, but we want to do this for
|
||||
|
@ -412,7 +453,7 @@ impl Unifier {
|
|||
pub fn is_concrete(&mut self, a: Type, allowed_typevars: &[Type]) -> bool {
|
||||
use TypeEnum::*;
|
||||
match &*self.get_ty(a) {
|
||||
TRigidVar { .. } => true,
|
||||
TRigidVar { .. } | TConstant { .. } => true,
|
||||
TVar { .. } => allowed_typevars.iter().any(|b| self.unification_table.unioned(a, *b)),
|
||||
TCall { .. } => false,
|
||||
TList { ty } => self.is_concrete(*ty, allowed_typevars),
|
||||
|
@ -560,8 +601,8 @@ impl Unifier {
|
|||
};
|
||||
match (&*ty_a, &*ty_b) {
|
||||
(
|
||||
TVar { fields: fields1, id, name: name1, loc: loc1, .. },
|
||||
TVar { fields: fields2, id: id2, name: name2, loc: loc2, .. },
|
||||
TVar { fields: fields1, id, name: name1, loc: loc1, is_const_generic: false, .. },
|
||||
TVar { fields: fields2, id: id2, name: name2, loc: loc2, is_const_generic: false, .. },
|
||||
) => {
|
||||
let new_fields = match (fields1, fields2) {
|
||||
(None, None) => None,
|
||||
|
@ -616,10 +657,11 @@ impl Unifier {
|
|||
range,
|
||||
name: name1.or(*name2),
|
||||
loc: loc1.or(*loc2),
|
||||
is_const_generic: false,
|
||||
}),
|
||||
);
|
||||
}
|
||||
(TVar { fields: None, range, .. }, _) => {
|
||||
(TVar { fields: None, range, is_const_generic: false, .. }, _) => {
|
||||
// We check for the range of the type variable to see if unification is allowed.
|
||||
// Note that although b may be compatible with a, we may have to constrain type
|
||||
// variables in b to make sure that instantiations of b would always be compatible
|
||||
|
@ -636,7 +678,7 @@ impl Unifier {
|
|||
self.unify_impl(x, b, false)?;
|
||||
self.set_a_to_b(a, x);
|
||||
}
|
||||
(TVar { fields: Some(fields), range, .. }, TTuple { ty }) => {
|
||||
(TVar { fields: Some(fields), range, is_const_generic: false, .. }, TTuple { ty }) => {
|
||||
let len = ty.len() as i32;
|
||||
for (k, v) in fields.iter() {
|
||||
match *k {
|
||||
|
@ -666,7 +708,7 @@ impl Unifier {
|
|||
self.unify_impl(x, b, false)?;
|
||||
self.set_a_to_b(a, x);
|
||||
}
|
||||
(TVar { fields: Some(fields), range, .. }, TList { ty }) => {
|
||||
(TVar { fields: Some(fields), range, is_const_generic: false, .. }, TList { ty }) => {
|
||||
for (k, v) in fields.iter() {
|
||||
match *k {
|
||||
RecordKey::Int(_) => {
|
||||
|
@ -681,6 +723,35 @@ impl Unifier {
|
|||
self.unify_impl(x, b, false)?;
|
||||
self.set_a_to_b(a, x);
|
||||
}
|
||||
|
||||
(TVar { id: id1, range: ty1, is_const_generic: true, .. }, TVar { id: id2, range: ty2, .. }) => {
|
||||
let ty1 = ty1[0];
|
||||
let ty2 = ty2[0];
|
||||
|
||||
if id1 != id2 {
|
||||
self.unify_impl(ty1, ty2, false)?;
|
||||
}
|
||||
|
||||
self.set_a_to_b(a, b);
|
||||
}
|
||||
|
||||
(TVar { range: ty1, is_const_generic: true, .. }, TConstant { ty: ty2, .. }) => {
|
||||
let ty1 = ty1[0];
|
||||
|
||||
self.unify_impl(ty1, *ty2, false)?;
|
||||
self.set_a_to_b(a, b);
|
||||
}
|
||||
|
||||
(TConstant { value: val1, ty: ty1, .. }, TConstant { value: val2, ty: ty2, .. }) => {
|
||||
if val1 != val2 {
|
||||
eprintln!("VALUE MISMATCH: lhs={val1:?} rhs={val2:?} eq={}", val1 == val2);
|
||||
return self.incompatible_types(a, b)
|
||||
}
|
||||
self.unify_impl(*ty1, *ty2, false)?;
|
||||
|
||||
self.set_a_to_b(a, b);
|
||||
}
|
||||
|
||||
(TTuple { ty: ty1 }, TTuple { ty: ty2 }) => {
|
||||
if ty1.len() != ty2.len() {
|
||||
return Err(TypeError::new(TypeErrorKind::IncompatibleTypes(a, b), None));
|
||||
|
@ -775,7 +846,14 @@ impl Unifier {
|
|||
if id1 != id2 {
|
||||
self.incompatible_types(a, b)?;
|
||||
}
|
||||
for (x, y) in zip(params1.values(), params2.values()) {
|
||||
|
||||
// Sort the type arguments by its UnificationKey first, since `HashMap::iter` visits
|
||||
// all K-V pairs "in arbitrary order"
|
||||
let (tv1, tv2) = (
|
||||
params1.iter().sorted_by_key(|(k, _)| *k).map(|(_, v)| v).collect_vec(),
|
||||
params2.iter().sorted_by_key(|(k, _)| *k).map(|(_, v)| v).collect_vec(),
|
||||
);
|
||||
for (x, y) in zip(tv1, tv2) {
|
||||
if self.unify_impl(*x, *y, false).is_err() {
|
||||
return Err(TypeError::new(TypeErrorKind::IncompatibleTypes(a, b), None));
|
||||
};
|
||||
|
@ -928,6 +1006,9 @@ impl Unifier {
|
|||
};
|
||||
n
|
||||
}
|
||||
TypeEnum::TConstant { value, name, .. } => {
|
||||
format!("const[{}]", name.map(|n| format!("{n}={value}")).unwrap_or(value.to_string()))
|
||||
}
|
||||
TypeEnum::TTuple { ty } => {
|
||||
let mut fields =
|
||||
ty.iter().map(|v| self.internal_stringify(*v, obj_to_name, var_to_name, notes));
|
||||
|
@ -983,8 +1064,8 @@ impl Unifier {
|
|||
}
|
||||
}
|
||||
|
||||
/// Unifies `a` and `b` together, and set the value to the value of `b`.
|
||||
fn set_a_to_b(&mut self, a: Type, b: Type) {
|
||||
// unify a and b together, and set the value to b's value.
|
||||
let table = &mut self.unification_table;
|
||||
let ty_b = table.probe_value(b).clone();
|
||||
table.unify(a, b);
|
||||
|
@ -1207,6 +1288,7 @@ impl Unifier {
|
|||
range,
|
||||
name: name2.or(*name),
|
||||
loc: loc2.or(*loc),
|
||||
is_const_generic: false,
|
||||
};
|
||||
Ok(Some(self.unification_table.new_key(ty.into())))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
A = NonTypeVar("A", int32)
|
||||
B = NonTypeVar("B", uint32)
|
||||
|
||||
class NTGClass(Const[A]):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
class NTG2Class(Const[A, B]):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@extern
|
||||
def make_ntg_2() -> NTGClass[2]:
|
||||
...
|
||||
|
||||
@extern
|
||||
def make_ntg2_1_2() -> NTG2Class[1, 2]:
|
||||
...
|
||||
|
||||
def ntg(instance: NTGClass[2]):
|
||||
pass
|
||||
|
||||
def ntg2(instance: NTG2Class[1, 2]):
|
||||
pass
|
||||
|
||||
def f():
|
||||
ntg(make_ntg_2())
|
||||
ntg2(make_ntg2_1_2())
|
||||
|
||||
def run() -> int32:
|
||||
return 0
|
|
@ -25,7 +25,7 @@ use nac3core::{
|
|||
},
|
||||
};
|
||||
use nac3parser::{
|
||||
ast::{Expr, ExprKind, StmtKind},
|
||||
ast::{Constant, Expr, ExprKind, StmtKind},
|
||||
parser,
|
||||
};
|
||||
|
||||
|
@ -94,6 +94,7 @@ fn handle_typevar_definition(
|
|||
primitives,
|
||||
x,
|
||||
Default::default(),
|
||||
None,
|
||||
)?;
|
||||
get_type_from_type_annotation_kinds(
|
||||
def_list, unifier, primitives, &ty, &mut None
|
||||
|
@ -113,11 +114,16 @@ fn handle_typevar_definition(
|
|||
primitives,
|
||||
&args[1],
|
||||
Default::default(),
|
||||
None,
|
||||
)?;
|
||||
let constraint = get_type_from_type_annotation_kinds(
|
||||
def_list, unifier, primitives, &ty, &mut None
|
||||
)?;
|
||||
Ok(unifier.get_fresh_var_with_range(&[constraint], None, None).0)
|
||||
let ExprKind::Constant { value: Constant::Str(ty_name), .. } = &args[0].node else {
|
||||
unreachable!("Expected string constant for first parameter of `NonTypeVar`, got {:?}", &args[0].node)
|
||||
};
|
||||
|
||||
Ok(unifier.get_fresh_const_generic_var(constraint, Some(ty_name.to_string().into()), None).0)
|
||||
}
|
||||
|
||||
_ => Err(format!(
|
||||
|
|
Loading…
Reference in New Issue