You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiler or analyzer should warn for lower-cased type names #56653
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.
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.
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.
@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.
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.
@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).
@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.
Activity
CyrusNajmabadi commentedon Sep 23, 2021
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.
jcouv commentedon Sep 23, 2021
Just realized, one issue we'll have to figure out is how we define "lower-case".
CyrusNajmabadi commentedon Sep 23, 2021
All(char.IsLower)?
jmarolf commentedon Sep 23, 2021
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.
jcouv commentedon Sep 24, 2021
@jaredpar Should we contemplate doing this as part of warning wave 6 (ie. in .NET 6)?
Youssef1313 commentedon Sep 26, 2021
Will prefixing with
@remove the warning?Otherwise, I can't think of a good workaround, for example, for Xamarin's
System.nuint:jcouv commentedon Sep 26, 2021
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.JoeRobich commentedon Sep 27, 2021
Design Meeting Notes:
Proposal: Keep IDE analyzers as they are. The compiler will add a diagnostic behind a warning wave for VS 17.1 Preview 1.
27 remaining items
soc commentedon Apr 12, 2022
So when compiling the BCL we'll get warnings for
int,float,string?CyrusNajmabadi commentedon Apr 12, 2022
No. The bcl does not define those. So there's nothing to warn on.
333fred commentedon Apr 12, 2022
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 forstringisSystem.String, which does indeed follow this convention.soc commentedon Apr 12, 2022
@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.
soc commentedon Apr 12, 2022
@333fred I'm not confused thank you.
Youssef1313 commentedon Apr 12, 2022
There is really no exception.
int(and friends) are keywords. They're not type names (the corresponding type names areSystem.Int32and friends - as @333fred pointed out). The compiler warns for lower-cased type names.soc commentedon Apr 12, 2022
@Youssef1313 If you discuss type names then the "type-ness" of "type keywords" is more relevant that their "keyword-ness".
CyrusNajmabadi commentedon Apr 12, 2022
Those are not type-names. They are built in keywords. We're not warning on keywords here, only type names. Thanks :)
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.
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).
soc commentedon Apr 12, 2022
CyrusNajmabadi commentedon Apr 12, 2022
@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 (likerecord, 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, orunmanaged, 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.davidwengier commentedon Apr 12, 2022
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