Skip to content

Commit b6cc211

Browse files
committed
Add couple more tests
1 parent 5a332c4 commit b6cc211

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

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

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,25 +3937,51 @@ static class Extensions
39373937
);
39383938
}
39393939

3940-
[Fact]
3941-
public void ReceiverParameter_RefScope()
3940+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78491")]
3941+
public void ReceiverParameter_RefScope_01()
39423942
{
39433943
var src = """
3944+
int i = 42;
3945+
i.M();
3946+
39443947
static class Extensions
39453948
{
39463949
extension(scoped ref int receiver)
39473950
{
3951+
public void M() => System.Console.Write(receiver);
39483952
}
39493953
}
39503954
""";
39513955
var comp = CreateCompilation(src);
39523956

3953-
CompileAndVerify(comp, symbolValidator: (m) =>
3957+
CompileAndVerify(comp, expectedOutput: "42", symbolValidator: (m) =>
39543958
{
39553959
AssertEx.Equal(ScopedKind.ScopedRef, m.GlobalNamespace.GetMember<MethodSymbol>("Extensions.<>E__0.<Extension>$").Parameters[0].EffectiveScope);
39563960
}).VerifyDiagnostics();
39573961
}
39583962

3963+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78491")]
3964+
public void ReceiverParameter_RefScope_02()
3965+
{
3966+
var src = """
3967+
int i = 42;
3968+
i.M();
3969+
3970+
static class Extensions
3971+
{
3972+
extension(scoped ref int receiver)
3973+
{
3974+
public ref int M() => ref receiver;
3975+
}
3976+
}
3977+
""";
3978+
var comp = CreateCompilation(src);
3979+
comp.VerifyEmitDiagnostics(
3980+
// (8,35): error CS9075: Cannot return a parameter by reference 'receiver' because it is scoped to the current method
3981+
// public ref int M() => ref receiver;
3982+
Diagnostic(ErrorCode.ERR_RefReturnScopedParameter, "receiver").WithArguments("receiver").WithLocation(8, 35));
3983+
}
3984+
39593985
[Fact]
39603986
public void ReceiverParameter_Nullability()
39613987
{
@@ -40019,4 +40045,32 @@ static class E
4001940045
var model = comp.GetSemanticModel(tree);
4002040046
Assert.Equal(["(T, null)", "(T, T)"], PrintXmlNameSymbols(tree, model));
4002140047
}
40048+
40049+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78487")]
40050+
public void Async_01()
40051+
{
40052+
string source = """
40053+
await System.Threading.Tasks.Task.FromResult(true).M();
40054+
await System.Threading.Tasks.Task<bool>.M2();
40055+
40056+
static class E
40057+
{
40058+
extension<T>(System.Threading.Tasks.Task<T> source)
40059+
{
40060+
public async System.Threading.Tasks.Task M()
40061+
{
40062+
System.Console.Write(await source);
40063+
System.Console.Write(" ran ");
40064+
}
40065+
40066+
public static async System.Threading.Tasks.Task M2()
40067+
{
40068+
await System.Threading.Tasks.Task.FromResult(default(T));
40069+
System.Console.Write("ran2");
40070+
}
40071+
}
4002240072
}
40073+
""";
40074+
var comp = CreateCompilation(source);
40075+
CompileAndVerify(comp, expectedOutput: "True ran ran2").VerifyDiagnostics();
40076+
}}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,5 +497,57 @@ public static class E
497497
var comp = CreateCompilation([src, OverloadResolutionPriorityAttributeDefinition]);
498498
CompileAndVerify(comp, expectedOutput: "property").VerifyDiagnostics();
499499
}
500+
501+
[Fact]
502+
public void AnonymousType_01()
503+
{
504+
var src = """
505+
var person = new { Name = "John", Age = 30 };
506+
person.M();
507+
person.M2();
508+
_ = person.P;
509+
510+
public static class E
511+
{
512+
extension<T>(T t)
513+
{
514+
public void M() { System.Console.Write("method "); }
515+
public int Property { get { System.Console.Write("property"); return 42; } }
516+
}
517+
518+
public static void M2<T>(this T t) { System.Console.Write("method2 "); }
519+
}
520+
""";
521+
// Tracked by https://github.com/dotnet/roslyn/issues/76130 : should work
522+
var comp = CreateCompilation(src);
523+
comp.VerifyEmitDiagnostics(
524+
// (4,12): error CS1061: '<anonymous type: string Name, int Age>' does not contain a definition for 'P' and no accessible extension method 'P' accepting a first argument of type '<anonymous type: string Name, int Age>' could be found (are you missing a using directive or an assembly reference?)
525+
// _ = person.P;
526+
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "P").WithArguments("<anonymous type: string Name, int Age>", "P").WithLocation(4, 12));
527+
}
528+
529+
[Fact]
530+
public void Attribute_01()
531+
{
532+
var src = """
533+
[My(Property = 42)]
534+
class C { }
535+
536+
public class MyAttribute : System.Attribute { }
537+
538+
public static class E
539+
{
540+
extension(MyAttribute a)
541+
{
542+
public int Property { get => throw null; set => throw null; }
543+
}
544+
}
545+
""";
546+
var comp = CreateCompilation(src);
547+
comp.VerifyEmitDiagnostics(
548+
// (1,5): error CS0246: The type or namespace name 'Property' could not be found (are you missing a using directive or an assembly reference?)
549+
// [My(Property = 42)]
550+
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "Property").WithArguments("Property").WithLocation(1, 5));
551+
}
500552
}
501553

0 commit comments

Comments
 (0)