-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Add support for collection expressions in attributes #69968
Conversation
var attr = (XAttribute)System.Attribute.GetCustomAttribute(typeof(C), typeof(XAttribute)); | ||
Console.Write(attr._values.Length); | ||
|
||
[X([])] |
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.
test with a string in the literal, and 'null' in the literal? #Resolved
|
||
CreateCompilation(source).VerifyEmitDiagnostics( | ||
// (1,14): error CS9176: There is no target type for the collection expression. | ||
// [X([1, 2, .. [3]])] |
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.
definitely an odd error :) #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.
I agree. It's an existing problem though and I'm not planning to address in this PR.
public class XAttribute : System.Attribute | ||
{ | ||
public int[] _values; | ||
public XAttribute(int[] values) { _values = values; } |
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.
test with param int[]
? #Resolved
// [X([1])] | ||
Diagnostic(ErrorCode.ERR_BadAttributeParamType, "X").WithArguments("a", "A").WithLocation(12, 2) | ||
); | ||
} |
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.
Good catch. Thanks
// [X([[1], [2]])] | ||
Diagnostic(ErrorCode.ERR_BadAttributeParamType, "X").WithArguments("values", "int[][]").WithLocation(1, 2) | ||
); | ||
} |
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.
@jcouv make sure to retarget this to |
} | ||
"""; | ||
|
||
CreateCompilation(source, targetFramework: TargetFramework.Net70).VerifyEmitDiagnostics( |
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.
string source = """ | ||
var attr = (XAttribute)System.Attribute.GetCustomAttribute(typeof(C), typeof(XAttribute)); | ||
var inner = (int[])attr._values[0]; | ||
System.Console.Write($"OuterLength={attr._values.Length} InnerValue={inner[0]} InnerLength={inner.Length}"); |
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.
Consider using attr._values.Report();
in this test and below. #Resolved
I haven't checked, but are there tests for using a collection-expr as an argument to an attribute 'named arg' and an attribute 'named field/prop'? #Resolved |
Thanks, that's a great suggestion. Not sure how I missed that. Will add some tests #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.
Done review pass
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 the additional tests. I do think we likely have a cycle here, but it's not introduced by this PR, probably pre-existing.
Woohoo! Thanks for getting this in! |
Fixes #69133