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

Add missing test for CallerArgumentExpression #57805

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 17, 2021

Choose a reason for hiding this comment

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

may only be applied to parameters with default values

It feels like we also need to test scenario that satisfies this requirement, even if that would be an error on its own. #Resolved

// 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, refType).WithLocation(5, 47)
);
}

[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);
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 17, 2021

Choose a reason for hiding this comment

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

targetFramework: TargetFramework.NetCoreApp

I guess default target framework would work just fine since the test is for CoreClrOnly. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

The 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 CallerArgumentExpression.


CompileAndVerify(comp, expectedOutput: "1 + 1").VerifyDiagnostics();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,42 @@ End Module
CompileAndVerify(compilation, expectedOutput:="<default>
value").VerifyDiagnostics()
End Sub

<ConditionalFact(GetType(CoreClrOnly))>
Public Sub TestCallerArgumentExpression_OnByRefParameter01()
Dim source As String = "
Imports System.Runtime.CompilerServices
Module Program
Private Const p As String = NameOf(p)
Sub Log(p As Integer, <CallerArgumentExpression(p)> ByRef arg As String)
End Sub
End Module
"

Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseDll, parseOptions:=TestOptions.RegularLatest)
compilation.AssertTheseDiagnostics()
End Sub

<ConditionalFact(GetType(CoreClrOnly))>
Public Sub TestCallerArgumentExpression_OnByRefParameter02()
Dim source As String = "
Imports System
Imports System.Runtime.CompilerServices
Module Program
Sub Main()
Log(1 + 1)
End Sub

Private Const p As String = NameOf(p)
Sub Log(p As Integer, <CallerArgumentExpression(p)> Optional ByRef arg As String = ""<default-value>"")
Console.WriteLine(arg)
End Sub
End Module
"

Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest)
CompileAndVerify(compilation, expectedOutput:="1 + 1").VerifyDiagnostics()
End Sub
#End Region

#Region "CallerArgumentExpression - Attributes"
Expand Down