|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Linq; |
| 6 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Test.Utilities; |
| 8 | +using Microsoft.CodeAnalysis.Test.Utilities; |
| 9 | +using Roslyn.Test.Utilities; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.CodeAnalysis.CSharp.UnitTests; |
| 13 | + |
| 14 | +public class TargetTypedSwitchExpressionTests : CSharpTestBase |
| 15 | +{ |
| 16 | + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81022")] |
| 17 | + public void ErrorRecovery_Return() |
| 18 | + { |
| 19 | + var source = """ |
| 20 | + class C |
| 21 | + { |
| 22 | + C M(int i) |
| 23 | + { |
| 24 | + return i switch |
| 25 | + { |
| 26 | + 1 => new(a), |
| 27 | + _ => default, |
| 28 | + }; |
| 29 | + } |
| 30 | + } |
| 31 | + """; |
| 32 | + |
| 33 | + var comp = CreateCompilation(source); |
| 34 | + comp.VerifyDiagnostics( |
| 35 | + // (7,22): error CS0103: The name 'a' does not exist in the current context |
| 36 | + // 1 => new(a), |
| 37 | + Diagnostic(ErrorCode.ERR_NameNotInContext, "a").WithArguments("a").WithLocation(7, 22)); |
| 38 | + |
| 39 | + var tree = comp.SyntaxTrees.First(); |
| 40 | + var model = comp.GetSemanticModel(tree); |
| 41 | + |
| 42 | + var switchExpression = tree.GetCompilationUnitRoot().DescendantNodes().OfType<SwitchExpressionSyntax>().Single(); |
| 43 | + var typeInfo = model.GetTypeInfo(switchExpression); |
| 44 | + Assert.Null(typeInfo.Type); |
| 45 | + Assert.Equal("C", typeInfo.ConvertedType.ToTestDisplayString()); |
| 46 | + |
| 47 | + var objectCreationExpression = switchExpression.DescendantNodes().OfType<ImplicitObjectCreationExpressionSyntax>().Single(); |
| 48 | + var objectCreationExpressionTypeInfo = model.GetTypeInfo(objectCreationExpression); |
| 49 | + Assert.Equal("C", objectCreationExpressionTypeInfo.Type.ToTestDisplayString()); |
| 50 | + Assert.Equal("C", objectCreationExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 51 | + var objectCreationExpressionSymbolInfo = model.GetSymbolInfo(objectCreationExpression); |
| 52 | + Assert.Null(objectCreationExpressionSymbolInfo.Symbol); |
| 53 | + Assert.Equal(CandidateReason.OverloadResolutionFailure, objectCreationExpressionSymbolInfo.CandidateReason); |
| 54 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolInfo.CandidateSymbols.ToTestDisplayStrings()); |
| 55 | + var objectCreationExpressionSymbolGroup = model.GetMemberGroup(objectCreationExpression); |
| 56 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolGroup.ToTestDisplayStrings()); |
| 57 | + |
| 58 | + var defaultLiteralExpression = switchExpression.DescendantNodes().OfType<LiteralExpressionSyntax>().Single(l => l.IsKind(SyntaxKind.DefaultLiteralExpression)); |
| 59 | + var defaultLiteralExpressionTypeInfo = model.GetTypeInfo(defaultLiteralExpression); |
| 60 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.Type.ToTestDisplayString()); |
| 61 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81022")] |
| 65 | + public void ErrorRecovery_VariableDeclaration() |
| 66 | + { |
| 67 | + var source = """ |
| 68 | + class C |
| 69 | + { |
| 70 | + void M(int i) |
| 71 | + { |
| 72 | + C c = i switch |
| 73 | + { |
| 74 | + 1 => new(a), |
| 75 | + _ => default, |
| 76 | + }; |
| 77 | + } |
| 78 | + } |
| 79 | + """; |
| 80 | + |
| 81 | + var comp = CreateCompilation(source); |
| 82 | + comp.VerifyDiagnostics( |
| 83 | + // (7,22): error CS0103: The name 'a' does not exist in the current context |
| 84 | + // 1 => new(a), |
| 85 | + Diagnostic(ErrorCode.ERR_NameNotInContext, "a").WithArguments("a").WithLocation(7, 22)); |
| 86 | + |
| 87 | + var tree = comp.SyntaxTrees.First(); |
| 88 | + var model = comp.GetSemanticModel(tree); |
| 89 | + |
| 90 | + var switchExpression = tree.GetCompilationUnitRoot().DescendantNodes().OfType<SwitchExpressionSyntax>().Single(); |
| 91 | + var typeInfo = model.GetTypeInfo(switchExpression); |
| 92 | + Assert.Null(typeInfo.Type); |
| 93 | + Assert.Equal("C", typeInfo.ConvertedType.ToTestDisplayString()); |
| 94 | + |
| 95 | + var objectCreationExpression = switchExpression.DescendantNodes().OfType<ImplicitObjectCreationExpressionSyntax>().Single(); |
| 96 | + var objectCreationExpressionTypeInfo = model.GetTypeInfo(objectCreationExpression); |
| 97 | + Assert.Equal("C", objectCreationExpressionTypeInfo.Type.ToTestDisplayString()); |
| 98 | + Assert.Equal("C", objectCreationExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 99 | + var objectCreationExpressionSymbolInfo = model.GetSymbolInfo(objectCreationExpression); |
| 100 | + Assert.Null(objectCreationExpressionSymbolInfo.Symbol); |
| 101 | + Assert.Equal(CandidateReason.OverloadResolutionFailure, objectCreationExpressionSymbolInfo.CandidateReason); |
| 102 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolInfo.CandidateSymbols.ToTestDisplayStrings()); |
| 103 | + var objectCreationExpressionSymbolGroup = model.GetMemberGroup(objectCreationExpression); |
| 104 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolGroup.ToTestDisplayStrings()); |
| 105 | + |
| 106 | + var defaultLiteralExpression = switchExpression.DescendantNodes().OfType<LiteralExpressionSyntax>().Single(l => l.IsKind(SyntaxKind.DefaultLiteralExpression)); |
| 107 | + var defaultLiteralExpressionTypeInfo = model.GetTypeInfo(defaultLiteralExpression); |
| 108 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.Type.ToTestDisplayString()); |
| 109 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81022")] |
| 113 | + public void ErrorRecovery_Assignment() |
| 114 | + { |
| 115 | + var source = """ |
| 116 | + class C |
| 117 | + { |
| 118 | + void M(int i) |
| 119 | + { |
| 120 | + C c; |
| 121 | + c = i switch |
| 122 | + { |
| 123 | + 1 => new(a), |
| 124 | + _ => default, |
| 125 | + }; |
| 126 | + } |
| 127 | + } |
| 128 | + """; |
| 129 | + |
| 130 | + var comp = CreateCompilation(source); |
| 131 | + comp.VerifyDiagnostics( |
| 132 | + // (8,22): error CS0103: The name 'a' does not exist in the current context |
| 133 | + // 1 => new(a), |
| 134 | + Diagnostic(ErrorCode.ERR_NameNotInContext, "a").WithArguments("a").WithLocation(8, 22)); |
| 135 | + |
| 136 | + var tree = comp.SyntaxTrees.First(); |
| 137 | + var model = comp.GetSemanticModel(tree); |
| 138 | + |
| 139 | + var switchExpression = tree.GetCompilationUnitRoot().DescendantNodes().OfType<SwitchExpressionSyntax>().Single(); |
| 140 | + var typeInfo = model.GetTypeInfo(switchExpression); |
| 141 | + Assert.Null(typeInfo.Type); |
| 142 | + Assert.Equal("C", typeInfo.ConvertedType.ToTestDisplayString()); |
| 143 | + |
| 144 | + var objectCreationExpression = switchExpression.DescendantNodes().OfType<ImplicitObjectCreationExpressionSyntax>().Single(); |
| 145 | + var objectCreationExpressionTypeInfo = model.GetTypeInfo(objectCreationExpression); |
| 146 | + Assert.Equal("C", objectCreationExpressionTypeInfo.Type.ToTestDisplayString()); |
| 147 | + Assert.Equal("C", objectCreationExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 148 | + var objectCreationExpressionSymbolInfo = model.GetSymbolInfo(objectCreationExpression); |
| 149 | + Assert.Null(objectCreationExpressionSymbolInfo.Symbol); |
| 150 | + Assert.Equal(CandidateReason.OverloadResolutionFailure, objectCreationExpressionSymbolInfo.CandidateReason); |
| 151 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolInfo.CandidateSymbols.ToTestDisplayStrings()); |
| 152 | + var objectCreationExpressionSymbolGroup = model.GetMemberGroup(objectCreationExpression); |
| 153 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolGroup.ToTestDisplayStrings()); |
| 154 | + |
| 155 | + var defaultLiteralExpression = switchExpression.DescendantNodes().OfType<LiteralExpressionSyntax>().Single(l => l.IsKind(SyntaxKind.DefaultLiteralExpression)); |
| 156 | + var defaultLiteralExpressionTypeInfo = model.GetTypeInfo(defaultLiteralExpression); |
| 157 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.Type.ToTestDisplayString()); |
| 158 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 159 | + } |
| 160 | + |
| 161 | + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81022")] |
| 162 | + public void ErrorRecovery_Call() |
| 163 | + { |
| 164 | + var source = """ |
| 165 | + class C |
| 166 | + { |
| 167 | + void M(int i) |
| 168 | + { |
| 169 | + N(i switch |
| 170 | + { |
| 171 | + 1 => new(a), |
| 172 | + _ => default, |
| 173 | + }); |
| 174 | + } |
| 175 | +
|
| 176 | + void N(C c) { } |
| 177 | + } |
| 178 | + """; |
| 179 | + |
| 180 | + var comp = CreateCompilation(source); |
| 181 | + comp.VerifyDiagnostics( |
| 182 | + // (7,18): error CS1729: 'C' does not contain a constructor that takes 1 arguments |
| 183 | + // 1 => new(a), |
| 184 | + Diagnostic(ErrorCode.ERR_BadCtorArgCount, "new(a)").WithArguments("C", "1").WithLocation(7, 18), |
| 185 | + // (7,22): error CS0103: The name 'a' does not exist in the current context |
| 186 | + // 1 => new(a), |
| 187 | + Diagnostic(ErrorCode.ERR_NameNotInContext, "a").WithArguments("a").WithLocation(7, 22)); |
| 188 | + |
| 189 | + var tree = comp.SyntaxTrees.First(); |
| 190 | + var model = comp.GetSemanticModel(tree); |
| 191 | + |
| 192 | + var switchExpression = tree.GetCompilationUnitRoot().DescendantNodes().OfType<SwitchExpressionSyntax>().Single(); |
| 193 | + var typeInfo = model.GetTypeInfo(switchExpression); |
| 194 | + Assert.Null(typeInfo.Type); |
| 195 | + Assert.Equal("C", typeInfo.ConvertedType.ToTestDisplayString()); |
| 196 | + |
| 197 | + var objectCreationExpression = switchExpression.DescendantNodes().OfType<ImplicitObjectCreationExpressionSyntax>().Single(); |
| 198 | + var objectCreationExpressionTypeInfo = model.GetTypeInfo(objectCreationExpression); |
| 199 | + Assert.Equal("C", objectCreationExpressionTypeInfo.Type.ToTestDisplayString()); |
| 200 | + Assert.Equal("C", objectCreationExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 201 | + var objectCreationExpressionSymbolInfo = model.GetSymbolInfo(objectCreationExpression); |
| 202 | + Assert.Null(objectCreationExpressionSymbolInfo.Symbol); |
| 203 | + Assert.Equal(CandidateReason.OverloadResolutionFailure, objectCreationExpressionSymbolInfo.CandidateReason); |
| 204 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolInfo.CandidateSymbols.ToTestDisplayStrings()); |
| 205 | + var objectCreationExpressionSymbolGroup = model.GetMemberGroup(objectCreationExpression); |
| 206 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolGroup.ToTestDisplayStrings()); |
| 207 | + |
| 208 | + var defaultLiteralExpression = switchExpression.DescendantNodes().OfType<LiteralExpressionSyntax>().Single(l => l.IsKind(SyntaxKind.DefaultLiteralExpression)); |
| 209 | + var defaultLiteralExpressionTypeInfo = model.GetTypeInfo(defaultLiteralExpression); |
| 210 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.Type.ToTestDisplayString()); |
| 211 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 212 | + } |
| 213 | + |
| 214 | + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81022")] |
| 215 | + public void ErrorRecovery_Cast() |
| 216 | + { |
| 217 | + var source = """ |
| 218 | + class C |
| 219 | + { |
| 220 | + void M(int i) |
| 221 | + { |
| 222 | + var c = (C)(i switch |
| 223 | + { |
| 224 | + 1 => new(a), |
| 225 | + _ => default, |
| 226 | + }); |
| 227 | + } |
| 228 | + } |
| 229 | + """; |
| 230 | + |
| 231 | + var comp = CreateCompilation(source); |
| 232 | + comp.VerifyDiagnostics( |
| 233 | + // (7,18): error CS1729: 'C' does not contain a constructor that takes 1 arguments |
| 234 | + // 1 => new(a), |
| 235 | + Diagnostic(ErrorCode.ERR_BadCtorArgCount, "new(a)").WithArguments("C", "1").WithLocation(7, 18), |
| 236 | + // (7,22): error CS0103: The name 'a' does not exist in the current context |
| 237 | + // 1 => new(a), |
| 238 | + Diagnostic(ErrorCode.ERR_NameNotInContext, "a").WithArguments("a").WithLocation(7, 22)); |
| 239 | + |
| 240 | + var tree = comp.SyntaxTrees.First(); |
| 241 | + var model = comp.GetSemanticModel(tree); |
| 242 | + |
| 243 | + var switchExpression = tree.GetCompilationUnitRoot().DescendantNodes().OfType<SwitchExpressionSyntax>().Single(); |
| 244 | + var typeInfo = model.GetTypeInfo(switchExpression); |
| 245 | + Assert.Null(typeInfo.Type); |
| 246 | + Assert.Null(typeInfo.ConvertedType); |
| 247 | + |
| 248 | + var objectCreationExpression = switchExpression.DescendantNodes().OfType<ImplicitObjectCreationExpressionSyntax>().Single(); |
| 249 | + var objectCreationExpressionTypeInfo = model.GetTypeInfo(objectCreationExpression); |
| 250 | + Assert.Equal("C", objectCreationExpressionTypeInfo.Type.ToTestDisplayString()); |
| 251 | + Assert.Equal("C", objectCreationExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 252 | + var objectCreationExpressionSymbolInfo = model.GetSymbolInfo(objectCreationExpression); |
| 253 | + Assert.Null(objectCreationExpressionSymbolInfo.Symbol); |
| 254 | + Assert.Equal(CandidateReason.OverloadResolutionFailure, objectCreationExpressionSymbolInfo.CandidateReason); |
| 255 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolInfo.CandidateSymbols.ToTestDisplayStrings()); |
| 256 | + var objectCreationExpressionSymbolGroup = model.GetMemberGroup(objectCreationExpression); |
| 257 | + AssertEx.SetEqual(["C..ctor()"], objectCreationExpressionSymbolGroup.ToTestDisplayStrings()); |
| 258 | + |
| 259 | + var defaultLiteralExpression = switchExpression.DescendantNodes().OfType<LiteralExpressionSyntax>().Single(l => l.IsKind(SyntaxKind.DefaultLiteralExpression)); |
| 260 | + var defaultLiteralExpressionTypeInfo = model.GetTypeInfo(defaultLiteralExpression); |
| 261 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.Type.ToTestDisplayString()); |
| 262 | + Assert.Equal("C", defaultLiteralExpressionTypeInfo.ConvertedType.ToTestDisplayString()); |
| 263 | + } |
| 264 | +} |
0 commit comments