From 82965b7674453005ba250e9940cfce43bd90d231 Mon Sep 17 00:00:00 2001 From: Yash Kumar Sain Date: Fri, 6 May 2022 13:12:12 +0530 Subject: [PATCH 1/3] add strict type checking for tuple --- src/libasr/asr_utils.h | 7 ++++++- tests/errors/test_tuple1.py | 5 +++++ tests/tests.toml | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/errors/test_tuple1.py diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 6012c509a9..ecd9364259 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -210,7 +210,8 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t) return "str"; } case ASR::ttypeType::Tuple: { - return "tuple"; + ASR::Tuple_t *t = (ASR::Tuple_t *)t; + return "tuple[" + type_to_str_python(t->m_type) + "]"; } case ASR::ttypeType::Set: { ASR::Set_t *s = (ASR::Set_t *)t; @@ -1084,6 +1085,10 @@ inline bool is_same_type_pointer(ASR::ttype_t* source, ASR::ttype_t* dest) { ASR::ttype_t *y_value_type = ASR::down_cast(y)->m_value_type; return (check_equal_type(x_key_type, y_key_type) && check_equal_type(x_value_type, y_value_type)); + } else if (ASR::is_a(*x) && ASR::is_a(*y)) { + x = ASR::down_cast(x)->m_type; + y = ASR::down_cast(y)->m_type; + return check_equal_type(x, y); } if( x->type == y->type ) { return true; diff --git a/tests/errors/test_tuple1.py b/tests/errors/test_tuple1.py new file mode 100644 index 0000000000..fc68fe5b86 --- /dev/null +++ b/tests/errors/test_tuple1.py @@ -0,0 +1,5 @@ +def main(): + t: tuple[i32, str] + t = (1, 2) + +main() \ No newline at end of file diff --git a/tests/tests.toml b/tests/tests.toml index 583369c801..4ab74ac229 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -465,3 +465,7 @@ tokens = true [[test]] filename = "tokens/errors/paren4.py" tokens = true + +[[test]] +filename = "errors/test_tuple1.py" +asr = true From 9ca180c6299357ecd923e5523578695801faa2f3 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sat, 14 May 2022 16:41:32 +0530 Subject: [PATCH 2/3] Add type checking for tuple --- src/libasr/asr_utils.h | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index ecd9364259..426a656faf 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -210,8 +210,16 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t) return "str"; } case ASR::ttypeType::Tuple: { - ASR::Tuple_t *t = (ASR::Tuple_t *)t; - return "tuple[" + type_to_str_python(t->m_type) + "]"; + ASR::Tuple_t *tup = ASR::down_cast(t); + std::string result = "tuple["; + for (size_t i=0; in_type; i++) { + result += type_to_str_python(tup->m_type[i]); + if (i+1 != tup->n_type) { + result += ", "; + } + } + result += "]"; + return result; } case ASR::ttypeType::Set: { ASR::Set_t *s = (ASR::Set_t *)t; @@ -1086,9 +1094,19 @@ inline bool is_same_type_pointer(ASR::ttype_t* source, ASR::ttype_t* dest) { return (check_equal_type(x_key_type, y_key_type) && check_equal_type(x_value_type, y_value_type)); } else if (ASR::is_a(*x) && ASR::is_a(*y)) { - x = ASR::down_cast(x)->m_type; - y = ASR::down_cast(y)->m_type; - return check_equal_type(x, y); + ASR::Tuple_t *a = ASR::down_cast(x); + ASR::Tuple_t *b = ASR::down_cast(y); + if(a->n_type != b->n_type) { + return false; + } + bool result = true; + for (size_t i=0; in_type; i++) { + result = result && check_equal_type(a->m_type[i], b->m_type[i]); + if (!result) { + return false; + } + } + return result; } if( x->type == y->type ) { return true; From 67b7064104f4a6554fcd35dbcbf8e43b8a00dde2 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sat, 14 May 2022 16:41:54 +0530 Subject: [PATCH 3/3] Update tests --- tests/reference/asr-test_tuple1-7abe88f.json | 13 +++++++++++++ tests/reference/asr-test_tuple1-7abe88f.stderr | 5 +++++ 2 files changed, 18 insertions(+) create mode 100644 tests/reference/asr-test_tuple1-7abe88f.json create mode 100644 tests/reference/asr-test_tuple1-7abe88f.stderr diff --git a/tests/reference/asr-test_tuple1-7abe88f.json b/tests/reference/asr-test_tuple1-7abe88f.json new file mode 100644 index 0000000000..e308970eea --- /dev/null +++ b/tests/reference/asr-test_tuple1-7abe88f.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_tuple1-7abe88f", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_tuple1.py", + "infile_hash": "4716ea56ce9d57a0d88ffeb9fca4e09204f7a2c1166a5315c4d1245c", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_tuple1-7abe88f.stderr", + "stderr_hash": "56df3d46c63077fcdd09c3b54b63e6e096d7d39b2f8cfb61dab0930a", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_tuple1-7abe88f.stderr b/tests/reference/asr-test_tuple1-7abe88f.stderr new file mode 100644 index 0000000000..c66cc64946 --- /dev/null +++ b/tests/reference/asr-test_tuple1-7abe88f.stderr @@ -0,0 +1,5 @@ +semantic error: Type mismatch in assignment, the types must be compatible + --> tests/errors/test_tuple1.py:3:3 + | +3 | t = (1, 2) + | ^ ^^^^^^ type mismatch ('tuple[i32, str]' and 'tuple[i32, i32]')