Skip to content

(rebased) Add 'annotations' and 'warnings' support to nullable directive#36464

Merged
RikkiGibson merged 19 commits intodotnet:masterfrom
RikkiGibson:nullable-35748
Jun 19, 2019
Merged

(rebased) Add 'annotations' and 'warnings' support to nullable directive#36464
RikkiGibson merged 19 commits intodotnet:masterfrom
RikkiGibson:nullable-35748

Conversation

@RikkiGibson
Copy link
Copy Markdown
Member

@RikkiGibson RikkiGibson commented Jun 14, 2019

Resolves #35730. Resolves #35748. Resolves #35747.

You may wish to review this PR commit-by-commit, as several of the commits are just boilerplate to add or remove keywords from syntax nodes, etc.

@RikkiGibson RikkiGibson marked this pull request as ready for review June 14, 2019 21:29
@RikkiGibson RikkiGibson requested review from a team as code owners June 14, 2019 21:29
@jcouv jcouv added the Feature - Nullable Reference Types Nullable Reference Types label Jun 14, 2019
@jcouv jcouv added this to the 16.2.P4 milestone Jun 14, 2019
@RikkiGibson RikkiGibson requested review from a team and cston June 17, 2019 22:00
}
}

private static IEnumerable<string> ParseWarnAsErrorWarnings(string value)
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 17, 2019

Choose a reason for hiding this comment

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

ParseWarnAsErrorWarnings [](start = 43, length = 24)

This raises an interesting question: should we allow warnAsError:nullable the same way we allow noWarn:nullable.
@AlekseyTs, @gafter, @cston, what do you think? #Closed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aleksey also thought supporting warnAsError:nullable would be good and consistent.
Could you factor the code back together?


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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

re-activated this


In reply to: 295037412 [](ancestors = 295037412,294552006)

@jcouv jcouv added the Area-IDE label Jun 17, 2019
var position = location.SourceSpan.Start;

bool isNullableFlowAnalysisWarning = ErrorFacts.NullableFlowAnalysisWarnings.Contains(id);
if (isNullableFlowAnalysisWarning)
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 17, 2019

Choose a reason for hiding this comment

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

if (isNullableFlowAnalysisWarning) [](start = 12, length = 34)

I suspect this fixes #32742
nit: I'm tempted to move this block (checking the nullability "warnings" flag) at the top of this method (before step 1). #Closed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you think of moving the isNullableFlowAnalysisWarning handling at the start of this method?
As it stands, we're computing report above this block, but we're checking it below this block, which is weird.


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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think you're right, it might as well occur before step 1.


In reply to: 295034304 [](ancestors = 295034304,294555110)

Copy link
Copy Markdown
Member

@jcouv jcouv Jun 18, 2019

Choose a reason for hiding this comment

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

Could you add some tests to confirm whether you've fixed #32742 with this PR?

One scenario to start:

#nullable enable annotations
class Base { virtual void M(List<string> s) { } }
class C : Base { override void M(List<string?> s) { } } // expecting no nullability warning since nullable warnings context is false, even though the relevant OHI warning (CS8610) isn't produced by NullableWalker

In reply to: 295034304 [](ancestors = 295034304,294555110)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm adding a few tests however I believe this isn't resolved by this PR because it's simply implemented by checking the ImmutableHashSet<ErrorCode> NullableFlowAnalysisWarnings.


In reply to: 295043412 [](ancestors = 295043412,295034304,294555110)

@@ -41,13 +53,15 @@ private NullableDirectiveMap(ImmutableArray<(int Position, bool? State)> directi
}

/// <summary>
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 18, 2019

Choose a reason for hiding this comment

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

summary [](start = 13, length = 7)

This doc should probably be moved to NullableDirective now. #Closed

@@ -624,7 +624,7 @@ internal PragmaWarningState GetPragmaDirectiveWarningState(string id, int positi
/// `enable` or `safeonly`, false if `disable`, and null if no preceding directive,
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 18, 2019

Choose a reason for hiding this comment

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

safeonly [](start = 25, length = 8)

Could you fix or remove this comment as well? safeonly is no longer an option. Thanks #Closed

@RikkiGibson
Copy link
Copy Markdown
Member Author

<Field Name="SettingToken" Type="SyntaxToken">

Marking 'wontfix', only to mean that I don't intend to act on this in this PR. But it might make sense in a future PR.


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


Refers to: src/Compilers/CSharp/Portable/Syntax/Syntax.xml:4437 in 27861ef. [](commit_id = 27861ef, deletion_comment = False)

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Done with review pass (iteration 14).
I'd recommend you use CodeFlow to ensure you don't miss some feedback.

@agocke
Copy link
Copy Markdown
Member

agocke commented Jun 18, 2019

reviewing now #Closed

@jcouv
Copy link
Copy Markdown
Member

jcouv commented Jun 18, 2019

<Field Name="SettingToken" Type="SyntaxToken">

Filed #36553


In reply to: 503325030 [](ancestors = 503325030,502893961)


Refers to: src/Compilers/CSharp/Portable/Syntax/Syntax.xml:4437 in 27861ef. [](commit_id = 27861ef, deletion_comment = False)

CompileAndVerify(comp3, symbolValidator:
(ModuleSymbol m) =>
{
(string type, string attribute)[] baseline = new[]
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 18, 2019

Choose a reason for hiding this comment

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

new[] [](start = 65, length = 5)

could be factored out #Closed

("System.Collections.Generic.Dictionary<System.Object?, System.Int32>?", "System.Runtime.CompilerServices.NullableAttribute({2, 2, 0})")
};

var comp3 = CreateCompilation(new[] { source }, options: WithNonNullTypes(NullableContextOptions.Annotations));
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 18, 2019

Choose a reason for hiding this comment

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

CreateCompilation(new[] { source } [](start = 28, length = 34)

Consider also testing with directive by testing CreateCompilation("#nullable enable annotations /*newline*/" + source) #Closed

ushort number;
if (ushort.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out number) &&
ErrorFacts.IsWarning((ErrorCode)number))
if (id.ToLowerInvariant() == "nullable")
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 18, 2019

Choose a reason for hiding this comment

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

if (id.ToLowerInvariant() == "nullable") [](start = 16, length = 40)

Probably should use if (string.Equals(accessibility, "nullable", StringComparison.OrdinalIgnoreCase)) ... as the rest of this file does #Closed

Copy link
Copy Markdown
Member

@agocke agocke Jun 19, 2019

Choose a reason for hiding this comment

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

Yes, that should avoid allocating an extra string, and Invariant is the wrong comparer for strings. #Closed

{
var builder = ArrayBuilder<(int Position, bool? State)>.GetInstance();
// Generated files have an initial nullable context that is "disabled"
var previousDirective = isGeneratedCode
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 18, 2019

Choose a reason for hiding this comment

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

var previousDirective = isGeneratedCode [](start = 12, length = 39)

nit: consider factoring this and similar snippet above (line 73) out to a method GetDirectiveForFileStart(bool isGeneratedCode) #Closed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Marking this closed because I assume your approval includes approval for this change


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

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Done with review pass (iteration 15)

ushort number;
if (ushort.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out number) &&
ErrorFacts.IsWarning((ErrorCode)number))
if (id.ToLowerInvariant() == "nullable")
Copy link
Copy Markdown
Member

@agocke agocke Jun 19, 2019

Choose a reason for hiding this comment

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

Yes, that should avoid allocating an extra string, and Invariant is the wrong comparer for strings. #Closed

/// where <see langword="true"/> means the context is 'enable', <see langword="false"/> means the context is 'disable',
/// and <see langword="null"/> means the context is 'restore' or not specified.
/// </summary>
internal readonly struct NullableDirective
Copy link
Copy Markdown
Member

@agocke agocke Jun 19, 2019

Choose a reason for hiding this comment

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

I would call this something else, because it doesn't really represent a NullableDirective. Maybe NullableContextState? #Resolved

Copy link
Copy Markdown
Member Author

@RikkiGibson RikkiGibson Jun 19, 2019

Choose a reason for hiding this comment

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

OK, using PragmaWarningStateMap as a precedent, how about we change the name NullableDirective to NullableContextState, and change NullableDirectiveMap to NullableContextStateMap. #Resolved

var builder = ArrayBuilder<(int Position, bool? State)>.GetInstance();
// Generated files have an initial nullable context that is "disabled"
var previousDirective = isGeneratedCode
? new NullableDirective(0, false, false)
Copy link
Copy Markdown
Member

@agocke agocke Jun 19, 2019

Choose a reason for hiding this comment

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

Named arguments for these literals? #Resolved

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks! (iteration 17)

@RikkiGibson
Copy link
Copy Markdown
Member Author

RikkiGibson commented Jun 19, 2019

@cston I believe my changes conflicted with yours, but the resolution looked pretty simple. Would appreciate if you had a glance at 208c404. #Closed

@RikkiGibson RikkiGibson requested a review from agocke June 19, 2019 20:40
Copy link
Copy Markdown
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

LGTM

@RikkiGibson RikkiGibson merged commit 6d4fcfa into dotnet:master Jun 19, 2019
@RikkiGibson RikkiGibson deleted the nullable-35748 branch June 19, 2019 22:46
@jasonmalinowski
Copy link
Copy Markdown
Member

@RikkiGibson Did this need any IDE signoff?

@RikkiGibson
Copy link
Copy Markdown
Member Author

It adds some pretty rudimentary keyword recommenders and classifier changes, it didn't seem necessary to have a dedicated IDE review.

@jcouv
Copy link
Copy Markdown
Member

jcouv commented Jun 20, 2019

@jasonmalinowski Sorry about that. We should have waited for an IDE sign-off.
For what it's worth, I did review the IDE part too and it is pretty straightforward and looks good to me.
Could you take a look as well? We'll address any feedback in a separate PR asap. Thanks

@jasonmalinowski
Copy link
Copy Markdown
Member

@jcouv @RikkiGibson I agree that in this case it probably didn't, but sometimes we'll still see what you have done and decide if we need to add additional tests or file additional bugs for additional work.

333fred added a commit to 333fred/roslyn that referenced this pull request Jun 20, 2019
* dotnet/master: (85 commits)
  Don't complete statement when typing semicolon inside comments in an argument list (dotnet#36521)
  Set focus to editor before finding text
  Add a bunch of nullability support to some code generation helpers
  Add 'annotations' and 'warnings' support to nullable directive (dotnet#36464)
  Fixed IDE services touching `notnull` constraint (dotnet#36508)
  Update compiler toolset to arcade version (dotnet#36549)
  Fix for 923157
  Do not include value types in NullableAttribute byte[] (dotnet#36519)
  Fix a deadlock in InvokeOnUIThread
  Apply a hang mitigating timeout to UI thread operations
  Move to a different lowering from for nullable value types to work around a bug in TransformCompoundAssignmentLHS. Addressed PR feedback.
  Fix stack overflow in requesting syntax directives (dotnet#36347)
  crash on ClassifyUpdate for EventFields (dotnet#35962)
  fixing bad merge with refactoring that was checked in later
  added basic completion statement telemetry
  Remove duplication in AbstractSymbolCompletionProvider.CreateItems
  Disable move type when the options service isn't present (dotnet#36334)
  Fix crash where type inference doing method inference needs to drop nullability
  Revert "Use IVsSolution to look up IVsHierarchy by project GUID (dotnet#35746)"
  Fix parsing bug in invalid using statements (dotnet#36428)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants