Skip to content
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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

333fred
Copy link
Member

@333fred 333fred commented Jun 27, 2024

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

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 27, 2024
@333fred
Copy link
Member Author

333fred commented Jun 27, 2024

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)
Copy link
Member Author

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
Copy link
Member Author

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.

@333fred 333fred marked this pull request as ready for review June 28, 2024 17:01
@333fred 333fred requested a review from a team as a code owner June 28, 2024 17:01
@jcouv jcouv self-assigned this Jun 28, 2024
@333fred 333fred requested review from cston and jcouv June 28, 2024 17:03
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false

Was this change intentional?

Copy link
Member Author

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.

Copy link
Member

@cston cston Jul 1, 2024

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

@333fred
Copy link
Member Author

333fred commented Jul 1, 2024

@cston addressed or answered your comments, please take another look. @jcouv for review.

Copy link
Member

@jcouv jcouv left a 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>
Copy link
Member

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?

Copy link
Member Author

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
Copy link
Member

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

Copy link
Member Author

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;
Copy link
Member

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?

Copy link
Member Author

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)
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

@jcouv jcouv Jul 2, 2024

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)]
Copy link
Member

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))
{
Copy link
Member

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, ...)?

Copy link
Member Author

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)
Copy link
Member

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?

Copy link
Member Author

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 }) }, _, ..]:
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Overload Resolution Priority untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants