Skip to content

Match expression is evaluated twice if it returns a struct union #5689

Closed

Description

@cmeeren

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
  0

Expected behavior

The message should only be printed once for each use of match.

Actual behavior

image

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

Activity

  1. zpodlovics commented on Sep 22, 2018

    @zpodlovics

    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:

    Option inlined
    Option outside
    ValueOption inlined
    ValueOption outside
    Result inlined
    Result outside
    Custom value union inlined
    Custom value union outside
    Custom struct union inlined
    Custom struct union outside
    Custom struct union inlined with catch-all
    Custom struct record inlined
    
    

    Debug:

    Option inlined
    Option outside
    ValueOption inlined
    ValueOption outside
    Result inlined
    Result outside
    Custom value union inlined
    Custom value union outside
    Custom struct union inlined
    Custom struct union outside
    Custom struct union inlined with catch-all
    Custom struct record inlined
    
    
  2. cmeeren commented on Sep 22, 2018

    @cmeeren
    ContributorAuthor

    Here's a fresh repro solution with binaries; I had some troubles with the original one on another device.

    ConsoleApp1.zip

  3. zpodlovics commented on Sep 22, 2018

    @zpodlovics

    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)

        IL_0044:  ldstr      "ValueOption inlined"
        IL_0049:  newobj     instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [System.Runtime.Extensions]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
        IL_004e:  stloc.0
        IL_004f:  call       class [System.IO]System.IO.TextWriter [System.Console]System.Console::get_Out()
        IL_0054:  ldloc.0
        IL_0055:  call       !!0 [FSharp.Core]Microsoft.FSharp.Core.PrintfModule::PrintFormatLineToTextWriter<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [System.Runtime.Extensions]System.IO.TextWriter,
                                                                                                                                                             class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0,class [System.Runtime.Extensions]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
        IL_005a:  pop
        IL_005b:  ldnull
        IL_005c:  call       valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::NewValueSome(!0)
        IL_0061:  stloc.2
        IL_0062:  ldloca.s   V_2
        IL_0064:  call       instance int32 valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::get_Tag()
        IL_0069:  ldc.i4.0
        IL_006a:  bne.un.s   IL_0070
    
        IL_006c:  nop
        IL_006d:  nop
        IL_006e:  br.s       IL_0072
    
        IL_0070:  nop
        IL_0071:  nop
        IL_0072:  ldstr      "ValueOption outside"
    

    5689_ok.txt

    vs

        IL_0044:  ldstr      "ValueOption inlined"
        IL_0049:  newobj     instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [System.Runtime.Extensions]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
        IL_004e:  stloc.0
        IL_004f:  call       class [System.IO]System.IO.TextWriter [System.Console]System.Console::get_Out()
        IL_0054:  ldloc.0
        IL_0055:  call       !!0 [FSharp.Core]Microsoft.FSharp.Core.PrintfModule::PrintFormatLineToTextWriter<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [System.Runtime.Extensions]System.IO.TextWriter,
                                                                                                                                                             class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0,class [System.Runtime.Extensions]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
        IL_005a:  pop
        IL_005b:  ldnull
        IL_005c:  call       valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::NewValueSome(!0)
        IL_0061:  stloc.2
        IL_0062:  ldstr      "ValueOption inlined"
        IL_0067:  newobj     instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [System.Runtime.Extensions]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
        IL_006c:  stloc.0
        IL_006d:  call       class [System.IO]System.IO.TextWriter [System.Console]System.Console::get_Out()
        IL_0072:  ldloc.0
        IL_0073:  call       !!0 [FSharp.Core]Microsoft.FSharp.Core.PrintfModule::PrintFormatLineToTextWriter<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [System.Runtime.Extensions]System.IO.TextWriter,
                                                                                                                                                             class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0,class [System.Runtime.Extensions]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
        IL_0078:  pop
        IL_0079:  ldnull
        IL_007a:  call       valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::NewValueSome(!0)
        IL_007f:  stloc.3
        IL_0080:  ldloca.s   V_3
        IL_0082:  call       instance int32 valuetype [FSharp.Core]Microsoft.FSharp.Core.FSharpValueOption`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::get_Tag()
        IL_0087:  ldc.i4.0
        IL_0088:  bne.un.s   IL_008e
    
        IL_008a:  nop
        IL_008b:  nop
        IL_008c:  br.s       IL_0090
    
        IL_008e:  nop
        IL_008f:  nop
        IL_0090:  ldstr      "ValueOption outside"
    

    5689_error.txt

  4. cmeeren commented on Sep 22, 2018

    @cmeeren
    ContributorAuthor

    it would be good to see the AST difference between the two compilation.

    Your message was a bit unclear on whether this is something you want me to provide. If so, I have no idea how.

  5. zpodlovics commented on Sep 22, 2018

    @zpodlovics

    In order to dig it deeper it would require to check the compiler internals (AST=Abstract Syntax Tree [1]). There is an --ast hidden 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

  6. cmeeren commented on Sep 22, 2018

    @cmeeren
    ContributorAuthor

    I interpret this to mean that someone other than me has to look into this.

  7. TIHan commented on Sep 24, 2018

    @TIHan
    Contributor

    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.

  8. self-assigned this
    on Sep 24, 2018
  9. added
    Impact-High
    on Sep 24, 2018
  10. cmeeren commented on Sep 24, 2018

    @cmeeren
    ContributorAuthor

    I can't reproduce this in 15.8.5, but I can reproduce it in master (also nightlies).

    I can confirm this. I uninstalled the nightly and am unable to reproduce the behavior on 15.8.5.

  11. TIHan commented on Sep 25, 2018

    @TIHan
    Contributor

    This PR caused the regression: #5148

    We will look into it.

  12. added this to the 16.0 milestone on Oct 1, 2018
  13. TIHan commented on Oct 2, 2018

    @TIHan
    Contributor

    This has been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Impact-HighRegression

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions