diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDesignTimeNodeWriter.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDesignTimeNodeWriter.cs index 6aa033d2beb..f8d06e7c49f 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDesignTimeNodeWriter.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentDesignTimeNodeWriter.cs @@ -1119,6 +1119,7 @@ protected override void WriteReferenceCaptureInnards(CodeRenderingContext contex var captureTypeName = node.IsComponentCapture ? TypeNameHelper.GetGloballyQualifiedNameIfNeeded(node.ComponentCaptureTypeName) : ComponentsApi.ElementReference.FullTypeName; + var nullSuppression = !context.Options.SuppressNullabilityEnforcement ? "!" : string.Empty; WriteCSharpCode(context, new CSharpCodeIntermediateNode { Source = node.Source, @@ -1128,7 +1129,7 @@ protected override void WriteReferenceCaptureInnards(CodeRenderingContext contex new IntermediateToken { Kind = TokenKind.CSharp, - Content = $" = default({captureTypeName});" + Content = $" = default({captureTypeName}){nullSuppression};" } } }); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs index 518c0ffa064..2ad0225d33b 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -7628,6 +7628,58 @@ public class MyComponent : ComponentBase CompileToAssembly(generated); } + [Fact] // https://github.com/dotnet/razor/issues/8170 + public void Component_WithRef_Nullable() + { + // Act + var generated = CompileToCSharp(""" + + + @code { + private TestComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } + } + """, + nullableEnable: true); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated); + } + + [Fact] // https://github.com/dotnet/razor/issues/8170 + public void Component_WithRef_Nullable_Generic() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(""" + using Microsoft.AspNetCore.Components; + + namespace Test; + + public class MyComponent : ComponentBase + { + [Parameter] public T MyParameter { get; set; } = default!; + } + """)); + + // Act + var generated = CompileToCSharp(""" + + + @code { + private MyComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } + } + """, + nullableEnable: true); + + // Assert + AssertDocumentNodeMatchesBaseline(generated.CodeDocument); + AssertCSharpDocumentMatchesBaseline(generated.CodeDocument); + CompileToAssembly(generated); + } + [Fact] public void Component_WithRef_WithChildContent() { diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs index 24931be4822..d833bdb2633 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs @@ -48,7 +48,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. )); #nullable restore #line 7 "x:\dir\subdir\Test\TestComponent.cshtml" - myComponentReference = default(global::Test.TemplatedComponent); + myComponentReference = default(global::Test.TemplatedComponent)!; #line default #line hidden @@ -71,7 +71,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. #nullable disable #nullable restore #line 13 "x:\dir\subdir\Test\TestComponent.cshtml" - myElementReference = default(Microsoft.AspNetCore.Components.ElementReference); + myElementReference = default(Microsoft.AspNetCore.Components.ElementReference)!; #line default #line hidden diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt index 6ee494fd447..858def71a9c 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt @@ -22,32 +22,32 @@ Source Location: (439:10,1 [38] x:\dir\subdir\Test\TestComponent.cshtml) |if (DateTime.Now.Year > 1950) { | -Generated Location: (1965:64,1 [38] ) +Generated Location: (1966:64,1 [38] ) |if (DateTime.Now.Year > 1950) { | Source Location: (511:12,38 [18] x:\dir\subdir\Test\TestComponent.cshtml) |myElementReference| -Generated Location: (2164:73,38 [18] ) +Generated Location: (2165:73,38 [18] ) |myElementReference| Source Location: (557:12,84 [6] x:\dir\subdir\Test\TestComponent.cshtml) | | -Generated Location: (2379:78,84 [6] ) +Generated Location: (2381:78,84 [6] ) | | Source Location: (589:13,30 [10] x:\dir\subdir\Test\TestComponent.cshtml) |myVariable| -Generated Location: (2574:83,30 [10] ) +Generated Location: (2576:83,30 [10] ) |myVariable| Source Location: (637:13,78 [3] x:\dir\subdir\Test\TestComponent.cshtml) | }| -Generated Location: (2947:92,78 [3] ) +Generated Location: (2949:92,78 [3] ) | }| @@ -62,7 +62,7 @@ Source Location: (651:16,7 [245] x:\dir\subdir\Test\TestComponent.cshtml) for (var i = 0; i < 10; i++) { | -Generated Location: (3129:102,7 [245] ) +Generated Location: (3131:102,7 [245] ) | ElementReference myElementReference; TemplatedComponent myComponentReference; @@ -76,12 +76,12 @@ Generated Location: (3129:102,7 [245] ) Source Location: (912:25,28 [1] x:\dir\subdir\Test\TestComponent.cshtml) |i| -Generated Location: (3541:119,28 [1] ) +Generated Location: (3543:119,28 [1] ) |i| Source Location: (925:25,41 [1] x:\dir\subdir\Test\TestComponent.cshtml) |i| -Generated Location: (3717:127,41 [1] ) +Generated Location: (3719:127,41 [1] ) |i| Source Location: (931:25,47 [166] x:\dir\subdir\Test\TestComponent.cshtml) @@ -93,7 +93,7 @@ Source Location: (931:25,47 [166] x:\dir\subdir\Test\TestComponent.cshtml) System.GC.KeepAlive(myVariable); } | -Generated Location: (3889:134,47 [166] ) +Generated Location: (3891:134,47 [166] ) | } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs index 24e9e7958d9..8e0e0d9b2ae 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.codegen.cs @@ -27,7 +27,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. )); #nullable restore #line 1 "x:\dir\subdir\Test\TestComponent.cshtml" - myInstance = default(global::Test.MyComponent); + myInstance = default(global::Test.MyComponent)!; #line default #line hidden diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt index 900c022bba5..c4265a0eb10 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef/TestComponent.mappings.txt @@ -8,7 +8,7 @@ Source Location: (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) private Test.MyComponent myInstance; public void Foo() { System.GC.KeepAlive(myInstance); } | -Generated Location: (1496:45,7 [104] ) +Generated Location: (1497:45,7 [104] ) | private Test.MyComponent myInstance; public void Foo() { System.GC.KeepAlive(myInstance); } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.codegen.cs new file mode 100644 index 00000000000..4fde7088fa3 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.codegen.cs @@ -0,0 +1,53 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 219 + private void __RazorDirectiveTokenHelpers__() { + } + #pragma warning restore 219 + #pragma warning disable 0414 + private static object __o = null; + #pragma warning restore 0414 + #pragma warning disable 1998 + protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) + { + __builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => { + } + )); +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myComponent = default(global::Test.TestComponent)!; + +#line default +#line hidden +#nullable disable +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +__o = typeof(global::Test.TestComponent); + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 +#nullable restore +#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" + + private TestComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } + +#line default +#line hidden +#nullable disable + } +} +#pragma warning restore 1591 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.ir.txt new file mode 100644 index 00000000000..31e008726d0 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.ir.txt @@ -0,0 +1,22 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [12] ) - System + UsingDirective - (18:2,1 [32] ) - System.Collections.Generic + UsingDirective - (53:3,1 [17] ) - System.Linq + UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase - + DesignTimeDirective - + CSharpCode - + IntermediateToken - - CSharp - #pragma warning disable 0414 + CSharpCode - + IntermediateToken - - CSharp - private static object __o = null; + CSharpCode - + IntermediateToken - - CSharp - #pragma warning restore 0414 + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [36] x:\dir\subdir\Test\TestComponent.cshtml) - TestComponent + ReferenceCapture - (21:0,21 [11] x:\dir\subdir\Test\TestComponent.cshtml) - myComponent + HtmlContent - (36:0,36 [4] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (36:0,36 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n + CSharpCode - (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private TestComponent myComponent = null!;\n public void Use() { System.GC.KeepAlive(myComponent); }\n diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.mappings.txt new file mode 100644 index 00000000000..96370f4aa78 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (21:0,21 [11] x:\dir\subdir\Test\TestComponent.cshtml) +|myComponent| +Generated Location: (1045:27,21 [11] ) +|myComponent| + +Source Location: (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml) +| + private TestComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| +Generated Location: (1437:43,7 [111] ) +| + private TestComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| + diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.codegen.cs new file mode 100644 index 00000000000..66e16f168cd --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.codegen.cs @@ -0,0 +1,85 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 219 + private void __RazorDirectiveTokenHelpers__() { + } + #pragma warning restore 219 + #pragma warning disable 0414 + private static object __o = null; + #pragma warning restore 0414 + #pragma warning disable 1998 + protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) + { + var __typeInference_CreateMyComponent_0 = global::__Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(__builder, -1, -1, +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + 1 + +#line default +#line hidden +#nullable disable + , -1, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myComponent = __value; + +#line default +#line hidden +#nullable disable + } + ); + __o = __typeInference_CreateMyComponent_0. +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + MyParameter + +#line default +#line hidden +#nullable disable + ; +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +__o = typeof(global::Test.MyComponent<>); + +#line default +#line hidden +#nullable disable + } + #pragma warning restore 1998 +#nullable restore +#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" + + private MyComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } + +#line default +#line hidden +#nullable disable + } +} +namespace __Blazor.Test.TestComponent +{ + #line hidden + internal static class TypeInference + { + public static global::Test.MyComponent CreateMyComponent_0(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, T __arg0, int __seq1, System.Action> __arg1) + { + __builder.OpenComponent>(seq); + __builder.AddAttribute(__seq0, "MyParameter", __arg0); + __builder.AddComponentReferenceCapture(__seq1, (__value) => { __arg1((global::Test.MyComponent)__value); }); + __builder.CloseComponent(); + return default; + } + } +} +#pragma warning restore 1591 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.ir.txt new file mode 100644 index 00000000000..88be72d3faa --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.ir.txt @@ -0,0 +1,27 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [12] ) - System + UsingDirective - (18:2,1 [32] ) - System.Collections.Generic + UsingDirective - (53:3,1 [17] ) - System.Linq + UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase - + DesignTimeDirective - + CSharpCode - + IntermediateToken - - CSharp - #pragma warning disable 0414 + CSharpCode - + IntermediateToken - - CSharp - private static object __o = null; + CSharpCode - + IntermediateToken - - CSharp - #pragma warning restore 0414 + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [50] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ReferenceCapture - (19:0,19 [11] x:\dir\subdir\Test\TestComponent.cshtml) - myComponent + ComponentAttribute - (45:0,45 [1] x:\dir\subdir\Test\TestComponent.cshtml) - MyParameter - MyParameter - AttributeStructure.DoubleQuotes + LazyIntermediateToken - (45:0,45 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 1 + HtmlContent - (50:0,50 [4] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (50:0,50 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n + CSharpCode - (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent myComponent = null!;\n public void Use() { System.GC.KeepAlive(myComponent); }\n + NamespaceDeclaration - - __Blazor.Test.TestComponent + ClassDeclaration - - internal static - TypeInference - - + ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.mappings.txt new file mode 100644 index 00000000000..83ae92fc576 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.mappings.txt @@ -0,0 +1,26 @@ +Source Location: (45:0,45 [1] x:\dir\subdir\Test\TestComponent.cshtml) +|1| +Generated Location: (1051:25,45 [1] ) +|1| + +Source Location: (19:0,19 [11] x:\dir\subdir\Test\TestComponent.cshtml) +|myComponent| +Generated Location: (1227:33,19 [11] ) +|myComponent| + +Source Location: (32:0,32 [11] x:\dir\subdir\Test\TestComponent.cshtml) +|MyParameter| +Generated Location: (1490:43,32 [11] ) +|MyParameter| + +Source Location: (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml) +| + private MyComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| +Generated Location: (1857:60,7 [114] ) +| + private MyComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| + diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs index 57b4c725a83..0c75b1428b6 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.codegen.cs @@ -26,7 +26,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. )); #nullable restore #line 1 "x:\dir\subdir\Test\TestComponent.cshtml" - myInstance = default(global::Test.MyComponent); + myInstance = default(global::Test.MyComponent)!; #line default #line hidden diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt index 30f29389a7d..5c315dcca50 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithRef_WithChildContent/TestComponent.mappings.txt @@ -8,7 +8,7 @@ Source Location: (108:4,7 [104] x:\dir\subdir\Test\TestComponent.cshtml) private Test.MyComponent myInstance; public void Foo() { System.GC.KeepAlive(myInstance); } | -Generated Location: (1452:44,7 [104] ) +Generated Location: (1453:44,7 [104] ) | private Test.MyComponent myInstance; public void Foo() { System.GC.KeepAlive(myInstance); } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs index d9ddda62d76..3d4076acf5c 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.codegen.cs @@ -22,7 +22,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. { #nullable restore #line 1 "x:\dir\subdir\Test\TestComponent.cshtml" - myElem = default(Microsoft.AspNetCore.Components.ElementReference); + myElem = default(Microsoft.AspNetCore.Components.ElementReference)!; #line default #line hidden diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt index 312650039db..5eb383394e2 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef/TestComponent.mappings.txt @@ -8,7 +8,7 @@ Source Location: (91:2,7 [128] x:\dir\subdir\Test\TestComponent.cshtml) private Microsoft.AspNetCore.Components.ElementReference myElem; public void Foo() { System.GC.KeepAlive(myElem); } | -Generated Location: (1143:33,7 [128] ) +Generated Location: (1144:33,7 [128] ) | private Microsoft.AspNetCore.Components.ElementReference myElem; public void Foo() { System.GC.KeepAlive(myElem); } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs index 71f1130e17f..beedbf59c25 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.codegen.cs @@ -31,7 +31,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. ; #nullable restore #line 1 "x:\dir\subdir\Test\TestComponent.cshtml" - _element = default(Microsoft.AspNetCore.Components.ElementReference); + _element = default(Microsoft.AspNetCore.Components.ElementReference)!; #line default #line hidden diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt index 85e5f66b8fb..da11f7b54d3 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Element_WithRef_AndOtherAttributes/TestComponent.mappings.txt @@ -15,7 +15,7 @@ Source Location: (72:2,7 [164] x:\dir\subdir\Test\TestComponent.cshtml) [Parameter] public int Min { get; set; } public void Foo() { System.GC.KeepAlive(_element); } | -Generated Location: (1354:42,7 [164] ) +Generated Location: (1355:42,7 [164] ) | private ElementReference _element; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.codegen.cs index 1429ae56b2d..33b86fc921d 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.codegen.cs @@ -43,7 +43,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components. )); #nullable restore #line 1 "x:\dir\subdir\Test\TestComponent.cshtml" - _my = default(global::Test.MyComponent); + _my = default(global::Test.MyComponent)!; #line default #line hidden diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.mappings.txt index 300df4c3ee9..10206cac394 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/GenericComponent_WithComponentRef_CreatesDiagnostic/TestComponent.mappings.txt @@ -15,7 +15,7 @@ Generated Location: (1524:45,38 [3] ) Source Location: (23:0,23 [4] x:\dir\subdir\Test\TestComponent.cshtml) |Item| -Generated Location: (1775:53,23 [4] ) +Generated Location: (1776:53,23 [4] ) |Item| Source Location: (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) @@ -23,7 +23,7 @@ Source Location: (56:2,7 [90] x:\dir\subdir\Test\TestComponent.cshtml) private MyComponent _my; public void Foo() { System.GC.KeepAlive(_my); } | -Generated Location: (2135:70,7 [90] ) +Generated Location: (2136:70,7 [90] ) | private MyComponent _my; public void Foo() { System.GC.KeepAlive(_my); } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.codegen.cs new file mode 100644 index 00000000000..37f8eb49d50 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.codegen.cs @@ -0,0 +1,41 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 1998 + protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) + { + __builder.OpenComponent(0); + __builder.AddComponentReferenceCapture(1, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myComponent = (Test.TestComponent)__value; + +#line default +#line hidden +#nullable disable + } + ); + __builder.CloseComponent(); + } + #pragma warning restore 1998 +#nullable restore +#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" + + private TestComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } + +#line default +#line hidden +#nullable disable + } +} +#pragma warning restore 1591 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.ir.txt new file mode 100644 index 00000000000..29d0db6a84e --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.ir.txt @@ -0,0 +1,13 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [36] x:\dir\subdir\Test\TestComponent.cshtml) - TestComponent + ReferenceCapture - (21:0,21 [11] x:\dir\subdir\Test\TestComponent.cshtml) - myComponent + CSharpCode - (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private TestComponent myComponent = null!;\n public void Use() { System.GC.KeepAlive(myComponent); }\n diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.mappings.txt new file mode 100644 index 00000000000..5b59b175caa --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (21:0,21 [11] x:\dir\subdir\Test\TestComponent.cshtml) +|myComponent| +Generated Location: (760:19,21 [11] ) +|myComponent| + +Source Location: (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml) +| + private TestComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| +Generated Location: (1052:31,7 [111] ) +| + private TestComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| + diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.codegen.cs new file mode 100644 index 00000000000..b967734f501 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.codegen.cs @@ -0,0 +1,61 @@ +// +#pragma warning disable 1591 +namespace Test +{ + #line hidden + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Components; + public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase + { + #pragma warning disable 1998 + protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) + { + global::__Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(__builder, 0, 1, +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + 1 + +#line default +#line hidden +#nullable disable + , 2, (__value) => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" + myComponent = __value; + +#line default +#line hidden +#nullable disable + } + ); + } + #pragma warning restore 1998 +#nullable restore +#line 3 "x:\dir\subdir\Test\TestComponent.cshtml" + + private MyComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } + +#line default +#line hidden +#nullable disable + } +} +namespace __Blazor.Test.TestComponent +{ + #line hidden + internal static class TypeInference + { + public static void CreateMyComponent_0(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, T __arg0, int __seq1, System.Action> __arg1) + { + __builder.OpenComponent>(seq); + __builder.AddAttribute(__seq0, "MyParameter", __arg0); + __builder.AddComponentReferenceCapture(__seq1, (__value) => { __arg1((global::Test.MyComponent)__value); }); + __builder.CloseComponent(); + } + } +} +#pragma warning restore 1591 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.ir.txt new file mode 100644 index 00000000000..8c6774242aa --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.ir.txt @@ -0,0 +1,18 @@ +Document - + NamespaceDeclaration - - Test + UsingDirective - (3:1,1 [14] ) - System + UsingDirective - (18:2,1 [34] ) - System.Collections.Generic + UsingDirective - (53:3,1 [19] ) - System.Linq + UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks + UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components + ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase - + MethodDeclaration - - protected override - void - BuildRenderTree + Component - (0:0,0 [50] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent + ReferenceCapture - (19:0,19 [11] x:\dir\subdir\Test\TestComponent.cshtml) - myComponent + ComponentAttribute - (45:0,45 [1] x:\dir\subdir\Test\TestComponent.cshtml) - MyParameter - MyParameter - AttributeStructure.DoubleQuotes + LazyIntermediateToken - (45:0,45 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 1 + CSharpCode - (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml) + LazyIntermediateToken - (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent myComponent = null!;\n public void Use() { System.GC.KeepAlive(myComponent); }\n + NamespaceDeclaration - - __Blazor.Test.TestComponent + ClassDeclaration - - internal static - TypeInference - - + ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0 diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.mappings.txt new file mode 100644 index 00000000000..b7259814ccb --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/Component_WithRef_Nullable_Generic/TestComponent.mappings.txt @@ -0,0 +1,16 @@ +Source Location: (19:0,19 [11] x:\dir\subdir\Test\TestComponent.cshtml) +|myComponent| +Generated Location: (921:26,19 [11] ) +|myComponent| + +Source Location: (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml) +| + private MyComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| +Generated Location: (1152:37,7 [114] ) +| + private MyComponent myComponent = null!; + public void Use() { System.GC.KeepAlive(myComponent); } +| + diff --git a/src/Compiler/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs b/src/Compiler/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs index 4a9f8576f21..456446e831d 100644 --- a/src/Compiler/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs +++ b/src/Compiler/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorIntegrationTestBase.cs @@ -176,12 +176,30 @@ internal RazorProjectItem CreateProjectItem(string cshtmlRelativePath, string cs }; } - protected CompileToCSharpResult CompileToCSharp(string cshtmlContent, bool throwOnFailure = true, string cssScope = null, bool supportLocalizedComponentNames = false) + protected CompileToCSharpResult CompileToCSharp( + string cshtmlContent, + bool throwOnFailure = true, + string cssScope = null, + bool supportLocalizedComponentNames = false, + bool nullableEnable = false) { - return CompileToCSharp(DefaultFileName, cshtmlContent, throwOnFailure, cssScope: cssScope, supportLocalizedComponentNames: supportLocalizedComponentNames); + return CompileToCSharp( + DefaultFileName, + cshtmlContent, + throwOnFailure, + cssScope: cssScope, + supportLocalizedComponentNames: supportLocalizedComponentNames, + nullableEnable: nullableEnable); } - protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, string cshtmlContent, bool throwOnFailure = true, string fileKind = null, string cssScope = null, bool supportLocalizedComponentNames = false) + protected CompileToCSharpResult CompileToCSharp( + string cshtmlRelativePath, + string cshtmlContent, + bool throwOnFailure = true, + string fileKind = null, + string cssScope = null, + bool supportLocalizedComponentNames = false, + bool nullableEnable = false) { if (DeclarationOnly && DesignTime) { @@ -193,6 +211,13 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin throw new InvalidOperationException($"{nameof(DeclarationOnly)} cannot be used with {nameof(UseTwoPhaseCompilation)}."); } + var baseCompilation = BaseCompilation; + + if (nullableEnable) + { + baseCompilation = baseCompilation.WithOptions(baseCompilation.Options.WithNullableContextOptions(NullableContextOptions.Enable)); + } + if (UseTwoPhaseCompilation) { // The first phase won't include any metadata references for component discovery. This mirrors @@ -215,7 +240,7 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin codeDocument = projectEngine.ProcessDeclarationOnly(projectItem); var declaration = new CompileToCSharpResult { - BaseCompilation = BaseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees), + BaseCompilation = baseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees), CodeDocument = codeDocument, Code = codeDocument.GetCSharpDocument().GeneratedCode, Diagnostics = codeDocument.GetCSharpDocument().Diagnostics, @@ -225,7 +250,7 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin var tempAssembly = CompileToAssembly(declaration, throwOnFailure); // Add the 'temp' compilation as a metadata reference - var references = BaseCompilation.References.Concat(new[] { tempAssembly.Compilation.ToMetadataReference() }).ToArray(); + var references = baseCompilation.References.Concat(new[] { tempAssembly.Compilation.ToMetadataReference() }).ToArray(); projectEngine = CreateProjectEngine(Configuration, references, supportLocalizedComponentNames); // Now update the any additional files @@ -245,7 +270,7 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin codeDocument = DesignTime ? projectEngine.ProcessDesignTime(projectItem) : projectEngine.Process(projectItem); return new CompileToCSharpResult { - BaseCompilation = BaseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees), + BaseCompilation = baseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees), CodeDocument = codeDocument, Code = codeDocument.GetCSharpDocument().GeneratedCode, Diagnostics = codeDocument.GetCSharpDocument().Diagnostics, @@ -255,7 +280,7 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin { // For single phase compilation tests just use the base compilation's references. // This will include the built-in components. - var projectEngine = CreateProjectEngine(Configuration, BaseCompilation.References.ToArray(), supportLocalizedComponentNames); + var projectEngine = CreateProjectEngine(Configuration, baseCompilation.References.ToArray(), supportLocalizedComponentNames); var projectItem = CreateProjectItem(cshtmlRelativePath, cshtmlContent, fileKind, cssScope); @@ -275,7 +300,7 @@ protected CompileToCSharpResult CompileToCSharp(string cshtmlRelativePath, strin return new CompileToCSharpResult { - BaseCompilation = BaseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees), + BaseCompilation = baseCompilation.AddSyntaxTrees(AdditionalSyntaxTrees), CodeDocument = codeDocument, Code = codeDocument.GetCSharpDocument().GeneratedCode, Diagnostics = codeDocument.GetCSharpDocument().Diagnostics,