Repository navigation
Closed
Labels
Impact-High(Internal MS Team use only) Describes an issue with extreme impact on existing code.Regression
Milestone
Description
Potentially critical issue: If the expression (e.g. a function call) inside a match ... with returns a struct union (e.g. ValueOption<_>, Result<_,_> or a custom struct union), the expression is evaluated twice, including any side effects.
Repro steps
Repro solution: Test.zip ConsoleApp1.zip
The repro solution consists of a console app with the following code, showing various examples and aspects of this bug:
open System
type CustomValueUnion = V
[<Struct>]
type CustomStructUnion = S
[<Struct>]
type CustomStructRecord =
{ A: CustomStructUnion }
[<EntryPoint>]
let main argv =
match printfn "Option inlined"; Some () with
| Some () -> ()
| None -> ()
let x = printfn "Option outside"; Some ()
match x with
| Some () -> ()
| None -> ()
match printfn "ValueOption inlined"; ValueSome () with
| ValueSome () -> ()
| ValueNone -> ()
let x = printfn "ValueOption outside"; ValueSome ()
match x with
| ValueSome () -> ()
| ValueNone -> ()
match printfn "Result inlined"; Ok () with
| Ok () -> ()
| Error () -> ()
let x = printfn "Result outside"; Ok ()
match x with
| Ok () -> ()
| Error () -> ()
match printfn "Custom value union inlined"; V with
| V -> ()
let x = printfn "Custom value union outside"; V
match x with
| V -> ()
match printfn "Custom struct union inlined"; S with
| S -> ()
let x = printfn "Custom struct union outside"; S
match x with
| S -> ()
match printfn "Custom struct union inlined with catch-all"; S with
| _ -> ()
match printfn "Custom struct record inlined"; { A = S } with
| { A = S } -> ()
printfn "Press Enter to exit..."
Console.ReadLine() |> ignore
0Expected behavior
The message should only be printed once for each use of match.
Actual behavior
Known workarounds
Bind the expression to a named value and use that value in the match expression, like the example shows.
Related information
- Visual Studio version: 15.8.5
- Visual F# tools 10.2 for F# 4.5, 15.8.0.0, commit hash 92247b8
- Severity: Assumed high
Reacted by Abel Braaksma
Metadata
Metadata
Assignees
Labels
Impact-High(Internal MS Team use only) Describes an issue with extreme impact on existing code.Regression

Activity
zpodlovics commentedon Sep 22, 2018
Could you also attach the compiled binaries to check the generated code? Unfortunately I cannot reproduce it on .NET Core SDK 2.1.402-1 on Linux (Debug/Release).
Release:
Debug:
cmeeren commentedon Sep 22, 2018
Here's a fresh repro solution with binaries; I had some troubles with the original one on another device.
ConsoleApp1.zip
zpodlovics commentedon Sep 22, 2018
It seems that the offending code inlined two times, it would be good to see the AST difference between the two compilation.
The ILDASM update to your #5689 (comment)
5689_ok.txt
vs
5689_error.txt
cmeeren commentedon Sep 22, 2018
Your message was a bit unclear on whether this is something you want me to provide. If so, I have no idea how.
zpodlovics commentedon Sep 22, 2018
In order to dig it deeper it would require to check the compiler internals (AST=Abstract Syntax Tree [1]). There is an
--asthidden option available [2] for fsc/fsharpc, but the output seems missing when enabled with<OptArgs>--ast</OptArgs>in a new style .net project...[1] https://fsharp.github.io/FSharp.Compiler.Service/untypedtree.html
[2] https://github.com/Microsoft/visualfsharp/blob/fead0aac540485683f694524eadad79983ec28d9/src/fsharp/CompileOptions.fs#L882
cmeeren commentedon Sep 22, 2018
I interpret this to mean that someone other than me has to look into this.
TIHan commentedon Sep 24, 2018
Yes, this would classify as very high priority.
I can't reproduce this in 15.8.5, but I can reproduce it in master (also nightlies). That means a recent change happened in master for this to occur. I'm going to test 15.9 preview to see if I can reproduce it there.
Thank you for spotting this @cmeeren . We can make a test case out of this one.
cmeeren commentedon Sep 24, 2018
I can confirm this. I uninstalled the nightly and am unable to reproduce the behavior on 15.8.5.
TIHan commentedon Sep 25, 2018
This PR caused the regression: #5148
We will look into it.
TIHan commentedon Oct 2, 2018
This has been resolved.