Skip to content

Commit

Permalink
If Length and Count are both present, only the former is assumed to b…
Browse files Browse the repository at this point in the history
…e non-negative (#57268)
  • Loading branch information
alrz authored Oct 22, 2021
1 parent 039cc3d commit 3fc766e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
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.
}
}
}
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

0 comments on commit 3fc766e

Please sign in to comment.