dotnet / dotnet/fsharp

NullReferenceException with Debug build and recursive reference

Open
#16,546 1 comment 0 reactions 0 assignees View on GitHub
Area-Compiler-CodeGen Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

I'm using dotnet 8.0.101 with Windows 10. The program below, when run in Debug mode, will throw a NullReferenceException. If run in Release mode, it completes successfully. Both versions generate a warning about the reference to `parse` in `paramParse` (in `Type.parse`) needing a runtime check; I believe it is that reference to `parse` which is null in the Debug build.
```
type Ident = string

type Type =
| TPrim of Ident
| TParam of Ident * Type

let tryParam pv x =
match x with
| TParam (name, typ) ->
pv typ |> Result.map (fun t -> [name, t])
| _ -> Error "unexpected"

let tryPrim = function
| TPrim name -> Ok name
| _ -> Error "unused"

module Type =
let parse =
let rec paramParse = tryParam parse
and parse node =
match tryPrim node with
| Ok name -> Ok(TPrim name)
| _ ->
match paramParse node with
| Ok [name, typ] -> Ok(TParam(name,typ))
| _ -> Error "invalid type"
parse

[]
let main args =
printfn "%A" (Type.parse (TParam ("ptr", TPrim "float")))
0
```
The workaround is to reorder `parse` and `paramParse`; this gets rid of the warning and allows both Debug and Release to succeed. E.g.:
```
module Type =
let parse =
let rec parse node =
match tryPrim node with
| Ok name -> Ok(TPrim name)
| _ ->
match paramParse node with
| Ok [name, typ] -> Ok(TParam(name,typ))
| _ -> Error "invalid type"
and paramParse = tryParam parse
parse
```

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.