Skip to content
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

Weird pattern matching behavior #63560

Closed
CorrM opened this issue Aug 24, 2022 · 1 comment
Closed

Weird pattern matching behavior #63560

CorrM opened this issue Aug 24, 2022 · 1 comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@CorrM
Copy link

CorrM commented Aug 24, 2022

Version Used:
Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET
C:\Program Files\dotnet\sdk\6.0.302\Roslyn\bincore\csc.dll

Steps to Reproduce:
Why this code compiles

public static void MakePropertyValue<T>(T val)
{
    if (val is Span<byte> || val is ReadOnlySpan<byte>)
    {
    }
}

but this code can't compiles!

public static void MakePropertyValue<T>(T val)
{
	if (val is Span<byte> or ReadOnlySpan<byte>)
    {
    }
}

Expected Behavior:
Compile !

Actual Behavior:
Error CS8121 : An expression of type 'T' cannot be handled by a pattern of type 'Span'.
Error CS8121 : An expression of type 'T' cannot be handled by a pattern of type 'ReadOnlySpan'.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 24, 2022
@svick
Copy link
Contributor

svick commented Aug 27, 2022

Your first example compiles, but doesn't work the way you want, and you get warnings about that:

warning CS0184: The given expression is never of the provided ('Span<byte>') type

This is because the is operator doesn't work for ref structs like Span<T>, see #63085.

As for why one is a warning and the other an error, I suspect this is because the is type test is an old feature that works the way it always did, but pattern matching is a new feature, so it can be stricter. Though per #63085 (comment), this might change in the future.

In effect, I think this issue can be closed as a duplicate of #63085.

@CorrM CorrM closed this as completed Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants