diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs index 1b9f37eba2219..f9f3b86e81005 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs @@ -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, 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); + + CompileAndVerify(comp, expectedOutput: "1 + 1").VerifyDiagnostics(); + } } } diff --git a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb index fab0fa3d959e4..dd7bf971417da 100644 --- a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb +++ b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb @@ -919,6 +919,42 @@ End Module CompileAndVerify(compilation, expectedOutput:=" value").VerifyDiagnostics() End Sub + + + 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, 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 + + + 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, Optional ByRef arg As String = """") + 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"