forked from M-Labs/nac3
nac3core: keep checks for class constructors
This commit is contained in:
parent
ef7a7f55ce
commit
d8caaed2d1
@ -167,7 +167,7 @@ impl TopLevelComposer {
|
|||||||
) -> Result<(StrRef, DefinitionId, Option<Type>), String> {
|
) -> Result<(StrRef, DefinitionId, Option<Type>), String> {
|
||||||
let defined_names = &mut self.defined_names;
|
let defined_names = &mut self.defined_names;
|
||||||
match &ast.node {
|
match &ast.node {
|
||||||
ast::StmtKind::ClassDef { name: class_name, body, .. } => {
|
ast::StmtKind::ClassDef { name: class_name, bases, body, .. } => {
|
||||||
if self.keyword_list.contains(class_name) {
|
if self.keyword_list.contains(class_name) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"cannot use keyword `{}` as a class name (at {})",
|
"cannot use keyword `{}` as a class name (at {})",
|
||||||
@ -221,9 +221,19 @@ impl TopLevelComposer {
|
|||||||
// we do not push anything to the def list, so we keep track of the index
|
// we do not push anything to the def list, so we keep track of the index
|
||||||
// and then push in the correct order after the for loop
|
// and then push in the correct order after the for loop
|
||||||
let mut class_method_index_offset = 0;
|
let mut class_method_index_offset = 0;
|
||||||
|
let init_id = "__init__".into();
|
||||||
|
let exception_id = "Exception".into();
|
||||||
|
// TODO: Fix this hack. We will generate constructor for classes that inherit
|
||||||
|
// from Exception class (directly or indirectly), but this code cannot handle
|
||||||
|
// subclass of other exception classes.
|
||||||
|
let mut contains_constructor = bases
|
||||||
|
.iter().any(|base| matches!(base.node, ast::ExprKind::Name { id, .. } if id == exception_id));
|
||||||
|
|
||||||
for b in body {
|
for b in body {
|
||||||
if let ast::StmtKind::FunctionDef { name: method_name, .. } = &b.node {
|
if let ast::StmtKind::FunctionDef { name: method_name, .. } = &b.node {
|
||||||
|
if method_name == &init_id {
|
||||||
|
contains_constructor = true;
|
||||||
|
}
|
||||||
if self.keyword_list.contains(method_name) {
|
if self.keyword_list.contains(method_name) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"cannot use keyword `{}` as a method name (at {})",
|
"cannot use keyword `{}` as a method name (at {})",
|
||||||
@ -289,7 +299,9 @@ impl TopLevelComposer {
|
|||||||
self.definition_ast_list.push((def, Some(ast)));
|
self.definition_ast_list.push((def, Some(ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let result_ty = Some(constructor_ty);
|
// Quick way to fix #221, contains constructor can be used in the future for some implementation if required.
|
||||||
|
let result_ty = if contains_constructor { Some(constructor_ty) } else { Some(constructor_ty) };
|
||||||
|
|
||||||
Ok((class_name, DefinitionId(class_def_id), result_ty))
|
Ok((class_name, DefinitionId(class_def_id), result_ty))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user