← All articles

Verifying Roslyn led to three parsing bugs

I'm building a Z3-based verification harness for RyuJIT to learn more about formal verification.

Note: Z3 is a solver for mathematical constraints. It can find an input that breaks a stated property, or prove that none exists within the model.

While trying to figure out how to utilize Z3 to suit my needs, I built a differential engine that checks if Roslyn's (the C# compiler: C# -> IL) incremental parser worked as intended. Needed it to establish correctness of the Roslyn foundation before moving on to using Z3 on RyuJIT (the JIT compiler: IL -> CPU instructions).

Roslyn can update a syntax tree from an old tree and a text edit. It can also parse the final text from scratch. Given the same final text and parse options, those routes should agree on the syntax and diagnostics they expose.

I generated edits, built trees through both routes, and compared their public results. Across 4224 edit states, the checker recorded 54 differences. These are observations, not 54 separate bugs.

Every one of them was on invalid source, and several were related. I reduced individual cases to one edit, checked whether the difference reached compiler APIs, and traced the relevant parser code. That led to three Roslyn issues. One of them came from following up a Script difference, which turned up a valid final text that crashes the compiler.

A valid Script that crashes after an edit#

The first case starts with incomplete Script text:

Text
void w() {} class C{w();

Insert } before w(); and the final text is valid:

Text
void w() {} class C{}w();

A fresh parse makes w(); a global statement. The incremental parse reused it as a ConstructorDeclaration from the old class body. It reported no parse errors, but asking the resulting compilation for diagnostics threw InvalidCastException. Calling Emit() separately threw the same exception.

The reuse decision in LanguageParser.CanReuseMemberDeclaration allowed a constructor even when the parser was at Script root. Later, NamedTypeSymbol.GetScriptConstructor assumed a Script constructor there had been synthesized and cast it accordingly. The reused source constructor broke that assumption.

I reported it in issue #86575.

An interpolated string loses an error#

The next case changes an expression-bodied method into an invalid member declaration:

Text
Before: class C { string M() => $"{1 }}"; }
After:  class C { string M() ; $"{1 }}"; }

The extra } inside the interpolated string should produce CS8086. A fresh parse reports it at offset 29, along with the expected recovery error CS1519. The incremental parse reports only CS1519. The source is already invalid, but an editor relying on incremental parsing can hide the more useful error until the file is parsed from scratch.

The old interpolated-string expression has a diagnostic, so the parser cannot reuse that expression node. It turns the expression back into an InterpolatedStringToken for reparsing. RescanInterpolatedString does not reconstruct lexical diagnostics on that token. That token can then be reused as it is. The new parser puts it in skipped text without lexing it again, so the diagnostic disappears.

I reported it in issue #86577.

A missing brace points to the wrong place#

The third case is smaller. Start with class C{/ // note\nx, then insert a newline immediately after {. The final text is class C{\n/ // note\nx, where \n represents a newline. A fresh parse puts the missing-brace diagnostic CS1513 at offset 20, the end of the text. An incremental parse puts it at offset 11, on the previous line at the start of the // note comment.

Text
fresh:       CS1513 [20..20)
incremental: CS1513 [11..11)

Both trees contain the same missing } token at offset 20. The difference is the diagnostic location. The likely cause is a cached piece of trailing trivia in SyntaxParser: EatNode consumes the reused x node without updating _prevTokenTrailingTrivia. The field still holds the trailing trivia of the / token, // note\n. When the parser creates the missing brace, it sees a newline in that stale trivia and moves the diagnostic back by the trivia's full width, nine characters. The size of the shift depends on that trivia; with a bare newline it is one character.

I reported it in issue #86581.