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

Fix struct layout error when nullable enabled: #34128

Merged
merged 2 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52712,9 +52712,6 @@ class Program
}";
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),
Expand Down Expand Up @@ -84789,5 +84786,21 @@ 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 =
@"
#pragma warning disable 0169 // suppress field never used warning
Copy link
Member

@jcouv jcouv Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could also eliminate the warning by making the field public #Resolved

#nullable enable
struct Foo<T>
{
static Foo<T> Bar;
}
";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics();
}
}
}