Skip to content

Commit f2ba0ef

Browse files
authored
Merge pull request #1975 from Shaikh-Ubaid/error_for_incorrect_type_ann
Error for incorrect type annotation
2 parents fcf70be + 1dded4d commit f2ba0ef

15 files changed

+139
-14
lines changed

src/libasr/asr_utils.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,11 @@ inline bool ttype_set_dimensions(ASR::ttype_t** x,
17601760
case ASR::ttypeType::Real:
17611761
case ASR::ttypeType::Complex:
17621762
case ASR::ttypeType::Character:
1763-
case ASR::ttypeType::Logical: {
1763+
case ASR::ttypeType::Logical:
1764+
case ASR::ttypeType::Struct:
1765+
case ASR::ttypeType::Enum:
1766+
case ASR::ttypeType::Union:
1767+
case ASR::ttypeType::TypeParameter: {
17641768
*x = ASRUtils::make_Array_t_util(al, (*x)->base.loc, *x, m_dims, n_dims);
17651769
return true;
17661770
}

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,32 +1070,32 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
10701070
if (var_sym->m_type->type == ASR::ttypeType::TypeParameter) {
10711071
ASR::TypeParameter_t *type_param = ASR::down_cast<ASR::TypeParameter_t>(var_sym->m_type);
10721072
type = ASRUtils::TYPE(ASR::make_TypeParameter_t(al, loc, type_param->m_param));
1073-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
1073+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
10741074
}
10751075
} else {
10761076
ASR::symbol_t *der_sym = ASRUtils::symbol_get_past_external(s);
10771077
if( der_sym ) {
10781078
if ( ASR::is_a<ASR::StructType_t>(*der_sym) ) {
10791079
type = ASRUtils::TYPE(ASR::make_Struct_t(al, loc, s));
1080-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
1080+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
10811081
} else if( ASR::is_a<ASR::EnumType_t>(*der_sym) ) {
10821082
type = ASRUtils::TYPE(ASR::make_Enum_t(al, loc, s));
1083-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
1083+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
10841084
} else if( ASR::is_a<ASR::UnionType_t>(*der_sym) ) {
10851085
type = ASRUtils::TYPE(ASR::make_Union_t(al, loc, s));
1086-
return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
1086+
type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size());
10871087
}
10881088
}
10891089
}
10901090
} else if (var_annotation == "S") {
10911091
type = ASRUtils::TYPE(ASR::make_SymbolicExpression_t(al, loc));
1092-
return type;
1093-
}
1094-
if( raise_error ) {
1095-
throw SemanticError("Unsupported type annotation: " + var_annotation, loc);
10961092
}
10971093
}
10981094

1095+
if( !type && raise_error ) {
1096+
throw SemanticError("Unsupported type annotation: " + var_annotation, loc);
1097+
}
1098+
10991099
return type;
11001100
}
11011101

@@ -1669,7 +1669,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
16691669
if (AST::is_a<AST::Name_t>(annotation)) {
16701670
AST::Name_t *n = AST::down_cast<AST::Name_t>(&annotation);
16711671
var_annotation = n->m_id;
1672-
} else if (AST::is_a<AST::Subscript_t>(annotation)) {
1672+
return get_type_from_var_annotation(var_annotation, annotation.base.loc, dims, m_args, n_args, raise_error);
1673+
}
1674+
1675+
if (AST::is_a<AST::Subscript_t>(annotation)) {
16731676
AST::Subscript_t *s = AST::down_cast<AST::Subscript_t>(&annotation);
16741677
if (AST::is_a<AST::Name_t>(*s->m_value)) {
16751678
AST::Name_t *n = AST::down_cast<AST::Name_t>(s->m_value);
@@ -1679,6 +1682,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
16791682
loc);
16801683
}
16811684

1685+
if (var_annotation == "In" || var_annotation == "InOut" || var_annotation == "Out") {
1686+
throw SemanticError("Intent annotation '" + var_annotation + "' cannot be used here", s->base.base.loc);
1687+
}
1688+
16821689
if (var_annotation == "tuple") {
16831690
Vec<ASR::ttype_t*> types;
16841691
types.reserve(al, 4);
@@ -1771,6 +1778,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
17711778
ASR::ttype_t *type = ast_expr_to_asr_type(loc, *s->m_slice, is_allocatable);
17721779
return ASRUtils::TYPE(ASR::make_Const_t(al, loc, type));
17731780
} else {
1781+
ASR::ttype_t* type = get_type_from_var_annotation(var_annotation, annotation.base.loc, dims, m_args, n_args, raise_error);
1782+
17741783
if (AST::is_a<AST::Slice_t>(*s->m_slice)) {
17751784
ASR::dimension_t dim;
17761785
dim.loc = loc;
@@ -1793,6 +1802,12 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
17931802
ASR::expr_t *value = ASRUtils::EXPR(tmp);
17941803
fill_dims_for_asr_type(dims, value, loc);
17951804
}
1805+
1806+
if (ASRUtils::ttype_set_dimensions(&type, dims.p, dims.size(), al)) {
1807+
return type;
1808+
}
1809+
1810+
throw SemanticError("ICE: Unable to set dimensions for: " + var_annotation, loc);
17961811
}
17971812
} else if (AST::is_a<AST::Attribute_t>(annotation)) {
17981813
AST::Attribute_t* attr_annotation = AST::down_cast<AST::Attribute_t>(&annotation);
@@ -1835,12 +1850,9 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
18351850
current_scope->add_symbol(import_name, import_struct_member);
18361851
}
18371852
return ASRUtils::TYPE(ASR::make_Union_t(al, attr_annotation->base.base.loc, import_struct_member));
1838-
} else {
1839-
throw SemanticError("Only Name, Subscript, and Call supported for now in annotation of annotated assignment.",
1840-
loc);
18411853
}
18421854

1843-
return get_type_from_var_annotation(var_annotation, annotation.base.loc, dims, m_args, n_args, raise_error);
1855+
throw SemanticError("Only Name, Subscript, and Call supported for now in annotation of annotated assignment.", loc);
18441856
}
18451857

18461858
ASR::expr_t *index_add_one(const Location &loc, ASR::expr_t *idx) {

tests/errors/func_08.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from lpython import InOut
2+
3+
def main0(x: In[In[i32]]):
4+
print(x)
5+
main0()

tests/errors/test_annassign_01.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def test_annassign_01():
2+
a: Optional[i32] = 5
3+
4+
test_annassign_01()

tests/errors/test_annassign_02.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def test_annassign_02():
2+
hex_pat : Pattern[str] = r'-?0[xX]+'
3+
4+
test_annassign_02()

tests/errors/test_annassign_03.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@dataclass
2+
class LasrLexer:
3+
_len: i32
4+
5+
def test_annassign_03():
6+
lexer : InOut[LasrLexer] = LasrLexer(5)
7+
8+
test_annassign_03()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-func_08-c137415",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/func_08.py",
5+
"infile_hash": "7db9398db0036bb22e787d74ea36241f6535928f25e66c09c59e76e3",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-func_08-c137415.stderr",
11+
"stderr_hash": "5fe28460d4f28e79ced8c17e75ba6da4cdfccd36e20d1c3a85d07cf1",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: Intent annotation 'In' cannot be used here
2+
--> tests/errors/func_08.py:3:17
3+
|
4+
3 | def main0(x: In[In[i32]]):
5+
| ^^^^^^^
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_annassign_01-2f18669",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_annassign_01.py",
5+
"infile_hash": "68f667b69b25d6a5f7f281a6dc7f2e2912511d8f4f20dbeab8442436",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_annassign_01-2f18669.stderr",
11+
"stderr_hash": "f3609cdad6bdb1be4a138587d5d36d0a28b71e4e51bb1304c2d2a01f",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: Unsupported type annotation: Optional
2+
--> tests/errors/test_annassign_01.py:2:8
3+
|
4+
2 | a: Optional[i32] = 5
5+
| ^^^^^^^^^^^^^

0 commit comments

Comments
 (0)