Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for #37309 and #58472 #58752

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6445,5 +6445,91 @@ class C2
Diagnostic(ErrorCode.ERR_DeprecatedSymbolStr, "(i, c) = new C2()").WithArguments("C.implicit operator bool(C)", "Obsolete error").WithLocation(6, 1)
);
}

[Fact, WorkItem(58472, "https://github.com/dotnet/roslyn/issues/58472")]
public void DeconstructionIntoImplicitIndexers()
{
var source = @"
var x = new int[1];
C.M(x);

var y = new int[1];
C.M2(y);

System.Console.Write((x[^1], y[^1]));

class C
{
public static void M<T>(T[] a)
{
(a[0], a[^1]) = (default, default);
}

public static void M2(int[] a)
{
(a[0], a[^1]) = (default, default);
}
}
";

var comp = CreateCompilationWithIndex(source);
// No IndexOutOfRangeException thrown
var verifier = CompileAndVerify(comp, expectedOutput: "(0, 0)");
verifier.VerifyDiagnostics();
verifier.VerifyIL("C.M<T>(T[])", @"
{
// Code size 41 (0x29)
.maxstack 3
.locals init (T[] V_0,
int V_1,
T V_2)
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: dup
IL_0003: stloc.0
IL_0004: ldlen
IL_0005: conv.i4
IL_0006: ldc.i4.1
IL_0007: sub
IL_0008: stloc.1
IL_0009: ldc.i4.0
IL_000a: ldloca.s V_2
IL_000c: initobj ""T""
IL_0012: ldloc.2
IL_0013: stelem ""T""
IL_0018: ldloc.0
IL_0019: ldloc.1
IL_001a: ldloca.s V_2
IL_001c: initobj ""T""
IL_0022: ldloc.2
IL_0023: stelem ""T""
IL_0028: ret
}
");
verifier.VerifyIL("C.M2", @"
{
// Code size 25 (0x19)
.maxstack 3
.locals init (int& V_0)
IL_0000: ldarg.0
IL_0001: ldc.i4.0
IL_0002: ldelema ""int""
IL_0007: stloc.0
IL_0008: ldarg.0
IL_0009: dup
IL_000a: ldlen
IL_000b: conv.i4
IL_000c: ldc.i4.1
IL_000d: sub
IL_000e: ldelema ""int""
IL_0013: ldloc.0
IL_0014: ldc.i4.0
IL_0015: stind.i4
IL_0016: ldc.i4.0
IL_0017: stind.i4
IL_0018: ret
}
");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10390,12 +10390,10 @@ void M()
compilation.VerifyDiagnostics();
}

[Fact]
[Fact, WorkItem(37309, "https://github.com/dotnet/roslyn/issues/37309")]
public void NonNullTypesTrue_Foreach()
{
var source = @"


class C
{
#nullable enable
Expand Down Expand Up @@ -10459,22 +10457,34 @@ public void M2()

compilation.VerifyTypes();
compilation.VerifyDiagnostics(
// (60,11): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (58,11): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// string?[] FalseNCollection() => throw null!; // 5
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(60, 11),
// (16,13): warning CS8602: Dereference of a possibly null reference.
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(58, 11),
// (14,13): warning CS8602: Dereference of a possibly null reference.
// ns /*T:string?*/ .ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns").WithLocation(16, 13),
// (26,13): warning CS8602: Dereference of a possibly null reference.
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns").WithLocation(14, 13),
// (24,13): warning CS8602: Dereference of a possibly null reference.
// ns1 /*T:string?*/ .ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns1").WithLocation(26, 13),
// (36,13): warning CS8602: Dereference of a possibly null reference.
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns1").WithLocation(24, 13),
// (34,13): warning CS8602: Dereference of a possibly null reference.
// ns /*T:string?*/ .ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns").WithLocation(36, 13),
// (46,13): warning CS8602: Dereference of a possibly null reference.
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns").WithLocation(34, 13),
// (44,13): warning CS8602: Dereference of a possibly null reference.
// ns1 /*T:string?*/ .ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns1").WithLocation(46, 13)
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "ns1").WithLocation(44, 13)
);

var tree = compilation.SyntaxTrees.Single();
var model = compilation.GetSemanticModel(tree);
var foreachSyntax = tree.GetRoot().DescendantNodes().OfType<ForEachStatementSyntax>().ToArray();

var type1 = foreachSyntax[0].Type;
Assert.Equal("string", type1.ToString());
Assert.Equal("System.String!", model.GetTypeInfo(type1).Type.ToTestDisplayString(includeNonNullable: true));

var type2 = foreachSyntax[1].Type;
Assert.Equal("string?", type2.ToString());
Assert.Equal("System.String?", model.GetTypeInfo(type2).Type.ToTestDisplayString(includeNonNullable: true));
}

[WorkItem(30840, "https://github.com/dotnet/roslyn/issues/30840")]
Expand Down