The Wayback Machine - https://web.archive.org/web/20220207230142/https://github.com/dotnet/aspnetcore/issues/40049
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

Razor parser confused by using reserved keywords as identifiers #40049

Open
1 task done
JakubJanowski opened this issue Feb 7, 2022 · 0 comments
Open
1 task done

Razor parser confused by using reserved keywords as identifiers #40049

JakubJanowski opened this issue Feb 7, 2022 · 0 comments

Comments

@JakubJanowski
Copy link

@JakubJanowski JakubJanowski commented Feb 7, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Identifiers can be prefixed with @ to use a reserved keyword as a name. This creates a problem when the identifier with the @ prefix is itself a keyword.

Having declared a class like this:

class @using {}

and writing in CSHTML file the following, it does not compile.

@{
    @using b = new @using();
}

This however does compile for some reason.

@{
    var a = new @using();
    @using b = new @using();
}

The errors that I'm getting when it's not compiling are:

  • error CS1041: Identifier expected; 'new' is a keyword,
  • error CS0116: A namespace cannot directly contain members such as fields, methods or statements,
  • error CS1022: Type or namespace definition, or end-of-file expected,
  • error CS1525: Invalid expression term ')'.
    Which suggests that it is recognising the @using as a directive for importing namespaces.

I'm noticing similar behavior with @addRazorTag, @if, @model, etc. The workaround is simple and obvious (don't use reserved keywords as identifiers) so it doesn't bother me much. Just wanted to document this here as it defeats the purpose of having such a feature in the first place.

Expected Behavior

Both code samples should compile and recognize b to be an instance of the @using class.

Steps To Reproduce

  1. In an ASP.NET Core Web App project create new Razor page file with the below contents:
@page
@{
    @functions {
        class @using {}
    }
    //var a = new @using();
    @using b = new @using();
}
  1. Uncomment the line to see it compile.

Exceptions (if any)

No response

.NET Version

6.0.101

Anything else?

No response

@JakubJanowski JakubJanowski changed the title Razor parser confused by using reserved keywords as type names Razor parser confused by using reserved keywords as identifiers Feb 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant