Skip to content

Commit 9941f2c

Browse files
authored
Merge pull request #1808 from Shaikh-Ubaid/struct_init_expr
Require structs to be initialized before using
2 parents b9850da + bf6ef8f commit 9941f2c

21 files changed

+6086
-204
lines changed

integration_tests/bindc_03.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def h(q_void: CPtr) -> None:
4242
def run():
4343
a: CPtr
4444
array_wrapped: ArrayWrapped = ArrayWrapped(a)
45-
array_wrapped1: ArrayWrapped
45+
array_wrapped1: ArrayWrapped = ArrayWrapped()
4646
size: i32
4747
size = 10
4848
a = get_array(size)

integration_tests/structs_01.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def change_struct(a: A):
1414
a.y = a.y + f32(1)
1515

1616
def g():
17-
x: A
18-
x = A(f32(3.25), 3)
17+
x: A = A(f32(3.25), 3)
1918
f(x)
2019
assert x.x == 3
2120
assert f64(x.y) == 3.25

integration_tests/structs_02.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ class A:
99
def f(a: CPtr) -> None:
1010
x: i32
1111
y: f32
12-
a1: A
12+
a1: A = A(3, f32(3.25))
1313
a2: Pointer[A]
14-
a1 = A(3, f32(3.25))
1514
a2 = pointer(a1)
1615
print(a2, pointer(a1))
1716
x = a2.x

integration_tests/structs_04.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class A:
88

99
@dataclass
1010
class B:
11-
a: A
1211
z: i32
12+
a: A = A(f32(0.0), 0)
1313

1414
def f(b: B):
1515
print(b.z, b.a.x, b.a.y)
@@ -20,7 +20,7 @@ def f(b: B):
2020
def g():
2121
a1: A = A(f32(1.0), 1)
2222
a2: A = A(f32(2.0), 2)
23-
b: B = B(a1, 1)
23+
b: B = B(1, a1)
2424
b.a = deepcopy(a2)
2525
b.z = 1
2626
b.a.x = 2

integration_tests/structs_05.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from lpython import i32, f64, i64, i16, i8, f32, dataclass
2+
from numpy import empty
23

34
@dataclass
45
class A:
@@ -49,9 +50,8 @@ def update_2(s: A[:]):
4950
s[1].c = i8(3)
5051

5152
def g():
52-
# TODO: Replace y: A[2] with y: A[2] = [None, None]
53-
# TODO: And enable cpython in integration_tests.
54-
y: A[2]
53+
# TODO: Enable cpython in integration_tests.
54+
y: A[2] = empty([2], dtype=A)
5555
y[0] = A(1.1, 1, i64(1), f32(1.1), i16(1), i8(1), True)
5656
y[1] = A(2.2, 2, i64(2), f32(2.2), i16(2), i8(2), True)
5757
verify(y, 1, 1.1, 2, 2.2)

integration_tests/structs_09.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class C:
77
@dataclass
88
class B:
99
z: i32
10-
bc: C
10+
bc: C = C(f32(0.0))
1111

1212
@dataclass
1313
class A:
1414
y: f32
1515
x: i32
16-
b: B
16+
b: B = B(0, C(f32(0.0)))
1717

1818

1919
def f(a: A):
@@ -22,8 +22,7 @@ def f(a: A):
2222
print(a.b.z)
2323

2424
def g():
25-
x: A
26-
x = A(f32(3.25), 3, B(71, C(f32(4.0))))
25+
x: A = A(f32(3.25), 3, B(71, C(f32(4.0))))
2726
f(x)
2827
assert x.x == 3
2928
assert f64(x.y) == 3.25

integration_tests/structs_10.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Vec:
1111

1212
@dataclass
1313
class MatVec:
14-
mat: Mat
15-
vec: Vec
14+
mat: Mat = Mat([f64(0.0), f64(0.0)])
15+
vec: Vec = Vec([f64(0.0), f64(0.0)])
1616

1717
def rotate(mat_vec: MatVec) -> f64[2]:
1818
rotated_vec: f64[2] = empty(2, dtype=float64)

integration_tests/structs_17.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class B:
66
@dataclass
77
class C:
88
cz: f32
9-
bc: C
9+
bc: C = C(f32(0.0))
1010

1111
@dataclass
1212
class A:
1313
y: f32
1414
x: i32
15-
b: B
15+
b: B = B(0, B.C(f32(0.0)))
1616

1717

1818
def f(a: A):

integration_tests/union_02.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class C:
1919
@ccall
2020
@union
2121
class D(Union):
22-
a: A
23-
b: B
24-
c: C
22+
a: A = A()
23+
b: B = B()
24+
c: C = C()
2525

2626
def test_struct_union():
2727
d: D = D()

src/libasr/asr_utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,15 +1192,15 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
11921192
}
11931193
case ASR::ttypeType::Struct: {
11941194
ASR::Struct_t* d = ASR::down_cast<ASR::Struct_t>(t);
1195-
return symbol_name(d->m_derived_type);
1195+
return "struct " + std::string(symbol_name(d->m_derived_type));
11961196
}
11971197
case ASR::ttypeType::Enum: {
11981198
ASR::Enum_t* d = ASR::down_cast<ASR::Enum_t>(t);
1199-
return symbol_name(d->m_enum_type);
1199+
return "enum " + std::string(symbol_name(d->m_enum_type));
12001200
}
12011201
case ASR::ttypeType::Union: {
12021202
ASR::Union_t* d = ASR::down_cast<ASR::Union_t>(t);
1203-
return symbol_name(d->m_union_type);
1203+
return "union " + std::string(symbol_name(d->m_union_type));
12041204
}
12051205
case ASR::ttypeType::Pointer: {
12061206
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(t);

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,6 +2465,11 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
24652465
is_c_p_pointer_call = false;
24662466
if (x.m_value) {
24672467
this->visit_expr(*x.m_value);
2468+
} else {
2469+
if (ASR::is_a<ASR::Struct_t>(*type)) {
2470+
throw SemanticError(ASRUtils::type_to_str_python(type) + " " + var_name
2471+
+ " must be initialized a value", x.base.base.loc);
2472+
}
24682473
}
24692474
if( is_c_p_pointer_call ) {
24702475
create_add_variable_to_scope(var_name, nullptr, type,
@@ -4098,6 +4103,10 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
40984103
void visit_If(const AST::If_t &/*x*/) {
40994104
// We skip this in the SymbolTable visitor, but visit it in the BodyVisitor
41004105
}
4106+
4107+
void visit_Call(const AST::Call_t &/*x*/) {
4108+
// We skip this in the SymbolTable visitor, but visit it in the BodyVisitor
4109+
}
41014110
};
41024111

41034112
Result<ASR::asr_t*> symbol_table_visitor(Allocator &al, LocationManager &lm, const AST::Module_t &ast,

tests/errors/structs_02.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from lpython import i32, dataclass
2+
3+
@dataclass
4+
class S:
5+
x: i32
6+
7+
def main0():
8+
s: S
9+
s.x = 2
10+
11+
main0()

tests/reference/asr-structs_01-be14d49.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"basename": "asr-structs_01-be14d49",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/structs_01.py",
5-
"infile_hash": "a17eed6995c1af36b3968cb80367bda33fb855a60793b6bdc770aad2",
5+
"infile_hash": "c8012b0c841b0d8e304c18ca7c6d4365f1d5e41235dc6f4e2dc21664",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-structs_01-be14d49.stdout",

tests/reference/asr-structs_02-2ab459a.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"basename": "asr-structs_02-2ab459a",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/structs_02.py",
5-
"infile_hash": "f101938e4f5608477de4e57be8f04196e51b97aab3ade62833cecf91",
5+
"infile_hash": "6d54aa7c2bb850cbce2c0add7b77f9f72c9323162ae080c7eef4867a",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-structs_02-2ab459a.stdout",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-structs_02-f95782c",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/structs_02.py",
5+
"infile_hash": "bc4446b7b96cad60bb368378e7af4a8f628bfaaecac2063a1bec5c06",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-structs_02-f95782c.stderr",
11+
"stderr_hash": "feebf3045d755a862d604df8c8ab0e0cb346f7fbc285256b18e9d559",
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: struct S s must be initialized a value
2+
--> tests/errors/structs_02.py:8:5
3+
|
4+
8 | s: S
5+
| ^^^^

tests/reference/asr-structs_04-387747b.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-structs_04-387747b",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/structs_04.py",
5-
"infile_hash": "b57d1dd265f7a7906398ff70e0d5713433a7c3354590d727b3e6306d",
5+
"infile_hash": "c19af3c3fbac1430c22c5aaf69aea7c622faa9d7c4e7734edbd0066d",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-structs_04-387747b.stdout",
9-
"stdout_hash": "596c5faeae119e44e06dc8f6501c1b84360bceed027db7b76498eb1f",
9+
"stdout_hash": "421cc9ffddc15f1c8ec8724fed6b2a87c54d03cfbfc288b13175d718",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-structs_04-387747b.stdout

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@
9595
})
9696
B
9797
[A]
98-
[a
99-
z]
98+
[z
99+
a]
100100
Source
101101
Public
102102
.false.
@@ -432,8 +432,8 @@
432432
(Var 5 b)
433433
(StructTypeConstructor
434434
8 B
435-
[((Var 5 a1))
436-
((IntegerConstant 1 (Integer 4 [])))]
435+
[((IntegerConstant 1 (Integer 4 [])))
436+
((Var 5 a1))]
437437
(Struct
438438
8 B
439439
[]

tests/reference/asr-structs_05-fa98307.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-structs_05-fa98307",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/structs_05.py",
5-
"infile_hash": "5c9d6218394744f26160b09fb545064c82ef9172e10b474d6be5fca2",
5+
"infile_hash": "0ca482232f99c40614dc5b994fa8c9f4865fbe72f5a133b02914b5ad",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-structs_05-fa98307.stdout",
9-
"stdout_hash": "eed215681e7afcff6553f61228ae7482df849e1b24c3a022f80c6da0",
9+
"stdout_hash": "4adb7d314cd3d28086e7e0ebb8a701b1de0d0f253d5de63730b6f113",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)