From 48b945e1b6f31b2e05241bc8229ebe43ca0c718f Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Sun, 22 Dec 2024 00:57:52 +0200 Subject: [PATCH] Make the output of fsyacc compatible with nullable reference types. --- src/FsYacc.Core/fsyaccdriver.fs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/FsYacc.Core/fsyaccdriver.fs b/src/FsYacc.Core/fsyaccdriver.fs index 2ff89ba..1e4a708 100644 --- a/src/FsYacc.Core/fsyaccdriver.fs +++ b/src/FsYacc.Core/fsyaccdriver.fs @@ -229,9 +229,6 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile writer.WriteLine "#nowarn \"1182\" // the generated code often has unused variable 'parseState'" - writer.WriteLine - "#nowarn \"3261\" // the generated code would need to properly annotate nulls, e.g. changing System.Object to `obj|null`" - for s in generatorState.opens do writer.WriteLine "open %s" s writer.WriteLineInterface "open %s" s @@ -371,7 +368,10 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile | None -> "") (match typ with | Some _ -> "Microsoft.FSharp.Core.Operators.box _fsyacc_x" - | None -> "(null : System.Object)") + // We can't use null here because if all tokens are untyped, the function will be generic. + // We used to use (null : obj) but that leads to warnings when nullable reference types are enabled. + // box null does the right thing regardless of NRT and gets optimized to a single ldnull. + | None -> "Microsoft.FSharp.Core.Operators.box null") for key, _ in spec.Types |> Seq.countBy fst |> Seq.filter (fun (_, n) -> n > 1) do failwithf "%s is given multiple %%type declarations" key