From 39333ab5a9e0b733c9713e166e88b75731ffbc9f Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Mon, 21 Oct 2024 14:48:50 +0200 Subject: [PATCH] Avoid ambiguous `object` reference in generic component recovery --- .../ComponentCodeGenerationTestBase.cs | 23 +++++++++++++++++++ .../src/CSharp/GenericTypeNameRewriter.cs | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) 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 b64479e99a5..f75c2760e10 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs @@ -9002,6 +9002,29 @@ public class MyComponent : ComponentBase CompileToAssembly(generated); } + [IntegrationTestFact] + public void GenericComponent_MissingTypeParameter_SystemInNamespace() + { + // Arrange + AdditionalSyntaxTrees.Add(Parse(""" + using Microsoft.AspNetCore.Components; + namespace Test; + public class MyComponent : ComponentBase; + """)); + + // Act + var generated = CompileToCSharp(""" + @namespace Test.System + + """); + + // Assert + CompileToAssembly(generated); + generated.RazorDiagnostics.Verify( + // x:\dir\subdir\Test\TestComponent.cshtml(2,1): error RZ10001: The type of component 'MyComponent' cannot be inferred based on the values provided. Consider specifying the type arguments directly using the following attributes: 'TItem'. + Diagnostic("RZ10001").WithLocation(2, 1)); + } + [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10827")] public void GenericTypeCheck() { diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/GenericTypeNameRewriter.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/GenericTypeNameRewriter.cs index b9c02b354d2..d07fb481539 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/GenericTypeNameRewriter.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/GenericTypeNameRewriter.cs @@ -48,7 +48,7 @@ public override SyntaxNode Visit(SyntaxNode node) // compared to leaving the type parameter in place. // // We add our own diagnostics for missing/invalid type parameters anyway. - var replacement = binding == null ? typeof(object).FullName : binding; + var replacement = binding ?? "object"; return identifier.Update(SyntaxFactory.Identifier(replacement).WithTriviaFrom(identifier.Identifier)); } }