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 regression with var pattern nullability #53691

Merged
merged 4 commits into from
May 28, 2021
Merged

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented May 26, 2021

There was a regression in LearnFromDecisionDag in 16.10 for top-level var patterns in #51001

Also fix #52925 (type of var should have an annotation) and #46236 (GetDeclaredSymbol on such var declarations)
Note that any change to the inferred type of var is breaking in ref var scenario. We've hit this in bootstrap build and I've added a test to illustrate.

@jcouv jcouv added this to the 17.0 milestone May 26, 2021
@jcouv jcouv self-assigned this May 26, 2021
@@ -319,7 +316,7 @@ public PossiblyConditionalState Clone()
tempMap.Add(rootTemp, (originalInputSlot, expressionType.Type));

var nodeStateMap = PooledDictionary<BoundDecisionDagNode, (PossiblyConditionalState state, bool believedReachable)>.GetInstance();
nodeStateMap.Add(decisionDag.RootNode, (state: initialState.Clone(), believedReachable: true));
nodeStateMap.Add(decisionDag.RootNode, (state: PossiblyConditionalState.Create(this), believedReachable: true));
Copy link
Member Author

Choose a reason for hiding this comment

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

📝 The issue was that makeDagTempSlot above was recording the type of the var pattern local into State, based on correct state (from expressionType), but we'd drop that by using initialState which captured a prior state.

@jcouv jcouv marked this pull request as ready for review May 27, 2021 17:50
@jcouv jcouv requested a review from a team as a code owner May 27, 2021 17:50
@jcouv
Copy link
Member Author

jcouv commented May 27, 2021

@RikkiGibson @cston @dotnet/roslyn-compiler for review. Thanks

@jcouv
Copy link
Member Author

jcouv commented May 27, 2021

I'll take a look to see if this also fixed #46236 (Thanks Chuck)

@RikkiGibson RikkiGibson self-assigned this May 27, 2021
@@ -442,7 +442,7 @@ public Enumerator GetEnumerator()
ConcurrentOperation:
ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported();
ReturnFound:
ref var value = ref entry._value;
ref TValue value = ref entry._value;
Copy link
Contributor

Choose a reason for hiding this comment

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

It wasn't clear to me why this needed to change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wouldn't bootstrap without this change

@@ -87,7 +87,7 @@ public TypeWithAnnotations ToTypeWithAnnotations(CSharpCompilation compilation,
if (Type?.IsTypeParameterDisallowingAnnotationInCSharp8() == true)
{
var type = TypeWithAnnotations.Create(Type, NullableAnnotation.NotAnnotated);
return State == NullableFlowState.MaybeDefault ? type.SetIsAnnotated(compilation) : type;
return (State == NullableFlowState.MaybeDefault || asAnnotatedType) ? type.SetIsAnnotated(compilation) : type;
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this all that was needed to fix the 'foreach' scenario?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the for loop starts with a local declaration. That's handled by VisitLocalDeclaration, which correctly calls ToAnnotatedTypeWithAnnotations when the local has an inferred type, but when we get to ToTypeWithAnnotations the annotation would get dropped.

@jcouv jcouv enabled auto-merge (squash) May 28, 2021 05:36
@jcouv jcouv merged commit 3fa5dff into dotnet:main May 28, 2021
@ghost ghost modified the milestones: 17.0, Next May 28, 2021
@jcouv jcouv deleted the var-nullability branch May 28, 2021 19:06
jcouv added a commit to jcouv/roslyn that referenced this pull request Jun 1, 2021
@RikkiGibson RikkiGibson removed this from the Next milestone Jun 29, 2021
@RikkiGibson RikkiGibson added this to the 17.0.P2 milestone Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

var nullability analysis regression
3 participants