lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 158 forks source link

Error: `x: list[i64] = [1, 2, 3]` #873

Open Smit-create opened 2 years ago

Smit-create commented 2 years ago
def main0():
    x: list[i64] = [1, 2, 3]

main0()

Error:

semantic error: Type mismatch in annotation-assignment, the types must be compatible
 --> expr.py:2:5
  |
2 |     x: list[i64] = [1, 2, 3]
  |     ^              ^^^^^^^^^ type mismatch ('list[i64]' and 'list[i32]')
certik commented 2 years ago

Just like for x: i64 = 5 we cast the default integer(4) to integer(8), we should cast the integer(4) [1, 2, 3] to integer(8) [1, 2, 3]. So indeed the above is a bug that we need to fix.

czgdp1807 commented 2 years ago

This is a casting error. I think we should revamp the casting mechanism of LPython. Right now its a very long if-else-if ladder which is very hard to read and update. I think this can be abstracted out.

https://github.com/lcompilers/lpython/blob/25416f46d4b59e2b2c2ac9654c0350382de88e93/src/lpython/semantics/python_ast_to_asr.cpp#L879-L965

Smit-create commented 2 years ago

Yeah, I agree too for separating the casting logic in a new file.

Thirumalai-Shaktivel commented 2 years ago

Other issues

$ lpython examples/expr2.py --new-parser --show-asr
semantic error: Type mismatch in annotation-assignment, the types must be compatible
 --> examples/expr2.py:4:5
  |
4 |     x: list[f32] = [1.0, 2.0]
  |     ^              ^^^^^^^^^^ type mismatch ('list[f32]' and 'list[f64]')
$ lpython examples/expr2.py --new-parser --show-asr
semantic error: Type mismatch in annotation-assignment, the types must be compatible
 --> examples/expr2.py:4:5
  |
4 |     x: tuple[i32, f32] = (1, 1.0)
  |     ^                    ^^^^^^^^ type mismatch ('tuple[i32, f32]' and 'tuple[i32, f64]')
$ lpython examples/expr2.py --new-parser --show-asr
semantic error: Type mismatch in annotation-assignment, the types must be compatible
 --> examples/expr2.py:4:5
  |
4 |     x: tuple[i64, f64] = (1, 1.0)
  |     ^                    ^^^^^^^^ type mismatch ('tuple[i64, f64]' and 'tuple[i32, f64]')
czgdp1807 commented 2 years ago

So here are my thoughts on your examples,

  1. A casting issue, can be and should be fixed.
  2. The last two examples - IMO, tuples should be kept strictly typed for now. From this I mean, tuple[i32, f32] and tuple[i32, f64] are of different types. Its tempting to to cast the second element of tuple i.e., f64 to f32 but the problem is that copying tuples is not the same as doing, t1[i] = t2[i] (tuples are immutable data structures, either all the elements can be copied from one to another tuples without casting or error). Eventually in distant future if we need, we can allow casting on individual elements of tuples but for now IMO spending time on this may not be worth it.
certik commented 2 years ago

@czgdp1807 and I discussed this and it seems we should not allow any such casts implicitly. For containers like lists and arrays, the casting is not a quick operation. And perhaps we should not allow automatic casting even for simple types, since it's always a source of confusion what exactly happens.