Skip to content

Commit

Permalink
Merge pull request #1808 from Shaikh-Ubaid/struct_init_expr
Browse files Browse the repository at this point in the history
Require structs to be initialized before using
  • Loading branch information
Shaikh-Ubaid authored May 14, 2023
2 parents b9850da + bf6ef8f commit 9941f2c
Show file tree
Hide file tree
Showing 21 changed files with 6,086 additions and 204 deletions.
2 changes: 1 addition & 1 deletion integration_tests/bindc_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def h(q_void: CPtr) -> None:
def run():
a: CPtr
array_wrapped: ArrayWrapped = ArrayWrapped(a)
array_wrapped1: ArrayWrapped
array_wrapped1: ArrayWrapped = ArrayWrapped()
size: i32
size = 10
a = get_array(size)
Expand Down
3 changes: 1 addition & 2 deletions integration_tests/structs_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def change_struct(a: A):
a.y = a.y + f32(1)

def g():
x: A
x = A(f32(3.25), 3)
x: A = A(f32(3.25), 3)
f(x)
assert x.x == 3
assert f64(x.y) == 3.25
Expand Down
3 changes: 1 addition & 2 deletions integration_tests/structs_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class A:
def f(a: CPtr) -> None:
x: i32
y: f32
a1: A
a1: A = A(3, f32(3.25))
a2: Pointer[A]
a1 = A(3, f32(3.25))
a2 = pointer(a1)
print(a2, pointer(a1))
x = a2.x
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/structs_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class A:

@dataclass
class B:
a: A
z: i32
a: A = A(f32(0.0), 0)

def f(b: B):
print(b.z, b.a.x, b.a.y)
Expand All @@ -20,7 +20,7 @@ def f(b: B):
def g():
a1: A = A(f32(1.0), 1)
a2: A = A(f32(2.0), 2)
b: B = B(a1, 1)
b: B = B(1, a1)
b.a = deepcopy(a2)
b.z = 1
b.a.x = 2
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/structs_05.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from lpython import i32, f64, i64, i16, i8, f32, dataclass
from numpy import empty

@dataclass
class A:
Expand Down Expand Up @@ -49,9 +50,8 @@ def update_2(s: A[:]):
s[1].c = i8(3)

def g():
# TODO: Replace y: A[2] with y: A[2] = [None, None]
# TODO: And enable cpython in integration_tests.
y: A[2]
# TODO: Enable cpython in integration_tests.
y: A[2] = empty([2], dtype=A)
y[0] = A(1.1, 1, i64(1), f32(1.1), i16(1), i8(1), True)
y[1] = A(2.2, 2, i64(2), f32(2.2), i16(2), i8(2), True)
verify(y, 1, 1.1, 2, 2.2)
Expand Down
7 changes: 3 additions & 4 deletions integration_tests/structs_09.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class C:
@dataclass
class B:
z: i32
bc: C
bc: C = C(f32(0.0))

@dataclass
class A:
y: f32
x: i32
b: B
b: B = B(0, C(f32(0.0)))


def f(a: A):
Expand All @@ -22,8 +22,7 @@ def f(a: A):
print(a.b.z)

def g():
x: A
x = A(f32(3.25), 3, B(71, C(f32(4.0))))
x: A = A(f32(3.25), 3, B(71, C(f32(4.0))))
f(x)
assert x.x == 3
assert f64(x.y) == 3.25
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/structs_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Vec:

@dataclass
class MatVec:
mat: Mat
vec: Vec
mat: Mat = Mat([f64(0.0), f64(0.0)])
vec: Vec = Vec([f64(0.0), f64(0.0)])

def rotate(mat_vec: MatVec) -> f64[2]:
rotated_vec: f64[2] = empty(2, dtype=float64)
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/structs_17.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class B:
@dataclass
class C:
cz: f32
bc: C
bc: C = C(f32(0.0))

@dataclass
class A:
y: f32
x: i32
b: B
b: B = B(0, B.C(f32(0.0)))


def f(a: A):
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/union_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class C:
@ccall
@union
class D(Union):
a: A
b: B
c: C
a: A = A()
b: B = B()
c: C = C()

def test_struct_union():
d: D = D()
Expand Down
6 changes: 3 additions & 3 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1192,15 +1192,15 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
}
case ASR::ttypeType::Struct: {
ASR::Struct_t* d = ASR::down_cast<ASR::Struct_t>(t);
return symbol_name(d->m_derived_type);
return "struct " + std::string(symbol_name(d->m_derived_type));
}
case ASR::ttypeType::Enum: {
ASR::Enum_t* d = ASR::down_cast<ASR::Enum_t>(t);
return symbol_name(d->m_enum_type);
return "enum " + std::string(symbol_name(d->m_enum_type));
}
case ASR::ttypeType::Union: {
ASR::Union_t* d = ASR::down_cast<ASR::Union_t>(t);
return symbol_name(d->m_union_type);
return "union " + std::string(symbol_name(d->m_union_type));
}
case ASR::ttypeType::Pointer: {
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(t);
Expand Down
9 changes: 9 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,11 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
is_c_p_pointer_call = false;
if (x.m_value) {
this->visit_expr(*x.m_value);
} else {
if (ASR::is_a<ASR::Struct_t>(*type)) {
throw SemanticError(ASRUtils::type_to_str_python(type) + " " + var_name
+ " must be initialized a value", x.base.base.loc);
}
}
if( is_c_p_pointer_call ) {
create_add_variable_to_scope(var_name, nullptr, type,
Expand Down Expand Up @@ -4098,6 +4103,10 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
void visit_If(const AST::If_t &/*x*/) {
// We skip this in the SymbolTable visitor, but visit it in the BodyVisitor
}

void visit_Call(const AST::Call_t &/*x*/) {
// We skip this in the SymbolTable visitor, but visit it in the BodyVisitor
}
};

Result<ASR::asr_t*> symbol_table_visitor(Allocator &al, LocationManager &lm, const AST::Module_t &ast,
Expand Down
11 changes: 11 additions & 0 deletions tests/errors/structs_02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from lpython import i32, dataclass

@dataclass
class S:
x: i32

def main0():
s: S
s.x = 2

main0()
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_01-be14d49.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"basename": "asr-structs_01-be14d49",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_01.py",
"infile_hash": "a17eed6995c1af36b3968cb80367bda33fb855a60793b6bdc770aad2",
"infile_hash": "c8012b0c841b0d8e304c18ca7c6d4365f1d5e41235dc6f4e2dc21664",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_01-be14d49.stdout",
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_02-2ab459a.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"basename": "asr-structs_02-2ab459a",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_02.py",
"infile_hash": "f101938e4f5608477de4e57be8f04196e51b97aab3ade62833cecf91",
"infile_hash": "6d54aa7c2bb850cbce2c0add7b77f9f72c9323162ae080c7eef4867a",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_02-2ab459a.stdout",
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/asr-structs_02-f95782c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-structs_02-f95782c",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/structs_02.py",
"infile_hash": "bc4446b7b96cad60bb368378e7af4a8f628bfaaecac2063a1bec5c06",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-structs_02-f95782c.stderr",
"stderr_hash": "feebf3045d755a862d604df8c8ab0e0cb346f7fbc285256b18e9d559",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-structs_02-f95782c.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: struct S s must be initialized a value
--> tests/errors/structs_02.py:8:5
|
8 | s: S
| ^^^^
4 changes: 2 additions & 2 deletions tests/reference/asr-structs_04-387747b.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-structs_04-387747b",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_04.py",
"infile_hash": "b57d1dd265f7a7906398ff70e0d5713433a7c3354590d727b3e6306d",
"infile_hash": "c19af3c3fbac1430c22c5aaf69aea7c622faa9d7c4e7734edbd0066d",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_04-387747b.stdout",
"stdout_hash": "596c5faeae119e44e06dc8f6501c1b84360bceed027db7b76498eb1f",
"stdout_hash": "421cc9ffddc15f1c8ec8724fed6b2a87c54d03cfbfc288b13175d718",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
8 changes: 4 additions & 4 deletions tests/reference/asr-structs_04-387747b.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
})
B
[A]
[a
z]
[z
a]
Source
Public
.false.
Expand Down Expand Up @@ -432,8 +432,8 @@
(Var 5 b)
(StructTypeConstructor
8 B
[((Var 5 a1))
((IntegerConstant 1 (Integer 4 [])))]
[((IntegerConstant 1 (Integer 4 [])))
((Var 5 a1))]
(Struct
8 B
[]
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-structs_05-fa98307.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-structs_05-fa98307",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_05.py",
"infile_hash": "5c9d6218394744f26160b09fb545064c82ef9172e10b474d6be5fca2",
"infile_hash": "0ca482232f99c40614dc5b994fa8c9f4865fbe72f5a133b02914b5ad",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_05-fa98307.stdout",
"stdout_hash": "eed215681e7afcff6553f61228ae7482df849e1b24c3a022f80c6da0",
"stdout_hash": "4adb7d314cd3d28086e7e0ebb8a701b1de0d0f253d5de63730b6f113",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit 9941f2c

Please sign in to comment.