|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +using System.Collections.Immutable; |
5 | 6 | using System.Linq;
|
6 | 7 | using System.Reflection;
|
7 | 8 | using System.Runtime.InteropServices;
|
8 | 9 | using Microsoft.CodeAnalysis.CSharp.Symbols;
|
| 10 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
9 | 11 | using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
|
10 | 12 | using Microsoft.CodeAnalysis.Test.Utilities;
|
11 | 13 | using Roslyn.Test.Utilities;
|
@@ -1131,6 +1133,188 @@ static void verifyMetadata(ModuleSymbol module)
|
1131 | 1133 | }
|
1132 | 1134 | }
|
1133 | 1135 |
|
| 1136 | + [Fact] |
| 1137 | + public void GetDeclaredSymbol() |
| 1138 | + { |
| 1139 | + var source = (""" |
| 1140 | + partial class C |
| 1141 | + { |
| 1142 | + public partial event System.Action E, F; |
| 1143 | + public partial event System.Action E { add { } remove { } } |
| 1144 | + public partial event System.Action F { add { } remove { } } |
| 1145 | +
|
| 1146 | + public partial C(); |
| 1147 | + public partial C() { } |
| 1148 | + } |
| 1149 | + """, "Program.cs"); |
| 1150 | + |
| 1151 | + var comp = CreateCompilation(source).VerifyDiagnostics(); |
| 1152 | + var tree = comp.SyntaxTrees.Single(); |
| 1153 | + var model = comp.GetSemanticModel(tree); |
| 1154 | + |
| 1155 | + var eventDefs = tree.GetRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().ToImmutableArray(); |
| 1156 | + Assert.Equal(2, eventDefs.Length); |
| 1157 | + |
| 1158 | + var eventImpls = tree.GetRoot().DescendantNodes().OfType<EventDeclarationSyntax>().ToImmutableArray(); |
| 1159 | + Assert.Equal(2, eventImpls.Length); |
| 1160 | + |
| 1161 | + { |
| 1162 | + var defSymbol = (IEventSymbol)model.GetDeclaredSymbol(eventDefs[0])!; |
| 1163 | + Assert.Equal("event System.Action C.E", defSymbol.ToTestDisplayString()); |
| 1164 | + |
| 1165 | + IEventSymbol implSymbol = model.GetDeclaredSymbol(eventImpls[0])!; |
| 1166 | + Assert.Equal("event System.Action C.E", implSymbol.ToTestDisplayString()); |
| 1167 | + |
| 1168 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1169 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1170 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1171 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1172 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1173 | + |
| 1174 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1175 | + } |
| 1176 | + |
| 1177 | + { |
| 1178 | + var defSymbol = (IEventSymbol)model.GetDeclaredSymbol(eventDefs[1])!; |
| 1179 | + Assert.Equal("event System.Action C.F", defSymbol.ToTestDisplayString()); |
| 1180 | + |
| 1181 | + IEventSymbol implSymbol = model.GetDeclaredSymbol(eventImpls[1])!; |
| 1182 | + Assert.Equal("event System.Action C.F", implSymbol.ToTestDisplayString()); |
| 1183 | + |
| 1184 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1185 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1186 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1187 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1188 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1189 | + |
| 1190 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1191 | + } |
| 1192 | + |
| 1193 | + { |
| 1194 | + var ctors = tree.GetRoot().DescendantNodes().OfType<ConstructorDeclarationSyntax>().ToImmutableArray(); |
| 1195 | + Assert.Equal(2, ctors.Length); |
| 1196 | + |
| 1197 | + IMethodSymbol defSymbol = model.GetDeclaredSymbol(ctors[0])!; |
| 1198 | + Assert.Equal("C..ctor()", defSymbol.ToTestDisplayString()); |
| 1199 | + |
| 1200 | + IMethodSymbol implSymbol = model.GetDeclaredSymbol(ctors[1])!; |
| 1201 | + Assert.Equal("C..ctor()", implSymbol.ToTestDisplayString()); |
| 1202 | + |
| 1203 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1204 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1205 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1206 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1207 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1208 | + |
| 1209 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1210 | + } |
| 1211 | + } |
| 1212 | + |
| 1213 | + [Fact] |
| 1214 | + public void GetDeclaredSymbol_GenericContainer() |
| 1215 | + { |
| 1216 | + var source = (""" |
| 1217 | + partial class C<T> |
| 1218 | + { |
| 1219 | + public partial event System.Action E; |
| 1220 | + public partial event System.Action E { add { } remove { } } |
| 1221 | +
|
| 1222 | + public partial C(); |
| 1223 | + public partial C() { } |
| 1224 | + } |
| 1225 | + """, "Program.cs"); |
| 1226 | + |
| 1227 | + var comp = CreateCompilation(source).VerifyDiagnostics(); |
| 1228 | + var tree = comp.SyntaxTrees.Single(); |
| 1229 | + var model = comp.GetSemanticModel(tree); |
| 1230 | + |
| 1231 | + { |
| 1232 | + var defSymbol = (IEventSymbol)model.GetDeclaredSymbol(tree.GetRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().Single())!; |
| 1233 | + Assert.Equal("event System.Action C<T>.E", defSymbol.ToTestDisplayString()); |
| 1234 | + |
| 1235 | + IEventSymbol implSymbol = model.GetDeclaredSymbol(tree.GetRoot().DescendantNodes().OfType<EventDeclarationSyntax>().Single())!; |
| 1236 | + Assert.Equal("event System.Action C<T>.E", implSymbol.ToTestDisplayString()); |
| 1237 | + |
| 1238 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1239 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1240 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1241 | + |
| 1242 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1243 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1244 | + |
| 1245 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1246 | + |
| 1247 | + var intSymbol = comp.GetSpecialType(SpecialType.System_Int32); |
| 1248 | + var cOfTSymbol = defSymbol.ContainingType!; |
| 1249 | + var cOfIntSymbol = cOfTSymbol.Construct([intSymbol]); |
| 1250 | + |
| 1251 | + var defOfIntSymbol = (IEventSymbol)cOfIntSymbol.GetMember("E"); |
| 1252 | + Assert.Equal("event System.Action C<System.Int32>.E", defOfIntSymbol.ToTestDisplayString()); |
| 1253 | + Assert.Null(defOfIntSymbol.PartialImplementationPart); |
| 1254 | + Assert.False(defOfIntSymbol.IsPartialDefinition); |
| 1255 | + } |
| 1256 | + |
| 1257 | + { |
| 1258 | + var ctors = tree.GetRoot().DescendantNodes().OfType<ConstructorDeclarationSyntax>().ToImmutableArray(); |
| 1259 | + Assert.Equal(2, ctors.Length); |
| 1260 | + |
| 1261 | + IMethodSymbol defSymbol = model.GetDeclaredSymbol(ctors[0])!; |
| 1262 | + Assert.Equal("C<T>..ctor()", defSymbol.ToTestDisplayString()); |
| 1263 | + |
| 1264 | + IMethodSymbol implSymbol = model.GetDeclaredSymbol(ctors[1])!; |
| 1265 | + Assert.Equal("C<T>..ctor()", implSymbol.ToTestDisplayString()); |
| 1266 | + |
| 1267 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1268 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1269 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1270 | + |
| 1271 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1272 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1273 | + |
| 1274 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1275 | + |
| 1276 | + var intSymbol = comp.GetSpecialType(SpecialType.System_Int32); |
| 1277 | + var cOfTSymbol = defSymbol.ContainingType!; |
| 1278 | + var cOfIntSymbol = cOfTSymbol.Construct([intSymbol]); |
| 1279 | + |
| 1280 | + var defOfIntSymbol = (IMethodSymbol)cOfIntSymbol.GetMember(".ctor"); |
| 1281 | + Assert.Equal("C<System.Int32>..ctor()", defOfIntSymbol.ToTestDisplayString()); |
| 1282 | + Assert.Null(defOfIntSymbol.PartialImplementationPart); |
| 1283 | + Assert.False(defOfIntSymbol.IsPartialDefinition); |
| 1284 | + } |
| 1285 | + } |
| 1286 | + |
| 1287 | + [Fact] |
| 1288 | + public void GetDeclaredSymbol_ConstructorParameter() |
| 1289 | + { |
| 1290 | + var source = (""" |
| 1291 | + partial class C |
| 1292 | + { |
| 1293 | + public partial C(int i); |
| 1294 | + public partial C(int i) { } |
| 1295 | + } |
| 1296 | + """, "Program.cs"); |
| 1297 | + |
| 1298 | + var comp = CreateCompilation(source).VerifyDiagnostics(); |
| 1299 | + var tree = comp.SyntaxTrees.Single(); |
| 1300 | + var model = comp.GetSemanticModel(tree); |
| 1301 | + |
| 1302 | + var parameters = tree.GetRoot().DescendantNodes().OfType<ParameterSyntax>().ToArray(); |
| 1303 | + Assert.Equal(2, parameters.Length); |
| 1304 | + |
| 1305 | + IParameterSymbol defSymbol = model.GetDeclaredSymbol(parameters[0])!; |
| 1306 | + Assert.Equal("System.Int32 i", defSymbol.ToTestDisplayString()); |
| 1307 | + |
| 1308 | + IParameterSymbol implSymbol = model.GetDeclaredSymbol(parameters[1])!; |
| 1309 | + Assert.Equal("System.Int32 i", implSymbol.ToTestDisplayString()); |
| 1310 | + |
| 1311 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1312 | + Assert.Same(implSymbol, ((IMethodSymbol)defSymbol.ContainingSymbol).PartialImplementationPart!.Parameters.Single()); |
| 1313 | + Assert.Same(defSymbol, ((IMethodSymbol)implSymbol.ContainingSymbol).PartialDefinitionPart!.Parameters.Single()); |
| 1314 | + |
| 1315 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1316 | + } |
| 1317 | + |
1134 | 1318 | [Fact]
|
1135 | 1319 | public void SequencePoints()
|
1136 | 1320 | {
|
|
0 commit comments