-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Remove usage of !! from dotnet/runtime #68178
Conversation
862ffac
to
acd1231
Compare
Review based on acd12317c3d8d1b54ffd14a8a6624be8ab35059d
|
The GitHub UI is misbehaving trying to leave feedback within the PR. But in ClaimsIdentity.cs and ClaimsPrincipal.cs: the 6.0 behavior is that for iterators, the ArgumentNullException is thrown lazily, where in 7.0 it's thrown eagerly. I believe your 7.0 behavior is correct but wanted to surface it as a potential breaking change and make sure we were ok with it. |
....Security.Cryptography/src/System/Security/Cryptography/X509Certificates/ChainPal.Windows.cs
Outdated
Show resolved
Hide resolved
Thanks, yes, I should have mentioned that. In going through and manually reviewing every iterator and async method that had |
35b2112
to
1239cfb
Compare
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.
159 / 1412 files viewed
I will continue tomorrow.
src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Xml/src/XmlConfigurationElementTextContent.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyInjection/src/DefaultServiceProviderFactory.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ConstantCallSite.cs
Show resolved
Hide resolved
Just wondering, how did you undo the changes, it's a lot of changes and a large code base
And if you did more than 1,2 how did you decide on that method? |
Revert wasn't ideal because:
My process:
|
Thank you for the detailed answer. |
I'll assume the downvotes are about C# 11 removing the |
I've taken a look through a couple of files. One thing that has struck me is how nice it'd be if we could shorten (yes, I recognise the irony!) the null checking code, e.g, instead of: ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType); we could have ... ArgumentNullException.ThrowIfNull(element, attributeType); I think this'd work if we added overloads, e.g.: public static void ThrowIfNull(
[NotNull] object? argument1,
[NotNull] object? argument2,
[CallerArgumentExpression("argument1")] string? paramName1 = null,
[CallerArgumentExpression("argument2")] string? paramName2 = null)
{
if (argument1 is null)
{
Throw(paramName1);
}
if (argument2 is null)
{
Throw(paramName2);
}
} We could have up to 5 parameters for normal software (and maybe a special 'Enterprise' version for Finance sector developers, with up to 30 parameters! ;) ) |
Actually, forget my idea above; I implemented it, but method overload resolution means it likely will call the wrong overload. Examples in the PR. |
1239cfb
to
dbe7139
Compare
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.
600 / 1416 files viewed. To be continued as soon as possible.
I found we're down to about 70 occurrences of an assignment with ?? throw new ArgumentNullException(nameof(...
. If we want to eliminate that pattern, we could do so in a follow-up PR pretty quickly, but I don't know if that was the aim or not.
src/libraries/Microsoft.Extensions.Hosting/src/Internal/ConsoleLifetime.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/src/Internal/ServiceFactoryAdapter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Collections.NonGeneric/src/System/Collections/Queue.cs
Show resolved
Hide resolved
...s/System.ComponentModel.Primitives/src/System/ComponentModel/InvalidEnumArgumentException.cs
Show resolved
Hide resolved
src/libraries/System.Composition.Runtime/src/System/Composition/Runtime/Util/Formatters.cs
Show resolved
Hide resolved
src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/ContractUtils.cs
Show resolved
Hide resolved
dbe7139
to
45f6a32
Compare
My goal wasn't to remove them, but rather if a project already had a ThrowHelper, to use it consistently. |
45f6a32
to
8872ebe
Compare
Thanks for trying that out, @SteveDunn. The idea was intriguing. |
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.
I left one comment that calls out a subtle change in logic that we might want to back out for caution. Several other comments around code comments. And a couple formatting fixes/suggestions.
@stephentoub - your devotion to this was admirable. This was a major undertaking in both directions! And even though !!
got removed, your pair of PRs around null argument handling resulted in better consistency as well as mitigations to some lurking NullReferenceException possibilities.
Thank you for the effort that went into this and for teaching me a few things during the review.
...ibraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/InterpretedFrame.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Text/Decoder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Text/Decoder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Text/Encoder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Core/ReadContentAsBinaryHelperAsync.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Core/XmlCharCheckingReaderAsync.cs
Outdated
Show resolved
Hide resolved
...dContext/src/System/Reflection/TypeLoading/Assemblies/Ecma/EcmaAssembly.ManifestResources.cs
Show resolved
Hide resolved
src/libraries/System.Security.AccessControl/src/System/Security/AccessControl/ObjectSecurity.cs
Outdated
Show resolved
Hide resolved
- Use ArgumentNullException.ThrowIfNull instead where possible. It's only usable for projects that only target .NET 6+, and it can't be used in places like this(...) or base(...). - In other cases, if the project already has a ThrowHelper, augment it for null as needed and use that. - For most of the extensions projects, add a ThrowHelper.ThrowIfNull that replicates ArgumentNullException.ThrowIfNull. - For everything else, just use "throw new".
8872ebe
to
bae32d1
Compare
bae32d1
to
ce36537
Compare
Thanks, @GrabYourPitchforks and @jeffhandley, for reviewing. |
React to:
https://devblogs.microsoft.com/dotnet/csharp-11-preview-updates/#remove-parameter-null-checking-from-c-11
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-04-13.md#c-language-design-meeting-for-april-11th-2022