int.DivRem throws OverflowException even in unchecked context
#124932
-
|
Now, I don't think the current behavior should be changed, but I think it should at least be made clear in the documentation ( |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The behavior you're observing is a C# specific semantic around constant folding which the spec leaves as implementation-defined and which Roslyn has "succeeding" instead of blocking it: https://github.com/dotnet/csharpstandard/blob/draft-v8/standard/expressions.md#12123-division-operator
The ECMA-335 runtime behavior, which is also what the .NET standard library must conform to, is stricter:
And thus, if you actually execute the code (instead of representing it in a form that allows C# to constant fold), then you will always get the overflow: This is the general behavior of division if done at runtime in any capacity: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7AHwAEAmABgFgAoUgRmuoEs8MACAE0YDdGBnaVgF5WcWgG5qRWgE4AFPjAALGGADWMdrOYYAdAFlmANQCGAGxwxWAeg7c+0AJQOJNGfLxKV6zdv1GzFqwApLY8/FBOLlJyCspqGlosOgAi3ABKMAC2iboGeCbmMGih9hGR1EA== It's also worth noting that Please feel free to open a PR on dotnet-api-docs adding a clarification to the exception behavior so that we can call out it always throws, as per the runtime requirements. |
Beta Was this translation helpful? Give feedback.
The behavior you're observing is a C# specific semantic around constant folding which the spec leaves as implementation-defined and which Roslyn has "succeeding" instead of blocking it: https://github.com/dotnet/csharpstandard/blob/draft-v8/standard/expressions.md#12123-division-operator