未验证 提交 087195e7 编写于 作者: C Chris Sienkiewicz 提交者: GitHub

Fix struct layout error when nullable enabled: (#34128)

* Fix struct layout error when nullable enabled:
- Change type comparison to ignore nullable
- Add a test
- Fix an existing test
上级 fb27fd78
......@@ -2095,7 +2095,7 @@ private static bool InfiniteFlatteningGraph(SourceMemberContainerTypeSymbol top,
if (instanceMap.TryGetValue(tOriginal, out oldInstance))
{
// short circuit when we find a cycle, but only return true when the cycle contains the top struct
return (!TypeSymbol.Equals(oldInstance, t, TypeCompareKind.ConsiderEverything2)) && ReferenceEquals(tOriginal, top);
return (!TypeSymbol.Equals(oldInstance, t, TypeCompareKind.AllNullableIgnoreOptions)) && ReferenceEquals(tOriginal, top);
}
else
{
......
......@@ -53767,9 +53767,6 @@ static void F<T>() where T : class, new()
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (5,26): error CS0523: Struct member 'S<T>.Instance' of type 'S<T>' causes a cycle in the struct layout
// internal static S<T> Instance = new S<T>();
Diagnostic(ErrorCode.ERR_StructLayoutCycle, "Instance").WithArguments("S<T>.Instance", "S<T>").WithLocation(5, 26),
// (11,27): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
// S<T>.Instance.F = null; // 1
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(11, 27),
......@@ -85823,5 +85820,20 @@ static void MN2()
// node.ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "node").WithLocation(118, 9));
}
[Fact, WorkItem(32934, "https://github.com/dotnet/roslyn/issues/32934")]
public void NoCycleInStructLayout()
{
var source =
@"
#nullable enable
public struct Foo<T>
{
public static Foo<T> Bar;
}
";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册