M
Modular11mo ago
sora

Should the language server warn about this case?

Consider the following code:
fn f() -> Int:
<body>
fn f() -> Int:
<body>
Mojo language server doesn't produce a type error when body doesn't contain a return. There are cases where this makes sense, like when <body> contains only pass or .... However, for the majority of cases, this clearly won't type check. So, I'm not sure if it's a bug or intended behaviour. FYI, Python language server like pyright produces the following error message:
def f() -> int:
# ^^^ Function with declared return type "int" must return value on all code paths
a = 1
def f() -> int:
# ^^^ Function with declared return type "int" must return value on all code paths
a = 1
9 Replies
Jack Clayton
Jack Clayton11mo ago
Thanks yeah that's should work in the LSP, as it does produce a compiler error:
/tmp/main.mojo:5:1: error: return expected at end of function with results
fn timer() -> Int:
^
/tmp/main.mojo:5:1: error: return expected at end of function with results
fn timer() -> Int:
^
Would you mind raising an issue for it?
sora
sora11mo ago
Didn't sleep last night. Will raise an issue when I wake up. Filed here: https://github.com/modularml/mojo/issues/1159
ModularBot
ModularBot11mo ago
Congrats @sora, you just advanced to level 5!
Jack Clayton
Jack Clayton11mo ago
Thanks heaps @sora just seen it 🙏
gabrieldemarmiesse
I'm interested in this subject, I encountered a few weird things with the LSP, where the compiler produced an error but not the LSP. Should we always raise an issue on github when that happens? If I reformulate, is the expected behavior of the LSP to raise the same error messages as the compiler?
sora
sora11mo ago
I can imagine that there maybe cases where only the compiler can produce an error, maybe the compiler run more pass than the LSP.
gabrieldemarmiesse
Exactly @sora that's what I had in mind
sora
sora11mo ago
Maybe you could share the things you encountered and we can ask @Jack Clayton about it.
gabrieldemarmiesse
so yeah clarification would be appreciated
Want results from more Discord servers?
Add your server