-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Extensions: improve diagnostics for operators #80928
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,340 @@ | ||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||
| // See the LICENSE file in the project root for more information. | ||||||||
|
|
||||||||
| using System.Diagnostics; | ||||||||
| using System.Diagnostics.CodeAnalysis; | ||||||||
| using Microsoft.CodeAnalysis.CSharp.Symbols; | ||||||||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||||||||
| using Microsoft.CodeAnalysis.PooledObjects; | ||||||||
|
|
||||||||
| namespace Microsoft.CodeAnalysis.CSharp; | ||||||||
|
|
||||||||
| internal partial class Binder | ||||||||
| { | ||||||||
| /// <summary> | ||||||||
| /// This type collects different kinds of results from operator scenarios and provides a unified way to report diagnostics. | ||||||||
| /// It collects the first non-empty result for extensions and non-extensions separately. | ||||||||
| /// This follows a similar logic to ResolveMethodGroupInternal and OverloadResolutionResult.ReportDiagnostics | ||||||||
| /// </summary> | ||||||||
| private struct OperatorResolutionForReporting | ||||||||
| { | ||||||||
| private object? _nonExtensionResult; | ||||||||
| private object? _extensionResult; | ||||||||
|
|
||||||||
| /// <returns>Returns true if the result was set and <see cref="OperatorResolutionForReporting"/> took ownership of the result.</returns> | ||||||||
| private bool SaveResult(object result, ref object? savedResult) | ||||||||
| { | ||||||||
| if (savedResult is null) | ||||||||
| { | ||||||||
| savedResult = result; | ||||||||
| AssertInvariant(); | ||||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| /// <returns>Returns true if the result was set and <see cref="OperatorResolutionForReporting"/> took ownership of the result.</returns> | ||||||||
| public bool SaveResult(OverloadResolutionResult<MethodSymbol> result, bool isExtension) | ||||||||
| { | ||||||||
| if (result.ResultsBuilder.IsEmpty) | ||||||||
| { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| return SaveResult(result, ref isExtension ? ref _extensionResult : ref _nonExtensionResult); | ||||||||
| } | ||||||||
|
|
||||||||
| /// <returns>Returns true if the result was set and <see cref="OperatorResolutionForReporting"/> took ownership of the result.</returns> | ||||||||
| public bool SaveResult(BinaryOperatorOverloadResolutionResult result, bool isExtension) | ||||||||
| { | ||||||||
| if (result.Results.IsEmpty) | ||||||||
| { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| return SaveResult(result, ref isExtension ? ref _extensionResult : ref _nonExtensionResult); | ||||||||
| } | ||||||||
|
|
||||||||
| /// <returns>Returns true if the result was set and <see cref="OperatorResolutionForReporting"/> took ownership of the result.</returns> | ||||||||
| public bool SaveResult(UnaryOperatorOverloadResolutionResult result, bool isExtension) | ||||||||
| { | ||||||||
| if (result.Results.IsEmpty) | ||||||||
| { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| return SaveResult(result, ref isExtension ? ref _extensionResult : ref _nonExtensionResult); | ||||||||
| } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Follows a very simplified version of OverloadResolutionResult.ReportDiagnostics which can be expanded in the future if needed. | ||||||||
| /// </summary> | ||||||||
| internal bool TryReportDiagnostics(SyntaxNode node, BindingDiagnosticBag diagnostics, Binder binder, object leftDisplay, object? rightDisplay) | ||||||||
|
||||||||
| { | ||||||||
| object? resultToUse = pickResultToUse(_nonExtensionResult, _extensionResult); | ||||||||
| if (resultToUse is null) | ||||||||
| { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| var results = ArrayBuilder<(MethodSymbol?, OperatorAnalysisResultKind)>.GetInstance(); | ||||||||
| populateResults(results, resultToUse); | ||||||||
|
|
||||||||
| bool reported = tryReportDiagnostics(node, diagnostics, binder, results, leftDisplay, rightDisplay); | ||||||||
| results.Free(); | ||||||||
|
|
||||||||
| return reported; | ||||||||
|
|
||||||||
| static bool tryReportDiagnostics(SyntaxNode node, BindingDiagnosticBag diagnostics, Binder binder, ArrayBuilder<(MethodSymbol? member, OperatorAnalysisResultKind resultKind)> results, object leftDisplay, object? rightDisplay) | ||||||||
| { | ||||||||
| assertNone(results, OperatorAnalysisResultKind.Undefined); | ||||||||
|
|
||||||||
| if (hadAmbiguousBestMethods(results, node, diagnostics, binder)) | ||||||||
| { | ||||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
| if (results.Any(m => m.resultKind == OperatorAnalysisResultKind.Applicable)) | ||||||||
| { | ||||||||
| return false; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for instance |
||||||||
| } | ||||||||
|
|
||||||||
| assertNone(results, OperatorAnalysisResultKind.Applicable); | ||||||||
| assertNone(results, OperatorAnalysisResultKind.Worse); | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went back and forth on this. I'll put fallback handling here instead, even though I don't think it's currently reachable due to how we produce operator results. It'll be more robust |
||||||||
|
|
||||||||
| Debug.Assert(results.All(r => r.resultKind == OperatorAnalysisResultKind.Inapplicable)); | ||||||||
|
|
||||||||
| // There is much room to improve diagnostics on inapplicable candidates, but for now we just report the candidate if there is a single one. | ||||||||
| if (results.Count == 1 && results[0].member is { } inapplicableMember) | ||||||||
|
||||||||
| if (results.Count == 1 && results[0].member is { } inapplicableMember) | |
| if (results is [{ member: { } inapplicableMember }]) | |
| ``` #Resolved |
Outdated
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.
Outdated
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.
Outdated
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.
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 code path reachable?
I guess this shouldn't be a concern for this component.
Uh oh!
There was an error while loading. Please reload this page.
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.
It would be good to document what types can be stored in these fields #Closed