|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 | #nullable disable
|
5 | 5 |
|
| 6 | +using System; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Test.Utilities; |
6 | 8 | using Microsoft.CodeAnalysis.Test.Utilities;
|
7 | 9 | using Roslyn.Test.Utilities;
|
8 | 10 | using Xunit;
|
@@ -239,5 +241,261 @@ static class E
|
239 | 241 | // null.M2("");
|
240 | 242 | Diagnostic(ErrorCode.ERR_BadUnaryOp, "null.M2").WithArguments(".", "<null>").WithLocation(2, 1));
|
241 | 243 | }
|
| 244 | + |
| 245 | + [Fact] |
| 246 | + public void RemoveLowerPriorityMembers_Deconstruct() |
| 247 | + { |
| 248 | + var src = """ |
| 249 | +var (x, y) = ""; |
| 250 | +
|
| 251 | +public static class E |
| 252 | +{ |
| 253 | + extension(object o) |
| 254 | + { |
| 255 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 256 | + public void Deconstruct(out int i2, out int i3) { System.Console.Write("ran"); i2 = i3 = 43; } |
| 257 | + } |
| 258 | + extension(string s) |
| 259 | + { |
| 260 | + public void Deconstruct(out int i2, out int i3) => throw null; |
| 261 | + } |
| 262 | +} |
| 263 | +"""; |
| 264 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 265 | + CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics(); |
| 266 | + |
| 267 | + src = """ |
| 268 | +var (x, y) = ""; |
| 269 | +
|
| 270 | +public static class E |
| 271 | +{ |
| 272 | + extension(object o) |
| 273 | + { |
| 274 | + public void Deconstruct(out int i2, out int i3) => throw null; |
| 275 | + } |
| 276 | + extension(string s) |
| 277 | + { |
| 278 | + public void Deconstruct(out int i2, out int i3) { System.Console.Write("ran"); i2 = i3 = 43; } |
| 279 | + } |
| 280 | +} |
| 281 | +"""; |
| 282 | + comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 283 | + CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics(); |
| 284 | + } |
| 285 | + |
| 286 | + [Fact] |
| 287 | + public void RemoveLowerPriorityMembers_Foreach_GetEnumerator() |
| 288 | + { |
| 289 | + var src = """ |
| 290 | +using System.Collections.Generic; |
| 291 | +
|
| 292 | +foreach (var x in new C()) { System.Console.Write(x); } |
| 293 | +
|
| 294 | +public class C { } |
| 295 | +
|
| 296 | +public static class E |
| 297 | +{ |
| 298 | + extension(object o) |
| 299 | + { |
| 300 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 301 | + public IEnumerator<int> GetEnumerator() { yield return 42; } |
| 302 | + } |
| 303 | +
|
| 304 | + extension(C c) |
| 305 | + { |
| 306 | + public IEnumerator<int> GetEnumerator() => throw null; |
| 307 | + } |
| 308 | +} |
| 309 | +"""; |
| 310 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 311 | + try |
| 312 | + { |
| 313 | + // Tracked by https://github.com/dotnet/roslyn/issues/76130 : assertion in NullableWalker |
| 314 | + CompileAndVerify(comp, expectedOutput: "42").VerifyDiagnostics(); |
| 315 | + } |
| 316 | + catch (InvalidOperationException) |
| 317 | + { |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + [Fact] |
| 322 | + public void RemoveLowerPriorityMembers_CollectionInitializer() |
| 323 | + { |
| 324 | + var src = """ |
| 325 | +using System.Collections; |
| 326 | +using System.Collections.Generic; |
| 327 | +
|
| 328 | +_ = new C() { 42 }; |
| 329 | +
|
| 330 | +public class C : IEnumerable<int>, IEnumerable |
| 331 | +{ |
| 332 | + IEnumerator<int> IEnumerable<int>.GetEnumerator() => throw null; |
| 333 | + IEnumerator IEnumerable.GetEnumerator() => throw null; |
| 334 | +} |
| 335 | +
|
| 336 | +public static class E |
| 337 | +{ |
| 338 | + extension(object o) |
| 339 | + { |
| 340 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 341 | + public void Add(int i) { System.Console.Write("add"); } |
| 342 | + } |
| 343 | +
|
| 344 | + extension(C c) |
| 345 | + { |
| 346 | + public void Add(int i) => throw null; |
| 347 | + } |
| 348 | +} |
| 349 | +"""; |
| 350 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 351 | + CompileAndVerify(comp, expectedOutput: "add").VerifyDiagnostics(); |
| 352 | + } |
| 353 | + |
| 354 | + [Fact] |
| 355 | + public void RemoveLowerPriorityMembers_Fixed() |
| 356 | + { |
| 357 | + var src = """ |
| 358 | +unsafe class C |
| 359 | +{ |
| 360 | + public static void Main() |
| 361 | + { |
| 362 | + fixed (int* p = new Fixable()) { } |
| 363 | + } |
| 364 | +} |
| 365 | +
|
| 366 | +public class Fixable { } |
| 367 | +
|
| 368 | +public static class E |
| 369 | +{ |
| 370 | + extension(object o) |
| 371 | + { |
| 372 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 373 | + public ref int GetPinnableReference() { System.Console.Write("ran"); return ref (new int[] { 1, 2, 3 })[0]; } |
| 374 | + } |
| 375 | +
|
| 376 | + extension(Fixable f) |
| 377 | + { |
| 378 | + public ref int GetPinnableReference() => throw null; |
| 379 | + } |
| 380 | +} |
| 381 | +"""; |
| 382 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition], options: TestOptions.UnsafeDebugExe); |
| 383 | + CompileAndVerify(comp, expectedOutput: "ran", verify: Verification.Skipped).VerifyDiagnostics(); |
| 384 | + } |
| 385 | + |
| 386 | + [Fact] |
| 387 | + public void RemoveLowerPriorityMembers_Await() |
| 388 | + { |
| 389 | + var src = """ |
| 390 | +using System; |
| 391 | +using System.Runtime.CompilerServices; |
| 392 | +
|
| 393 | +int i = await new C(); |
| 394 | +System.Console.Write(i); |
| 395 | +
|
| 396 | +public class C { } |
| 397 | +
|
| 398 | +public class D : INotifyCompletion |
| 399 | +{ |
| 400 | + public int GetResult() => 42; |
| 401 | + public void OnCompleted(Action continuation) => throw null; |
| 402 | + public bool IsCompleted => true; |
| 403 | +} |
| 404 | +
|
| 405 | +public static class E |
| 406 | +{ |
| 407 | + extension(object o) |
| 408 | + { |
| 409 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 410 | + public D GetAwaiter() => new D(); |
| 411 | + } |
| 412 | +
|
| 413 | + extension(C c) |
| 414 | + { |
| 415 | + public D GetAwaiter() => throw null; |
| 416 | + } |
| 417 | +} |
| 418 | +"""; |
| 419 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 420 | + CompileAndVerify(comp, expectedOutput: "42").VerifyDiagnostics(); |
| 421 | + } |
| 422 | + |
| 423 | + [Fact] |
| 424 | + public void RemoveLowerPriorityMembers_ObjectInitializer() |
| 425 | + { |
| 426 | + var src = """ |
| 427 | +_ = new C() { Property = 42 }; |
| 428 | +
|
| 429 | +public class C { } |
| 430 | +
|
| 431 | +public static class E |
| 432 | +{ |
| 433 | + extension(object o) |
| 434 | + { |
| 435 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 436 | + public int Property { set { System.Console.Write("property"); } } |
| 437 | + } |
| 438 | +
|
| 439 | + extension(C c) |
| 440 | + { |
| 441 | + public int Property => throw null; |
| 442 | + } |
| 443 | +} |
| 444 | +"""; |
| 445 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 446 | + CompileAndVerify(comp, expectedOutput: "property").VerifyDiagnostics(); |
| 447 | + } |
| 448 | + |
| 449 | + [Fact] |
| 450 | + public void RemoveLowerPriorityMembers_With() |
| 451 | + { |
| 452 | + var src = """ |
| 453 | +_ = new S() with { Property = 42 }; |
| 454 | +
|
| 455 | +public struct S { } |
| 456 | +
|
| 457 | +public static class E |
| 458 | +{ |
| 459 | + extension(object o) |
| 460 | + { |
| 461 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 462 | + public int Property { set { System.Console.Write("property"); } } |
| 463 | + } |
| 464 | +
|
| 465 | + extension(S s) |
| 466 | + { |
| 467 | + public int Property { set => throw null; } |
| 468 | + } |
| 469 | +} |
| 470 | +"""; |
| 471 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 472 | + CompileAndVerify(comp, expectedOutput: "property").VerifyDiagnostics(); |
| 473 | + } |
| 474 | + |
| 475 | + [Fact] |
| 476 | + public void RemoveLowerPriorityMembers_PropertyPattern() |
| 477 | + { |
| 478 | + var src = """ |
| 479 | +_ = new C() is { Property: 42 }; |
| 480 | +
|
| 481 | +public class C{ } |
| 482 | +
|
| 483 | +public static class E |
| 484 | +{ |
| 485 | + extension(object o) |
| 486 | + { |
| 487 | + [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] |
| 488 | + public int Property { get { System.Console.Write("property"); return 42; } } |
| 489 | + } |
| 490 | +
|
| 491 | + extension(C c) |
| 492 | + { |
| 493 | + public int Property => throw null; |
| 494 | + } |
| 495 | +} |
| 496 | +"""; |
| 497 | + var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]); |
| 498 | + CompileAndVerify(comp, expectedOutput: "property").VerifyDiagnostics(); |
| 499 | + } |
242 | 500 | }
|
243 | 501 |
|
0 commit comments