-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Handle potential null in extension lookup #80621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There are specific scenarios where applicability will remove a symbol from lookup entirely; no error, just silent removal. This introduces a `null` that consumers were not expecting and caused an NRE. I've handled that NRE here and annotated the API. Fixes https://developercommunity.visualstudio.com/t/NRE-in-Roslyn-v500-225451107/10979295.
| var code2 = """ | ||
| namespace N2; | ||
|
|
||
| file static class E |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One such "remove without any errors" scenario is when a file class is out of scope:
There are other scenarios in which lookup can return a result with a null symbol, but this proved to be the easiest to replicate as @viceroypenguin gave me a dump where he hit it. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this. I don't quite understand how the scenario differs from existing InstanceMethodInvocation_VariousScopes_Errors which also has a file-scoped extension (therefore inaccessible) :-/
Update: ah, the receiver is non existent...
|
@dotnet/roslyn-compiler for review |
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Reflection.Metadata.Ecma335; | ||
| using System.Text; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this using necessary? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for Encoding.
|
|
||
| var parseOptions = TestOptions.Regular.WithLanguageVersion(languageVersion); | ||
| var comp = CreateCompilation([ | ||
| CSharpSyntaxTree.ParseText(code, path: "file1.cs", encoding: Encoding.UTF8, options: parseOptions), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parseOptions can be passed to CreateCompilation.
| } | ||
| """; | ||
|
|
||
| var comp = CreateCompilation([CSharpSyntaxTree.ParseText(code, path: "file1.cs", encoding: Encoding.UTF8), CSharpSyntaxTree.ParseText(code2, path: "file2.cs", encoding: Encoding.UTF8)]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[CSharpSyntaxTree.ParseText(code, path: "file1.cs", encoding: Encoding.UTF8), CSharpSyntaxTree.ParseText(code2, path: "file2.cs", encoding: Encoding.UTF8)
Similar suggestion to simplify compilation creation code. Encoding.UTF8 is probably not required to reproduce the problem and even if it is, it looks like it is what our test helpers use by default anyway. #Closed
| { | ||
| Symbol extensionMember = singleLookupResult.Symbol; | ||
| Symbol? extensionMember = singleLookupResult.Symbol; | ||
| if (extensionMember is null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| internal SingleLookupResult(LookupResultKind kind, Symbol symbol, DiagnosticInfo error) | ||
| internal SingleLookupResult(LookupResultKind kind, Symbol? symbol, DiagnosticInfo? error) | ||
| { | ||
| this.Kind = kind; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better long term to filter out empty results in Refers to: src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:10895 in 1a6213d. [](commit_id = 1a6213d, deletion_comment = False) |
|
Done with review pass (commit 1) #Closed |
| foreach (SingleLookupResult singleLookupResult in singleLookupResults) | ||
| { | ||
| Symbol extensionMember = singleLookupResult.Symbol; | ||
| var extensionMember = singleLookupResult.Symbol; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that it is probably driven by added nullable annotations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's the reason.
| result.Add(resultOfThisMember); | ||
| } | ||
|
|
||
| if (candidate is MethodSymbol { IsStatic: false } shadows && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Done with review pass (commit 2) #Closed |
AlekseyTs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 3)
|
@dotnet/roslyn-compiler for a second review. |
jcouv
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks (commit 3)
|
nevermind, will be in december release |
|
/backport to release/dev18.0 |
|
Started backporting to |
|
@MaceWindu apologies, I got my milestones mixed up and this is not in 10.0.1xx. I'm working on it now. |
|
@333fred backporting to git am output$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch
Applying: Handle potential null in extension lookup
Using index info to reconstruct a base tree...
M src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
M src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests2.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Auto-merging src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests2.cs
CONFLICT (content): Merge conflict in src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests2.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Handle potential null in extension lookup
Error: The process '/usr/bin/git' failed with exit code 128 |
There are specific scenarios where applicability will remove a symbol from lookup entirely; no error, just silent removal. This introduces a `null` that consumers were not expecting and caused an NRE. I've handled that NRE here and annotated the API. Fixes https://developercommunity.visualstudio.com/t/NRE-in-Roslyn-v500-225451107/10979295. (cherry picked from commit 16883a5)
There are specific scenarios where applicability will remove a symbol from lookup entirely; no error, just silent removal. This introduces a
nullthat consumers were not expecting and caused an NRE. I've handled that NRE here and annotated the API. Fixes https://developercommunity.visualstudio.com/t/NRE-in-Roslyn-v500-225451107/10979295.