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);

0 commit comments

Comments
 (0)