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

If Length and Count are both present, only the former is assumed to be non-negative #57268

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,8 @@ private ImmutableArray<BoundPropertySubpattern> BindPropertyPatternClause(
{
isLengthOrCount = receiverType.IsSZArray()
? ReferenceEquals(memberSymbol, Compilation.GetSpecialTypeMember(SpecialMember.System_Array__Length))
: TryPerformPatternIndexerLookup(node, receiverType, argIsIndex: true, indexerAccess: out _, patternSymbol: out _, lengthProperty: out _, BindingDiagnosticBag.Discarded);
: TryPerformPatternIndexerLookup(node, receiverType, argIsIndex: true, indexerAccess: out _, patternSymbol: out _, out PropertySymbol? lengthProperty, BindingDiagnosticBag.Discarded) &&
memberSymbol.Equals(lengthProperty, TypeCompareKind.ConsiderEverything); // If Length and Count are both present, only the former is assumed to be non-negative.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Linking to #53891 (comment) if more investigation is needed.

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1608,9 +1608,9 @@ public static void Test4<T>(T t) where T : IIndexable, ICountableViaLength
public static void Test5<T>(T t) where T : IIndexable, ICountableViaLength, ICountableViaCount
{
_ = t is { Length: -1 }; // 5
_ = t is { Count: -1 }; // 6
_ = new { t } is { t.Count: -1 }; // 7
_ = new { t } is { t.Length: -1 }; // 8
_ = t is { Count: -1 };
_ = new { t } is { t.Length: -1 }; // 6
_ = new { t } is { t.Count: -1 };
}
}
";
Expand All @@ -1631,15 +1631,10 @@ public static void Test5<T>(T t) where T : IIndexable, ICountableViaLength, ICou
// (38,13): error CS8518: An expression of type 'T' can never match the provided pattern.
// _ = t is { Length: -1 }; // 5
Diagnostic(ErrorCode.ERR_IsPatternImpossible, "t is { Length: -1 }").WithArguments("T").WithLocation(38, 13),
// (39,13): error CS8518: An expression of type 'T' can never match the provided pattern.
// _ = t is { Count: -1 }; // 6
Diagnostic(ErrorCode.ERR_IsPatternImpossible, "t is { Count: -1 }").WithArguments("T").WithLocation(39, 13),
// (40,13): error CS8518: An expression of type '<anonymous type: T t>' can never match the provided pattern.
// _ = new { t } is { t.Count: -1 }; // 7
Diagnostic(ErrorCode.ERR_IsPatternImpossible, "new { t } is { t.Count: -1 }").WithArguments("<anonymous type: T t>").WithLocation(40, 13),
// (41,13): error CS8518: An expression of type '<anonymous type: T t>' can never match the provided pattern.
// _ = new { t } is { t.Length: -1 }; // 8
Diagnostic(ErrorCode.ERR_IsPatternImpossible, "new { t } is { t.Length: -1 }").WithArguments("<anonymous type: T t>").WithLocation(41, 13));
// _ = new { t } is { t.Length: -1 }; // 6
Diagnostic(ErrorCode.ERR_IsPatternImpossible, "new { t } is { t.Length: -1 }").WithArguments("<anonymous type: T t>").WithLocation(40, 13)
);
}

[Fact]
Expand Down