ArgumentOutOfRangeException throw helpers#69767
ArgumentOutOfRangeException throw helpers#69767hrrrrustic wants to merge 25 commits intodotnet:mainfrom
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. |
|
Tagging subscribers to this area: @dotnet/area-meta Issue DetailsConverting usages to possible new helpers (#69590) without /tests directories Compilation errors:
Remaining manual throwing:
|
|
@hrrrrustic I assigned myself to ensure you get a review on this once the build errors are fixed. |
|
I'll try to finish work this week. Converting to draft for now |
|
Jobs are still failing, some errors can be fixed by reverting changes in old libraries, others related to |
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value)); | ||
| } | ||
| ArgumentOutOfRangeException.ThrowIfNotBetween(value, 0, int.MaxValue); |
There was a problem hiding this comment.
ThrowIfNotBetween suggests an exclusive range, but the code here treated the ends of the range as valid. Should it be something like ThrowIfOutsideRange?
There was a problem hiding this comment.
Initially discussed here
#69590 (comment)
But can be easily changed to ThrowIfOutsideRange. Added this to questions section in issue
There was a problem hiding this comment.
ThrowIfNotBetween is perhaps a good name, but i think it would not be the method you use here. Anyway, as you say, naming can be discussed at review.
| { | ||
| throw new ArgumentOutOfRangeException(nameof(segment)); | ||
| } | ||
| ArgumentOutOfRangeException.ThrowIf(segment.Offset < 0 || segment.Count < 0 || segment.Count > (segment.Array.Length - segment.Offset)); |
There was a problem hiding this comment.
This no longer includes the argument name.
There was a problem hiding this comment.
Ah, I see you mentioned that, but it would be good to fix to see what it looks like.
There was a problem hiding this comment.
Yep, I'll add message (or paramName) arg to ThrowIf after regenerating diff. Just not sure should it be simple string or interpolation handler. Mentioned that in linked issue
|
|
||
| ArgumentOutOfRangeException.ThrowIfLessThan(bytes.Length - byteIndex, byteCount); | ||
|
|
||
| ArgumentOutOfRangeException.ThrowIfLessThan(chars.Length - charIndex, charCount); |
There was a problem hiding this comment.
Some relatively simple checks here turned into a lot of method calls - maybe inlined - is the generated code larger?
There was a problem hiding this comment.
I didn't check that, what's the easiest way to do that?
Also current implementation should use same private helper as ArgumentNullException for better inlining I guess
|
Closing per https://github.com/dotnet/runtime/blob/main/docs/project/api-review-process.md#pull-requests. We can reopen this PR once the API has been approved. |
This PR was created to address #69590 (comment), but I think it's ok to close it until api review process |
Thanks. We don't actually need a PR for that, since we can't "pull" anything until the APIs are approved. The goal of doing the work is to be able to tweak the APIs into they're as successful as possible, which can be done just with local changes and a commit linked from the API issue. I appreciate your efforts here. |


Converting usages to possible new helpers (#69590) without /tests directories
It's possible that this PR contains a few wrong conversions/casts/bugs, but I'll fix them in smaller PRs after approving API. This one is just for collecting statistic
Did not find any meaningful custom messages and dropped them all for now. But can be revisited more carefully in smaller separate PRs.
It can't have it as lazy.ThrowIfmethod should contains additionalmessageargument that I will add in separate PRs (Just need to regenerate all this diff and don't drop messages from old throwing)ThrowIfwill be removed it separate PRsThe main problem is that all tests with
Assert.Throws( () => {})verifiesparamName(that's ok). But if some cast was used while passing argument to helper it will also appear inparamNameand fail the test (e.g(uint) index)Remaining manual throwing:
else if (...) throw OutOfRange elseifandthrow(Debug.Failor logging)defaultMain (search for /libraries):

This PR:
