Skip to content

Commit

Permalink
Add support for /nullable:warnings and /nullable:safeonlywarnings (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyTs authored Jan 2, 2019
1 parent 2b3d783 commit 92b9427
Show file tree
Hide file tree
Showing 25 changed files with 776 additions and 375 deletions.
4 changes: 2 additions & 2 deletions docs/features/nullable-reference-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Reference types may be nullable, non-nullable, or null-oblivious (abbreviated he

Project level nullable context can be set by using "nullable" command line switch:
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly} Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings} Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

Through msbuild the context could be set by supplying an argument for a "NullableContextOptions" parameter of Csc build task.
Accepted values are "enable", "disable", "safeonly", or null (for the default nullable context according to the compiler).
Accepted values are "enable", "disable", "safeonly", "warnings", "safeonlywarnings", or null (for the default nullable context according to the compiler).
The Microsoft.CSharp.Core.targets passes value of msbuild property named "NullableContextOptions" for that parameter.

## Annotations
Expand Down
15 changes: 14 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/BuckStopsHereBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,20 @@ internal override Symbol ContainingMemberOrLambda

internal override bool IsNullableGloballyEnabled()
{
return Compilation.Options.NullableContextOptions != NullableContextOptions.Disable;
switch (Compilation.Options.NullableContextOptions)
{
case NullableContextOptions.Enable:
case NullableContextOptions.SafeOnly:
return true;

case NullableContextOptions.Disable:
case NullableContextOptions.Warnings:
case NullableContextOptions.SafeOnlyWarnings:
return false;

default:
throw ExceptionUtilities.UnexpectedValue(Compilation.Options.NullableContextOptions);
}
}

internal override Binder GetBinder(SyntaxNode node)
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4603,8 +4603,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down Expand Up @@ -5647,6 +5647,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>An expression tree may not contain a null coalescing assignment</value>
</data>
<data name="ERR_BadNullableContextOption" xml:space="preserve">
<value>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</value>
<value>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
Debug.Assert(loweredValue == nameof(NullableContextOptions.SafeOnly).ToLower());
nullableContextOptions = NullableContextOptions.SafeOnly;
break;
case "warnings":
Debug.Assert(loweredValue == nameof(NullableContextOptions.Warnings).ToLower());
nullableContextOptions = NullableContextOptions.Warnings;
break;
case "safeonlywarnings":
Debug.Assert(loweredValue == nameof(NullableContextOptions.SafeOnlyWarnings).ToLower());
nullableContextOptions = NullableContextOptions.SafeOnlyWarnings;
break;
default:
AddDiagnostic(diagnostics, ErrorCode.ERR_BadNullableContextOption, value);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,19 @@ internal static ReportDiagnostic GetDiagnosticReport(
break;

case NullableContextOptions.SafeOnly:
case NullableContextOptions.SafeOnlyWarnings:
if (isNullableFlowAnalysisNonSafetyWarning)
{
return ReportDiagnostic.Suppress;
}
break;

case NullableContextOptions.Enable:
case NullableContextOptions.Warnings:
break;

default:
throw ExceptionUtilities.UnexpectedValue(nullableOption);
}

// Unless specific warning options are defined (/warnaserror[+|-]:<n> or /nowarn:<n>,
Expand Down
10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/NullableContextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@ public enum NullableContextOptions : byte
/// Nullable annotation context is enabled and the nullable warning context is safeonly.
/// </summary>
SafeOnly,

/// <summary>
/// Nullable annotation context is disabled and the nullable warning context is enabled.
/// </summary>
Warnings,

/// <summary>
/// Nullable annotation context is disabled and the nullable warning context is safeonly.
/// </summary>
SafeOnlyWarnings,
}
}
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Microsoft.CodeAnalysis.CSharp.NullableContextOptions
Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Disable = 0 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions
Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Enable = 1 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions
Microsoft.CodeAnalysis.CSharp.NullableContextOptions.SafeOnly = 2 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions
Microsoft.CodeAnalysis.CSharp.NullableContextOptions.SafeOnlyWarnings = 4 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions
Microsoft.CodeAnalysis.CSharp.NullableContextOptions.Warnings = 3 -> Microsoft.CodeAnalysis.CSharp.NullableContextOptions
Microsoft.CodeAnalysis.CSharp.Syntax.ForEachStatementSyntax.Update(Microsoft.CodeAnalysis.SyntaxToken awaitKeyword, Microsoft.CodeAnalysis.SyntaxToken forEachKeyword, Microsoft.CodeAnalysis.SyntaxToken openParenToken, Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax type, Microsoft.CodeAnalysis.SyntaxToken identifier, Microsoft.CodeAnalysis.SyntaxToken inKeyword, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax expression, Microsoft.CodeAnalysis.SyntaxToken closeParenToken, Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax statement) -> Microsoft.CodeAnalysis.CSharp.Syntax.ForEachStatementSyntax
Microsoft.CodeAnalysis.CSharp.Syntax.ForEachVariableStatementSyntax.Update(Microsoft.CodeAnalysis.SyntaxToken awaitKeyword, Microsoft.CodeAnalysis.SyntaxToken forEachKeyword, Microsoft.CodeAnalysis.SyntaxToken openParenToken, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax variable, Microsoft.CodeAnalysis.SyntaxToken inKeyword, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax expression, Microsoft.CodeAnalysis.SyntaxToken closeParenToken, Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax statement) -> Microsoft.CodeAnalysis.CSharp.Syntax.ForEachVariableStatementSyntax
Microsoft.CodeAnalysis.CSharp.Syntax.NullableDirectiveTriviaSyntax.NullableKeyword.get -> Microsoft.CodeAnalysis.SyntaxToken
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ Pokud chcete odstranit toto varování, můžete místo toho použít /reference
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ Um die Warnung zu beheben, können Sie stattdessen /reference verwenden (Einbett
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ Para eliminar la advertencia puede usar /reference (establezca la propiedad Embe
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ Pour supprimer l'avertissement, vous pouvez utiliser la commande /reference (dé
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ Per rimuovere l'avviso, è invece possibile usare /reference (impostare la propr
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadNullableContextOption">
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable' or 'safeonly'</target>
<source>Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</source>
<target state="new">Invalid option '{0}' for /nullable; must be 'disable', 'enable', 'safeonly', 'warnings' or 'safeonlywarnings'</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down Expand Up @@ -7954,8 +7954,8 @@ Aby usunąć ostrzeżenie, możesz zamiast tego użyć opcji /reference (ustaw w
`latest` (latest version, including minor versions),
or specific versions like `6` or `7.1`
-nullable[+|-] Specify nullable context option enable|disable.
-nullable:{enable|disable|safeonly}
Specify nullable context option enable|disable|safeonly.
-nullable:{enable|disable|safeonly|warnings|safeonlywarnings}
Specify nullable context option enable|disable|safeonly|warnings|safeonlywarnings.

- SECURITY -
-delaysign[+|-] Delay-sign the assembly using only the public
Expand Down
Loading

0 comments on commit 92b9427

Please sign in to comment.