enso-org / enso

Hybrid visual and textual functional programming.
https://enso.org
Apache License 2.0
7.32k stars 318 forks source link

Indented comment breaks parsing of import statements before it #7827

Open radeusgd opened 9 months ago

radeusgd commented 9 months ago

Let's take this program:

from Standard.Base import all

    ## Indented comment

main =
    IO.println "Hello world!"

It runs ok and prints Hello world!.

Now let's add an indented comment:

 from Standard.Base import all
+ 
+    ## Indented comment

 main =
     IO.println "Hello world!"

It now fails and we can see that the import is not resolved:

C:\NBO\repr\compiler-error.enso:[1:1-3:23]: error: Unexpected expression.
    1 | from Standard.Base import all
    2 |
    3 |     ## Indented comment
C:\NBO\repr\compiler-error.enso:6:5: error: The name `IO` could not be found.
    6 |     IO.println "Hello world!"
      |     ^~
Aborting due to 2 errors and 0 warnings.
Execution finished with an error: Compilation aborted due to errors.

If we take a similar program, but with a polyglot import:

 from Standard.Base import all

 polyglot java import org.enso.base.Time_Utils
+
+    ## Indented comment

 main =
     IO.println "Hello world!"

Without the comment it runs and prints like above (the import is unused, doesn't matter). But with the added comment, we can see that the import is parsed in some unexpected way:

[INFO] [2023-09-18T14:37:51+02:00] [enso.org.enso.interpreter.runtime.DefaultPackageRepository] Found library Standard.Base @ 0.0.0-dev at [***\0.0.0-dev].
C:\NBO\repr\compiler-error.enso:[3:22-5:23]: error: Invalid Import: Expected qualified name..
    3 | polyglot java import org.enso.base.Time_Utils
    4 |
    5 |     ## Indented comment
Aborting due to 1 errors and 0 warnings.
Execution finished with an error: Compilation aborted due to errors.

We can see that for some reason the import is processed together with the comment that comes after it.

kazcw commented 9 months ago

Anything indented belongs to the most recent less-indented line. I will make the error message more specific--I think if it mentions that the unexpected item encountered was a block it would be more clear what is happening.

kazcw commented 9 months ago

There is also the question of whether doc-comments should create blocks: see #5443. I think unlike plain comments, an indented doc comment should create a block. I think it's better to assume the input is incomplete rather than incorrect, so a block ending in a doc comment should be treated as missing a following item (which would be a warning), not incorrectly indented (which could cause incorrect documentation association if we try to recover from).