dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
502 stars 195 forks source link

C# 9.0 Range switch does not work in blazor @code template #8351

Open nothrow opened 1 year ago

nothrow commented 1 year ago

Is there an existing issue for this?

Describe the bug

Using expr switch { <0 => something } statement with ranges inside @code{} does not compile. the < character in switch apparently confuses tokenizer. using regular switch expr { case <0: return something; } works.

Expected Behavior

I'd expect the page to compile

Steps To Reproduce

@page "/test"

@Sign(1)

@code
{
    private static int Sign(int number)
    {
        return number switch
        {
            < 0 => -1,
            0 => 0,
            > 0 => +1
        };
    }
}

Exceptions (if any)

Test.razor(5,1,5,2): error RZ1006: The code block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
Test.razor(6,1,6,2): error RZ1006: The code block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
Test.razor(11,13,11,19): error RZ9980: Unclosed tag '' with no matching end tag.
Test.razor(11,14,11,14): error RZ1025: The "" element was not closed.  All elements must be either self-closing or have a matching end tag.
Test.razor(7,5,7,12): error CS0106: The modifier 'private' is not valid for this item

.NET Version

7.0.200

Anything else?

ASP.NET Core version: 7.0.3

XorZy commented 1 year ago

Still present on net 8 RC1. Adding parentheses solves the problem but it still looks like a bug to me.

meenzen commented 8 months ago

I just ran into this. The compiler errors cause massive debugging headaches, I was only able to find this to be the culprit after a lot of trial and error.

snow-jallen commented 1 month ago

I thought the razor compiler was supposed to be getting better? Needing to wrap my patterns in parentheses is pretty lame when it's perfectly valid c# syntax without them. :(