ArgOutOfRangeException throw helpers#78222
Conversation
|
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
|
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
|
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsContribute to #69590, add a few replacements to this PR for root System namespace in corelib No idea how to write better tests for this 😄
|
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Outdated
Show resolved
Hide resolved
| ThrowIfNegative(value, paramName); | ||
| ThrowIfZero(value, paramName); |
There was a problem hiding this comment.
This will result in two Throw calls at the call site. Along with my previous comment about keeping things streamlined, this should be more like:
if (T.IsNegative(value) || T.IsZero(value))
{
Throw...
}There was a problem hiding this comment.
@tannergooding, you were suggesting these methods not be constrained to IComparabe and to use IsNegative and IsZero instead... what about these combined scenarios? Currently it seems like the JIT doesn't do a great job at combining the comparisons:
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIGYACMhgYQYG8aHunHiUGAWXIAKAJYA7DAzEBKLj07UeKmQDMG4hgF5tDAAwNChGQwA8B+ctXclNmwBUAFlAgB3EbIDcCmwF9fBgDrbkD6JgFBUnEpGSsVO3sxDS0zPX14+0T7HmdXD29AlWDiwMCABygxADdsDBgmciQIhjz3Tx0APgYMF3cGCRg3BgBBKABzAFd8GCkAeUmMObUAJWwJcZgAUQQwGHKMMQgJTx9qPyA=
@EgorBo, is that fixable?
Co-authored-by: Stephen Toub <stoub@microsoft.com>
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
Show resolved
Hide resolved
| { | ||
| throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_FileTimeInvalid); | ||
| } | ||
| ArgumentOutOfRangeException.ThrowIfNegative(ticks, null); |
There was a problem hiding this comment.
We're losing context in the message here. We need to decide how much we care.
There was a problem hiding this comment.
You marked this as resolved, so replacement is fine?
I just thinking that I can revert that one to make this initial PR like more straightforward without any edge cases. We can return here in another separate PR with replacements
There was a problem hiding this comment.
I just thinking that I can revert that one to make this initial PR like more straightforward without any edge cases. We can return here in another separate PR with replacements
Ok, thanks.
|
For these helpers and the similar existing ones, perf is a wash, right? The advantage of using them is code is shorter? Do we think it would be useful to have an analyzer/fixer that would replace existing code with throw helpers - these and others? I found only #68326 |
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
Co-authored-by: Dan Moseley <danmose@microsoft.com>
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs
Outdated
Show resolved
Hide resolved
IL is smaller, and with current JIT resulting asm is smaller plus containing method has a resulting better chance of being inlined.
Yes: #68326 (comment) |
|
Analyzer/fixer implementation should be done in roslyn-analyzers repo, there is nothing to change more in /runtime, right? I think I can try to implement them 🤔 |
Correct.
This PR only uses the new helpers in corelib; we want to use them in the rest of the libraries as well. But we can also wait to do so and use it as a test case for the analyzer/fixer.
I'm actually going to take it on, as it'll be more comprehensive than just these ArgumentOutOfRangeExceptions. Thanks, though. If you'd like to experiment with writing analyzers, there are a bunch that are approved and waiting for someone to tackle them. |
stephentoub
left a comment
There was a problem hiding this comment.
A few remaining nits, otherwise LGTM. Thanks for working on this!
src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
Outdated
Show resolved
Hide resolved
| { | ||
| throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_FileTimeInvalid); | ||
| } | ||
| ArgumentOutOfRangeException.ThrowIfNegative(ticks, null); |
There was a problem hiding this comment.
I just thinking that I can revert that one to make this initial PR like more straightforward without any edge cases. We can return here in another separate PR with replacements
Ok, thanks.
Contribute to #69590, add a few replacements to this PR for root System namespace in corelib
No idea how to write better tests for this 😄