aardappel / lobster

The Lobster Programming Language
http://strlen.com/lobster
2.25k stars 119 forks source link

Wrong error report when “multiple return” misusage #322

Closed inferrna closed 1 month ago

inferrna commented 1 month ago
  1. Wrong parameters passing,
    
    def help(x, y) -> int, int:
    return "hi", 5

let a, b, c = help(2)

 complains about caller at line 13: works fine
```python
tut2.lobster(13): error: cannot implicitly bind type variable `B` in call to `help` (argument doesn't match?)
  1. Wrong returns usage,
    
    def help(x, y) -> int, int:
    return "hi", 5

let a, b, c = help(2,1)

 switched caller line (13) and function definition line (11): works wrong
```python
tut2.lobster(11): error: `multiple return` returns 2 values, 3 needed
  in tut2.lobster(13): help(x:int, y:int) -> int, int
aardappel commented 1 month ago

Case 1 was fixed, now says error: no version of function "help" takes 1 arguments

Case 2 is as I would expect it.. how would you like this error to be different?

Note that Lobster typechecks each function based on the caller, in this case the caller is expecting there to be 3 return values, and it comes across a return statement only returning 2, and flags that as an error.

This is also an optimization: if you call a function ignoring a return value, it will compile that function in a way that it doesn't even generate that return value.

inferrna commented 1 month ago

I was ok with case 1, but now error message is even better, thanks. Case 2: look to the line numbers. image image

Comparing to case 1, case 2 complains about function itself instead of caller.

To see case 2 in vscode this patch to be applied (relative to https://github.com/aardappel/lobster/pull/321 )

diff --git a/dev/lsp/src/lobster.ts b/dev/lsp/src/lobster.ts
index 4368a53..0202820 100644
--- a/dev/lsp/src/lobster.ts
+++ b/dev/lsp/src/lobster.ts
@@ -72,13 +72,13 @@ export async function parseLobster(
             return []; // TODO make "can't open file" a normal error instead of an unformatted error
         }

-        const regex = /^(.*)\((\d+)\): (warning|error): (.*)/g;
+        const regex = /^(.*)\((\d+)\): (warning|error): (.*)/gm;
         let match;
         const output: Diagnostic[] = [];

         while ((match = regex.exec(input))) {
             const at = match[1];
-            if (!file.endsWith(at)) return [];
+            //if (!file.endsWith(at)) return [];

             const line = parseInt(match[2]) - 1; // Its zero based
             const messageType = match[3].trim();
aardappel commented 1 month ago

Read my explanation in the last message.

The way the language works, the function is the problem, not the caller :)

inferrna commented 1 month ago

Whatever, I sort of fixed it on LSP side, will ship after https://github.com/aardappel/lobster/pull/321. image