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 783 forks source link

Wrong let expression parsing #7741

Open auduchinok opened 5 years ago

auduchinok commented 5 years ago

The contents of do in the following code should be parsed as a sequence of let expression and x, but it's parsed as let expression in sequence of the sum and x.

do
    let x = 1 in x + 1
    x

In addition to the unexpected tree form it also creates a wrong scope for x.

Screenshot 2019-10-17 at 14 35 50

AST viewer

abelbraaksma commented 4 years ago

Wait, doesn't in introduce scope? Shouldn't the last line x be out of scope for x?

(I admit I'm not sure of this, I use the in syntax rarely)

auduchinok commented 4 years ago

@abelbraaksma It does, and its scope should be x + 1 here.

abelbraaksma commented 4 years ago

Edit, I may misunderstand the scoping here, as this seems to work ~properly~ well, it works, not sure of "properly":

let _ =
    let x = 1 in x + 2
    x  // in scope?

And if I take your code and paste it in VS, it correctly seems to say for each line that you're returning a value when you should return unit. But it appears to get the line number wrong for the second error.

If I remove the in it still gets the line number wrong (maybe not related to this issue): image

auduchinok commented 4 years ago

I'd expect it to be parsed as:

do
    (let x = 1 in (x + 1));
    x

And the latter x would be unresolved.

dsyme commented 4 years ago

This is a long-standing issue and I think there are various duplicates of it. I suspect the right approach here is to start raising warnings when this formulation is used

auduchinok commented 4 years ago

@dsyme How bad would be changing the parsing rules here?