Skip to content

Commit 48b945e

Browse files
committed
Make the output of fsyacc compatible with nullable reference types.
1 parent e8a4481 commit 48b945e

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/FsYacc.Core/fsyaccdriver.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,6 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
229229

230230
writer.WriteLine "#nowarn \"1182\" // the generated code often has unused variable 'parseState'"
231231

232-
writer.WriteLine
233-
"#nowarn \"3261\" // the generated code would need to properly annotate nulls, e.g. changing System.Object to `obj|null`"
234-
235232
for s in generatorState.opens do
236233
writer.WriteLine "open %s" s
237234
writer.WriteLineInterface "open %s" s
@@ -371,7 +368,10 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
371368
| None -> "")
372369
(match typ with
373370
| Some _ -> "Microsoft.FSharp.Core.Operators.box _fsyacc_x"
374-
| None -> "(null : System.Object)")
371+
// We can't use null here because if all tokens are untyped, the function will be generic.
372+
// We used to use (null : obj) but that leads to warnings when nullable reference types are enabled.
373+
// box null does the right thing regardless of NRT and gets optimized to a single ldnull.
374+
| None -> "Microsoft.FSharp.Core.Operators.box null")
375375

376376
for key, _ in spec.Types |> Seq.countBy fst |> Seq.filter (fun (_, n) -> n > 1) do
377377
failwithf "%s is given multiple %%type declarations" key

0 commit comments

Comments
 (0)