From 1b3f19666450ed65141caee7088365cb4bc0ebd8 Mon Sep 17 00:00:00 2001 From: Kevin Sun Date: Fri, 14 Aug 2020 14:02:52 -0700 Subject: [PATCH 1/7] Update IntroduceVariableTests.cs --- .../IntroduceVariable/IntroduceVariableTests.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs index baedaba4d3817..adca08e5a671d 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs @@ -3212,7 +3212,7 @@ void M() } [WorkItem(1065661, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1065661")] - [Fact(Skip = "PROTOTYPE(CONSTISTR) - Switched from introducing a local to introducing a constant."), Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] public async Task TestIntroduceVariableTextDoesntSpanLines2() { await TestSmartTagTextAsync( @@ -3226,7 +3226,7 @@ void M() c""|]; } }", -string.Format(FeaturesResources.Introduce_local_for_0, @"$@""a b c""")); +string.Format(FeaturesResources.Introduce_constant_for_0, @"$@""a b c""")); } [WorkItem(1097147, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1097147")] @@ -4565,7 +4565,7 @@ class TestClass [WorkItem(976, "https://github.com/dotnet/roslyn/issues/976")] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] - public async Task TestNoConstantForInterpolatedStrings1() + public async Task TestNoConstantForInterpolatedStrings() { var code = @"using System; @@ -4592,8 +4592,8 @@ static void Test(string[] args) } [WorkItem(976, "https://github.com/dotnet/roslyn/issues/976")] - [Fact(Skip = "PROTOTYPE(CONSTISTR) - Expected changed var into a private const."), Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] - public async Task TestNoConstantForInterpolatedStrings2() + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] + public async Task TestConstantForInterpolatedStrings() { var code = @"using System; @@ -4610,11 +4610,12 @@ static void Test(string[] args) @"using System; class TestClass { + private const string {|Rename:Value|} = $""Text{{s}}""; + static void Test(string[] args) { - var {|Rename:value|} = $""Text{{s}}""; - Console.WriteLine(value); - Console.WriteLine(value); + Console.WriteLine(Value); + Console.WriteLine(Value); } }"; From 4e2f8fee4d5924e1c988357108e8a288750feace Mon Sep 17 00:00:00 2001 From: Kevin Sun Date: Fri, 14 Aug 2020 09:48:10 -0700 Subject: [PATCH 2/7] Update ConstantTests.cs --- .../Test/Semantic/Semantics/ConstantTests.cs | 142 ++++++++++-------- 1 file changed, 81 insertions(+), 61 deletions(-) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs index 8c96eba740bf2..9dbdd3491cd34 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs @@ -3520,20 +3520,30 @@ public class A : System.Attribute public A(string name) { this.name = name; - } + } } [A($""ITEM"")] class C { - void M(string S1 = $""Testing"") + string S0 = $""Faaaaaaaaaaaaaaaaaaaaaaaaall""; + + class Namae { - switch(S1){ - case $""Level 5"": - break; - case $""Radio Noise"": - goto case $""Level 5""; + public string X { get; } + } + + void M(string S1 = $""Testing"", Namae n = null) + { + if (n is Namae { X : $""ConstantInterpolatedString""}){ + switch(S1){ + case $""Level 5"": + break; + case $""Radio Noise"": + goto case $""Level 5""; + } } + S0 = S1; } }"; var comp = CreateCompilation(source, parseOptions: TestOptions.RegularPreview); @@ -3645,6 +3655,11 @@ public A(string name) [A($""ITEM"")] class C { + class Namae + { + public string X { get; } + } + #pragma warning disable CS0219 // unused locals void M1() { @@ -3660,74 +3675,79 @@ void M1() const string S6 = $""Failed to {VS}""; } - void M2(string S1 = $""Testing"", object O1 = null) + void M2(string S1 = $""Testing"", object O = null, Namae N = null) { switch(S1){ case $""Level 5"": break; } - switch(O1){ - case $""Number 3"": - break; - case $""Radio Noise"": - goto case $""Number 3""; + if (N is Namae { X : $""ConstantInterpolatedString""}){ + switch(O){ + case $""Number 3"": + break; + case $""Radio Noise"": + goto case $""Number 3""; + } } } }"; var comp = CreateCompilation(source, parseOptions: TestOptions.RegularPreview); comp.VerifyDiagnostics( - // (15,27): error CS0133: The expression being assigned to 'S6' must be constant - // const string S6 = $"Failed to {VS}"; - Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(27, 27)); + // (32,27): error CS0133: The expression being assigned to 'S6' must be constant + // const string S6 = $"Failed to {VS}"; + Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(32, 27)); comp = CreateCompilation(source, parseOptions: TestOptions.Regular8); comp.VerifyDiagnostics( - // (12,4): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // [A($"ITEM")] - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ITEM""").WithArguments("constant interpolated strings").WithLocation(12, 4), - // (18,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string S1 = $"Testing"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(18, 27), - // (19,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string S2 = $"{"Level 5"} {"Number 3"}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{""Level 5""} {""Number 3""}""").WithArguments("constant interpolated strings").WithLocation(19, 27), - // (20,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string S3 = $"{$"{"Spinning Top"}"}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{$""{""Spinning Top""}""}""").WithArguments("constant interpolated strings").WithLocation(20, 27), - // (21,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string S4 = $"Hybrid" + "Testing" + "123"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Hybrid""").WithArguments("constant interpolated strings").WithLocation(21, 27), - // (22,50): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string S5 = "Hybrid" + "Testing" + $"321"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""321""").WithArguments("constant interpolated strings").WithLocation(22, 50), - // (23,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string F1 = $"{S1}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{S1}""").WithArguments("constant interpolated strings").WithLocation(23, 27), - // (24,32): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string F2 = F1 + $" the {S2}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$"" the {S2}""").WithArguments("constant interpolated strings").WithLocation(24, 32), - // (27,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // const string S6 = $"Failed to {VS}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Failed to {VS}""").WithArguments("constant interpolated strings").WithLocation(27, 27), - // (27,27): error CS0133: The expression being assigned to 'S6' must be constant - // const string S6 = $"Failed to {VS}"; - Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(27, 27), - // (30,25): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // void M2(string S1 = $"Testing", object O1 = null) - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(30, 25), - // (33,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // case $"Level 5": - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Level 5""").WithArguments("constant interpolated strings").WithLocation(33, 18), - // (38,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // case $"Number 3": - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(38, 18), - // (40,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // case $"Radio Noise": - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Radio Noise""").WithArguments("constant interpolated strings").WithLocation(40, 18), - // (41,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // goto case $"Number 3"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(41, 27)); + // (12,4): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // [A($"ITEM")] + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ITEM""").WithArguments("constant interpolated strings").WithLocation(12, 4), + // (23,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string S1 = $"Testing"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(23, 27), + // (24,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string S2 = $"{"Level 5"} {"Number 3"}"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{""Level 5""} {""Number 3""}""").WithArguments("constant interpolated strings").WithLocation(24, 27), + // (25,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string S3 = $"{$"{"Spinning Top"}"}"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{$""{""Spinning Top""}""}""").WithArguments("constant interpolated strings").WithLocation(25, 27), + // (26,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string S4 = $"Hybrid" + "Testing" + "123"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Hybrid""").WithArguments("constant interpolated strings").WithLocation(26, 27), + // (27,50): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string S5 = "Hybrid" + "Testing" + $"321"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""321""").WithArguments("constant interpolated strings").WithLocation(27, 50), + // (28,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string F1 = $"{S1}"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{S1}""").WithArguments("constant interpolated strings").WithLocation(28, 27), + // (29,32): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string F2 = F1 + $" the {S2}"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$"" the {S2}""").WithArguments("constant interpolated strings").WithLocation(29, 32), + // (32,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string S6 = $"Failed to {VS}"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Failed to {VS}""").WithArguments("constant interpolated strings").WithLocation(32, 27), + // (32,27): error CS0133: The expression being assigned to 'S6' must be constant + // const string S6 = $"Failed to {VS}"; + Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(32, 27), + // (35,25): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // void M2(string S1 = $"Testing", object O = null, Namae N = null) + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(35, 25), + // (38,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // case $"Level 5": + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Level 5""").WithArguments("constant interpolated strings").WithLocation(38, 18), + // (42,30): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // if (N is Namae { X : $"ConstantInterpolatedString"}){ + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ConstantInterpolatedString""").WithArguments("constant interpolated strings").WithLocation(42, 30), + // (44,22): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // case $"Number 3": + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(44, 22), + // (46,22): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // case $"Radio Noise": + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Radio Noise""").WithArguments("constant interpolated strings").WithLocation(46, 22), + // (47,31): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // goto case $"Number 3"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(47, 31)); } } From 8333853c50a4dfb31a1155a934466d04de199a49 Mon Sep 17 00:00:00 2001 From: Kevin Sun Date: Mon, 13 Jul 2020 13:45:04 -0700 Subject: [PATCH 3/7] Update SemanticModelAPITests.cs --- .../Test/Symbol/Compilation/SemanticModelAPITests.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs index 0710d024ae9d4..c500d45a160b4 100644 --- a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs @@ -4025,7 +4025,7 @@ static void F() } [WorkItem(976, "https://github.com/dotnet/roslyn/issues/976")] - [Fact(Skip = "PROTOTYPE(CONSTISTR) - Asserts that Interpolated Strings are not constant, but the first can be.")] + [Fact] public void ConstantValueOfInterpolatedString() { var source = @" @@ -4041,9 +4041,15 @@ static void Main(string[] args) var comp = CreateCompilation(source, options: TestOptions.ReleaseExe); var tree = comp.SyntaxTrees.Single(); var model = comp.GetSemanticModel(tree); + + var expected = new bool[2]; + expected[0] = true; + expected[1] = false; + var iterator = 0; foreach (var interp in tree.GetRoot().DescendantNodes().OfType()) { - Assert.False(model.GetConstantValue(interp).HasValue); + Assert.Equal(expected[iterator], model.GetConstantValue(interp).HasValue); + iterator++; } } From 419ac8a09a98b030d7218df721ee162d311244bc Mon Sep 17 00:00:00 2001 From: Kevin Sun Date: Mon, 17 Aug 2020 08:56:03 -0700 Subject: [PATCH 4/7] PR changes --- .../Test/Semantic/Semantics/ConstantTests.cs | 71 ++++++++++--------- .../Compilation/SemanticModelAPITests.cs | 14 ++-- .../IntroduceVariableTests.cs | 2 +- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs index 9dbdd3491cd34..16f4fbe429b66 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs @@ -3526,7 +3526,7 @@ public A(string name) [A($""ITEM"")] class C { - string S0 = $""Faaaaaaaaaaaaaaaaaaaaaaaaall""; + const string S0 = $""Faaaaaaaaaaaaaaaaaaaaaaaaall""; class Namae { @@ -3543,7 +3543,7 @@ void M(string S1 = $""Testing"", Namae n = null) goto case $""Level 5""; } } - S0 = S1; + S1 = S0; } }"; var comp = CreateCompilation(source, parseOptions: TestOptions.RegularPreview); @@ -3655,6 +3655,8 @@ public A(string name) [A($""ITEM"")] class C { + const string S0 = $""Post""; + class Namae { public string X { get; } @@ -3696,58 +3698,61 @@ void M2(string S1 = $""Testing"", object O = null, Namae N = null) comp.VerifyDiagnostics( // (32,27): error CS0133: The expression being assigned to 'S6' must be constant // const string S6 = $"Failed to {VS}"; - Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(32, 27)); + Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(34, 27)); comp = CreateCompilation(source, parseOptions: TestOptions.Regular8); comp.VerifyDiagnostics( // (12,4): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // [A($"ITEM")] Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ITEM""").WithArguments("constant interpolated strings").WithLocation(12, 4), - // (23,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // (15,23): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // const string S0 = $"Post"; + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Post""").WithArguments("constant interpolated strings").WithLocation(15, 23), + // (25,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string S1 = $"Testing"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(23, 27), - // (24,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(25, 27), + // (26,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string S2 = $"{"Level 5"} {"Number 3"}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{""Level 5""} {""Number 3""}""").WithArguments("constant interpolated strings").WithLocation(24, 27), - // (25,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{""Level 5""} {""Number 3""}""").WithArguments("constant interpolated strings").WithLocation(26, 27), + // (27,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string S3 = $"{$"{"Spinning Top"}"}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{$""{""Spinning Top""}""}""").WithArguments("constant interpolated strings").WithLocation(25, 27), - // (26,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{$""{""Spinning Top""}""}""").WithArguments("constant interpolated strings").WithLocation(27, 27), + // (28,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string S4 = $"Hybrid" + "Testing" + "123"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Hybrid""").WithArguments("constant interpolated strings").WithLocation(26, 27), - // (27,50): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Hybrid""").WithArguments("constant interpolated strings").WithLocation(28, 27), + // (29,50): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string S5 = "Hybrid" + "Testing" + $"321"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""321""").WithArguments("constant interpolated strings").WithLocation(27, 50), - // (28,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""321""").WithArguments("constant interpolated strings").WithLocation(29, 50), + // (30,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string F1 = $"{S1}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{S1}""").WithArguments("constant interpolated strings").WithLocation(28, 27), - // (29,32): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{S1}""").WithArguments("constant interpolated strings").WithLocation(30, 27), + // (31,32): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string F2 = F1 + $" the {S2}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$"" the {S2}""").WithArguments("constant interpolated strings").WithLocation(29, 32), - // (32,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$"" the {S2}""").WithArguments("constant interpolated strings").WithLocation(31, 32), + // (34,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // const string S6 = $"Failed to {VS}"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Failed to {VS}""").WithArguments("constant interpolated strings").WithLocation(32, 27), - // (32,27): error CS0133: The expression being assigned to 'S6' must be constant + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Failed to {VS}""").WithArguments("constant interpolated strings").WithLocation(34, 27), + // (34,27): error CS0133: The expression being assigned to 'S6' must be constant // const string S6 = $"Failed to {VS}"; - Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(32, 27), - // (35,25): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(34, 27), + // (37,25): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // void M2(string S1 = $"Testing", object O = null, Namae N = null) - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(35, 25), - // (38,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(37, 25), + // (40,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // case $"Level 5": - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Level 5""").WithArguments("constant interpolated strings").WithLocation(38, 18), - // (42,30): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Level 5""").WithArguments("constant interpolated strings").WithLocation(40, 18), + // (44,30): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // if (N is Namae { X : $"ConstantInterpolatedString"}){ - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ConstantInterpolatedString""").WithArguments("constant interpolated strings").WithLocation(42, 30), - // (44,22): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. - // case $"Number 3": - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(44, 22), + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ConstantInterpolatedString""").WithArguments("constant interpolated strings").WithLocation(44, 30), // (46,22): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + // case $"Number 3": + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(46, 22), + // (48,22): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // case $"Radio Noise": - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Radio Noise""").WithArguments("constant interpolated strings").WithLocation(46, 22), - // (47,31): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Radio Noise""").WithArguments("constant interpolated strings").WithLocation(48, 22), + // (49,31): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. // goto case $"Number 3"; - Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(47, 31)); + Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(49, 31)); } } diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs index c500d45a160b4..2a958ae3454bf 100644 --- a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs @@ -4042,15 +4042,11 @@ static void Main(string[] args) var tree = comp.SyntaxTrees.Single(); var model = comp.GetSemanticModel(tree); - var expected = new bool[2]; - expected[0] = true; - expected[1] = false; - var iterator = 0; - foreach (var interp in tree.GetRoot().DescendantNodes().OfType()) - { - Assert.Equal(expected[iterator], model.GetConstantValue(interp).HasValue); - iterator++; - } + var expected = new[] { true, false }; + var actual = tree.GetRoot().DescendantNodes().OfType().ToArray(); + Assert.Equal(expected[0], model.GetConstantValue(actual[0]).HasValue); + Assert.Equal("Hello, world!", model.GetConstantValue(actual[0]).Value); + Assert.Equal(expected[1], model.GetConstantValue(actual[1]).HasValue); } [WorkItem(814, "https://github.com/dotnet/roslyn/issues/814")] diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs index adca08e5a671d..d7cdea9286854 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs @@ -4583,7 +4583,7 @@ class TestClass { static void Test(string[] args) { - var {|Rename:value|} = $""{DateTime.Now.ToString()}Text{args[0]}""; + var {|Rename:value|} = $""{DateTime.Now.ToString()}Text{args[0]}""; Console.WriteLine(value); } }"; From 54ce24c3053ca5266c5cf12347668a7427a1870e Mon Sep 17 00:00:00 2001 From: Kevin Sun Date: Mon, 17 Aug 2020 11:45:29 -0700 Subject: [PATCH 5/7] PR changes --- .../Compilation/SemanticModelAPITests.cs | 5 ++- .../IntroduceVariableTests.cs | 31 ++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs index 2a958ae3454bf..ff6000cbc9f76 100644 --- a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs @@ -4042,11 +4042,10 @@ static void Main(string[] args) var tree = comp.SyntaxTrees.Single(); var model = comp.GetSemanticModel(tree); - var expected = new[] { true, false }; var actual = tree.GetRoot().DescendantNodes().OfType().ToArray(); - Assert.Equal(expected[0], model.GetConstantValue(actual[0]).HasValue); + Assert.True(model.GetConstantValue(actual[0]).HasValue); Assert.Equal("Hello, world!", model.GetConstantValue(actual[0]).Value); - Assert.Equal(expected[1], model.GetConstantValue(actual[1]).HasValue); + Assert.False(model.GetConstantValue(actual[1]).HasValue); } [WorkItem(814, "https://github.com/dotnet/roslyn/issues/814")] diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs index d7cdea9286854..176280cef13fc 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs @@ -4583,7 +4583,7 @@ class TestClass { static void Test(string[] args) { - var {|Rename:value|} = $""{DateTime.Now.ToString()}Text{args[0]}""; + var {|Rename:value|} = $""{DateTime.Now.ToString()}Text{args[0]}""; Console.WriteLine(value); } }"; @@ -4622,6 +4622,35 @@ static void Test(string[] args) await TestInRegularAndScriptAsync(code, expected, index: 1, options: ImplicitTypingEverywhere()); } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] + public async Task TestConstantForInterpolatedStringsInvalid() + { + var code = + @"using System; +class TestClass +{ + static void Test(string[] args) + { + Console.WriteLine([|$""Text{0}""|]); + Console.WriteLine($""Text{0}""); + } +}"; + + var expected = + @"using System; +class TestClass +{ + static void Test(string[] args) + { + var {|Rename:value|} = $""Text{0}""; + Console.WriteLine(value); + Console.WriteLine(value); + } +}"; + + await TestInRegularAndScriptAsync(code, expected, index: 1, options: ImplicitTypingEverywhere()); + } + [WorkItem(909152, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/909152")] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] public async Task TestMissingOnNullLiteral() From 43b2c9c942395b539c02edd72be9aeea154f9d02 Mon Sep 17 00:00:00 2001 From: Kevin Sun Date: Mon, 17 Aug 2020 12:38:13 -0700 Subject: [PATCH 6/7] Update IntroduceVariableTests.cs --- .../IntroduceVariableTests.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs index 176280cef13fc..9bea68fd176a7 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceVariable/IntroduceVariableTests.cs @@ -4612,6 +4612,36 @@ class TestClass { private const string {|Rename:Value|} = $""Text{{s}}""; + static void Test(string[] args) + { + Console.WriteLine(Value); + Console.WriteLine(Value); + } +}"; + + await TestInRegularAndScriptAsync(code, expected, index: 1, options: ImplicitTypingEverywhere()); + } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] + public async Task TestConstantForInterpolatedStringsNested() + { + var code = +@"using System; +class TestClass +{ + static void Test(string[] args) + { + Console.WriteLine([|$""{""Level 5""} {""Number 3""}""|]); + Console.WriteLine($""{""Level 5""} {""Number 3""}""); + } +}"; + + var expected = + @"using System; +class TestClass +{ + private const string {|Rename:Value|} = $""{""Level 5""} {""Number 3""}""; + static void Test(string[] args) { Console.WriteLine(Value); From 219f1551a6e16e6a116596eb4db8546b93414715 Mon Sep 17 00:00:00 2001 From: Kevin Sun Date: Wed, 19 Aug 2020 12:51:08 -0700 Subject: [PATCH 7/7] Update SemanticModelAPITests.cs --- .../Compilation/SemanticModelAPITests.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs index ff6000cbc9f76..d829cb3d3a8ff 100644 --- a/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs @@ -4048,6 +4048,62 @@ static void Main(string[] args) Assert.False(model.GetConstantValue(actual[1]).HasValue); } + [Fact] + public void ConstantValueOfInterpolatedStringComplex() + { + var source = @" +class Program +{ + static void Main(string[] args) + { + const string S1 = $""Number 3""; + Console.WriteLine($""{""Level 5""} {S1}""); + Console.WriteLine($""Level {5} {S1}""); + } + + void M(string S1 = $""Testing"", Namae n = null) + { + if (n is Namae { X : $""ConstantInterpolatedString""}){ + switch(S1){ + case $""Level 5"": + break; + case $""Radio Noise"": + goto case $""Level 5""; + } + } + S1 = S0; + } +}"; + + var comp = CreateCompilation(source, options: TestOptions.ReleaseExe); + var tree = comp.SyntaxTrees.Single(); + var model = comp.GetSemanticModel(tree); + + var actual = tree.GetRoot().DescendantNodes().OfType().ToArray(); + Assert.Equal(@"$""Number 3""", actual[0].ToString()); + Assert.Equal("Number 3", model.GetConstantValue(actual[0]).Value); + + Assert.Equal(@"$""{""Level 5""} {S1}""", actual[1].ToString()); + Assert.Equal("Level 5 Number 3", model.GetConstantValue(actual[1]).Value); + + Assert.False(model.GetConstantValue(actual[2]).HasValue); + + Assert.Equal(@"$""Testing""", actual[3].ToString()); + Assert.Equal("Testing", model.GetConstantValue(actual[3]).Value); + + Assert.Equal(@"$""ConstantInterpolatedString""", actual[4].ToString()); + Assert.Equal("ConstantInterpolatedString", model.GetConstantValue(actual[4]).Value); + + Assert.Equal(@"$""Level 5""", actual[5].ToString()); + Assert.Equal("Level 5", model.GetConstantValue(actual[5]).Value); + + Assert.Equal(@"$""Radio Noise""", actual[6].ToString()); + Assert.Equal("Radio Noise", model.GetConstantValue(actual[6]).Value); + + Assert.Equal(@"$""Level 5""", actual[7].ToString()); + Assert.Equal("Level 5", model.GetConstantValue(actual[7]).Value); + } + [WorkItem(814, "https://github.com/dotnet/roslyn/issues/814")] [Fact] public void TypeOfDynamic()