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

Avoid ambiguous object reference in generic component recovery #11053

Merged
merged 2 commits into from
Oct 22, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9002,6 +9002,29 @@ public class MyComponent<TService, TKey> : ComponentBase
CompileToAssembly(generated);
}

[IntegrationTestFact]
public void GenericComponent_MissingTypeParameter_SystemInNamespace()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse("""
using Microsoft.AspNetCore.Components;
namespace Test;
public class MyComponent<TItem> : ComponentBase;
"""));

// Act
var generated = CompileToCSharp("""
@namespace Test.System
<MyComponent />
""");

// 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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ internal static class TypeInference
__builder.CloseComponent();
return default;
}
public static global::Test.Column<System.Object> CreateColumn_1<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
public static global::Test.Column<object> CreateColumn_1<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
{
__builder.OpenComponent<global::Test.Column<System.Object>>(seq);
__builder.OpenComponent<global::Test.Column<object>>(seq);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ namespace __Blazor.Test.TestComponent
#line hidden
internal static class TypeInference
{
public static global::Test.Column<System.Object> CreateColumn_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
public static global::Test.Column<object> CreateColumn_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
{
__builder.OpenComponent<global::Test.Column<System.Object>>(seq);
__builder.OpenComponent<global::Test.Column<object>>(seq);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public static void CreateGrid_0_CaptureParameters<TItem, TUnrelated>(global::Sys
{
__arg0_out = __arg0;
}
public static global::Test.Column<System.Object> CreateColumn_1<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
public static global::Test.Column<object> CreateColumn_1<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
{
__builder.OpenComponent<global::Test.Column<System.Object>>(seq);
__builder.OpenComponent<global::Test.Column<object>>(seq);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ namespace __Blazor.Test.TestComponent
#line hidden
internal static class TypeInference
{
public static global::Test.MyComponent<T, System.Object> CreateMyComponent_0<T, T2>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::Microsoft.AspNetCore.Components.EventCallback<T> __arg0)
public static global::Test.MyComponent<T, object> CreateMyComponent_0<T, T2>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::Microsoft.AspNetCore.Components.EventCallback<T> __arg0)
{
__builder.OpenComponent<global::Test.MyComponent<T, System.Object>>(seq);
__builder.OpenComponent<global::Test.MyComponent<T, object>>(seq);
__builder.AddAttribute(__seq0, "OnClick", (object)__arg0);
__builder.CloseComponent();
return default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ namespace __Blazor.Test.TestComponent
#line hidden
internal static class TypeInference
{
public static global::Test.MyComponent<System.Object, System.Object> CreateMyComponent_0<T, T2>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Object __arg0)
public static global::Test.MyComponent<object, object> CreateMyComponent_0<T, T2>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Object __arg0)
{
__builder.OpenComponent<global::Test.MyComponent<System.Object, System.Object>>(seq);
__builder.OpenComponent<global::Test.MyComponent<object, object>>(seq);
__builder.AddAttribute(__seq0, "OnClick", (object)__arg0);
__builder.CloseComponent();
return default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void CreateGrid_0<TItem>(global::Microsoft.AspNetCore.Components.R
}
public static void CreateColumn_1<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
{
__builder.OpenComponent<global::Test.Column<System.Object>>(seq);
__builder.OpenComponent<global::Test.Column<object>>(seq);
__builder.CloseComponent();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static class TypeInference
{
public static void CreateColumn_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
{
__builder.OpenComponent<global::Test.Column<System.Object>>(seq);
__builder.OpenComponent<global::Test.Column<object>>(seq);
__builder.CloseComponent();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void CreateGrid_0_CaptureParameters<TItem, TUnrelated>(global::Sys
}
public static void CreateColumn_1<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq)
{
__builder.OpenComponent<global::Test.Column<System.Object>>(seq);
__builder.OpenComponent<global::Test.Column<object>>(seq);
__builder.CloseComponent();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ internal static class TypeInference
{
public static void CreateMyComponent_0<T, T2>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::Microsoft.AspNetCore.Components.EventCallback<T> __arg0)
{
__builder.OpenComponent<global::Test.MyComponent<T, System.Object>>(seq);
__builder.AddComponentParameter(__seq0, nameof(global::Test.MyComponent<T, System.Object>.
__builder.OpenComponent<global::Test.MyComponent<T, object>>(seq);
__builder.AddComponentParameter(__seq0, nameof(global::Test.MyComponent<T, object>.
#nullable restore
#line (1,14)-(1,21) "x:\dir\subdir\Test\TestComponent.cshtml"
OnClick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Generated Location: (783:23,0 [28] )

Source Location: (13:0,13 [7] x:\dir\subdir\Test\TestComponent.cshtml)
|OnClick|
Generated Location: (1477:43,0 [7] )
Generated Location: (1463:43,0 [7] )
|OnClick|

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal static class TypeInference
{
public static void CreateMyComponent_0<T, T2>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Object __arg0)
{
__builder.OpenComponent<global::Test.MyComponent<System.Object, System.Object>>(seq);
__builder.OpenComponent<global::Test.MyComponent<object, object>>(seq);
__builder.AddComponentParameter(__seq0, "OnClick", __arg0);
__builder.CloseComponent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class GenericTypeNameRewriterTest
[Theory]
[InlineData("TItem2", "Type2")]

// Unspecified argument -> System.Object
[InlineData("TItem3", "System.Object")]
// Unspecified argument -> object
[InlineData("TItem3", "object")]

// Not a type parameter
[InlineData("TItem4", "TItem4")]
Expand All @@ -24,8 +24,8 @@ public class GenericTypeNameRewriterTest
[InlineData("TItem1.TItem2", "TItem1.TItem2")]

// Type parameters can't have type parameters
[InlineData("TItem1.TItem2<TItem1, TItem2, TItem3>", "TItem1.TItem2<Type1, Type2, System.Object>")]
[InlineData("TItem2<TItem1<TItem3>, System.TItem2, RenderFragment<List<TItem1>>", "TItem2<TItem1<System.Object>, System.TItem2, RenderFragment<List<Type1>>")]
[InlineData("TItem1.TItem2<TItem1, TItem2, TItem3>", "TItem1.TItem2<Type1, Type2, object>")]
[InlineData("TItem2<TItem1<TItem3>, System.TItem2, RenderFragment<List<TItem1>>", "TItem2<TItem1<object>, System.TItem2, RenderFragment<List<Type1>>")]

// Tuples
[InlineData("List<(TItem1 X, TItem2 Y)>", "List<(Type1 X, Type2 Y)>")]
Expand Down
Loading