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

CallerArgumentExpression attributes on partial method parameters look at the implemention rather than definition parameter names #73483

Open
RikkiGibson opened this issue May 15, 2024 · 6 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@RikkiGibson
Copy link
Contributor

RikkiGibson commented May 15, 2024

Version Used: fca6e1f

Steps to Reproduce: SharpLab

using System.Runtime.CompilerServices;

public partial class C
{
    public partial void M1(string a, [CallerArgumentExpression("a")] string? b = null); // warning CS8963: The CallerArgumentExpressionAttribute applied to parameter 'y' will have no effect. It is applied with an invalid parameter name.
    public partial void M1(string x, string? y) { }
    
    public partial void M2(string a, [CallerArgumentExpression("a")] string? b = null); // ok
    public partial void M2(string a, string? b) { }
    
    void M()
    {
        // Call sites use the definition part parameter names
        M1(a: "1"); // ok
        M1(x: "1"); // error
    }
}

Expected Behavior: The definition part parameter name is used to decide what parameter to include a caller argument expression for

Actual Behavior: The implementation part parameter name is used

caller-argument-expression.md does not appear to specify what should happen here.

Note that using the attribute on the implementation part results in the attribute being ignored in source. So, it feels like the attribute's parameter name references should be oriented around the declaration where usage of the attribute is supported.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 15, 2024
@Youssef1313
Copy link
Member

That's an interesting scenario 🤓

I think the current behavior is indeed not the correct one.

@Youssef1313
Copy link
Member

Youssef1313 commented May 15, 2024

This may need to be adjusted for VB as well.

(Guessing): The relevant code is:

var parameters = ContainingSymbol.GetParameters();

Instead of using ContainingSymbol, This probably should be (ContainingSymbol as MethodSymbol)?.PartialDefinitionPart ?? ContainingSymbol.

Edit: Reading the code and thinking again, I think ContainingSymbol should actually have been the definition part. Needs a bit of debugging to see what ContainingSymbol is in this code path.

@jaredpar
Copy link
Member

Agree the declaration name makes more sense but this is now 15 year old behavior. Changing it is very risky. Think this should be documented but not changed.

@Youssef1313
Copy link
Member

@jaredpar That is 3 or 4 years only I think?

@jaredpar
Copy link
Member

Partial members go back to C# 2.0. I cannot recall definitively if we allowed call info in partial but believe we did.

@Youssef1313
Copy link
Member

This seems very specific to caller argument expression and not caller info attributes in general. Am I missing some scenario where a similar issue could be noticed with other caller infos?

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

No branches or pull requests

3 participants