-
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
Add missing test for CallerArgumentExpression #57805
Changes from 3 commits
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 |
---|---|---|
|
@@ -5752,5 +5752,71 @@ static void Main() | |
ABC | ||
"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("out")] | ||
[InlineData("ref")] | ||
[InlineData("in")] | ||
public void CallerArgumentExpression_OnRefParameter01(string refType) | ||
{ | ||
var comp = CreateCompilation(@$" | ||
using System.Runtime.CompilerServices; | ||
#pragma warning disable CS8321 | ||
|
||
void M(int i, [CallerArgumentExpression(""i"")] {refType} string s) | ||
{{ | ||
{(refType == "out" ? "s = null;" : "")} | ||
}} | ||
", targetFramework: TargetFramework.NetCoreApp); | ||
|
||
comp.VerifyDiagnostics( | ||
// (5,16): error CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values | ||
// void M(int i, [CallerArgumentExpression("i")] ref string s) | ||
Diagnostic(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, "CallerArgumentExpression").WithLocation(5, 16) | ||
); | ||
} | ||
|
||
[Theory] | ||
[InlineData("out")] | ||
[InlineData("ref")] | ||
public void CallerArgumentExpression_OnRefParameter02(string refType) | ||
{ | ||
var comp = CreateCompilation(@$" | ||
using System.Runtime.CompilerServices; | ||
#pragma warning disable CS8321 | ||
|
||
void M(int i, [CallerArgumentExpression(""i"")] {refType} string s = null) | ||
{{ | ||
{(refType == "out" ? "s = null;" : "")} | ||
}} | ||
", targetFramework: TargetFramework.NetCoreApp); | ||
|
||
comp.VerifyDiagnostics( | ||
// (5,16): error CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values | ||
// void M(int i, [CallerArgumentExpression("i")] out string s = null) | ||
Diagnostic(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, "CallerArgumentExpression").WithLocation(5, 16), | ||
// (5,47): error CS1741: A ref or out parameter cannot have a default value | ||
// void M(int i, [CallerArgumentExpression("i")] out string s = null) | ||
Diagnostic(ErrorCode.ERR_RefOutDefaultValue, "out").WithLocation(5, 47) | ||
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. |
||
); | ||
} | ||
|
||
[ConditionalFact(typeof(CoreClrOnly))] | ||
public void CallerArgumentExpression_OnRefParameter03() | ||
{ | ||
var comp = CreateCompilation(@" | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
M(1 + 1); | ||
|
||
void M(int i, [CallerArgumentExpression(""i"")] in string s = ""default value"") | ||
{ | ||
Console.WriteLine(s); | ||
} | ||
", targetFramework: TargetFramework.NetCoreApp); | ||
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. 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. No, it will still compile against Netstandard2.0 APIs, which doesn't have |
||
|
||
CompileAndVerify(comp, expectedOutput: "1 + 1").VerifyDiagnostics(); | ||
} | ||
} | ||
} |
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 feels like we also need to test scenario that satisfies this requirement, even if that would be an error on its own. #Resolved