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

Add "Nullable" compilation option. #30479

Merged

Conversation

AlekseyTs
Copy link
Contributor

No description provided.

@AlekseyTs
Copy link
Contributor Author

@333fred, @cston, @jcouv, @dotnet/roslyn-compiler Please review

/// <summary>
/// Whether Nullable Reference Types feature is enabled globally.
/// </summary>
public bool? Nullable { get; private set; }
Copy link
Member

@jcouv jcouv Oct 12, 2018

Choose a reason for hiding this comment

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

bool? [](start = 15, length = 5)

Why use a nullable bool that defaults to null, rather than a bool with default false? (like AllowUnsafe above) #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.

Why use a nullable bool that defaults to null, rather than a bool with default false? (like AllowUnsafe above)

This just preserves what we have today, the context can be in three different states. If we think that was not intentional or should change, that can be addressed later.


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

Copy link
Member

Choose a reason for hiding this comment

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

Seems a PROTOTYPE comment then.


In reply to: 224930060 [](ancestors = 224930060,224928022)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems a PROTOTYPE comment then.

I'll open an issue for that


In reply to: 224934637 [](ancestors = 224934637,224930060,224928022)

@@ -123,7 +123,6 @@ Microsoft.CodeAnalysis.CSharp.CSharpCompilation.WithReferences(System.Collection
Microsoft.CodeAnalysis.CSharp.CSharpCompilation.WithScriptCompilationInfo(Microsoft.CodeAnalysis.CSharp.CSharpScriptCompilationInfo info) -> Microsoft.CodeAnalysis.CSharp.CSharpCompilation
Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions
Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.AllowUnsafe.get -> bool
Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions.CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind outputKind, bool reportSuppressedDiagnostics = false, string moduleName = null, string mainTypeName = null, string scriptClassName = null, System.Collections.Generic.IEnumerable<string> usings = null, Microsoft.CodeAnalysis.OptimizationLevel optimizationLevel = Microsoft.CodeAnalysis.OptimizationLevel.Debug, bool checkOverflow = false, bool allowUnsafe = false, string cryptoKeyContainer = null, string cryptoKeyFile = null, System.Collections.Immutable.ImmutableArray<byte> cryptoPublicKey = default(System.Collections.Immutable.ImmutableArray<byte>), bool? delaySign = null, Microsoft.CodeAnalysis.Platform platform = Microsoft.CodeAnalysis.Platform.AnyCpu, Microsoft.CodeAnalysis.ReportDiagnostic generalDiagnosticOption = Microsoft.CodeAnalysis.ReportDiagnostic.Default, int warningLevel = 4, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, Microsoft.CodeAnalysis.ReportDiagnostic>> specificDiagnosticOptions = null, bool concurrentBuild = true, bool deterministic = false, Microsoft.CodeAnalysis.XmlReferenceResolver xmlReferenceResolver = null, Microsoft.CodeAnalysis.SourceReferenceResolver sourceReferenceResolver = null, Microsoft.CodeAnalysis.MetadataReferenceResolver metadataReferenceResolver = null, Microsoft.CodeAnalysis.AssemblyIdentityComparer assemblyIdentityComparer = null, Microsoft.CodeAnalysis.StrongNameProvider strongNameProvider = null, bool publicSign = false, Microsoft.CodeAnalysis.MetadataImportOptions metadataImportOptions = Microsoft.CodeAnalysis.MetadataImportOptions.Public) -> void
Copy link
Member

Choose a reason for hiding this comment

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

CSharpCompilationOptions [](start = 55, length = 24)

FYI @jaredpar @gafter for public API change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI @jaredpar @gafter for public API change.

I believe, this is just a removal of optional values because a new (more complete) overload is added.


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

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I understand. We're supposed to ping them on any PR with public API change (whether shipped or unshipped).


In reply to: 224930250 [](ancestors = 224930250,224928238)

parsedArgs = DefaultParse(new[] { "a.cs", "/langversion:7.3" }, _baseDirectory);
parsedArgs.Errors.Verify();
Assert.Null(parsedArgs.CompilationOptions.Nullable);
}
Copy link
Member

@jcouv jcouv Oct 12, 2018

Choose a reason for hiding this comment

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

} [](start = 8, length = 1)

If I run csc.exe /nullable+ /langversion:7.3 a.cs, would I get two diagnostics (one from parsing command-line options and one from compilation diagnostics), or just one? #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.

If I run csc.exe /nullable+ /langversion:7.3 a.cs, would I get two diagnostics (one from parsing command-line options and one from compilation diagnostics), or just one?

Just the one reported by the command line parser. Note that both places report errors originating from options.


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

@@ -32837,11 +32850,9 @@ static void G3<T>() where T : new()

// No warnings with C#7.3.
Copy link
Member

@jcouv jcouv Oct 12, 2018

Choose a reason for hiding this comment

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

// No warnings with C#7.3. [](start = 12, length = 26)

Stale comment? #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.

Stale comment?

I have no idea what this comment is about, there always were warnings for this scenario. Perhaps it refers to WRN_NullAsNonNullable warnings


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

@jcouv
Copy link
Member

jcouv commented Oct 12, 2018

            //Diagnostic(ErrorCode.ERR_NonNullTypesNotAvailable, "System.Runtime.CompilerServices.NonNullTypes").WithArguments("8.0").WithLocation(10, 2)

I think the #nonnull pragma should also be disallowed with older LangVersion, for same reason we disallow global option. #Resolved


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:284 in 33f5934. [](commit_id = 33f5934, deletion_comment = False)


case "nullable-":
if (value != null)
break;
Copy link
Member

@333fred 333fred Oct 12, 2018

Choose a reason for hiding this comment

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

Nit: surround with braces to match file. #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.

Nit: surround with braces to match file.

Matches the implementation for "checked" and I believe does not violate the style guidelines.


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

@AlekseyTs
Copy link
Contributor Author

            //Diagnostic(ErrorCode.ERR_NonNullTypesNotAvailable, "System.Runtime.CompilerServices.NonNullTypes").WithArguments("8.0").WithLocation(10, 2)

I think the #nonnull pragma should also be disallowed with older LangVersion, for same reason we disallow global option.

Agree. Hence the PROTOTYPE comment above. This warning is not in scope for this PR because it is not focused on parsing the new directive.


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


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:284 in 33f5934. [](commit_id = 33f5934, deletion_comment = False)

@jcouv
Copy link
Member

jcouv commented Oct 12, 2018

    public void SuppressionOnUnconstrainedTypeParameter()

This test should use NonNullTypes=null context. No warning should be produced if the context is true or false.

Never mindd, I misread. This test behaves correctly. #Closed


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs:330 in 33f5934. [](commit_id = 33f5934, deletion_comment = False)

}

protected static CSharpCompilationOptions WithNonNullTypesFalse(CSharpCompilationOptions options = null)
{
return (options ?? TestOptions.ReleaseDll).WithSpecificDiagnosticOptions(
new[] { new System.Collections.Generic.KeyValuePair<string, ReportDiagnostic>("CS" + ((int)ErrorCode.WRN_PragmaNonNullTypes).ToString("0000"), ReportDiagnostic.Suppress) });
return (options ?? TestOptions.ReleaseDll).WithNullable(false);
Copy link
Member

@jcouv jcouv Oct 12, 2018

Choose a reason for hiding this comment

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

WithNullable(false) [](start = 55, length = 19)

Do we have any tests that exercise the option null? #Closed

Copy link
Member

@cston cston Oct 12, 2018

Choose a reason for hiding this comment

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

We should change Nullable to a bool so we don't need to test that case.
Nevermind, I guess allowing null is a transitional state.


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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we have any tests that exercise the option null?

Yes. Those are all the tests that didn't apply NonNullTypes attribute on a module.


In reply to: 224933817 [](ancestors = 224933817,224933444)

Copy link
Member

Choose a reason for hiding this comment

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

Any test that doesn't call WithNonNullTypesTrue/False will use null. So the case should be covered.
Closing


In reply to: 224933817 [](ancestors = 224933817,224933444)

Copy link
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 but we need to follow-up on finalize public API shape for new option. Thanks

@AlekseyTs AlekseyTs merged commit 93af170 into dotnet:features/NullableReferenceTypes Oct 12, 2018
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.

4 participants