Skip to content

Update SyntaxNodeAnalysisContext.Compilation to not return null#47537

Merged
sharwell merged 1 commit intodotnet:masterfrom
sharwell:non-null-compilation
Sep 20, 2020
Merged

Update SyntaxNodeAnalysisContext.Compilation to not return null#47537
sharwell merged 1 commit intodotnet:masterfrom
sharwell:non-null-compilation

Conversation

@sharwell
Copy link
Contributor

@sharwell sharwell commented Sep 8, 2020

No description provided.

/// <see cref="CodeAnalysis.Compilation"/> containing the <see cref="SyntaxNode"/>.
/// </summary>
public Compilation? Compilation => _semanticModel?.Compilation;
public Compilation Compilation => _semanticModel?.Compilation ?? throw new InvalidOperationException();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can _semanticModel ever be null? Can we check and throw an ArgumentNullException in the SyntaxNodeAnalysisContext constructors below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

_semanticModel is already annotated to not be null from construction. However, this is a value type so the ?. here is used to avoid a NullReferenceException since non-defaultable value types aren't a thing yet.

Copy link
Member

Choose a reason for hiding this comment

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

Could you clarify why it is more desirable to null-check and throw InvalidOperationException versus not checking and just letting the NullReferenceException happen?

Copy link
Contributor Author

@sharwell sharwell Sep 15, 2020

Choose a reason for hiding this comment

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

It could really go either way. Eventually I'd like to see non-defaultable value types cover the scenario. Ideally the places where the explicit check is included would use a generic message for the exception indicating that use of a default instance of this type is not valid. Maybe even a custom exception type derived from this one.

@333fred
Copy link
Member

333fred commented Sep 17, 2020

@sharwell please make sure to rerun tests before merging to make sure nothing else snuck in.

@dotnet dotnet deleted a comment from azure-pipelines bot Sep 17, 2020
@sharwell sharwell merged commit d148f06 into dotnet:master Sep 20, 2020
@ghost ghost added this to the Next milestone Sep 20, 2020
@sharwell sharwell deleted the non-null-compilation branch September 20, 2020 07:51
@dibarbet dibarbet modified the milestones: Next, 16.8.P4 Sep 21, 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.

6 participants