From 5ae48be36e31e6f84c5e9e1f7c59a59dcacb3c53 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 15 Jul 2021 11:57:03 +0200 Subject: [PATCH 01/11] Get CallerArgumentExpression ready --- .../AttributeTests_CallerInfoAttributes.cs | 50 +++++++++++++++++++ .../VisualBasic/Portable/LanguageVersion.vb | 1 - .../Portable/Parser/ParserFeature.vb | 1 - 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs index 0019a7f9d4bc5..c57e826e9886c 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs @@ -896,6 +896,56 @@ .maxstack 8 CompileAndVerify(compilation, expectedOutput: @" value").VerifyDiagnostics(); } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestInterpolatedStringHandler() + { + string source = @" +using System; +using System.Runtime.CompilerServices; +using System.Text; + +M(1 + /**/ 1, $""""); // Should print ""1 + 1"" + +void M(object o, [InterpolatedStringHandlerArgument(""o"")] CustomHandler c) => Console.WriteLine(c.ToString()); + +[InterpolatedStringHandler] +public ref struct CustomHandler +{ + private readonly StringBuilder _builder; + public CustomHandler(int literalLength, int formattedCount, object o, [CallerArgumentExpression(""o"")] string s = """") + { + _builder = new StringBuilder(); + _builder.Append(s); + } + public void AppendLiteral(string s) => _builder.AppendLine(s.ToString()); + public void AppendFormatted(object o) => _builder.AppendLine(""value:"" + o.ToString()); + public override string ToString() => _builder.ToString(); +} + +namespace System.Runtime.CompilerServices +{ + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] + public sealed class InterpolatedStringHandlerArgumentAttribute : Attribute + { + public InterpolatedStringHandlerArgumentAttribute(string argument) => Arguments = new string[] { argument }; + public InterpolatedStringHandlerArgumentAttribute(params string[] arguments) => Arguments = arguments; + public string[] Arguments { get; } + } + + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)] + public sealed class InterpolatedStringHandlerAttribute : Attribute + { + public InterpolatedStringHandlerAttribute() + { + } + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + CompileAndVerify(compilation, expectedOutput: "1 + /**/ 1").VerifyDiagnostics(); + } #endregion #region CallerArgumentExpression - Attribute constructor diff --git a/src/Compilers/VisualBasic/Portable/LanguageVersion.vb b/src/Compilers/VisualBasic/Portable/LanguageVersion.vb index 806fb59903df4..e73993f8eaec3 100644 --- a/src/Compilers/VisualBasic/Portable/LanguageVersion.vb +++ b/src/Compilers/VisualBasic/Portable/LanguageVersion.vb @@ -21,7 +21,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic VisualBasic15_5 = 1505 VisualBasic16 = 1600 VisualBasic16_9 = 1609 - ' PROTOTYPE(caller-expr): Map to language version matching whatever VS version the feature will be shipped in. VisualBasic17 = 1700 Latest = Integer.MaxValue diff --git a/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb b/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb index daf2b35628e7a..79db3a6a53f60 100644 --- a/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb +++ b/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb @@ -108,7 +108,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return LanguageVersion.VisualBasic16_9 Case Feature.CallerArgumentExpression - ' PROTOTYPE(caller-expr): Map to language version matching whatever VS version the feature will be shipped in. Return LanguageVersion.VisualBasic17 Case Else From baebf8240af3e376f145592deba25f625ef9eb19 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 15 Jul 2021 12:00:24 +0200 Subject: [PATCH 02/11] Compact error messages --- .../CSharp/Portable/Errors/ErrorCode.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index a5dddc573267c..52e1288c51118 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -1981,16 +1981,14 @@ internal enum ErrorCode ERR_FileScopedAndNormalNamespace = 8955, ERR_FileScopedNamespaceNotBeforeAllMembers = 8956, ERR_NoImplicitConvTargetTypedConditional = 8957, - - // PROTOTYPE: Compact these before merge - WRN_CallerArgumentExpressionParamForUnconsumedLocation = 9000, - ERR_NoConversionForCallerArgumentExpressionParam = 9001, - WRN_CallerLineNumberPreferredOverCallerArgumentExpression = 9002, - WRN_CallerFilePathPreferredOverCallerArgumentExpression = 9003, - WRN_CallerMemberNamePreferredOverCallerArgumentExpression = 9004, - WRN_CallerArgumentExpressionAttributeHasInvalidParameterName = 9005, - ERR_BadCallerArgumentExpressionParamWithoutDefaultValue = 9006, - WRN_CallerArgumentExpressionAttributeSelfReferential = 9007, + WRN_CallerArgumentExpressionParamForUnconsumedLocation = 8958, + ERR_NoConversionForCallerArgumentExpressionParam = 8959, + WRN_CallerLineNumberPreferredOverCallerArgumentExpression = 8960, + WRN_CallerFilePathPreferredOverCallerArgumentExpression = 8961, + WRN_CallerMemberNamePreferredOverCallerArgumentExpression = 8962, + WRN_CallerArgumentExpressionAttributeHasInvalidParameterName = 8963, + ERR_BadCallerArgumentExpressionParamWithoutDefaultValue = 8964, + WRN_CallerArgumentExpressionAttributeSelfReferential = 8965, #endregion From db433641270916076a0b235c41db21da37860fdd Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 15 Jul 2021 12:05:15 +0200 Subject: [PATCH 03/11] Small fixes --- .../Source/SourceComplexParameterSymbol.cs | 16 ++-- .../AttributeTests_CallerInfoAttributes.cs | 90 +++++++++---------- 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceComplexParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceComplexParameterSymbol.cs index 9a7a016efc932..f9c34c5b0922c 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceComplexParameterSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceComplexParameterSymbol.cs @@ -1071,13 +1071,13 @@ private void ValidateCallerArgumentExpressionAttribute(AttributeSyntax node, CSh if (!IsValidCallerInfoContext(node)) { - // CS9000: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect because it applies to a + // CS8958: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect because it applies to a // member that is used in contexts that do not allow optional arguments diagnostics.Add(ErrorCode.WRN_CallerArgumentExpressionParamForUnconsumedLocation, node.Name.Location, CSharpSyntaxNode.Identifier.ValueText); } else if (!compilation.Conversions.HasCallerInfoStringConversion(TypeWithAnnotations.Type, ref useSiteInfo)) { - // CS9001: CallerArgumentExpressionAttribute cannot be applied because there are no standard conversions from type '{0}' to type '{1}' + // CS8959: CallerArgumentExpressionAttribute cannot be applied because there are no standard conversions from type '{0}' to type '{1}' TypeSymbol stringType = compilation.GetSpecialType(SpecialType.System_String); diagnostics.Add(ErrorCode.ERR_NoConversionForCallerArgumentExpressionParam, node.Name.Location, stringType, TypeWithAnnotations.Type); } @@ -1085,33 +1085,33 @@ private void ValidateCallerArgumentExpressionAttribute(AttributeSyntax node, CSh { // Unconsumed location checks happen first, so we require a default value. - // CS9006: The CallerArgumentExpressionAttribute may only be applied to parameters with default values + // CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values diagnostics.Add(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, node.Name.Location); } else if (IsCallerLineNumber) { - // CS9002: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is overridden by the CallerLineNumberAttribute. + // CS8960: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is overridden by the CallerLineNumberAttribute. diagnostics.Add(ErrorCode.WRN_CallerLineNumberPreferredOverCallerArgumentExpression, node.Name.Location, CSharpSyntaxNode.Identifier.ValueText); } else if (IsCallerFilePath) { - // CS9003: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is overridden by the CallerFilePathAttribute. + // CS8961: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is overridden by the CallerFilePathAttribute. diagnostics.Add(ErrorCode.WRN_CallerFilePathPreferredOverCallerArgumentExpression, node.Name.Location, CSharpSyntaxNode.Identifier.ValueText); } else if (IsCallerMemberName) { - // CS9004: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is overriden by the CallerMemberNameAttribute. + // CS8962: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is overriden by the CallerMemberNameAttribute. diagnostics.Add(ErrorCode.WRN_CallerMemberNamePreferredOverCallerArgumentExpression, node.Name.Location, CSharpSyntaxNode.Identifier.ValueText); } else if (attribute.CommonConstructorArguments.Length == 1 && GetEarlyDecodedWellKnownAttributeData()?.CallerArgumentExpressionParameterIndex == -1) { - // CS9005: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is applied with an invalid parameter name. + // CS8963: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect. It is applied with an invalid parameter name. diagnostics.Add(ErrorCode.WRN_CallerArgumentExpressionAttributeHasInvalidParameterName, node.Name.Location, CSharpSyntaxNode.Identifier.ValueText); } else if (GetEarlyDecodedWellKnownAttributeData()?.CallerArgumentExpressionParameterIndex == Ordinal) { - // CS9006: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect because it's self-referential. + // CS8965: The CallerArgumentExpressionAttribute applied to parameter '{0}' will have no effect because it's self-referential. diagnostics.Add(ErrorCode.WRN_CallerArgumentExpressionAttributeSelfReferential, node.Name.Location, CSharpSyntaxNode.Identifier.ValueText); } diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs index c57e826e9886c..4952626cec487 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs @@ -64,7 +64,7 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); // Begin/EndInvoke are not currently supported. CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Program.Main", @" { @@ -130,7 +130,7 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); // Begin/EndInvoke are not currently supported. CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Program.Main", @" { @@ -195,9 +195,9 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); compilation.VerifyDiagnostics( - // (29,22): error CS9006: The CallerArgumentExpressionAttribute may only be applied to parameters with default values + // (29,22): error CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values // delegate void D([CallerArgumentExpression(s5)] [Optional] [DefaultParameterValue("default")] ref string s1, string s2, string s3, string s4, string s5); Diagnostic(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, "CallerArgumentExpression").WithLocation(29, 22), // (38,11): error CS7036: There is no argument given that corresponds to the required formal parameter 's1' of 'Program.D.EndInvoke(ref string, IAsyncResult)' @@ -251,9 +251,9 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); compilation.VerifyDiagnostics( - // (28,33): error CS9006: The CallerArgumentExpressionAttribute may only be applied to parameters with default values + // (28,33): error CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values // delegate void D(string s1, [CallerArgumentExpression(s1)] ref string s2 = "default"); Diagnostic(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, "CallerArgumentExpression").WithLocation(28, 33), // (28,63): error CS1741: A ref or out parameter cannot have a default value @@ -310,9 +310,9 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); compilation.VerifyDiagnostics( - // (29,33): error CS9006: The CallerArgumentExpressionAttribute may only be applied to parameters with default values + // (29,33): error CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values // delegate void D(string s1, [CallerArgumentExpression(s1)] [Optional] [DefaultParameterValue("default")] ref string s2); Diagnostic(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, "CallerArgumentExpression").WithLocation(29, 33), // (39,11): error CS7036: There is no argument given that corresponds to the required formal parameter 's2' of 'Program.D.EndInvoke(ref string, IAsyncResult)' @@ -367,9 +367,9 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation).VerifyDiagnostics( - // (29,33): warning CS9005: The CallerArgumentExpressionAttribute applied to parameter 's2' will have no effect. It is applied with an invalid parameter name. + // (29,33): warning CS8963: The CallerArgumentExpressionAttribute applied to parameter 's2' will have no effect. It is applied with an invalid parameter name. // delegate void D(string s1, [CallerArgumentExpression(callback)] [Optional] [DefaultParameterValue("default")] string s2); Diagnostic(ErrorCode.WRN_CallerArgumentExpressionAttributeHasInvalidParameterName, "CallerArgumentExpression").WithArguments("s2").WithLocation(29, 33) ).VerifyIL("Program.Main", @" @@ -412,7 +412,7 @@ static void Log(int p, [CallerArgumentExpression(p)] string arg = """).VerifyDiagnostics(); } @@ -518,7 +518,7 @@ static void Log(int p, [CallerArgumentExpression(p)] string arg = "" value").VerifyDiagnostics( - // (11,10): warning CS9007: The CallerArgumentExpressionAttribute applied to parameter 'p' will have no effect because it's self-referential. + // (11,10): warning CS8965: The CallerArgumentExpressionAttribute applied to parameter 'p' will have no effect because it's self-referential. // [CallerArgumentExpression("p")] string p = "") Diagnostic(ErrorCode.WRN_CallerArgumentExpressionAttributeSelfReferential, "CallerArgumentExpression").WithArguments("p").WithLocation(11, 10) ); @@ -977,7 +977,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "123").VerifyDiagnostics(); } @@ -1011,7 +1011,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"123 /* comment */ + 5").VerifyDiagnostics(); @@ -1044,7 +1044,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "124, 123, 124").VerifyDiagnostics(); } @@ -1081,7 +1081,7 @@ static void Main() } "; - var compilation = CreateCompilation(source2, references: new[] { ref1 }, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source2, references: new[] { ref1 }, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "2 + 2").VerifyDiagnostics(); } @@ -1187,7 +1187,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"target default value arg default value @@ -1229,7 +1229,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"target default value arg default value @@ -1275,7 +1275,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"param1: param1_default, param2: param2_default param1: param1_value, param2: ""param1_value"" @@ -1351,7 +1351,7 @@ static void Main() var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular9); CompileAndVerify(compilation, expectedOutput: @" value").VerifyDiagnostics( - // (10,10): warning CS9007: The CallerArgumentExpressionAttribute applied to parameter 'p' will have no effect because it's self-referential. + // (10,10): warning CS8965: The CallerArgumentExpressionAttribute applied to parameter 'p' will have no effect because it's self-referential. // [CallerArgumentExpression("p")] string p = "") Diagnostic(ErrorCode.WRN_CallerArgumentExpressionAttributeSelfReferential, "CallerArgumentExpression").WithArguments("p").WithLocation(10, 10) ); @@ -1440,7 +1440,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "'Hello', '\"Hello\"'").VerifyDiagnostics(); var namedType = compilation.GetTypeByMetadataName("Program").GetPublicSymbol(); var attributeArguments = namedType.GetAttributes().Single().ConstructorArguments; @@ -1472,7 +1472,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "'Hello', '\"Hello\"'").VerifyDiagnostics(); var namedType = compilation.GetTypeByMetadataName("Program").GetPublicSymbol(); var attributeArguments = namedType.GetAttributes().Single().ConstructorArguments; @@ -1504,7 +1504,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "'Hello', 'World', '\"Hello\"'").VerifyDiagnostics(); var namedType = compilation.GetTypeByMetadataName("Program").GetPublicSymbol(); var attributeArguments = namedType.GetAttributes().Single().ConstructorArguments; @@ -1537,7 +1537,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "'Hello', 'World', '\"Hello\"'").VerifyDiagnostics(); var namedType = compilation.GetTypeByMetadataName("Program").GetPublicSymbol(); var attributeArguments = namedType.GetAttributes().Single().ConstructorArguments; @@ -1576,7 +1576,7 @@ static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"param1: param1_default, param2: param2_default param1: param1_default, param2: param2_default @@ -1620,7 +1620,7 @@ static void Main() _ = new MyAttribute(0+0, c: 1+1); } }"; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"'0', '2', '2', '0+0', '', '1+1' '0', '2', '2', '0+0', '', '1+1'").VerifyDiagnostics(); } @@ -1652,7 +1652,7 @@ static void Main() typeof(Program).GetCustomAttribute(typeof(MyAttribute)); } }"; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "'', ''").VerifyDiagnostics(); } @@ -1711,7 +1711,7 @@ static void Main() typeof(Program).GetCustomAttribute(typeof(MyAttribute)); } }"; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"'3', '1+2'").VerifyDiagnostics(); } @@ -1739,7 +1739,7 @@ static void Main() typeof(Program).GetCustomAttribute(typeof(MyAttribute)); } }"; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); compilation.VerifyDiagnostics( // (8,36): error CS8913: CallerArgumentExpressionAttribute cannot be applied because there are no standard conversions from type 'string' to type 'int' // public MyAttribute(int a = 1, [CallerArgumentExpression("a")] int expr_a = 0) @@ -1799,7 +1799,7 @@ static void Main() typeof(Program).GetCustomAttribute(typeof(MyAttribute)); } }"; - var compilation = CreateCompilationWithILAndMscorlib40(source, il, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilationWithILAndMscorlib40(source, il, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: "'3', '0'").VerifyDiagnostics(); var arguments = compilation.GetTypeByMetadataName("Program").GetAttributes().Single().CommonConstructorArguments; Assert.Equal(2, arguments.Length); @@ -1833,7 +1833,7 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"2, 1+ 1 4, explicit-value").VerifyDiagnostics(); } @@ -1869,7 +1869,7 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"s1: s1-arg s2: s2-arg s3: @@ -1907,7 +1907,7 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation, expectedOutput: @"s1: s1-arg s2: s2-arg s3: @@ -1946,7 +1946,7 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Program.Main", @" { // Code size 29 (0x1d) @@ -1997,7 +1997,7 @@ public static void Main() } "; - var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview); + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Program.Main", @" { // Code size 29 (0x1d) From 5ebabce4352db0b6b46d1683b6997bd01b36946a Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Tue, 20 Jul 2021 22:42:58 +0200 Subject: [PATCH 04/11] Add more tests. TODO: remaining tests --- .../AttributeTests_CallerInfoAttributes.cs | 387 ++++++++++++++++++ ...AttributeTests_CallerArgumentExpression.vb | 274 +++++++++++++ 2 files changed, 661 insertions(+) diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs index 4952626cec487..9367683a2dfa5 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs @@ -690,6 +690,393 @@ static void Log(int p, [CallerArgumentExpression(p)] [CallerMemberName] string a ); } + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWithMemberNameAttributes2() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] [CallerMemberName] string arg = """") + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + CompileAndVerify(compilation, expectedOutput: "Main").VerifyDiagnostics( + // (12,29): warning CS8917: The CallerArgumentExpressionAttribute applied to parameter 'arg' will have no effect. It is overriden by the MemberNameAttribute. + // static void Log(int p, [CallerArgumentExpression(p)] [CallerMemberName] string arg = "") + Diagnostic(ErrorCode.WRN_CallerMemberNamePreferredOverCallerArgumentExpression, "CallerArgumentExpression").WithArguments("arg").WithLocation(12, 29) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWitLineNumberAttributes() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] string arg = """") + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular9); + compilation.VerifyDiagnostics( + // (9,9): error CS0029: Cannot implicitly convert type 'int' to 'string' + // Log(default(int)); + Diagnostic(ErrorCode.ERR_NoImplicitConv, "Log(default(int))").WithArguments("int", "string").WithLocation(9, 9), + // (12,29): warning CS8960: The CallerArgumentExpressionAttribute applied to parameter 'arg' will have no effect. It is overridden by the CallerLineNumberAttribute. + // static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] string arg = "") + Diagnostic(ErrorCode.WRN_CallerLineNumberPreferredOverCallerArgumentExpression, "CallerArgumentExpression").WithArguments("arg").WithLocation(12, 29), + // (12,59): error CS4017: CallerLineNumberAttribute cannot be applied because there are no standard conversions from type 'int' to type 'string' + // static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] string arg = "") + Diagnostic(ErrorCode.ERR_NoConversionForCallerLineNumberParam, "CallerLineNumber").WithArguments("int", "string").WithLocation(12, 59) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWithLineNumberAttributes2() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] string arg = """") + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + compilation.VerifyDiagnostics( + // (9,9): error CS0029: Cannot implicitly convert type 'int' to 'string' + // Log(default(int)); + Diagnostic(ErrorCode.ERR_NoImplicitConv, "Log(default(int))").WithArguments("int", "string").WithLocation(9, 9), + // (12,29): warning CS8960: The CallerArgumentExpressionAttribute applied to parameter 'arg' will have no effect. It is overridden by the CallerLineNumberAttribute. + // static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] string arg = "") + Diagnostic(ErrorCode.WRN_CallerLineNumberPreferredOverCallerArgumentExpression, "CallerArgumentExpression").WithArguments("arg").WithLocation(12, 29), + // (12,59): error CS4017: CallerLineNumberAttribute cannot be applied because there are no standard conversions from type 'int' to type 'string' + // static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] string arg = "") + Diagnostic(ErrorCode.ERR_NoConversionForCallerLineNumberParam, "CallerLineNumber").WithArguments("int", "string").WithLocation(12, 59) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWitLineNumberAttributes3() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] int arg = 0) + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular9); + compilation.VerifyDiagnostics( + // (12,29): error CS8959: CallerArgumentExpressionAttribute cannot be applied because there are no standard conversions from type 'string' to type 'int' + // static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] int arg = 0) + Diagnostic(ErrorCode.ERR_NoConversionForCallerArgumentExpressionParam, "CallerArgumentExpression").WithArguments("string", "int").WithLocation(12, 29) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWithLineNumberAttributes4() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] int arg = 0) + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + compilation.VerifyDiagnostics( + // (12,29): error CS8959: CallerArgumentExpressionAttribute cannot be applied because there are no standard conversions from type 'string' to type 'int' + // static void Log(int p, [CallerArgumentExpression(p)] [CallerLineNumber] int arg = 0) + Diagnostic(ErrorCode.ERR_NoConversionForCallerArgumentExpressionParam, "CallerArgumentExpression").WithArguments("string", "int").WithLocation(12, 29) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentNonOptionalParameter() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] string arg) + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + compilation.VerifyDiagnostics( + // (9,9): error CS7036: There is no argument given that corresponds to the required formal parameter 'arg' of 'Program.Log(int, string)' + // Log(default(int)); + Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "Log").WithArguments("arg", "Program.Log(int, string)").WithLocation(9, 9), + // (12,29): error CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values + // static void Log(int p, [CallerArgumentExpression(p)] string arg) + Diagnostic(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, "CallerArgumentExpression").WithLocation(12, 29) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentNonOptionalParameter2() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] string arg) + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular9); + compilation.VerifyDiagnostics( + // (9,9): error CS7036: There is no argument given that corresponds to the required formal parameter 'arg' of 'Program.Log(int, string)' + // Log(default(int)); + Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "Log").WithArguments("arg", "Program.Log(int, string)").WithLocation(9, 9), + // (12,29): error CS8964: The CallerArgumentExpressionAttribute may only be applied to parameters with default values + // static void Log(int p, [CallerArgumentExpression(p)] string arg) + Diagnostic(ErrorCode.ERR_BadCallerArgumentExpressionParamWithoutDefaultValue, "CallerArgumentExpression").WithLocation(12, 29) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWithOverride() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +abstract class Base +{ + const string p = nameof(p); + public abstract void Log_RemoveAttributeInOverride(int p, [CallerArgumentExpression(p)] string arg =""default""); + public abstract void Log_AddAttributeInOverride(int p, string arg =""default""); +} + +class Derived : Base +{ + const string p = nameof(p); + public override void Log_AddAttributeInOverride(int p, [CallerArgumentExpression(p)] string arg = ""default"") + => Console.WriteLine(arg); + + public override void Log_RemoveAttributeInOverride(int p, string arg = ""default"") + => Console.WriteLine(arg); +} + +class Program +{ + public static void Main() + { + var derived = new Derived(); + derived.Log_AddAttributeInOverride(5 + 4); + derived.Log_RemoveAttributeInOverride(5 + 5); + + ((Base)derived).Log_AddAttributeInOverride(5 + 4); + ((Base)derived).Log_RemoveAttributeInOverride(5 + 5); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + CompileAndVerify(compilation, expectedOutput: @"5 + 4 +default +default +5 + 5").VerifyDiagnostics(); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWithUserDefinedConversionFromString() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class C +{ + public C(string s) => S = s; + public string S { get; } + public static implicit operator C(string s) => new C(s); +} + +class Program +{ + public static void Main() + { + Log(default(int)); + } + const string p = nameof(p); + static void Log(int p, [CallerArgumentExpression(p)] C arg = null) + { + Console.WriteLine(arg.S); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + compilation.VerifyDiagnostics( + // (19,29): error CS8959: CallerArgumentExpressionAttribute cannot be applied because there are no standard conversions from type 'string' to type 'C' + // static void Log(int p, [CallerArgumentExpression(p)] C arg = null) + Diagnostic(ErrorCode.ERR_NoConversionForCallerArgumentExpressionParam, "CallerArgumentExpression").WithArguments("string", "C").WithLocation(19, 29) + ); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWithExtensionGetEnumerator() + { + string source = @" +using System; +using System.Collections; +using System.Runtime.CompilerServices; + +public static class Extensions +{ + public static IEnumerator GetEnumerator(this IEnumerator enumerator, [CallerArgumentExpression(""enumerator"")] string s = ""default"") + { + Console.WriteLine(s); + return enumerator; + } +} + +class Program +{ + static void Main() + { + var x = new [] { """", """" }.GetEnumerator(); + + foreach (var y in x) + { + } + } +} + +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + CompileAndVerify(compilation, expectedOutput: "x").VerifyDiagnostics(); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestCallerArgumentWithExtensionDeconstruct() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +public static class Extensions +{ + public static void Deconstruct(this Person p, out string firstName, out string lastName, [CallerArgumentExpression(""firstName"")] string s = ""default"") + { + firstName = p.FirstName; + lastName = p.LastName; + Console.WriteLine(s); + } +} + +public class Person +{ + public Person(string firstName, string lastName) + => (FirstName, LastName) = (firstName, lastName); + + public string FirstName { get; } + public string LastName { get; } +} + +class Program +{ + static void Main() + { + var p = new Person(""myFirstName"", ""myLastName""); + var (first, last) = p; + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + compilation.VerifyDiagnostics( + // (30,14): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'first'. + // var (first, last) = p; + Diagnostic(ErrorCode.ERR_TypeInferenceFailedForImplicitlyTypedDeconstructionVariable, "first").WithArguments("first").WithLocation(30, 14), + // (30,21): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'last'. + // var (first, last) = p; + Diagnostic(ErrorCode.ERR_TypeInferenceFailedForImplicitlyTypedDeconstructionVariable, "last").WithArguments("last").WithLocation(30, 21), + // (30,29): error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'Person', with 2 out parameters and a void return type. + // var (first, last) = p; + Diagnostic(ErrorCode.ERR_MissingDeconstruct, "p").WithArguments("Person", "2").WithLocation(30, 29) + ); + } + [ConditionalFact(typeof(CoreClrOnly))] public void TestCallerArgumentExpressionWithOptionalTargetParameter() { diff --git a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb index c8f11f5f2ac92..9b4ea21eeafc5 100644 --- a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb +++ b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb @@ -461,6 +461,280 @@ End Module CompileAndVerify(compilation, expectedOutput:="Main").VerifyDiagnostics() End Sub + + Public Sub TestCallerArgumentWithMemberNameAttributes2() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, Optional 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:="Main").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentWithFilePathAttributes() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, Optional arg As String = """") + Console.WriteLine(arg) + End Sub +End Module +" + Dim compilation = CreateCompilation(Parse(source, "C:\\Program.cs", options:=TestOptions.Regular16_9), targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe) + CompileAndVerify(compilation, expectedOutput:="C:\\Program.cs").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentWithFilePathAttributes2() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, Optional arg As String = """") + Console.WriteLine(arg) + End Sub +End Module +" + Dim compilation = CreateCompilation(Parse(source, "C:\\Program.cs", options:=TestOptions.RegularLatest), targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe) + CompileAndVerify(compilation, expectedOutput:="C:\\Program.cs").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentWithLineNumberAttributes() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, Optional 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.Regular16_9) + CompileAndVerify(compilation, expectedOutput:="6").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentWithLineNumberAttributes2() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, Optional 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:="6").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentWithLineNumberAttributes3() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, Optional arg As Integer = 0) + Console.WriteLine(arg) + End Sub +End Module +" + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.Regular16_9) + CompileAndVerify(compilation, expectedOutput:="6").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentWithLineNumberAttributes4() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, Optional arg As Integer = 0) + 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:="6").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentNonOptionalParameter() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, 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) + compilation.AssertTheseDiagnostics( +) + End Sub + + + Public Sub TestCallerArgumentNonOptionalParameter2() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(0+ 0) + End Sub + + Private Const p As String = NameOf(p) + + Sub Log(p As Integer, 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.Regular16_9) + compilation.AssertTheseDiagnostics( +) + End Sub + + + Public Sub TestCallerArgumentWithOverride() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices + +MustInherit Class Base + Const p As String = NameOf(p) + Public MustOverride Sub Log_RemoveAttributeInOverride(p As Integer, Optional arg As String = ""default"") + Public MustOverride Sub Log_AddAttributeInOverride(p As Integer, Optional arg As String = ""default"") +End Class + +Class Derived : Inherits Base + + Const p As String = NameOf(p) + Public Overrides Sub Log_AddAttributeInOverride(p As Integer, Optional arg As String = ""default"") + Console.WriteLine(arg) + End Sub + + Public Overrides Sub Log_RemoveAttributeInOverride(p As Integer, Optional arg As String = ""default"") + Console.WriteLine(arg) + End Sub +End Class + +Class Program + Public Shared Sub Main() + Dim derived = New Derived() + derived.Log_AddAttributeInOverride(5 + 4) + derived.Log_RemoveAttributeInOverride(5 + 5) + + DirectCast(derived, Base).Log_AddAttributeInOverride(5 + 4) + DirectCast(derived, Base).Log_RemoveAttributeInOverride(5 + 5) + End Sub +End Class +" + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + CompileAndVerify(compilation, expectedOutput:="5 + 4 +default +default +5 + 5").VerifyDiagnostics() + End Sub + + + Public Sub TestCallerArgumentWithUserDefinedConversionFromString() + Dim source = " +Imports System +Imports System.Runtime.CompilerServices + +Class C + Public Sub New(s As String) + Prop = s + End Sub + + Public ReadOnly Property Prop As String + + Public Shared Widening Operator CType(s As String) As C + Return New C(s) + End Operator + +End Class + +Class Program + Public Shared Sub Main() + Log(0) + End Sub + Const p As String = NameOf(p) + Shared Sub Log(p As Integer, Optional arg As C = Nothing) + Console.WriteLine(arg Is Nothing) + End Sub +End Class +" + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + CompileAndVerify(compilation, expectedOutput:="True").VerifyDiagnostics() + End Sub + Public Sub TestCallerArgumentExpressionWithOptionalTargetParameter() Dim source = " From b28c9ed8443023d3c3c7ff9ee66af260d9eb0767 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 21 Jul 2021 13:44:59 +0200 Subject: [PATCH 05/11] Fix test --- .../AttributeTests_CallerInfoAttributes.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs index 9367683a2dfa5..45ba06269a136 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs @@ -1065,15 +1065,15 @@ static void Main() var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); compilation.VerifyDiagnostics( - // (30,14): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'first'. + // (29,14): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'first'. // var (first, last) = p; - Diagnostic(ErrorCode.ERR_TypeInferenceFailedForImplicitlyTypedDeconstructionVariable, "first").WithArguments("first").WithLocation(30, 14), - // (30,21): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'last'. + Diagnostic(ErrorCode.ERR_TypeInferenceFailedForImplicitlyTypedDeconstructionVariable, "first").WithArguments("first").WithLocation(29, 14), + // (29,21): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'last'. // var (first, last) = p; - Diagnostic(ErrorCode.ERR_TypeInferenceFailedForImplicitlyTypedDeconstructionVariable, "last").WithArguments("last").WithLocation(30, 21), - // (30,29): error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'Person', with 2 out parameters and a void return type. + Diagnostic(ErrorCode.ERR_TypeInferenceFailedForImplicitlyTypedDeconstructionVariable, "last").WithArguments("last").WithLocation(29, 21), + // (29,29): error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'Person', with 2 out parameters and a void return type. // var (first, last) = p; - Diagnostic(ErrorCode.ERR_MissingDeconstruct, "p").WithArguments("Person", "2").WithLocation(30, 29) + Diagnostic(ErrorCode.ERR_MissingDeconstruct, "p").WithArguments("Person", "2").WithLocation(29, 29) ); } From 2c02869f52a7233f1925fabdbc2937c22f0f8973 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 21 Jul 2021 15:02:16 +0200 Subject: [PATCH 06/11] Add test for base ctor call --- .../AttributeTests_CallerInfoAttributes.cs | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs index 45ba06269a136..4b7fef900ccb4 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs @@ -524,6 +524,112 @@ static void Log(int p, [CallerArgumentExpression(p)] string arg = """") + { + Console.WriteLine(arg); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + CompileAndVerify(compilation, expectedOutput: +@"123 + + #pragma warning disable IDE0002 + #nullable disable + 5 + + #pragma warning disable IDE0003 + #nullable restore + 5 +").VerifyDiagnostics(); + } + + [ConditionalFact(typeof(CoreClrOnly))] + public void TestGoodCallerArgumentExpressionAttribute_ImplicitAndExplicitConstructorBaseCalls() + { + string source = @" +using System; +using System.Runtime.CompilerServices; + +class Base +{ + const string p = nameof(p); + public Base(int p = 0, [CallerArgumentExpression(p)] string arg = """") + { + Console.WriteLine(""Base class: "" + arg); + } +} + +class Derived1 : Base +{ + const string ppp = nameof(ppp); + public Derived1(int ppp, [CallerArgumentExpression(ppp)] string arg = """") + : base(ppp) + { + Console.WriteLine(""Derived1 class: "" + arg); + } +} + +class Derived2 : Base +{ + const string p = nameof(p); + public Derived2(int p, [CallerArgumentExpression(p)] string arg = """") + { + Console.WriteLine(""Derived2 class: "" + arg); + } +} + + +class Program +{ + public static void Main(string[] args) + { + _ = new Base(1+4); + Console.WriteLine(); + _ = new Derived1(2+ 5); + Console.WriteLine(); + _ = new Derived2(3 + 6); + } +} +"; + + var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular10); + CompileAndVerify(compilation, expectedOutput: +@"Base class: 1+4 + +Base class: ppp +Derived1 class: 2+ 5 + +Base class: +Derived2 class: 3 + 6 +").VerifyDiagnostics(); + } + [ConditionalFact(typeof(CoreClrOnly))] public void TestGoodCallerArgumentExpressionAttribute_SwapArguments() { From 10f0998dea418f070ac266ee92c6de8072e4ef02 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 22 Jul 2021 06:41:02 +0200 Subject: [PATCH 07/11] VB feature flag --- .../Portable/Binding/Binder_Invocation.vb | 9 +- .../VisualBasic/Portable/LanguageVersion.vb | 16 +-- .../Portable/Parser/ParserFeature.vb | 6 +- .../Source/SourceComplexParameterSymbol.vb | 9 +- .../Symbols/Source/SourceParameterSymbol.vb | 14 ++- .../Source/SourceSimpleParameterSymbol.vb | 4 + .../Test/CommandLine/CommandLineTests.vb | 18 +--- ...AttributeTests_CallerArgumentExpression.vb | 99 +++++++++---------- 8 files changed, 81 insertions(+), 94 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Binding/Binder_Invocation.vb b/src/Compilers/VisualBasic/Portable/Binding/Binder_Invocation.vb index b8788604bf51e..8a9d8fe0d3ca6 100644 --- a/src/Compilers/VisualBasic/Portable/Binding/Binder_Invocation.vb +++ b/src/Compilers/VisualBasic/Portable/Binding/Binder_Invocation.vb @@ -3126,6 +3126,11 @@ ProduceBoundNode: Dim isCallerArgumentExpression = callerArgumentExpressionParameterIndex > -1 OrElse (reducedExtensionReceiverOpt IsNot Nothing AndAlso callerArgumentExpressionParameterIndex > -2) + If isCallerArgumentExpression AndAlso Not SyntaxTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then + ' Silently require feature flag for this feature until Aleksey approves. + isCallerArgumentExpression = False + End If + If isCallerLineNumber OrElse isCallerMemberName OrElse isCallerFilePath OrElse isCallerArgumentExpression Then Dim callerInfoValue As ConstantValue = Nothing @@ -3177,10 +3182,6 @@ ProduceBoundNode: End If If argumentSyntax IsNot Nothing Then - InternalSyntax.Parser.CheckFeatureAvailability(diagnostics, - argumentSyntax.Location, - DirectCast(argumentSyntax.SyntaxTree.Options, VisualBasicParseOptions).LanguageVersion, - InternalSyntax.Feature.CallerArgumentExpression) callerInfoValue = ConstantValue.Create(argumentSyntax.ToString()) End If End If diff --git a/src/Compilers/VisualBasic/Portable/LanguageVersion.vb b/src/Compilers/VisualBasic/Portable/LanguageVersion.vb index e73993f8eaec3..3b1b9698ad173 100644 --- a/src/Compilers/VisualBasic/Portable/LanguageVersion.vb +++ b/src/Compilers/VisualBasic/Portable/LanguageVersion.vb @@ -21,7 +21,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic VisualBasic15_5 = 1505 VisualBasic16 = 1600 VisualBasic16_9 = 1609 - VisualBasic17 = 1700 Latest = Integer.MaxValue End Enum @@ -40,8 +39,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic LanguageVersion.VisualBasic15_3, LanguageVersion.VisualBasic15_5, LanguageVersion.VisualBasic16, - LanguageVersion.VisualBasic16_9, - LanguageVersion.VisualBasic17 + LanguageVersion.VisualBasic16_9 Return True End Select @@ -73,8 +71,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Return "16" Case LanguageVersion.VisualBasic16_9 Return "16.9" - Case LanguageVersion.VisualBasic17 - Return "17" Case Else Throw ExceptionUtilities.UnexpectedValue(value) End Select @@ -91,9 +87,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Public Function MapSpecifiedToEffectiveVersion(version As LanguageVersion) As LanguageVersion Select Case version Case LanguageVersion.Latest - Return LanguageVersion.VisualBasic17 + Return LanguageVersion.VisualBasic16_9 Case LanguageVersion.Default - Return LanguageVersion.VisualBasic17 + Return LanguageVersion.VisualBasic16 Case Else Return version End Select @@ -101,7 +97,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Friend ReadOnly Property CurrentVersion As LanguageVersion Get - Return LanguageVersion.VisualBasic17 + Return LanguageVersion.VisualBasic16_9 End Get End Property @@ -132,8 +128,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Return "16" Case LanguageVersion.VisualBasic16_9 Return "16.9" - Case LanguageVersion.VisualBasic17 - Return "17" Case LanguageVersion.Default Return "default" Case LanguageVersion.Latest @@ -173,8 +167,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic result = LanguageVersion.VisualBasic16 Case "16.9" result = LanguageVersion.VisualBasic16_9 - Case "17", "17.0" - result = LanguageVersion.VisualBasic17 Case "default" result = LanguageVersion.Default Case "latest" diff --git a/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb b/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb index 79db3a6a53f60..0d9f7e1feed76 100644 --- a/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb +++ b/src/Compilers/VisualBasic/Portable/Parser/ParserFeature.vb @@ -48,6 +48,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Friend Function GetFeatureFlag(feature As Feature) As String Select Case feature + Case Feature.CallerArgumentExpression + Return NameOf(Feature.CallerArgumentExpression) Case Else Return Nothing End Select @@ -106,10 +108,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Case Feature.InitOnlySettersUsage Return LanguageVersion.VisualBasic16_9 - - Case Feature.CallerArgumentExpression - Return LanguageVersion.VisualBasic17 - Case Else Throw ExceptionUtilities.UnexpectedValue(feature) End Select diff --git a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceComplexParameterSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceComplexParameterSymbol.vb index 448082e89ab57..5a6dd49be6b9d 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceComplexParameterSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceComplexParameterSymbol.vb @@ -2,15 +2,11 @@ ' The .NET Foundation licenses this file to you under the MIT license. ' See the LICENSE file in the project root for more information. -Imports System.Collections.Generic Imports System.Collections.Immutable -Imports System.Runtime.InteropServices Imports System.Threading Imports Microsoft.CodeAnalysis.Text Imports Microsoft.CodeAnalysis.VisualBasic.Binder -Imports Microsoft.CodeAnalysis.VisualBasic.Symbols Imports Microsoft.CodeAnalysis.VisualBasic.Syntax -Imports TypeKind = Microsoft.CodeAnalysis.TypeKind Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ''' @@ -253,6 +249,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Friend Overrides ReadOnly Property CallerArgumentExpressionParameterIndex As Integer Get + If Not _syntaxRef.SyntaxTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then + ' Silently require feature flag for this feature until Aleksey approves. + Return -1 + End If + Dim attributeSource As SourceParameterSymbol = If(Me.BoundAttributesSource, Me) Dim data = attributeSource.GetEarlyDecodedWellKnownAttributeData() diff --git a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb index 47bd057387009..ba008fa7174ca 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb @@ -242,7 +242,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols arguments.GetOrCreateData(Of ParameterEarlyWellKnownAttributeData).HasCallerFilePathAttribute = True ElseIf VisualBasicAttributeData.IsTargetEarlyAttribute(arguments.AttributeType, arguments.AttributeSyntax, AttributeDescription.CallerMemberNameAttribute) Then arguments.GetOrCreateData(Of ParameterEarlyWellKnownAttributeData).HasCallerMemberNameAttribute = True - ElseIf VisualBasicAttributeData.IsTargetEarlyAttribute(arguments.AttributeType, arguments.AttributeSyntax, AttributeDescription.CallerArgumentExpressionAttribute) Then + ElseIf VisualBasicAttributeData.IsTargetEarlyAttribute(arguments.AttributeType, arguments.AttributeSyntax, AttributeDescription.CallerArgumentExpressionAttribute) AndAlso + Location.SourceTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then Dim index = -1 Dim attribute = arguments.Binder.GetAttribute(arguments.AttributeSyntax, arguments.AttributeType, False) If Not attribute.HasErrors Then @@ -266,6 +267,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Friend Overrides Iterator Function GetCustomAttributesToEmit(compilationState As ModuleCompilationState) As IEnumerable(Of VisualBasicAttributeData) Dim attributes = MyBase.GetCustomAttributesToEmit(compilationState) + + If Not Location.SourceTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then + ' Silently require feature flag for this feature until Aleksey approves. + For Each attribute In attributes + Yield attribute + Next + End If + For Each attribute In attributes If AttributeData.IsTargetEarlyAttribute(attributeType:=attribute.AttributeClass, attributeArgCount:=attribute.CommonConstructorArguments.Length, description:=AttributeDescription.CallerArgumentExpressionAttribute) Then Dim callerArgumentExpressionParameterIndex = Me.CallerArgumentExpressionParameterIndex @@ -357,7 +366,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols arguments.GetOrCreateData(Of CommonParameterWellKnownAttributeData)().HasOutAttribute = True ElseIf attrData.IsTargetAttribute(Me, AttributeDescription.MarshalAsAttribute) Then MarshalAsAttributeDecoder(Of CommonParameterWellKnownAttributeData, AttributeSyntax, VisualBasicAttributeData, AttributeLocation).Decode(arguments, AttributeTargets.Parameter, MessageProvider.Instance) - ElseIf attrData.IsTargetAttribute(Me, AttributeDescription.CallerArgumentExpressionAttribute) Then + ElseIf attrData.IsTargetAttribute(Me, AttributeDescription.CallerArgumentExpressionAttribute) AndAlso + Location.SourceTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then Dim index = GetEarlyDecodedWellKnownAttributeData()?.CallerArgumentExpressionParameterIndex If index = Ordinal Then DirectCast(arguments.Diagnostics, BindingDiagnosticBag).Add(ERRID.WRN_CallerArgumentExpressionAttributeSelfReferential, arguments.AttributeSyntaxOpt.Location, Me.Name) diff --git a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceSimpleParameterSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceSimpleParameterSymbol.vb index 253708a7046f6..0aaa227521dfd 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceSimpleParameterSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceSimpleParameterSymbol.vb @@ -131,6 +131,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Friend Overrides ReadOnly Property CallerArgumentExpressionParameterIndex As Integer Get + If Not Location.SourceTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then + ' Silently require feature flag for this feature until Aleksey approves. + Return -1 + End If Dim data = GetEarlyDecodedWellKnownAttributeData() If data Is Nothing Then Return -1 diff --git a/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb b/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb index 146e6dec801ae..56c4e341eb8f8 100644 --- a/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb +++ b/src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb @@ -1504,14 +1504,6 @@ End Module").Path parsedArgs.Errors.Verify() Assert.Equal(LanguageVersion.VisualBasic16_9, parsedArgs.ParseOptions.LanguageVersion) - parsedArgs = DefaultParse({"/langVERSION:17", "a.vb"}, _baseDirectory) - parsedArgs.Errors.Verify() - Assert.Equal(LanguageVersion.VisualBasic17, parsedArgs.ParseOptions.LanguageVersion) - - parsedArgs = DefaultParse({"/langVERSION:17.0", "a.vb"}, _baseDirectory) - parsedArgs.Errors.Verify() - Assert.Equal(LanguageVersion.VisualBasic17, parsedArgs.ParseOptions.LanguageVersion) - ' The canary check is a reminder that this test needs to be updated when a language version is added LanguageVersionAdded_Canary() @@ -2037,7 +2029,7 @@ End Module").Path ' - update the "UpgradeProject" codefixer (not yet supported in VB) ' - update all the tests that call this canary ' - update the command-line documentation (CommandLine.md) - AssertEx.SetEqual({"default", "9", "10", "11", "12", "14", "15", "15.3", "15.5", "16", "16.9", "17", "latest"}, + AssertEx.SetEqual({"default", "9", "10", "11", "12", "14", "15", "15.3", "15.5", "16", "16.9", "latest"}, System.Enum.GetValues(GetType(LanguageVersion)).Cast(Of LanguageVersion)().Select(Function(v) v.ToDisplayString())) ' For minor versions, the format should be "x.y", such as "15.3" End Sub @@ -2059,8 +2051,7 @@ End Module").Path "15.3", "15.5", "16", - "16.9", - "17" + "16.9" } AssertEx.SetEqual(versions, errorCodes) @@ -2081,9 +2072,6 @@ End Module").Path Assert.Equal(LanguageVersion.VisualBasic15_5, LanguageVersion.VisualBasic15_5.MapSpecifiedToEffectiveVersion()) Assert.Equal(LanguageVersion.VisualBasic16, LanguageVersion.VisualBasic16.MapSpecifiedToEffectiveVersion()) Assert.Equal(LanguageVersion.VisualBasic16_9, LanguageVersion.VisualBasic16_9.MapSpecifiedToEffectiveVersion()) - Assert.Equal(LanguageVersion.VisualBasic17, LanguageVersion.VisualBasic17.MapSpecifiedToEffectiveVersion()) - Assert.Equal(LanguageVersion.VisualBasic17, LanguageVersion.Default.MapSpecifiedToEffectiveVersion()) - Assert.Equal(LanguageVersion.VisualBasic17, LanguageVersion.Latest.MapSpecifiedToEffectiveVersion()) ' The canary check is a reminder that this test needs to be updated when a language version is added LanguageVersionAdded_Canary() @@ -2107,8 +2095,6 @@ End Module").Path InlineData("16", True, LanguageVersion.VisualBasic16), InlineData("16.0", True, LanguageVersion.VisualBasic16), InlineData("16.9", True, LanguageVersion.VisualBasic16_9), - InlineData("17", True, LanguageVersion.VisualBasic17), - InlineData("17.0", True, LanguageVersion.VisualBasic17), InlineData("DEFAULT", True, LanguageVersion.Default), InlineData("default", True, LanguageVersion.Default), InlineData("LATEST", True, LanguageVersion.Latest), diff --git a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb index 9b4ea21eeafc5..7962ffc4154f8 100644 --- a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb +++ b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb @@ -30,7 +30,7 @@ Module Program End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="123").VerifyDiagnostics() End Sub @@ -67,7 +67,7 @@ Class Program End Class " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="456").VerifyDiagnostics() End Sub @@ -104,7 +104,7 @@ Class Program End Class " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="").VerifyDiagnostics() End Sub @@ -125,7 +125,7 @@ Public Module Program End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="123").VerifyDiagnostics().VerifyTypeIL("Program", " .class public auto ansi sealed Program extends [System.Runtime]System.Object @@ -233,7 +233,7 @@ End Module - Dim compilation = CreateCompilationWithCustomILSource(source, il, options:=TestOptions.ReleaseExe, includeVbRuntime:=True, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilationWithCustomILSource(source, il, options:=TestOptions.ReleaseExe, includeVbRuntime:=True, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="0 + 1").VerifyDiagnostics() End Sub @@ -254,13 +254,8 @@ Module Program End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.Regular16_9) - compilation.AssertTheseDiagnostics( - -BC36716: Visual Basic 16.9 does not support caller argument expression. - Log(123) - ~~~ -) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.Regular16_9.WithFeature("CallerArgumentExpression")) + CompileAndVerify(compilation, expectedOutput:="123").VerifyDiagnostics() End Sub @@ -283,7 +278,7 @@ Module Program End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="123 + _ 5").VerifyDiagnostics() End Sub @@ -305,7 +300,7 @@ Module Program End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="124, 123, 124").VerifyDiagnostics() End Sub @@ -334,7 +329,7 @@ Module Program End Module " - Dim compilation = CreateCompilation(source2, references:={ref1, Net451.MicrosoftVisualBasic}, targetFramework:=TargetFramework.NetCoreApp, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source2, references:={ref1, Net451.MicrosoftVisualBasic}, targetFramework:=TargetFramework.NetCoreApp, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="2 + 2").VerifyDiagnostics() End Sub @@ -357,7 +352,7 @@ Module Program End Sub End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="myIntegerExpression").VerifyDiagnostics() End Sub @@ -380,7 +375,7 @@ Module Program End Sub End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="myIntegerExpression * 2").VerifyDiagnostics() End Sub @@ -403,7 +398,7 @@ Module Program End Sub End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="") compilation.AssertTheseDiagnostics( ") compilation.AssertTheseDiagnostics( ").VerifyDiagnostics() End Sub @@ -864,7 +859,7 @@ Module Program End Sub End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:=" value") compilation.AssertTheseDiagnostics( @@ -920,7 +915,7 @@ End Module - Dim compilation = CreateCompilationWithCustomILSource(source, il, options:=TestOptions.ReleaseExe, includeVbRuntime:=True, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilationWithCustomILSource(source, il, options:=TestOptions.ReleaseExe, includeVbRuntime:=True, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:=" value").VerifyDiagnostics() End Sub @@ -947,7 +942,7 @@ Public Module Program End Sub End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="123").VerifyDiagnostics() End Sub @@ -973,7 +968,7 @@ Public Module Program End Sub End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="123 _ ' comment + 5").VerifyDiagnostics() End Sub @@ -1004,7 +999,7 @@ Public Module Program End Sub End Module " - Dim compilation = CreateCompilation(source2, references:={ref1, Net451.MicrosoftVisualBasic}, targetFramework:=TargetFramework.NetCoreApp, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source2, references:={ref1, Net451.MicrosoftVisualBasic}, targetFramework:=TargetFramework.NetCoreApp, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="2 + 2").VerifyDiagnostics() End Sub @@ -1028,7 +1023,7 @@ Public Module Program End Sub End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="") compilation.AssertTheseDiagnostics( ', ''").VerifyDiagnostics() End Sub #End Region @@ -1162,7 +1157,7 @@ Class Program End Sub End Class " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="2, 1+ 1 4, explicit-value").VerifyDiagnostics() End Sub @@ -1187,7 +1182,7 @@ Class Program End Sub End Class " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Program.Main", " { // Code size 27 (0x1b) @@ -1226,7 +1221,7 @@ Class Program End Sub End Class " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) compilation.AssertTheseDiagnostics( - Dim compilation = CreateCompilationWithCustomILSource(source, il, options:=TestOptions.ReleaseExe, includeVbRuntime:=True, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilationWithCustomILSource(source, il, options:=TestOptions.ReleaseExe, includeVbRuntime:=True, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) compilation.VerifyDiagnostics() End Sub @@ -1468,7 +1463,7 @@ Class Program End Sub End Class " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest) + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.RegularLatest.WithFeature("CallerArgumentExpression")) CompileAndVerify(compilation, expectedOutput:="New value") compilation.AssertTheseDiagnostics( Date: Thu, 22 Jul 2021 06:47:05 +0200 Subject: [PATCH 08/11] Don't require C# 10 for caller argument expression --- src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs | 1 - src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs | 2 -- src/Compilers/CSharp/Portable/CSharpResources.resx | 3 --- src/Compilers/CSharp/Portable/Errors/MessageID.cs | 3 --- src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf | 5 ----- src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf | 5 ----- .../CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf | 5 ----- .../CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf | 5 ----- .../Emit/Attributes/AttributeTests_CallerInfoAttributes.cs | 5 +---- 18 files changed, 1 insertion(+), 78 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs index f4992eadd35cb..c33ecd544e1aa 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs @@ -815,7 +815,6 @@ private TypedConstant GetDefaultValueArgument(ParameterSymbol parameter, Attribu getCallerArgumentArgumentIndex(parameter, argumentsToParams) is int argumentIndex && argumentIndex > -1 && argumentIndex < argumentsCount) { Debug.Assert(argumentsCount <= syntax.ArgumentList.Arguments.Count); - CheckFeatureAvailability(syntax.ArgumentList, MessageID.IDS_FeatureCallerArgumentExpression, diagnostics); parameterType = GetSpecialType(SpecialType.System_String, diagnostics, syntax); kind = TypedConstantKind.Primitive; defaultValue = syntax.ArgumentList.Arguments[argumentIndex].Expression.ToString(); diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs index e9618256bb2c4..a8a26d49e404f 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs @@ -1388,8 +1388,6 @@ BoundExpression bindDefaultArgument(SyntaxNode syntax, ParameterSymbol parameter else if (callerSourceLocation is object && getArgumentIndex(parameter.CallerArgumentExpressionParameterIndex, argsToParamsOpt) is int argumentIndex && argumentIndex > -1 && argumentIndex < argumentsCount) { - CheckFeatureAvailability(syntax, MessageID.IDS_FeatureCallerArgumentExpression, diagnostics); - var argument = argumentsBuilder[argumentIndex]; defaultValue = new BoundLiteral(syntax, ConstantValue.Create(argument.Syntax.ToString()), Compilation.GetSpecialType(SpecialType.System_String)) { WasCompilerGenerated = true }; } diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index 3e809d5df7f5e..0a3a539008598 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -6706,9 +6706,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ The CallerArgumentExpressionAttribute applied to parameter will have no effect because it's self-refential. - - caller argument expression - sealed ToString in record diff --git a/src/Compilers/CSharp/Portable/Errors/MessageID.cs b/src/Compilers/CSharp/Portable/Errors/MessageID.cs index 5e74b2819e210..3dc221c18a0f2 100644 --- a/src/Compilers/CSharp/Portable/Errors/MessageID.cs +++ b/src/Compilers/CSharp/Portable/Errors/MessageID.cs @@ -233,8 +233,6 @@ internal enum MessageID IDS_FeatureLineSpanDirective = MessageBase + 12807, IDS_FeatureImprovedInterpolatedStrings = MessageBase + 12808, IDS_FeatureFileScopedNamespace = MessageBase + 12809, - // PROTOTYPE: compact before merge - IDS_FeatureCallerArgumentExpression = MessageBase + 13000, } // Message IDs may refer to strings that need to be localized. @@ -347,7 +345,6 @@ internal static LanguageVersion RequiredVersion(this MessageID feature) // C# 10.0 features. case MessageID.IDS_FeatureMixedDeclarationsAndExpressionsInDeconstruction: // semantic check - case MessageID.IDS_FeatureCallerArgumentExpression: // semantic check case MessageID.IDS_FeatureSealedToStringInRecord: // semantic check case MessageID.IDS_FeatureImprovedInterpolatedStrings: // semantic check case MessageID.IDS_FeatureRecordStructs: diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index 49868cf8d1819..d07e4115c66a0 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns kovariantní návratové hodnoty diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index 684df39e36984..35deb3f224a92 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns Covariante Rückgaben diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index cdd41eada8bdb..110e9fe7edb41 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns valores devueltos de covariante diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index 243406142ad2f..2a090cfb193fb 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns retours covariants diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index 1185f1960bc56..3e366b9187ef4 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns tipi restituiti covarianti diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index 785eff40e0fb9..642fb49fb4e00 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns covariant の戻り値 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index 0e7b5a8514bdc..b94a65a0c98ce 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns 공변(covariant) 반환 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index 638975b89adab..0ab375174cbed 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns zwroty kowariantne diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index 0ac870d4a2a88..2550bcbb03665 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns retornos de covariante diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index 31530cb3fc19f..36d89888331fc 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns ковариантные возвращаемые значения diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index 9029ad42f90b1..2ed509d1c2edb 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns birlikte değişken dönüşler diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 4603cbb99f8b0..b52f5c18d0695 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns 协变返回 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index 3623fcb444ebf..cc2da5952d266 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -1202,11 +1202,6 @@ async method builder override - - caller argument expression - caller argument expression - - covariant returns Covariant 傳回 diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs index 4b7fef900ccb4..c48dbc34c314c 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_CallerInfoAttributes.cs @@ -2174,10 +2174,7 @@ static void Main() } }"; var compilation = CreateCompilation(source, targetFramework: TargetFramework.NetCoreApp, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular9); - compilation.VerifyDiagnostics( - // (14,4): error CS8773: Feature 'caller argument expression' is not available in C# 9.0. Please use language version 10.0 or greater. - // [My(1+2)] - Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "(1+2)").WithArguments("caller argument expression", "10.0").WithLocation(14, 4)); + CompileAndVerify(compilation, expectedOutput: "'3', '1+2'").VerifyDiagnostics(); } [ConditionalFact(typeof(CoreClrOnly))] From 260953436d1d3af29266411d4b3f8c2cc3182e63 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 22 Jul 2021 19:52:11 +0200 Subject: [PATCH 09/11] Small fix and address feedback --- .../Portable/Symbols/Source/SourceParameterSymbol.vb | 4 +++- .../Attributes/AttributeTests_CallerArgumentExpression.vb | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb index ba008fa7174ca..8e71e2c0bab41 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb @@ -268,11 +268,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Friend Overrides Iterator Function GetCustomAttributesToEmit(compilationState As ModuleCompilationState) As IEnumerable(Of VisualBasicAttributeData) Dim attributes = MyBase.GetCustomAttributesToEmit(compilationState) - If Not Location.SourceTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then + If Not Location.IsInSource OrElse + Not Location.SourceTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then ' Silently require feature flag for this feature until Aleksey approves. For Each attribute In attributes Yield attribute Next + Return End If For Each attribute In attributes diff --git a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb index 7962ffc4154f8..80b9d3953cffe 100644 --- a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb +++ b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb @@ -238,7 +238,7 @@ End Module End Sub - Public Sub TestGoodCallerArgumentExpressionAttribute_Version16_9() + Public Sub TestGoodCallerArgumentExpressionAttribute_Version16_9_WithoutFeatureFlag() Dim source As String = " Imports System Imports System.Runtime.CompilerServices @@ -254,8 +254,8 @@ Module Program End Module " - Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.Regular16_9.WithFeature("CallerArgumentExpression")) - CompileAndVerify(compilation, expectedOutput:="123").VerifyDiagnostics() + Dim compilation = CreateCompilation(source, targetFramework:=TargetFramework.NetCoreApp, references:={Net451.MicrosoftVisualBasic}, options:=TestOptions.ReleaseExe, parseOptions:=TestOptions.Regular16_9) + CompileAndVerify(compilation, expectedOutput:="").VerifyDiagnostics() End Sub From 5cdfcb453b2714d4386037bdf7de8cbde48c8036 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 22 Jul 2021 20:23:05 +0200 Subject: [PATCH 10/11] Fix NRE --- .../Portable/Symbols/Source/SourceParameterSymbol.vb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb index 8e71e2c0bab41..5ca15b5542e78 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceParameterSymbol.vb @@ -268,7 +268,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Friend Overrides Iterator Function GetCustomAttributesToEmit(compilationState As ModuleCompilationState) As IEnumerable(Of VisualBasicAttributeData) Dim attributes = MyBase.GetCustomAttributesToEmit(compilationState) - If Not Location.IsInSource OrElse + If Location Is Nothing OrElse Not Location.IsInSource OrElse Not Location.SourceTree.Options.Features.ContainsKey(InternalSyntax.GetFeatureFlag(InternalSyntax.Feature.CallerArgumentExpression)) Then ' Silently require feature flag for this feature until Aleksey approves. For Each attribute In attributes From 7bd4716594c3b8a44ce2895c9e622cdc8f0af759 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Fri, 23 Jul 2021 01:17:16 +0200 Subject: [PATCH 11/11] Address feedback --- .../Portable/PublicAPI.Unshipped.txt | 1 - ...AttributeTests_CallerArgumentExpression.vb | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt b/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt index 3b30cdbe89642..264737b55b170 100644 --- a/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt +++ b/src/Compilers/VisualBasic/Portable/PublicAPI.Unshipped.txt @@ -1,5 +1,4 @@ Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic16_9 = 1609 -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion -Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic17 = 1700 -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion Microsoft.CodeAnalysis.VisualBasic.VisualBasicGeneratorDriver Overrides Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.GetLineMappings(cancellationToken As System.Threading.CancellationToken = Nothing) -> System.Collections.Generic.IEnumerable(Of Microsoft.CodeAnalysis.LineMapping) Shared Microsoft.CodeAnalysis.VisualBasic.VisualBasicGeneratorDriver.Create(generators As System.Collections.Immutable.ImmutableArray(Of Microsoft.CodeAnalysis.ISourceGenerator), additionalTexts As System.Collections.Immutable.ImmutableArray(Of Microsoft.CodeAnalysis.AdditionalText) = Nothing, parseOptions As Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions = Nothing, analyzerConfigOptionsProvider As Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptionsProvider = Nothing) -> Microsoft.CodeAnalysis.VisualBasic.VisualBasicGeneratorDriver diff --git a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb index 80b9d3953cffe..7c0399c5a9a97 100644 --- a/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb +++ b/src/Compilers/VisualBasic/Test/Emit/Attributes/AttributeTests_CallerArgumentExpression.vb @@ -34,6 +34,27 @@ End Module CompileAndVerify(compilation, expectedOutput:="123").VerifyDiagnostics() End Sub + + Public Sub TestGoodCallerArgumentExpressionAttribute_OldVersionWithFeatureFlag() + Dim source As String = " +Imports System +Imports System.Runtime.CompilerServices +Module Program + Sub Main() + Log(123) + End Sub + + Private Const p As String = NameOf(p) + Sub Log(p As Integer, Optional 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.Regular16.WithFeature("CallerArgumentExpression")) + CompileAndVerify(compilation, expectedOutput:="123").VerifyDiagnostics() + End Sub + Public Sub TestGoodCallerArgumentExpressionAttribute_MultipleAttributes() Dim source As String = "