dotnet / dotnet/fsharp

Crash when using code quotations in tests

Open
#14,225 0 comments 0 reactions 0 assignees View on GitHub
Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

We do our unit tests using [the unquote library](https://github.com/SwensenSoftware/unquote) to write our assertions. This relies a lot on F# code quotations. Most of the time our tests run just fine but once in a while a test fails with a NullReferenceException. The weird thing is that both the test and the code under test are pure so I find it hard to understand where the flaky behavior comes from.

I've [also reported the bug to unquote](https://github.com/SwensenSoftware/unquote/issues/160), but unfortunately the author also doesn't know. Because the stack trace only contains `FSharp.Core` calls I was hoping maybe someone here might have an idea:

```
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(112,0): at Microsoft.FSharp.Quotations.FSharpVar..ctor(String name, Type typ, FSharpOption`1 isMutable)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2070,0): at Microsoft.FSharp.Quotations.PatternsModule.u_VarDecl@2070.Invoke(BindingEnv env)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2048,0): at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@2048-2.Invoke(BindingEnv env)
D:\a\_work\1\s\src\FSharp.Core\local.fs(236,0): at Microsoft.FSharp.Primitives.Basics.List.mapToFreshConsTail[a,b](FSharpList`1 cons, FSharpFunc`2 f, FSharpList`1 x)
D:\a\_work\1\s\src\FSharp.Core\local.fs(246,0): at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2031,0): at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@2030.Invoke(BindingEnv env)
D:\a\_work\1\s\src\FSharp.Core\local.fs(245,0): at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2031,0): at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@2030.Invoke(BindingEnv env)
D:\a\_work\1\s\src\FSharp.Core\local.fs(245,0): at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2031,0): at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@2030.Invoke(BindingEnv env)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2059,0): at Microsoft.FSharp.Quotations.PatternsModule.u_Expr@2059-5.Invoke(BindingEnv env)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2568,0): at Microsoft.FSharp.Quotations.PatternsModule.deserialize(Type localAssembly, Type[] referencedTypeDefs, Type[] spliceTypes, FSharpExpr[] spliceExprs, Byte[] bytes)
D:\a\_work\1\s\src\FSharp.Core\quotations.fs(2781,0): at Microsoft.FSharp.Quotations.FSharpExpr.Deserialize40(Type qualifyingType, Type[] referencedTypes, Type[] spliceTypes, FSharpExpr[] spliceExprs, Byte[] bytes)
at Tomra.Ts.Core.MapTests.Keys works as expected()

```

This is the test:

```F#
let [] ``Keys works as expected``() =
let map = Map.ofList [ (1, "One"); (2, "Two"); (3, "Three") ]
test <@ map |> Map.keys |> List.ofSeq = [1; 2; 3] @>
```
And this is the code being tested:

```F#
namespace global

open System

[]
module Map =

let keys (map: Map<_, _>) =
seq {
for KeyValue(key, _) in map do
yield key
}
```

**Related information**

I'm using .NET SDK 6.0.300 and originally experienced the problem when using F# 5. This week we were finally able to switch to F# 6 and I was secretly hoping this might fix the problem but unfortunately it did not.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.