Fix tuple pattern matching in AotConverterGenerator to use ValueTuple types#2799
Fix tuple pattern matching in AotConverterGenerator to use ValueTuple types#2799
Conversation
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
… types Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
|
I believe this can be fixed in a more clean/less manual way by creating a special public static readonly SymbolDisplayFormat FullyQualifiedGenericWithGlobalPrefixForPatternMatching = FullyQualifiedGenericWithGlobalPrefix
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.ExpandValueTuple);This display type can then be used for any type that will be used in pattern matching, so there is no need to handle For global::System.ValueTuple<int, int>For global::System.ValueTuple<int, int, int, int, int, int, int, global::System.ValueTuple<int, int, int, int, int, int, int>>For For reference, For (int Value1, int Value2)For global::System.ValueTuple<int, int, int, int, int, int, int, (int, int, int, int, int)>For |
The AotConverterGenerator was generating incorrect pattern matching code for tuple types, causing compilation errors when using implicit operators with tuple parameters.
Problem
When generating AOT converters for implicit operators that accept tuple parameters like
(int Value1, int Value2), the generator was creating positional patterns:This syntax is interpreted as a positional pattern that looks for a
Deconstructmethod on theobjecttype, which doesn't exist, resulting in compilation errors:Solution
The fix detects when the source type is a tuple type using
IsTupleTypeand generates the correctValueTuple<T1, T2, ...>pattern matching syntax instead:This works correctly because it pattern matches against the actual underlying
ValueTupletype rather than using tuple literal syntax that gets interpreted as a positional pattern.Example
This code now compiles successfully:
The fix handles tuple types of any arity (2-tuple, 3-tuple, etc.) and maintains backward compatibility with existing non-tuple conversion operators.
Fixes #2798.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.