Skip to content

Commit 5a332c4

Browse files
committed
Add tests
1 parent f5025cf commit 5a332c4

File tree

2 files changed

+322
-0
lines changed

2 files changed

+322
-0
lines changed

src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38206,6 +38206,70 @@ static class E
3820638206
// (13,73): error CS0103: The name 'P' does not exist in the current context
3820738207
// [System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(P))]
3820838208
Diagnostic(ErrorCode.ERR_NameNotInContext, "P").WithArguments("P").WithLocation(13, 73));
38209+
38210+
src = """
38211+
#nullable enable
38212+
38213+
object o = new object();
38214+
if (o.M())
38215+
o.P.ToString(); // 1
38216+
else
38217+
o.P.ToString(); // 2
38218+
38219+
static class E
38220+
{
38221+
extension(object o)
38222+
{
38223+
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(object.P))]
38224+
public bool M() => throw null!;
38225+
38226+
public object? P => null;
38227+
}
38228+
}
38229+
""";
38230+
comp = CreateCompilation(src, targetFramework: TargetFramework.Net90);
38231+
comp.VerifyEmitDiagnostics(
38232+
// (5,5): warning CS8602: Dereference of a possibly null reference.
38233+
// o.P.ToString(); // 1
38234+
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(5, 5),
38235+
// (7,5): warning CS8602: Dereference of a possibly null reference.
38236+
// o.P.ToString(); // 2
38237+
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(7, 5),
38238+
// (13,73): error CS9286: 'object' does not contain a definition for 'P' and no accessible extension member 'P' for receiver of type 'object' could be found (are you missing a using directive or an assembly reference?)
38239+
// [System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(object.P))]
38240+
Diagnostic(ErrorCode.ERR_ExtensionResolutionFailed, "object.P").WithArguments("object", "P").WithLocation(13, 73));
38241+
38242+
src = """
38243+
#nullable enable
38244+
38245+
object o = new object();
38246+
if (o.M())
38247+
o.P.ToString(); // 1
38248+
else
38249+
o.P.ToString(); // 2
38250+
38251+
static class E
38252+
{
38253+
extension(object o)
38254+
{
38255+
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(new object().P))]
38256+
public bool M() => throw null!;
38257+
38258+
public object? P => null;
38259+
}
38260+
}
38261+
""";
38262+
comp = CreateCompilation(src, targetFramework: TargetFramework.Net90);
38263+
comp.VerifyEmitDiagnostics(
38264+
// (5,5): warning CS8602: Dereference of a possibly null reference.
38265+
// o.P.ToString(); // 1
38266+
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(5, 5),
38267+
// (7,5): warning CS8602: Dereference of a possibly null reference.
38268+
// o.P.ToString(); // 2
38269+
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(7, 5),
38270+
// (13,73): error CS8082: Sub-expression cannot be used in an argument to nameof.
38271+
// [System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(new object().P))]
38272+
Diagnostic(ErrorCode.ERR_SubexpressionNotInNameof, "new object()").WithLocation(13, 73));
3820938273
}
3821038274

3821138275
[Fact]

src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests2.cs

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44
#nullable disable
55

6+
using System;
7+
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
68
using Microsoft.CodeAnalysis.Test.Utilities;
79
using Roslyn.Test.Utilities;
810
using Xunit;
@@ -239,5 +241,261 @@ static class E
239241
// null.M2("");
240242
Diagnostic(ErrorCode.ERR_BadUnaryOp, "null.M2").WithArguments(".", "<null>").WithLocation(2, 1));
241243
}
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+
}
242500
}
243501

0 commit comments

Comments
 (0)