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

Enforce C# 8 nullability for user code. #349

Merged
merged 3 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
28 changes: 14 additions & 14 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
<MicrosoftBuildFrameworkPackageVersion>15.8.166</MicrosoftBuildFrameworkPackageVersion>
<MicrosoftBuildPackageVersion>15.8.166</MicrosoftBuildPackageVersion>
<MicrosoftBuildUtilitiesCorePackageVersion>15.8.166</MicrosoftBuildUtilitiesCorePackageVersion>
<MicrosoftCodeAnalysisCommonPackageVersion>2.11.0-beta1-final</MicrosoftCodeAnalysisCommonPackageVersion>
<MicrosoftCodeAnalysisCSharpPackageVersion>2.11.0-beta1-final</MicrosoftCodeAnalysisCSharpPackageVersion>
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>2.11.0-beta1-final</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
<MicrosoftCodeAnalysisCommonPackageVersion>3.0.0-beta4-final</MicrosoftCodeAnalysisCommonPackageVersion>
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 is the latest publicly available Roslyn bits on NuGet. Since we now generate code that's only understood by a newer Roslyn (#nullable restore/preview) I had to update our dependency to correspond with it. Thoughts? @rynowak

Copy link
Member

Choose a reason for hiding this comment

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

Seems right

<MicrosoftCodeAnalysisCSharpPackageVersion>3.0.0-beta4-final</MicrosoftCodeAnalysisCSharpPackageVersion>
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>3.0.0-beta4-final</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
<MicrosoftNETCoreApp21PackageVersion>2.1.3</MicrosoftNETCoreApp21PackageVersion>
<MicrosoftNETCoreApp30PackageVersion>$(MicrosoftNETCoreAppPackageVersion)</MicrosoftNETCoreApp30PackageVersion>
<MicrosoftNETFrameworkReferenceAssemblies>1.0.0-alpha-5</MicrosoftNETFrameworkReferenceAssemblies>
Expand Down Expand Up @@ -116,17 +116,17 @@
<VSMAC_NewtonsoftJsonPackageVersion>10.0.3</VSMAC_NewtonsoftJsonPackageVersion>
<TEST_NewtonsoftJsonPackageVersion>11.0.1</TEST_NewtonsoftJsonPackageVersion>
<StreamJsonRpcPackageVersion>1.3.23</StreamJsonRpcPackageVersion>
<VSIX_MicrosoftCodeAnalysisPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisPackageVersion>
<VSIX_MicrosoftCodeAnalysisCommonPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisCommonPackageVersion>
<VSIX_MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
<VSIX_MicrosoftCodeAnalysisCSharpPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisCSharpPackageVersion>
<VSIX_MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<VSIX_MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
<VSIX_MicrosoftCodeAnalysisRemoteRazorServiceHubPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisRemoteRazorServiceHubPackageVersion>
<VSIX_MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
<VSIX_MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
<VSIX_MicrosoftVisualStudioLanguageServicesPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftVisualStudioLanguageServicesPackageVersion>
<VSIX_MicrosoftVisualStudioLanguageServicesRazorRemoteClientPackageVersion>3.0.0-beta2-18612-05</VSIX_MicrosoftVisualStudioLanguageServicesRazorRemoteClientPackageVersion>
<VSIX_MicrosoftCodeAnalysisPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisPackageVersion>
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 is the current version that is available for the next VS release. Had to update for similar reasons to above (need a newer Roslyn to understand #nullable restore / preview)

<VSIX_MicrosoftCodeAnalysisCommonPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisCommonPackageVersion>
<VSIX_MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
<VSIX_MicrosoftCodeAnalysisCSharpPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisCSharpPackageVersion>
<VSIX_MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<VSIX_MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
<VSIX_MicrosoftCodeAnalysisRemoteRazorServiceHubPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisRemoteRazorServiceHubPackageVersion>
<VSIX_MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
<VSIX_MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
<VSIX_MicrosoftVisualStudioLanguageServicesPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftVisualStudioLanguageServicesPackageVersion>
<VSIX_MicrosoftVisualStudioLanguageServicesRazorRemoteClientPackageVersion>3.1.0-beta1-19156-03</VSIX_MicrosoftVisualStudioLanguageServicesRazorRemoteClientPackageVersion>
<XunitAnalyzersPackageVersion>0.10.0</XunitAnalyzersPackageVersion>
<XunitVersion>2.4.1</XunitVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void WriteInjectProperty(CodeRenderingContext context, InjectIntermediate

if (node.Source.HasValue)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.CodeWriter
.WriteLine(RazorInjectAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void WriteInjectProperty(CodeRenderingContext context, InjectIntermediate

if (node.Source.HasValue)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.CodeWriter
.WriteLine(RazorInjectAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void WriteInjectProperty(CodeRenderingContext context, InjectIntermediate

if (node.Source.HasValue)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.CodeWriter
.WriteLine(RazorInjectAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,15 @@ public static CSharpCodeWritingScope BuildMethodDeclaration(
return new CSharpCodeWritingScope(writer);
}

public static IDisposable BuildLinePragma(this CodeWriter writer, SourceSpan? span)
public static IDisposable BuildLinePragma(this CodeWriter writer, SourceSpan? span, CodeRenderingContext context)
{
if (string.IsNullOrEmpty(span?.FilePath))
{
// Can't build a valid line pragma without a file path.
return NullDisposable.Default;
}

return new LinePragmaWriter(writer, span.Value);
return new LinePragmaWriter(writer, span.Value, context.Options);
}

private static void WriteVerbatimStringLiteral(CodeWriter writer, string literal)
Expand Down Expand Up @@ -596,18 +596,34 @@ private void TryAutoSpace(string spaceCharacter)
private class LinePragmaWriter : IDisposable
{
private readonly CodeWriter _writer;
private readonly RazorCodeGenerationOptions _codeGenerationOptions;
private readonly int _startIndent;

public LinePragmaWriter(CodeWriter writer, SourceSpan span)
public LinePragmaWriter(
CodeWriter writer,
SourceSpan span,
RazorCodeGenerationOptions codeGenerationOptions)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

_writer = writer;
_codeGenerationOptions = codeGenerationOptions;
_startIndent = _writer.CurrentIndent;
_writer.CurrentIndent = 0;

if (!_codeGenerationOptions.SuppressNullabilityEnforcement)
{
var endsWithNewline = _writer.Length > 0 && _writer[_writer.Length - 1] == '\n';
if (!endsWithNewline)
{
_writer.WriteLine();
}
_writer.WriteLine("#nullable restore");
}

WriteLineNumberDirective(writer, span);
}

Expand All @@ -630,6 +646,11 @@ public void Dispose()
.WriteLine("#line default")
.WriteLine("#line hidden");

if (!_codeGenerationOptions.SuppressNullabilityEnforcement)
{
_writer.WriteLine("#nullable disable");
}

_writer.CurrentIndent = _startIndent;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override void WriteUsingDirective(CodeRenderingContext context, UsingDire
{
if (node.Source.HasValue)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.AddSourceMappingFor(node);
context.CodeWriter.WriteUsing(node.Content);
Expand Down Expand Up @@ -44,7 +44,7 @@ public override void WriteCSharpExpression(CodeRenderingContext context, CSharpE

if (node.Source != null)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
var offset = DesignTimeDirectivePass.DesignTimeVariable.Length + " = ".Length;
context.CodeWriter.WritePadding(offset, node.Source, context);
Expand Down Expand Up @@ -91,7 +91,7 @@ public override void WriteCSharpCode(CodeRenderingContext context, CSharpCodeInt
IDisposable linePragmaScope = null;
if (node.Source != null)
{
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value);
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value, context);

context.CodeWriter.WritePadding(0, node.Source.Value, context);
}
Expand Down Expand Up @@ -150,7 +150,7 @@ public override void WriteCSharpExpressionAttributeValue(CodeRenderingContext co
var firstChild = node.Children[0];
if (firstChild.Source != null)
{
using (context.CodeWriter.BuildLinePragma(firstChild.Source.Value))
using (context.CodeWriter.BuildLinePragma(firstChild.Source.Value, context))
{
var offset = DesignTimeDirectivePass.DesignTimeVariable.Length + " = ".Length;
context.CodeWriter.WritePadding(offset, firstChild.Source, context);
Expand Down Expand Up @@ -210,7 +210,7 @@ public override void WriteCSharpCodeAttributeValue(CodeRenderingContext context,
{
if (!isWhitespaceStatement)
{
linePragmaScope = context.CodeWriter.BuildLinePragma(token.Source.Value);
linePragmaScope = context.CodeWriter.BuildLinePragma(token.Source.Value, context);
}

context.CodeWriter.WritePadding(0, token.Source.Value, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void WriteUsingDirective(CodeRenderingContext context, UsingDire
{
if (node.Source.HasValue)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.CodeWriter.WriteUsing(node.Content);
}
Expand All @@ -58,7 +58,7 @@ public override void WriteCSharpExpression(CodeRenderingContext context, CSharpE
IDisposable linePragmaScope = null;
if (node.Source != null)
{
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value);
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value, context);
context.CodeWriter.WritePadding(WriteCSharpExpressionMethod.Length + 1, node.Source, context);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ public override void WriteCSharpCode(CodeRenderingContext context, CSharpCodeInt
IDisposable linePragmaScope = null;
if (node.Source != null)
{
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value);
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value, context);
context.CodeWriter.WritePadding(0, node.Source.Value, context);
}

Expand Down Expand Up @@ -200,7 +200,7 @@ public override void WriteHtmlAttributeValue(CodeRenderingContext context, HtmlA

public override void WriteCSharpExpressionAttributeValue(CodeRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
var prefixLocation = node.Source.Value.AbsoluteIndex;
context.CodeWriter
Expand Down Expand Up @@ -266,7 +266,7 @@ public override void WriteCSharpCodeAttributeValue(CodeRenderingContext context,
{
if (!isWhitespaceStatement)
{
linePragmaScope = context.CodeWriter.BuildLinePragma(token.Source.Value);
linePragmaScope = context.CodeWriter.BuildLinePragma(token.Source.Value, context);
}

context.CodeWriter.WritePadding(0, token.Source.Value, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void WriteUsingDirective(CodeRenderingContext context, UsingDire

if (node.Source.HasValue)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.AddSourceMappingFor(node);
context.CodeWriter.WriteUsing(node.Content);
Expand Down Expand Up @@ -92,7 +92,7 @@ public override void WriteCSharpExpression(CodeRenderingContext context, CSharpE

if (node.Source != null)
{
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
var offset = DesignTimeVariable.Length + " = ".Length;
context.CodeWriter.WritePadding(offset, node.Source, context);
Expand Down Expand Up @@ -162,7 +162,7 @@ public override void WriteCSharpCode(CodeRenderingContext context, CSharpCodeInt
{
if (!isWhitespaceStatement)
{
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value);
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value, context);
}

context.CodeWriter.WritePadding(0, node.Source.Value, context);
Expand Down Expand Up @@ -247,7 +247,7 @@ public override void WriteCSharpExpressionAttributeValue(CodeRenderingContext co
var firstChild = node.Children[0];
if (firstChild.Source != null)
{
using (context.CodeWriter.BuildLinePragma(firstChild.Source.Value))
using (context.CodeWriter.BuildLinePragma(firstChild.Source.Value, context))
{
var offset = DesignTimeVariable.Length + " = ".Length;
context.CodeWriter.WritePadding(offset, firstChild.Source, context);
Expand Down Expand Up @@ -462,7 +462,7 @@ public override void WriteComponent(CodeRenderingContext context, ComponentInter
// the "usings directive is unnecessary" message.
// Looks like:
// __o = typeof(SomeNamespace.SomeComponent);
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
Expand Down Expand Up @@ -835,7 +835,7 @@ private void WriteCSharpToken(CodeRenderingContext context, IntermediateToken to
return;
}

using (context.CodeWriter.BuildLinePragma(token.Source))
using (context.CodeWriter.BuildLinePragma(token.Source, context))
{
context.CodeWriter.WritePadding(0, token.Source.Value, context);
context.AddSourceMappingFor(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void WriteCSharpCode(CodeRenderingContext context, CSharpCodeInt
IDisposable linePragmaScope = null;
if (node.Source != null)
{
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value);
linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value, context);
context.CodeWriter.WritePadding(0, node.Source.Value, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ public DefaultRazorCodeGenerationOptions(
bool designTime,
string rootNamespace,
bool suppressChecksum,
bool supressMetadataAttributes,
bool suppressPrimaryMethodBody)
bool suppressMetadataAttributes,
bool suppressPrimaryMethodBody,
bool suppressNullabilityEnforcement)
{
IndentWithTabs = indentWithTabs;
IndentSize = indentSize;
DesignTime = designTime;
RootNamespace = rootNamespace;
SuppressChecksum = suppressChecksum;
SuppressMetadataAttributes = supressMetadataAttributes;
SuppressMetadataAttributes = suppressMetadataAttributes;
SuppressPrimaryMethodBody = suppressPrimaryMethodBody;
SuppressNullabilityEnforcement = suppressNullabilityEnforcement;
}

public override bool DesignTime { get; }
Expand All @@ -32,5 +34,7 @@ public DefaultRazorCodeGenerationOptions(
public override string RootNamespace { get; }

public override bool SuppressChecksum { get; }

public override bool SuppressNullabilityEnforcement { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public DefaultRazorCodeGenerationOptionsBuilder(bool designTime)

public override bool SuppressChecksum { get; set; }

public override bool SuppressNullabilityEnforcement { get; set; }

public override RazorCodeGenerationOptions Build()
{
return new DefaultRazorCodeGenerationOptions(
Expand All @@ -46,7 +48,8 @@ public override RazorCodeGenerationOptions Build()
RootNamespace,
SuppressChecksum,
SuppressMetadataAttributes,
SuppressPrimaryMethodBody);
SuppressPrimaryMethodBody,
SuppressNullabilityEnforcement);
}

public override void SetDesignTime(bool designTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void WriteTagHelperProperty(CodeRenderingContext context, DefaultTagHelpe
var firstMappedChild = node.Children.FirstOrDefault(child => child.Source != null) as IntermediateNode;
var valueStart = firstMappedChild?.Source;

using (context.CodeWriter.BuildLinePragma(node.Source))
using (context.CodeWriter.BuildLinePragma(node.Source, context))
{
var accessor = GetPropertyAccessor(node);
var assignmentPrefixLength = accessor.Length + " = ".Length;
Expand Down Expand Up @@ -422,7 +422,7 @@ public void WriteTagHelperProperty(CodeRenderingContext context, DefaultTagHelpe
}
else
{
using (context.CodeWriter.BuildLinePragma(node.Source))
using (context.CodeWriter.BuildLinePragma(node.Source, context))
{
context.CodeWriter.WriteStartAssignment(GetPropertyAccessor(node));

Expand Down
Loading