Open rebcabin opened 1 year ago
adding the return type of -> str
to the suspect function produces intelligible error messages. I think LPython should say something like return type of None doesn't match type of return value "c"
instead of throwing an assertion :)
This works:
def eat_white(self : InOut[LasrLexer]) -> None:
c : str = eat_white_debug_dump(self)
c = peek(self)
while c != EOF_SIGNAL and in_(c, WHITESPACE):
c = read(self.fd, 1)
if c == NEWLINE:
self.col = 1
self.line += 1
else:
self.col += 1
c = peek(self)
def eat_white_debug_dump(self : InOut[LasrLexer]) -> str:
print('self.fd._len = ', end='');
print(self.fd._len, end='');
print('.')
print('self.fd._0cursor = ', end='');
print(self.fd._0cursor, end='');
print('.')
c: str = peek(self)
print('eat-white line 175, c = "', end='')
print(c, end='');
print('".')
print('self.fd._len = ', end='');
print(self.fd._len, end='');
print('.')
print('self.fd._0cursor = ', end='');
print(self.fd._0cursor, end='');
print('.')
return c
We do have an error message for mis-match return type:
def f():
return "bug"
def g():
x:str = f()
g()
This will give:
semantic error: Return type of function is not defined
--> c.py:2:5
|
2 | return "bug"
| ^^^^^^^^^^^^
The main reason for failure in the code is because of this line: https://github.com/rebcabin/lpython/blob/77c8014cf215edc5d1fdd53561f8e1e683fee5ad/lasr/LP-pycharm/Issue1982/lasr_lexer.py#L175
Since, eat_white_debug_dump
is returning None
so it is a stmt
node (because it is considered a Subroutine Call).
The reason it didn't throw the return type mismatch first was that Return
is visited in body-visitor and so the check also comes from there, and, also function eat_white
is visited first in body-visitor. If you declare the function eat_white_debug_dump
above the function eat_white
(so it gets visited first), then you get the following error:
semantic error: Return type of function is not defined
--> a.py:190:5
|
190 | return c
| ^^^^^^^^
To show a good error message for this we may add a check over there to check if there's a Subroutine Call in the assignment throw an error saying the function doesn't return anything.
repro: https://github.com/rebcabin/lpython/tree/brian-lasr/lasr/LP-pycharm/Issue1982
The culprit seems to be this refactored function (it's crappy code, I know; PyCharm made it, but it's legal CPython!):
CPython:
LPython: