-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Overload Resolution Priority #74190
base: main
Are you sure you want to change the base?
Overload Resolution Priority #74190
Conversation
This feature is small enough that, rather than do a full feature branch with multiple PRs, I've just done it as a single PR. I don't really recommend a commit-by-commit review here, I'll go through and comment on the actually interesting parts of the implementation. Most of it is actually just tests and attribute decoding boilerplate. |
@@ -1698,6 +1711,78 @@ private int GetTheBestCandidateIndex<TMember>(ArrayBuilder<MemberResolutionResul | |||
return currentBestIndex; | |||
} | |||
|
|||
private static void RemoveLowerPriorityMembers<TMember>(ArrayBuilder<MemberResolutionResult<TMember>> results) |
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.
This method, and the calls to this method in the current file, are the actually interesting parts of the feature. This implements the rules from the speclet, and I've tried to be perf conscious with a couple of early outs to avoid any allocations in the very common scenarios where we have no adjusted priorities.
@@ -2346,5 +2346,10 @@ internal enum ErrorCode | |||
// Note: you will need to do the following after adding warnings: | |||
// 1) Re-generate compiler code (eng\generate-compiler-code.cmd). | |||
// 2) Update ErrorFacts.IsBuildOnlyDiagnostic (src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs) | |||
|
|||
// PROTOTYPE: Condense |
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'll address these prototypes after we do the test plan review with the compiler team. They're here for now to make it easier for me to merge in main
.
@@ -35,5 +35,5 @@ | |||
"azure-pipelines.customSchemaFile": ".vscode/dnceng-schema.json", | |||
"dotnet.defaultSolution": "Roslyn.sln", | |||
"dotnet.completion.showCompletionItemsFromUnimportedNamespaces": true, | |||
"dotnet.testWindow.disableAutoDiscovery": true | |||
"dotnet.testWindow.disableAutoDiscovery": false |
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.
Yes, though I can revert if you want to keep it separate.
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, though I can revert if you want to keep it separate.
I'm not sure what the change affects, but it looks like it's unrelated to overload resolution, so consider reverting and creating a separate PR if needed.
src/Compilers/CSharp/Portable/Symbols/Source/SourceMethodSymbolWithAttributes.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/Source/SourceMethodSymbolWithAttributes.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbolBase.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/Source/SourceMethodSymbolWithAttributes.cs
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Emit3/OverloadResolutionPriorityTests.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Emit3/OverloadResolutionPriorityTests.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs
Show resolved
Hide resolved
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.
Will this feature be implemented for VB as well?
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, some amount of the feature will be implemented for VB.
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 (iteration 15). Tests not looked at much yet
<value>Cannot use 'OverloadResolutionPriorityAttribute' on an accessor method.</value> | ||
</data> | ||
<data name="ERR_CannotApplyOverloadResolutionPriorityToExplicitImplementation" xml:space="preserve"> | ||
<value>Cannot use 'OverloadResolutionPriorityAttribute' on an explicit interface implementation.</value> |
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.
spec: Is this mentioned in the spec?
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's an ignored location. I can certainly update the spec to more completely list every location that is ignored by the language, but it does fall out of the rules.
int32 priority | ||
) cil managed | ||
{ | ||
// Method begins at RVA 0x2069 |
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.
nit: consider doing a bit of cleanup in the IL (remove unnecessary portions, compact the signatures)
nit: also consider moving this IL into OverloadResolutionPriorityTests.cs
since that's the only usage and it's unlikely we'll need this in other test files
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'll do some cleanup, but I always prefer to put attributes here. It's the only usage today, but there's no guarantee that continue to the be case, and in my experience we're more likely to copy definitions around in the future, not move them.
|
||
internal sealed override int? TryGetOverloadResolutionPriority() | ||
{ | ||
return 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.
Is there a test for destructor?
spec: Should it be an error?
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'll add it. It should indeed be an error, since it's ignored.
@@ -369,6 +369,28 @@ internal override (CSharpAttributeData?, BoundAttribute?) EarlyDecodeWellKnownAt | |||
// diagnostics that might later get thrown away as possible when binding method calls. | |||
return (null, null); | |||
} | |||
else if (CSharpAttributeData.IsTargetEarlyAttribute(arguments.AttributeType, arguments.AttributeSyntax, AttributeDescription.OverloadResolutionPriorityAttribute)) | |||
{ | |||
if (this is SourcePropertyAccessorSymbol or SourceEventAccessorSymbol) |
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.
spec: What is the plan for local functions and lambdas? Seems like they should be errors.
Same question for conversion and unary operators.
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.
Technically speaking, I don't think any of these (except lambdas) are places where the attribute would be ignored: it may be rather pointless to put them there, but overload resolution will run on operators, conversions, and local function invocations. Lambdas would be ignored though, so we can error there.
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.
Local functions and unary operators Yes, they do involve overload resolution, but overloads are not possible so an overload resolution attribute doesn't make sense. I thought we error on positions where the attribute will be ignored.
Update: I see your point that the attribute will be not ignored, merely that it doesn't do anything.
User-defined conversion operators: I don't see where overload resolution is involved in the spec or the code.
{ | ||
} | ||
|
||
[OverloadResolutionPriority(1)] |
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'd be good to leave a comment (attribute is intentionally ignored)
if (!_packedFlags.IsOverloadResolutionPriorityPopulated) | ||
{ | ||
if (_containingType.ContainingPEModule.Module.TryGetOverloadResolutionPriorityValue(_handle, out int priority)) | ||
{ |
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.
Should we ignore the priority when it is present on invalid PE members (property/event accessors, override, destructor, non-indexer properties, ...)?
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.
We can't easily do that for accessors (and I have tests demonstrating the behavior), since we don't track, at the PEMethodSymbol layer, if this method is an accessor for some property or event. I instead opted to just read them, and not care whether the language may not permit them to be declared. We could certainly optimize the override/destructor/non-indexer properties, but it didn't seem like it would be worth the effort to me. If a tree falls in the forest but no one is there to hear it, did it make a sound?
{ | ||
get | ||
{ | ||
if (!this.IsIndexer) |
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'm not very familiar with the concept, but I see that we also have "indexed properties" in metadata (see IsIndexedProperty
) which also have parameters. As far as I can tell, they may not be considered indexers (IsIndexer
). Should the attribute apply to those?
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.
Yeah, it probably should, thanks for the reminder.
case []: | ||
case [_]: | ||
// We only look at methods and indexers, so if this isn't one of those scenarios, we don't need to do anything. | ||
case [{ Member: not (MethodSymbol or PropertySymbol { IsIndexer: true }) }, _, ..]: |
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.
Aside from indexed properties, are there any other symbol that hits this case?
Depending on the decision on indexed properties (see comment elsewhere) this case may not be useful.
This implements the
Overload Resolution Priority
language feature.Test Plan: #74131
Champion issue: dotnet/csharplang#7706
Speclet: https://github.com/dotnet/csharplang/blob/main/proposals/overload-resolution-priority.md