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

Fixing VS Crash on invalid "MemberNotNull" #45563

Merged
merged 9 commits into from
Jul 2, 2020
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 @@ -113,12 +113,14 @@ protected virtual bool IsEmptyStructType(TypeSymbol type)
return _emptyStructTypeCache.IsEmptyStructType(type);
}

#nullable enable
/// <summary>
/// Force a variable to have a slot. Returns -1 if the variable has an empty struct type.
/// </summary>
protected virtual int GetOrCreateSlot(Symbol symbol, int containingSlot = 0, bool forceSlotEvenIfEmpty = false)
{
Debug.Assert(containingSlot >= 0);
Debug.Assert(symbol != null);

if (symbol.Kind == SymbolKind.RangeVariable) return -1;

Expand Down Expand Up @@ -169,6 +171,7 @@ protected virtual int GetOrCreateSlot(Symbol symbol, int containingSlot = 0, boo

return slot;
}
#nullable restore

private int GetSlotDepth(int slot)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,10 @@ int getSlotForFieldOrProperty(Symbol member)

if (!isStatic)
{
if (MethodThisParameter is null)
Copy link
Member

@333fred 333fred Jul 1, 2020

Choose a reason for hiding this comment

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

What is the Kind of the member in this scenario? #Closed

{
return -1;
}
Copy link
Member

Choose a reason for hiding this comment

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

Nit: newline

thisSlot = GetOrCreateSlot(MethodThisParameter);
if (thisSlot < 0)
{
Expand Down Expand Up @@ -1268,6 +1272,7 @@ static MethodSymbol getTopLevelMethod(MethodSymbol method)
}
}

#nullable enable
protected override int GetOrCreateSlot(Symbol symbol, int containingSlot = 0, bool forceSlotEvenIfEmpty = false)
Copy link
Contributor

@RikkiGibson RikkiGibson Jun 30, 2020

Choose a reason for hiding this comment

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

If it's possible for symbol to be null then the type should change to Symbol?. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't be possible, added an Assert to make sure in Commit 8.


In reply to: 447978511 [](ancestors = 447978511)

{

Expand All @@ -1276,6 +1281,7 @@ protected override int GetOrCreateSlot(Symbol symbol, int containingSlot = 0, bo

return base.GetOrCreateSlot(symbol, containingSlot, forceSlotEvenIfEmpty);
}
#nullable restore

private void VisitAndUnsplitAll<T>(ImmutableArray<T> nodes) where T : BoundNode
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129603,6 +129603,46 @@ public async Task Handle(Request request)
);
}


[Fact]
[WorkItem(44049, "https://github.com/dotnet/roslyn/issues/44049")]
public void MemberNotNull_InstanceMemberOnStaticMethod()
{
var source =
@"using System.Diagnostics.CodeAnalysis;

class C
{
public string? field;

[MemberNotNull(""field"")]
public static void M()
{
}

public void Test()
{
M();
field.ToString();
}
}";
var comp = CreateCompilation(
new[]
{
source, MemberNotNullAttributeDefinition
},
options: WithNonNullTypesTrue(),
targetFramework: TargetFramework.NetCoreApp30,
parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
// (5,20): warning CS0649: Field 'C.field' is never assigned to, and will always have its default value null
// public string? field;
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "field").WithArguments("C.field", "null").WithLocation(5, 20),
// (15,9): warning CS8602: Dereference of a possibly null reference.
// field.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field").WithLocation(15, 9));
}

[Fact, WorkItem(39417, "https://github.com/dotnet/roslyn/issues/39417")]
public void CustomAwaitable_DetectSettingNullableToNonNullableType()
{
Expand Down