Skip to content

Compiler or analyzer should warn for lower-cased type names #56653

Closed

Description

@jcouv

During LDM discussion on records, there was interest to have an analyzer warn on lower-cased type names. The motivation is that knowing that we discourage lower-cased type names would allow us to introduce new keywords (like record) with less concern about the breaking change.

I'm not sure whether this should be done by the compiler, a roslyn analyzer or some BCL analyzer.

Design:

  • We use char.IsLower(...) for lower-case check.
  • @ prefix avoids warning.
  • All named types, included delegate types, are checked.
  • Type and namespace aliases are checked too.

FYI @mikadumont @jmarolf

Activity

  1. ghost added
    untriagedIssues and PRs which have not yet been triaged by a lead
    on Sep 23, 2021
  2. CyrusNajmabadi commented on Sep 23, 2021

    @CyrusNajmabadi
    Contributor

    If this is about the language and our ability to add such types in a more confident manner, then i think this should be part of the compiler proper. That way every gets it, and everyone sees that this is the official guidance from the team on what you should/shouldn't do.

  3. jcouv commented on Sep 23, 2021

    @jcouv
    ContributorAuthor

    Just realized, one issue we'll have to figure out is how we define "lower-case".

  4. CyrusNajmabadi commented on Sep 23, 2021

    @CyrusNajmabadi
    Contributor

    All(char.IsLower)?

  5. jmarolf commented on Sep 23, 2021

    @jmarolf
    Contributor

    Agree with @CyrusNajmabadi that the ideal here is for the compiler to emit a new warning as part of warning wave 7. If that cannot happen then we can consider shipping it as an analyzer. Considering that this is a fundamental part of C# it would seem to be appropriate for the dotnet/roslyn repo since dotnet/roslyn-analyzers is for warnings about .NET libraries instead of core language concepts.

  6. jcouv commented on Sep 24, 2021

    @jcouv
    ContributorAuthor

    @jaredpar Should we contemplate doing this as part of warning wave 6 (ie. in .NET 6)?

  7. Youssef1313 commented on Sep 26, 2021

    @Youssef1313
    Member

    Will prefixing with @ remove the warning?

    Otherwise, I can't think of a good workaround, for example, for Xamarin's System.nuint:

    image

  8. jcouv commented on Sep 26, 2021

    @jcouv
    ContributorAuthor

    From discussion with Jared, it's too late for .NET 6. We could make this any early preview for release after that (such as 17.1).

    @Youssef1313 That's an excellent suggestion. It would make sense given that the @ prefix allows you to use other keywords as identifiers.

  9. added this to the 17.1 milestone on Sep 27, 2021
  10. JoeRobich commented on Sep 27, 2021

    @JoeRobich
    Member

    Design Meeting Notes:

    • Can this be covered by the NamingStyleAnalyzer?
      • By default these diagnostics are suggestions and compiler would prefer warnings.
      • Naming styles are configurable which is at odds with what the compiler is asking for.

    Proposal: Keep IDE analyzers as they are. The compiler will add a diagnostic behind a warning wave for VS 17.1 Preview 1.

  11. self-assigned this
    on Sep 28, 2021
  12. 27 remaining items

  13. soc commented on Apr 12, 2022

    @soc

    So when compiling the BCL we'll get warnings for int, float, string?

  14. CyrusNajmabadi commented on Apr 12, 2022

    @CyrusNajmabadi
    Contributor

    So when compiling the BCL we'll get warnings for int, float, string?

    No. The bcl does not define those. So there's nothing to warn on.

  15. 333fred commented on Apr 12, 2022

    @333fred
    Member

    I think there might be a bit of confusion: int, float, string, and all the rest of the keywords on https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types are not type names. They are C# keywords. The actual name for string is System.String, which does indeed follow this convention.

  16. soc commented on Apr 12, 2022

    @soc

    @CyrusNajmabadi Then "warn for lower-cased type names, except our own" or "warn for lower-cased type names, except for type keywords" would be correct, but the title as given is misleading.

  17. soc commented on Apr 12, 2022

    @soc

    @333fred I'm not confused thank you.

  18. Youssef1313 commented on Apr 12, 2022

    @Youssef1313
    Member

    There is really no exception. int (and friends) are keywords. They're not type names (the corresponding type names are System.Int32 and friends - as @333fred pointed out). The compiler warns for lower-cased type names.

  19. soc commented on Apr 12, 2022

    @soc

    @Youssef1313 If you discuss type names then the "type-ness" of "type keywords" is more relevant that their "keyword-ness".

  20. CyrusNajmabadi commented on Apr 12, 2022

    @CyrusNajmabadi
    Contributor

    @CyrusNajmabadi Then "warn for lower-cased type names, except our own"

    Those are not type-names. They are built in keywords. We're not warning on keywords here, only type names. Thanks :)

    but the title as given is misleading.

    The title is accurate (as is the linked issue). You're welcome to ask for clarity if the terminology isn't something you're totally familiar with of course.

    In this case though your point you raised isn't valid: "Do not make up rules for language users that you, as a language developer, don't intend to follow."

    We do you define type names in the language. That comes externally. We do define keywords, and we want to increase the likelihood that our keywords will not collide with user type names.

    @Youssef1313 If you discuss type names then the "type-ness" of "type keywords" is more relevant that their "keyword-ness".

    I'm not sure what point you're trying to make here. We're warning on lowercase type names because that's the space that we want to reserve for us for keywords (type or otherwise).

  21. soc commented on Apr 12, 2022

    @soc
  22. CyrusNajmabadi commented on Apr 12, 2022

    @CyrusNajmabadi
    Contributor

    @soc the relevance is the 'keyword-ness'. We want to be able to make keywords that do not collide with user types. It has nothing to do with the keyword being type-like (like int). For example, even basic non-type keywords (like record, which doesn't correspond to any .net type**) had a problem because it might collide with a user type. I think you're not understanding the reason for this work, or how it relates to the language so far, or what we're considering in the future.

    ** Other examples are things like notnull, or unmanaged, again, not aliases for some .Net type. They're just things we want as identifiers we can use as keywords safely, but which we have to expend extra implementation energy on as someone may have themselves defined a type with this name.

  23. davidwengier commented on Apr 12, 2022

    @davidwengier
    Member

    Is there already a recommended time table on which users can start replacing int, float, string and friends?

    If you or anyone else wants to disallow using language keywords like these in your own projects, you can enable this style rule and make it a warning or error, as you see fit: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0049

  24. moved this to Active/Investigating in Compiler: Julien's umbrellason Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions