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

[Blazor] CascadingTypeParameter problem #38479

Closed
daniel-p-tech opened this issue Nov 17, 2021 · 5 comments · Fixed by dotnet/razor-compiler#44
Closed

[Blazor] CascadingTypeParameter problem #38479

daniel-p-tech opened this issue Nov 17, 2021 · 5 comments · Fixed by dotnet/razor-compiler#44
Labels
area-razor.compiler This issue is related to the Razor compiler (now external) bug This issue describes a behavior which is not expected - a bug. feature-blazor-builtin-components Features related to the built in components we ship or could ship in the future Priority:1 Work that is critical for the release, but we could probably ship without
Milestone

Comments

@daniel-p-tech
Copy link

daniel-p-tech commented Nov 17, 2021

Hi,

I'm trying to refactor my existing components to use the newly introduced CascadingTypeParameter attribute but I'm running into compiler errors.

BlzTableGrid.razor (this is the main grid component)

@attribute [CascadingTypeParameter(nameof(TItem))]
@typeparam TItem

<CascadingValue Value="this" IsFixed="true">
    @ColumnsTemplate
</CascadingValue>
...

BlzTableGrid.razor.cs

public partial class BlzTableGrid<TItem> where TItem : class
{
    [Parameter]
    public RenderFragment ColumnsTemplate { get; set; }
...

BlzGridBaseColumn.cs

public abstract partial class BlzGridBaseColumn<TItem> : ComponentBase where TItem : class
{
    [CascadingParameter]
    internal BlzTableGrid<TItem> Grid { get; set; }
...

BlzGridColumn.cs

public class BlzGridColumn<TItem> : BlzGridBaseColumn<TItem>, IGridFieldColumn<TItem> where TItem : class
{
    [Parameter]
    public string FieldName { get; set; }
...

TableGrid.razor (page consuming the grid component)

<BlzTableGrid
    TItem="WeatherForecastLookup"
    Items="WeatherForecasts"

    <ColumnsTemplate>
        <BlzGridEditColumn />
        <BlzGridDeleteColumn />
        <BlzGridViewColumn />
        <BlzGridColumn Title="Date" FieldName="@nameof(WeatherForecastLookup.Date)" Format="d" Width="10rem" />
...

I'm getting the following compiler error for what seems to be each column in TableGrid.razor page:

>C:\Projects\Blazor-Components\DemoApp\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Pages_Grid_TableGrid_razor.g.cs(563,29,563,34): error CS0449: The 'class', 'struct', 'unmanaged', 'notnull', and 'default' constraints cannot be combined or duplicated, and must be specified first in the constraints list.

Thank you for your help.

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components feature-blazor-builtin-components Features related to the built in components we ship or could ship in the future labels Nov 17, 2021
@mkArtakMSFT mkArtakMSFT added area-razor.compiler This issue is related to the Razor compiler (now external) and removed area-blazor Includes: Blazor, Razor Components labels Nov 18, 2021
@mkArtakMSFT mkArtakMSFT added this to the .NET 7 Planning milestone Nov 18, 2021
@mkArtakMSFT mkArtakMSFT added bug This issue describes a behavior which is not expected - a bug. Priority:1 Work that is critical for the release, but we could probably ship without triaged labels Nov 18, 2021
@daniel-p-tech
Copy link
Author

@mkArtakMSFT I see that this issue has been assigned to the .NET 7 milestone. Does it mean that I will have to wait a year until the problem is resolved? I suspect other 3rd party component vendors will want to refactor their libraries to take advantage of the new feature and from what I've seen their grid components are structured in a similar fashion.

Thanks.

@ArgoZhang
Copy link

ArgoZhang commented Nov 29, 2021

@daniel-p-tech same error!

@javiercn
Copy link
Member

The generated code seems to be incorrect and incorrectly adds a comma, before the class constraint.

internal static class TypeInference
{
    public static void CreateBlzGridColumn_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, TItem __syntheticArg0, int __seq0, System.Object __arg0, int __seq1, global::System.String __arg1, int __seq2, System.Object __arg2, int __seq3, System.Object __arg3)
        where TItem : , class
    {
    __builder.OpenComponent<global::BlazorApp66.BlzGridColumn<TItem>>(seq);
    __builder.AddAttribute(__seq0, "Title", __arg0);
    __builder.AddAttribute(__seq1, "FieldName", __arg1);
    __builder.AddAttribute(__seq2, "Format", __arg2);
    __builder.AddAttribute(__seq3, "Width", __arg3);
    __builder.CloseComponent();
    }
}

@javiercn
Copy link
Member

Seems like the constraint is not being correctly represented in the intermediate node

image

@javiercn
Copy link
Member

Seems that this happens when we construct the constraint

image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-razor.compiler This issue is related to the Razor compiler (now external) bug This issue describes a behavior which is not expected - a bug. feature-blazor-builtin-components Features related to the built in components we ship or could ship in the future Priority:1 Work that is critical for the release, but we could probably ship without
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants