Compare commits

..

1 Commits

Author SHA1 Message Date
David Mak 181756fb3c core: Initial implementation for const generics 2023-12-06 15:32:55 +08:00
2 changed files with 9 additions and 24 deletions

View File

@ -58,7 +58,6 @@ 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(),
@ -402,7 +401,6 @@ 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,
@ -412,27 +410,14 @@ impl TopLevelComposer {
// should update the TopLevelDef::Class.typevars and the TypeEnum::TObj.params
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
));
}
ast::ExprKind::Name { id, .. } if id == &"Generic".into() => {
if !is_generic {
is_generic = true;
} else {
unreachable!()
return Err(format!(
"only single Generic[...] is allowed (at {})",
b.location
));
}
let type_var_list: Vec<&ast::Expr<()>>;
@ -549,7 +534,7 @@ impl TopLevelComposer {
ast::ExprKind::Subscript { value, .. }
if matches!(
&value.node,
ast::ExprKind::Name { id, .. } if id == &"Generic".into() || id == &"Const".into()
ast::ExprKind::Name { id, .. } if id == &"Generic".into()
)
) {
continue;

View File

@ -146,7 +146,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(), "Const".into(), "list".into(), "tuple".into(), "Option".into()].contains(id)
if vec!["virtual".into(), "Generic".into(), "list".into(), "tuple".into(), "Option".into()].contains(id)
{
return Err(format!("keywords cannot be class name (at {})", expr.location));
}