Skip to content

Commit 2aa32ae

Browse files
authored
Merge pull request #436 from Smit-create/435-1
Use DictItem for dict subscript
2 parents 1081efd + 4e111c3 commit 2aa32ae

File tree

5 files changed

+10
-8
lines changed

5 files changed

+10
-8
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,8 +1253,12 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
12531253
throw SemanticError("Key type should be '" + ASRUtils::type_to_str_python(key_type) +
12541254
"' instead of '" +
12551255
ASRUtils::type_to_str_python(ASRUtils::expr_type(index)) + "'",
1256-
x.base.base.loc);
1256+
index->base.loc);
12571257
}
1258+
tmp = make_DictItem_t(al, x.base.base.loc, s, index, nullptr,
1259+
ASR::down_cast<ASR::Dict_t>(type)->m_value_type);
1260+
return;
1261+
12581262
} else {
12591263
index = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
12601264
}
@@ -1270,8 +1274,6 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
12701274
type = ASR::down_cast<ASR::List_t>(type)->m_type;
12711275
} else if (ASR::is_a<ASR::Set_t>(*type)) {
12721276
throw SemanticError("'set' object is not subscriptable", x.base.base.loc);
1273-
} else if (ASR::is_a<ASR::Dict_t>(*type)) {
1274-
type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type;
12751277
}
12761278
args.push_back(al, ai);
12771279
tmp = ASR::make_ArrayRef_t(al, x.base.base.loc, s, args.p,

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": "e7bc564ba9f0544a67c0ff05f0825f70f8e56872521068389dd9134e",
9+
"stdout_hash": "92f1fa0d403591e2e096fc936d72fe6f6ebcd1a91e31cd56e8bd82a2",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 5 {}) main_program [] []), test_Dict: (Subroutine (SymbolTable 2 {x: (Variable 2 x Local () () Default (Dict (Integer 4 []) (Integer 4 [])) Source Public Required .false.), y: (Variable 2 y Local () () Default (Dict (Character 1 -2 () []) (Integer 4 [])) Source Public Required .false.), z: (Variable 2 z Local () () Default (Integer 4 []) Source Public Required .false.)}) test_Dict [] [(= (Var 2 x) (DictConstant [(IntegerConstant 1 (Integer 4 [])) (IntegerConstant 3 (Integer 4 []))] [(IntegerConstant 2 (Integer 4 [])) (IntegerConstant 4 (Integer 4 []))] (Dict (Integer 4 []) (Integer 4 []))) ()) (= (Var 2 y) (DictConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () []))] [(UnaryOp USub (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) (UnaryOp USub (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) (IntegerConstant -2 (Integer 4 [])))] (Dict (Character 1 1 () []) (Integer 4 []))) ()) (= (Var 2 z) (ArrayRef 2 y [(() (StringConstant "a" (Character 1 1 () [])) ())] (Integer 4 []) ()) ()) (= (Var 2 z) (ArrayRef 2 y [(() (StringConstant "b" (Character 1 1 () [])) ())] (Integer 4 []) ()) ()) (= (Var 2 z) (ArrayRef 2 x [(() (IntegerConstant 1 (Integer 4 [])) ())] (Integer 4 []) ()) ())] Source Public Implementation () .false. .false.), test_dict_get: (Subroutine (SymbolTable 4 {x: (Variable 4 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 4 y Local () () Default (Dict (Character 1 -2 () []) (Integer 4 [])) Source Public Required .false.)}) test_dict_get [] [(= (Var 4 y) (DictConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () []))] [(UnaryOp USub (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) (UnaryOp USub (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) (IntegerConstant -2 (Integer 4 [])))] (Dict (Character 1 1 () []) (Integer 4 []))) ()) (= (Var 4 x) (DictItem 4 y (StringConstant "a" (Character 1 1 () [])) () (Integer 4 [])) ()) (= (Var 4 x) (DictItem 4 y (StringConstant "a" (Character 1 1 () [])) (IntegerConstant 0 (Integer 4 [])) (Integer 4 [])) ())] Source Public Implementation () .false. .false.), test_dict_insert: (Subroutine (SymbolTable 3 {y: (Variable 3 y Local () () Default (Dict (Character 1 -2 () []) (Integer 4 [])) Source Public Required .false.)}) test_dict_insert [] [(= (Var 3 y) (DictConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () []))] [(UnaryOp USub (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) (UnaryOp USub (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) (IntegerConstant -2 (Integer 4 [])))] (Dict (Character 1 1 () []) (Integer 4 []))) ()) (DictInsert 3 y (StringConstant "c" (Character 1 1 () [])) (UnaryOp USub (IntegerConstant 3 (Integer 4 [])) (Integer 4 []) (IntegerConstant -3 (Integer 4 []))))] Source Public Implementation () .false. .false.)}) [])
1+
(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 5 {}) main_program [] []), test_Dict: (Subroutine (SymbolTable 2 {x: (Variable 2 x Local () () Default (Dict (Integer 4 []) (Integer 4 [])) Source Public Required .false.), y: (Variable 2 y Local () () Default (Dict (Character 1 -2 () []) (Integer 4 [])) Source Public Required .false.), z: (Variable 2 z Local () () Default (Integer 4 []) Source Public Required .false.)}) test_Dict [] [(= (Var 2 x) (DictConstant [(IntegerConstant 1 (Integer 4 [])) (IntegerConstant 3 (Integer 4 []))] [(IntegerConstant 2 (Integer 4 [])) (IntegerConstant 4 (Integer 4 []))] (Dict (Integer 4 []) (Integer 4 []))) ()) (= (Var 2 y) (DictConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () []))] [(UnaryOp USub (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) (UnaryOp USub (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) (IntegerConstant -2 (Integer 4 [])))] (Dict (Character 1 1 () []) (Integer 4 []))) ()) (= (Var 2 z) (DictItem 2 y (StringConstant "a" (Character 1 1 () [])) () (Integer 4 [])) ()) (= (Var 2 z) (DictItem 2 y (StringConstant "b" (Character 1 1 () [])) () (Integer 4 [])) ()) (= (Var 2 z) (DictItem 2 x (IntegerConstant 1 (Integer 4 [])) () (Integer 4 [])) ())] Source Public Implementation () .false. .false.), test_dict_get: (Subroutine (SymbolTable 4 {x: (Variable 4 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 4 y Local () () Default (Dict (Character 1 -2 () []) (Integer 4 [])) Source Public Required .false.)}) test_dict_get [] [(= (Var 4 y) (DictConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () []))] [(UnaryOp USub (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) (UnaryOp USub (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) (IntegerConstant -2 (Integer 4 [])))] (Dict (Character 1 1 () []) (Integer 4 []))) ()) (= (Var 4 x) (DictItem 4 y (StringConstant "a" (Character 1 1 () [])) () (Integer 4 [])) ()) (= (Var 4 x) (DictItem 4 y (StringConstant "a" (Character 1 1 () [])) (IntegerConstant 0 (Integer 4 [])) (Integer 4 [])) ())] Source Public Implementation () .false. .false.), test_dict_insert: (Subroutine (SymbolTable 3 {y: (Variable 3 y Local () () Default (Dict (Character 1 -2 () []) (Integer 4 [])) Source Public Required .false.)}) test_dict_insert [] [(= (Var 3 y) (DictConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () []))] [(UnaryOp USub (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) (UnaryOp USub (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) (IntegerConstant -2 (Integer 4 [])))] (Dict (Character 1 1 () []) (Integer 4 []))) ()) (DictInsert 3 y (StringConstant "c" (Character 1 1 () [])) (UnaryOp USub (IntegerConstant 3 (Integer 4 [])) (Integer 4 []) (IntegerConstant -3 (Integer 4 []))))] Source Public Implementation () .false. .false.)}) [])

tests/reference/asr-test_dict1-a45a075.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-test_dict1-a45a075.stderr",
11-
"stderr_hash": "5104626e2205c28aebb840fa54c2befaba84d02fc188a19d4151eba7",
11+
"stderr_hash": "a9fbe4acc4f82141b1111e9aef3d3c50f27540d41ff3382fe46da8ea",
1212
"returncode": 2
1313
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
semantic error: Key type should be 'i32' instead of 'str'
2-
--> tests/errors/test_dict1.py:5:9
2+
--> tests/errors/test_dict1.py:5:11
33
|
44
5 | z = x["a"]
5-
| ^^^^^^
5+
| ^^^

0 commit comments

Comments
 (0)