dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.89k stars 782 forks source link

Incorrect indentation mixed with #if gives surprising results #1564

Open mattklein999 opened 8 years ago

mattklein999 commented 8 years ago

Please provide a succinct description of the issue.

From this question on Stack Overflow, given the script:

//error.fsx

#if INTERACTIVE
    let msg = "Interactive"
#else
    let msg = "Not Interactive"
#endif

let add x y =
    x + y

printfn "%d" (add 1 2)

The output is: error.fsx(12,15): error FS0039: The value or constructor 'add' is not defined

Repro steps

Save the code given above (labeled as error.fsx) and run using fsi.exe version 4.0.

Expected behavior

Don't treat the printfn line as indented

Actual behavior

It seems that with the indentation, the F# parser actually parses the code as follows (from @tpetricek)

let msg = "Interactive"
let add x y =
  x + y 
    printfn "%d" (add 1 2)

The spacing before let msg causes the compiler to treat printfn as being indented too.

Known workarounds

Don't be a dumdum.

Related information

Running fsi.exe v4.0

dsyme commented 8 years ago

The code is lexed/parsed as

    let msg = "Interactive"
let add x y =
  x + y 
printfn "%d" (add 1 2)

That's not too unsurprising though. What's surprising is that the second line doesn't give a warning for being offside.

tpetricek commented 8 years ago

I thought the code is parsed in the way that @mattklein999 reported because of the confusing tool tips you get in VS - but perhaps that's just an artifact of some error recovery that happens after the code is parsed (?)

vs