Skip to content

Commit 714389e

Browse files
authored
Merge pull request #2705 from advikkabra/init-default
Initialize empty value to dictionaries without type given
2 parents fbbdbdd + 8116732 commit 714389e

7 files changed

+139
-4
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,11 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
25682568
ASR::Variable_t* v_variable = ASR::down_cast<ASR::Variable_t>(v_sym);
25692569
std::string var_name = v_variable->m_name;
25702570
ASR::ttype_t* type = v_variable->m_type;
2571+
if (!init_expr && ASR::is_a<ASR::Dict_t>(*type)) {
2572+
init_expr = ASRUtils::EXPR(ASR::make_DictConstant_t(al, loc,
2573+
nullptr, 0, nullptr, 0, type));
2574+
}
2575+
25712576
if( init_expr ) {
25722577
value = ASRUtils::expr_value(init_expr);
25732578
SetChar variable_dependencies_vec;

tests/reference/asr-dictionary1-a105a36.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-dictionary1-a105a36.stdout",
9-
"stdout_hash": "3ea42309cc8f2201f43bb2fdeb28a85feea890fe49db4063af5c46f8",
9+
"stdout_hash": "ac58817e3dc84de980d646cffeb63540c55bde9ca4229b8a7c58b77a",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-dictionary1-a105a36.stdout

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@
141141
[]
142142
[]
143143
[(Assignment
144+
(Var 3 x)
145+
(DictConstant
146+
[]
147+
[]
148+
(Dict
149+
(Integer 4)
150+
(Integer 4)
151+
)
152+
)
153+
()
154+
)
155+
(Assignment
144156
(Var 3 x)
145157
(DictConstant
146158
[(IntegerConstant 1 (Integer 4))
@@ -154,6 +166,18 @@
154166
)
155167
()
156168
)
169+
(Assignment
170+
(Var 3 y)
171+
(DictConstant
172+
[]
173+
[]
174+
(Dict
175+
(Character 1 -2 ())
176+
(Integer 4)
177+
)
178+
)
179+
()
180+
)
157181
(Assignment
158182
(Var 3 y)
159183
(DictConstant
@@ -286,6 +310,18 @@
286310
[]
287311
[]
288312
[(Assignment
313+
(Var 5 y)
314+
(DictConstant
315+
[]
316+
[]
317+
(Dict
318+
(Character 1 -2 ())
319+
(Integer 4)
320+
)
321+
)
322+
()
323+
)
324+
(Assignment
289325
(Var 5 y)
290326
(DictConstant
291327
[(StringConstant
@@ -390,6 +426,18 @@
390426
[]
391427
[]
392428
[(Assignment
429+
(Var 4 y)
430+
(DictConstant
431+
[]
432+
[]
433+
(Dict
434+
(Character 1 -2 ())
435+
(Integer 4)
436+
)
437+
)
438+
()
439+
)
440+
(Assignment
393441
(Var 4 y)
394442
(DictConstant
395443
[(StringConstant
@@ -494,6 +542,18 @@
494542
[]
495543
[]
496544
[(Assignment
545+
(Var 6 y)
546+
(DictConstant
547+
[]
548+
[]
549+
(Dict
550+
(Character 1 -2 ())
551+
(Integer 4)
552+
)
553+
)
554+
()
555+
)
556+
(Assignment
497557
(Var 6 y)
498558
(DictConstant
499559
[(StringConstant
@@ -574,7 +634,19 @@
574634
)
575635
[f]
576636
[]
577-
[(SubroutineCall
637+
[(Assignment
638+
(Var 8 x)
639+
(DictConstant
640+
[]
641+
[]
642+
(Dict
643+
(Integer 4)
644+
(Integer 4)
645+
)
646+
)
647+
()
648+
)
649+
(SubroutineCall
578650
2 f
579651
()
580652
[((Var 8 x))]

tests/reference/asr-print_list_tuple_03-9de3736.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-print_list_tuple_03-9de3736.stdout",
9-
"stdout_hash": "8962f3d49727ceb8f899acc2382f5fb6d24b16506a154ccf907400f5",
9+
"stdout_hash": "9bc9712a40c386ddbd519614bb9ed900ebde24b5db7d0876f7e88e95",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-print_list_tuple_03-9de3736.stdout

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@
110110
[]
111111
[]
112112
[(Assignment
113+
(Var 3 x)
114+
(DictConstant
115+
[]
116+
[]
117+
(Dict
118+
(Integer 4)
119+
(Tuple
120+
[(Integer 4)
121+
(Integer 4)]
122+
)
123+
)
124+
)
125+
()
126+
)
127+
(Assignment
113128
(Var 3 x)
114129
(DictConstant
115130
[(IntegerConstant 1 (Integer 4))
@@ -140,6 +155,20 @@
140155
)
141156
()
142157
)
158+
(Assignment
159+
(Var 3 y)
160+
(DictConstant
161+
[]
162+
[]
163+
(Dict
164+
(Integer 4)
165+
(List
166+
(Integer 4)
167+
)
168+
)
169+
)
170+
()
171+
)
143172
(Assignment
144173
(Var 3 y)
145174
(DictConstant

tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "pass_print_list_tuple-print_list_tuple_03-195fa9c.stdout",
9-
"stdout_hash": "f63197ac9c1a649cfb2d3a3ef6f6672964ad753593afc68ce6d567e9",
9+
"stdout_hash": "edb9d31c77ac27a72de4454275693936ef43c07263a2da687e16da5c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.stdout

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@
144144
[]
145145
[]
146146
[(Assignment
147+
(Var 3 x)
148+
(DictConstant
149+
[]
150+
[]
151+
(Dict
152+
(Integer 4)
153+
(Tuple
154+
[(Integer 4)
155+
(Integer 4)]
156+
)
157+
)
158+
)
159+
()
160+
)
161+
(Assignment
147162
(Var 3 x)
148163
(DictConstant
149164
[(IntegerConstant 1 (Integer 4))
@@ -174,6 +189,20 @@
174189
)
175190
()
176191
)
192+
(Assignment
193+
(Var 3 y)
194+
(DictConstant
195+
[]
196+
[]
197+
(Dict
198+
(Integer 4)
199+
(List
200+
(Integer 4)
201+
)
202+
)
203+
)
204+
()
205+
)
177206
(Assignment
178207
(Var 3 y)
179208
(DictConstant

0 commit comments

Comments
 (0)