-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Slice pattern with type pattern generates redundant type test (or fails to perform the test intended) #69053
Comments
|
Turns out that's no longer the case: dotnet/runtime#66011 the pattern here relies on the previous implementation. Compiler doesn't have any special knowledge on GetSubArray other than it is assumed that it won't return null. Other than that, I think the behavior is expected; |
Oh, I'd missed that the local is declared as The behavior is the same as for existing patterns:
|
@jcouv yes. but in this case, because we create the |
@jcouv Since list patterns are gonna be discussed on an LDM, I think this issue worth attention there as well. Namely the question here are:
|
Discussed in LDM 2023-11-27. |
PR that reverts to the previous behavior dotnet/runtime#95296 There is a question that needs to be answered. Should the actual type of array slice always match the type of the source array? (It is not the case even for the previous behavior.) |
Fixed by dotnet/runtime#95296 |
Closing as fixed. Thanks @jkotas |
Version Used: C# 11.0, .NET SDK 7.0.302.
Steps to Reproduce:
Compile this code:
Inspect the generated IL. The part that evaluates the slice pattern includes this code:
I believe that
isinst
instruction is aiming to deal with the fact that although the static type oflist
isobject[]
, the type compatibility rules for arrays mean it might actually be astring[]
(and in this example, it is). The thinking then appears to be that although thatstring[] theRest
doesn't match the static type oflist
, it might be a runtime match.So this calls
GetSubArray
, and then asks whether the array that came back is really astring[]
.Unfortunately, this serves no purpose.
GetSubArray<object>
always returns anobject[]
even when, as in this case, the input was actually astring[]
. (To put it another way, the compiler seems to assume thatGetSubArray
will preserve the array element type. But that's wrong. In factGetSubArray
returns an array where the element type is determined by the type argument.)So that
isinst string[]
will always fail.So at best, that test is redundant. At worst, this is a bug: if the intention here is that the pattern should succeed when the runtime type of
list
isstring[]
, this code fails to achieve that. That pattern only matches the array in this example if we changelist
's declared type tostring[]
. (Since in general, slice patterns reject impossible matches at compile time, it seems more likely that this is indeed a bug, and that this pattern was meant to match in this example.)Expected Behavior: well this depends on what the compiler is trying to achieve. If the actual behaviour of this code (the pattern does not match) is correct, then the compiler should not emit that
isinst string[]
test, because it will always fail. (And perhaps the compiler should be reporting an error, because it does that in most cases where a slice pattern is an impossible match.) But if that pattern is in fact meant to match in this scenario, then the compiler is emitting code that fails to achieve this.Actual Behavior: the compiler emits a type check that always fails.
The text was updated successfully, but these errors were encountered: