You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix crashes and code fixes found by syntax fuzzing - #4344
Found while exploring #4338. These are not a fix for that issue.
What was wrong
A deterministic syntax-mutation pass and a replay of extracted test snippets found four defects:
b++ at the end of an incomplete buffer crashed SA1003. TokenHelper.IsFirstInLine and IsLastInLine called GetLineSpan before checking that SyntaxTree was null.
SA1316 renamed only the tuple element declaration. value.nameA was left unchanged and no longer compiled.
SA1309 renamed _testEvent to testEvent, which was already the event name, and introduced CS0102.
A finalizer that is not inside a type made SA1643 and SA1600 throw NullReferenceException from GetTypeParameterList.
Review of the SA1316 fix found more ways that replacement could produce invalid code:
A tuple element referenced from documentation trivia, such as <see cref="Point.x"/>, was not found inside trivia. The fixer replaced the token that owned the comment (the class keyword) and threw ArgumentException.
Renaming the identifier in new { value.nameA } also changed the inferred anonymous property name. Later uses of projected.nameA no longer compiled, and new { value.nameA, NameA = 0 } gained duplicate properties.
Preserving that property name with ValueText dropped the @ from an escaped keyword, so new { value.@class } became class = value.Class.
An inferred tuple projection such as (false, value.nameA) lost the name nameA when the element was renamed. A projection that also had an explicit NameA element could not be renamed without selecting the wrong element.
A Visual Basic document can reference a public C# tuple element. Replacing that reference with a C# identifier throws, and skipping the document would leave the reference broken.
What changed
The null check in both helpers now happens before GetLineSpan.
SA1316 replaces the declared name and its references directly. Roslyn's renamer throws on tuple element symbols. Tuple literal names still have no symbol and keep the token-only replacement. Reference lookup searches trivia. An inferred anonymous projection is rewritten to an explicit initializer that keeps the original identifier, including @ escaping. An inferred tuple projection uses the corrected explicit name and updates consumers of that name. The fix is not offered when that corrected name would select another element, or when any reference is not in a C# document.
SA1309 is not offered when the stripped name collides with an existing member.
SA1642, SA1643, and SA1600 are not offered when the constructor or finalizer has no containing type to name.
Tests
Each new test was run before the corresponding fix:
The helper tests fail with the NullReferenceException at TokenHelper.cs.
The SA1316 reference test fails with the declaration renamed and value.nameA left unchanged.
The SA1309 test fails by showing the field and its uses renamed onto the existing event.
The SA1643 and SA1600 tests fail with the NullReferenceException from GetTypeParameterList.
The cref case throws from ClassDeclarationSyntax.Update.
The anonymous cases produce value.NameA without keeping the original property name, and the escaped-keyword case produces class = value.Class.
The inferred tuple case leaves projected.nameA pointing at a name that no longer exists. The colliding tuple case and the Visual Basic case would still offer a fix.
The SA1003, SA1309, SA1600, and SA1643 suites pass on the C# 6 project. The SA1316 suite passes on the C# 7 and C# 12 projects. The new tests also pass on the C# 15 project.
Perf
The analyzer callbacks are unchanged. The helper null check already existed; it now runs before GetLineSpan. The extra symbol, trivia, and ancestor checks are in the code fixes, which run only when a fix is requested.
Behavior
⚑ From C# 9, incomplete b++ reports SA1003 (++ should be followed by whitespace) instead of crashing, and the code fix inserts the space. Earlier compilers do not report it.
⚑ SA1309 no longer offers a rename that would collide with another member.
⚑ Documentation fixes are not offered for a constructor or finalizer that is not inside a type.
⚑ SA1316 now updates references inside documentation trivia, preserves an inferred anonymous property name as an explicit initializer, and keeps @ escaping on that name.
⚑ SA1316 is not offered when an inferred tuple projection would collide with another element, when a duplicate inferred name was suppressed, or when a reference is in a non-C# document. A non-colliding inferred tuple projection is renamed together with its consumers, including later projections of those consumers.
The experimental fuzz harness used to find these cases is not included.
IsFirstInLine and IsLastInLine checked SyntaxTree for null after calling
GetLineSpan, so a postfix operator at the end of an incomplete buffer
crashed SA1003. Check for a missing tree first.
The new helper tests fail on master with the NullReferenceException at
TokenHelper.cs. The incomplete b++ analyzer test locks the editor case.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
The code fix replaced only the declared name. A use such as value.nameA
then failed to compile. Tuple element symbols cannot go through Renamer,
so the declaration and its references are replaced directly. Tuple literal
names still have no symbol and keep the token-only replacement.
The new test fails on master with the declaration renamed and the use left
unchanged.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
Stripping the underscore from _testEvent produced testEvent, which was
already the event name, so the fix introduced CS0102. The code fix is now
withheld when the resulting name is not a valid member name.
The new test fails on master by showing the field and its uses renamed onto
the existing event.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
SA1642, SA1643, and SA1600 built standard text from the containing type
without checking that one exists. A finalizer in a loose block therefore
threw NullReferenceException from GetTypeParameterList. The fix is not
offered when there is no type to name.
Both new tests fail on master with that exception.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
After the null-tree crash is fixed, Roslyn 3.8 and later still see the
missing token after b++ and report that ++ should be followed by space.
Earlier compilers do not. The test now matches that split, and the code
fix inserts the space.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
❌ Patch coverage is 91.27517% with 65 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.65%. Comparing base (013effd) to head (03884e7). ⚠️ Report is 11 commits behind head on master.
FindToken without searching trivia returns the token that owns a documentation
comment, so a cref to a tuple element replaced the class keyword and threw
ArgumentException. Search trivia and replace only a token that starts at the
reference.
Renaming the identifier in an inferred anonymous projection also changed the
property name, so later uses no longer compiled and an explicit property of the
corrected name became a duplicate. Preserve the original inferred name as an
explicit initializer before renaming the remaining references.
Both regressions fail on the previous commit: the cref case throws from
ClassDeclarationSyntax.Update, and the anonymous cases produce value.NameA
without keeping the original property name.
The analyzer is unchanged. The extra work is in the code fix and runs only
when a fix is requested.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
Inferred tuple projections also need name preservation. In C# 7.1+, with (int nameA, int Other) value, var projected = (value.nameA, 0L); return projected.nameA; becomes (value.NameA, 0L) while the final access stays unchanged and no longer compiles. The projected field belongs to a different tuple type, so references to the original field do not include that consumer. Preserve the inferred name explicitly (nameA: value.NameA) before replacing the reference, and add a regression test.
ValueText drops the '@' from an escaped identifier, so renaming a tuple
element in `new { value.@Class }` produced `class = value.Class`, which
does not compile. Copy the original identifier token without trivia or
the rename annotation.
The new test fails on the previous commit by showing `class = value.Class`.
The analyzer is unchanged.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
Renaming a tuple element used as an inferred tuple name changed that name.
When the corrected name was free, the projection and its consumers now use
the corrected explicit name. When it would select another element, the fix
is not offered.
A Visual Basic reference cannot be rewritten with a C# token. Skipping it
would leave the reference broken, so the fix is not offered unless every
reference is in a C# document.
The inferred-name regressions fail on the previous commit by producing
value.NameA and leaving projected.nameA unchanged. The collision and Visual
Basic cases would still offer a fix that cannot be applied safely.
The analyzer is unchanged. The extra checks run only when a fix is requested.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
Microsoft.CodeAnalysis.VisualBasic.Workspaces 2.8.2 flowed through the C# 7
test project into later test projects, which resolve a newer Workspaces.Common.
That mismatch is NU1608 and failed the solution build. The package is only
needed to compile the mixed-language test, so it is private to that project.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
Consumer discovery stopped after the first inferred projection, so a second
projection was renamed while its own consumers were left unchanged. Newly
discovered consumers are now walked, including collision and language checks.
Duplicate inferred names are dropped by the compiler, so both elements remain
Item1 and Item2. The fix is not offered when the resolved element name is not
the inferred identifier.
Both regressions fail on the previous commit: the chain leaves second.nameA
unchanged, and the duplicate case still offers a fix.
The analyzer is unchanged.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found while exploring #4338. These are not a fix for that issue.
What was wrong
A deterministic syntax-mutation pass and a replay of extracted test snippets found four defects:
b++at the end of an incomplete buffer crashed SA1003.TokenHelper.IsFirstInLineandIsLastInLinecalledGetLineSpanbefore checking thatSyntaxTreewas null.value.nameAwas left unchanged and no longer compiled._testEventtotestEvent, which was already the event name, and introduced CS0102.NullReferenceExceptionfromGetTypeParameterList.Review of the SA1316 fix found more ways that replacement could produce invalid code:
<see cref="Point.x"/>, was not found inside trivia. The fixer replaced the token that owned the comment (theclasskeyword) and threwArgumentException.new { value.nameA }also changed the inferred anonymous property name. Later uses ofprojected.nameAno longer compiled, andnew { value.nameA, NameA = 0 }gained duplicate properties.ValueTextdropped the@from an escaped keyword, sonew { value.@class }becameclass = value.Class.(false, value.nameA)lost the namenameAwhen the element was renamed. A projection that also had an explicitNameAelement could not be renamed without selecting the wrong element.What changed
GetLineSpan.@escaping. An inferred tuple projection uses the corrected explicit name and updates consumers of that name. The fix is not offered when that corrected name would select another element, or when any reference is not in a C# document.Tests
Each new test was run before the corresponding fix:
NullReferenceExceptionatTokenHelper.cs.value.nameAleft unchanged.NullReferenceExceptionfromGetTypeParameterList.ClassDeclarationSyntax.Update.value.NameAwithout keeping the original property name, and the escaped-keyword case producesclass = value.Class.projected.nameApointing at a name that no longer exists. The colliding tuple case and the Visual Basic case would still offer a fix.The SA1003, SA1309, SA1600, and SA1643 suites pass on the C# 6 project. The SA1316 suite passes on the C# 7 and C# 12 projects. The new tests also pass on the C# 15 project.
Perf
The analyzer callbacks are unchanged. The helper null check already existed; it now runs before
GetLineSpan. The extra symbol, trivia, and ancestor checks are in the code fixes, which run only when a fix is requested.Behavior
b++reports SA1003 (++should be followed by whitespace) instead of crashing, and the code fix inserts the space. Earlier compilers do not report it.@escaping on that name.The experimental fuzz harness used to find these cases is not included.