← 输入框|github.com|刷新
Skip to content

LPython should give an error message instead of a cast #1142

Open

Description

@certik

This example

from ltypes import i32

def main0():
    x: i32
    x = 1/2
    print(x)

main0()

Should give a compile time error message in LPython, because in CPython it prints 0.5, which is incompatible with the declaration x: i32. Currently LPython casts it to an integer, which is wrong:

$ lpython --show-asr examples/expr2.py --new-parser
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 4 {}) _lpython_main_program [] [(SubroutineCall 1 main0 () [] ())] () Source Public Implementation () .false. .false. .false. .false. [] [] .false.), main0: (Function (SymbolTable 2 {x: (Variable 2 x Local () () Default (Integer 4 []) Source Public Required .false.)}) main0 [] [(= (Var 2 x) (Cast (RealBinOp (Cast (IntegerConstant 1 (Integer 4 [])) IntegerToReal (Real 8 []) ()) Div (Cast (IntegerConstant 2 (Integer 4 [])) IntegerToReal (Real 8 []) ()) (Real 8 []) ()) RealToInteger (Integer 4 []) ()) ()) (Print () [(Var 2 x)] () ())] () Source Public Implementation () .false. .false. .false. .false. [] [] .false.), main_program: (Program (SymbolTable 3 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])

This is related to #1006, we should just disable all such castings.

Activity

  1. Thirumalai-Shaktivel commented on Nov 13, 2022

    @Thirumalai-Shaktivel
    Collaborator

    We get the following error message:

    $ lpython examples/expr2.py
    semantic error: Type mismatch in assignment, the types must be compatible
     --> examples/expr2.py:5:5
      |
    5 |     x = 1/2
      |     ^   ^^^ type mismatch ('i32' and 'f64')
    
    
    Note: if any of the above error or warning messages are not clear or are lacking
    context please report it to us (we consider that a bug that needs to be fixed).
  2. added
    could close
    on Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    could close

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions