-
Notifications
You must be signed in to change notification settings - Fork 5k
Use is null/is not null for null checking #43360
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
Conversation
Thanks, @marek-safar. I spot checked and it looks fine. How did you make these changes? Via an analyzer/code fixer, or via some search/replace? |
Simple grep. There are more cases which will need changing because the use uncommon pattern.
There are few cases which compiler issues now warnings for and could be removed. For example https://github.com/dotnet/runtime/pull/43360/files#diff-ff940bf6ef3e4720479720d29abed80108b5a7ce256b073da88ee1581102b811L199 |
@stephentoub any though on why could this cause error in one of facades generator?
|
I haven't reviewed the code yet (3700 changed files), but are we sure that none of these call sites intended to call user-defined overloads if they exist, and were we careful to exclude things like tools and unit tests from the search-and-replace? Those tools or unit tests could be intentionally exercising user defined operators. |
What would be the scenario? All I can think of is to trigger some side-effect but that sounds like something we'd like to avoid anyway. |
Unit-testing user-defined operators, primarily. We risk lowering unit test coverage if we're changing unit test code. |
I didn't notice we were changing test code. I think we should start with just src. We don't run analyzers on tests, either. |
OK, I'll revert test code changes |
I started looking at little more deeply at the IL changes introduced by this PR, and it turns out it uncovered a latent Roslyn bug. (Hooray!) We should revert any code that changed this construct: void* somePtr = ...;
if (somePtr == null) { /* ... */ } To: void* somePtr = ...;
if (somePtr is null) { /* ... */ } The latter case produces invalid IL. |
Ouch. I assume you're opening an issue? |
Yeah Levi pinged us offline, we're looking into it. |
Roslyn issue: dotnet/roslyn#48563 |
42928d8
to
17bf2f4
Compare
2df057f
to
93b4ecb
Compare
@GrabYourPitchforks any tip how to find them? |
761d39a
to
6156e07
Compare
Maybe an analyzer could find them? I don't think it's possible via a pure regex. |
6156e07
to
86d1270
Compare
This avoids calling user operators where they exist and enables more C# compiler comparison checks
ca27b2f
to
33a718a
Compare
33a718a
to
8163fe6
Compare
@@ -30,6 +30,7 @@ | |||
release, increase this number by one. | |||
--> | |||
<NETStandardPatchVersion>0</NETStandardPatchVersion> | |||
<MicrosoftNetCompilersToolsetVersion>3.9.0-2.20516.4</MicrosoftNetCompilersToolsetVersion> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really needs a comment, an issue tracking removal of this line, or something similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment added
Latest PR still includes non-shipping code or projects which aren't guaranteed to be built using the latest toolset. See runtime/src/coreclr/src/System.Private.CoreLib/Tools/GenUnicodeProp/ for one example. |
87615e0
to
4379f39
Compare
Thinking on this a bit more, I wonder if the best course of action would instead be to start with an analyzer / fixer, then run that fixer over our code base. I get the intent of this PR, but it seems the majority of call sites don't actually result in more efficient codegen. It's also on occasion resulting in more verbose code, as "is not null" is longer than "!= null ". If we had a fixer that targeted types that has user defined equality operators, it could potentially result in a much smaller PR. It also avoids the problem where we make a bunch of changes now, only for our devs to go and reintroduce "== null" syntax in a future PR. |
@GrabYourPitchforks, I agree with you: dotnet/roslyn#38506. |
Is this what we'd like to do? Have a mix of all possible combinations for null checking? I'm trying to figure out what to do with this PR and if it's worth salvaging anything from it |
Why would you want the new syntax for only types that have custom equality operators? That seems to put a lot of undue burden on the end developer. Lots of tedious searching to do a null check. The idea of using I understand the inclination to stick with existing checks but only doing these checks on types that have equality operators misses the intent of embracing this style of checking (IMHO at least). |
My ideal is two diagnostic IDs:
|
@jaredpar I was under the impression that the C# team was still promoting @stephentoub's recommendation seems viable. If we had those fixers in place, it would solve both problems that I mentioned with this PR: it would prevent stray |
Think I'm not quite getting my point across. The C# team doesn't have any type of official position here (at least not that I'm aware of). Hence this isn't about conforming to some guidelines. Different teams make different choices about this. I personally love My comments are mostly in response to this
My read here is that you preferred the use of I feel like that approach though misses the point of embracing
Indeed. If the runtime team wants to embrace |
// Auto-generated message 69e114c which was merged 12/7 removed the intermediate src/coreclr/src/ folder. This PR needs to be updated as it touches files in that directory which causes conflicts. To update your commits you can use this bash script: https://gist.github.com/ViktorHofer/6d24f62abdcddb518b4966ead5ef3783. Feel free to use the comment section of the gist to improve the script for others. |
Sadly there seems to be disagreement on the prefered null check on libraries team. |
This avoids calling user operators where they exist and enables more
C# compiler comparison checks
Analyzers will be enabled in follow up PR with more tricky updates to follow so I don't need to keep resolving conflicts in 2964 changed files
@stephentoub