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

Check NotNullIfNotNull in implementation #47649

Merged
merged 10 commits into from
Sep 22, 2020

Conversation

RikkiGibson
Copy link
Contributor

@RikkiGibson RikkiGibson commented Sep 12, 2020

@RikkiGibson
Copy link
Contributor Author

RikkiGibson commented Sep 12, 2020

This introduces multiple new warnings to the bootstrap build. I think the warnings are.. fair? Interesting.

  • src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter.cs(183,13): error CS8825: Return value must be non-null because parameter 'node' is non-null. [src/Compilers/CSharp/Portable/Microsoft.CodeAnalysis.CSharp.csproj]

[return: NotNullIfNotNull("node")]
private BoundExpression? VisitExpression(BoundExpression? node)
{
if (node == null)
{
return node;
}
Debug.Assert(!node.HasErrors, "nodes with errors should not be lowered");
return VisitExpressionImpl(node);
}

  • src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxRewriter.cs(46,17): error CS8825: Return value must be non-null because parameter 'node' is non-null. [src/Compilers/CSharp/Portable/Microsoft.CodeAnalysis.CSharp.csproj]

[return: NotNullIfNotNull("node")]
public override SyntaxNode? Visit(SyntaxNode? node)
{
if (node != null)
{
_recursionDepth++;
StackGuard.EnsureSufficientExecutionStack(_recursionDepth);
var result = ((CSharpSyntaxNode)node).Accept(this);
_recursionDepth--;
return result;
}
else
{
return null;
}
}

@jcouv
Copy link
Member

jcouv commented Sep 12, 2020

The local rewriter warning should be fixed by annotating VisitExpressionImpl with NotNullIfNotNull.
The CSharpSyntaxRewriter.cs one needs a bit more validation. Can we rewrite to null? If not, then an assertion would do the trick.

@RikkiGibson
Copy link
Contributor Author

The local rewriter warning should be fixed by annotating VisitExpressionImpl with NotNullIfNotNull.

I think this might not be right, because VisitExpressionImpl's parameter has a non-null type, and adding the attribute introduces a different warning:

src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter.cs(232,13): warning CS8825: Return value must be non-null because parameter 'node' is non-null.

I think something about the Debug.Assert is giving the visited a maybe-null state afterwards. Perhaps the pure null check?

@RikkiGibson
Copy link
Contributor Author

I have opened a tracking issue #47682 and have suppressed the new warnings, since "proper" fixes for the warnings seem non-trivial.

@RikkiGibson

This comment has been minimized.

@RikkiGibson

This comment has been minimized.

@RikkiGibson
Copy link
Contributor Author

@dotnet/roslyn-compiler Please review

continue;
}

var annotations = parameter.FlowAnalysisAnnotations;
Copy link
Member

Choose a reason for hiding this comment

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

Why didn't u use GetFlowAnalysisAnnotations here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code was moved from parameterHasBadState below. However GetFlowAnalysisAnnotations(Symbol) here would just dispatch out to ParameterSymbol.FlowAnalysisAnnotations.

It could make sense to change this to GetParameterAnnotations in order to avoid attribute binding cycles. Since I can't come up with a crashing scenario for this off the top of my head I would like to punt changes to it to #47635

{
x = null;
return; // 4
}
Copy link
Member

Choose a reason for hiding this comment

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

Please add a few cases for constructors where the only code in the method is a :this call to another constructor and verify that we validate not-null correctly in those cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure exactly what this meant--do you want tests where constructor parameters have [NotNull] and [NotNullIfNotNull]?

@RikkiGibson RikkiGibson requested a review from a team as a code owner September 21, 2020 23:05
@RikkiGibson RikkiGibson changed the base branch from master to release/dev16.8 September 21, 2020 23:05
@RikkiGibson RikkiGibson requested review from a team as code owners September 21, 2020 23:05
@RikkiGibson RikkiGibson removed request for a team September 21, 2020 23:06
@RikkiGibson RikkiGibson self-assigned this Sep 21, 2020
@RikkiGibson
Copy link
Contributor Author

Changing base so we can get this change into 16.8.

@RikkiGibson RikkiGibson merged commit e39abad into dotnet:release/dev16.8 Sep 22, 2020
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.

Invalid NotNullIfNotNull contract is not reported
4 participants