Skip to content

Commit

Permalink
Use refactoring from alrz
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Oct 18, 2021
1 parent 4ff038d commit 7c98d01
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions src/Compilers/CSharp/Portable/Binder/PatternExplainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,33 +284,10 @@ string tryHandleNumericLimits(ref bool unnamedEnumValue)
(BoundDagNonNullTest _, true) => true,
_ => false
}) &&
ValueSetFactory.ForType(input.Type) is { } fac)
ValueSetFactory.ForInput(input) is { } fac)
{
// All we have are numeric constraints. Process them to compute a value not covered.
var remainingValues = fac.AllValues;
foreach (var constraint in constraints)
{
var (test, sense) = constraint;
switch (test)
{
case BoundDagValueTest v:
addRelation(BinaryOperatorKind.Equal, v.Value);
break;
case BoundDagRelationalTest r:
addRelation(r.Relation, r.Value);
break;
}
void addRelation(BinaryOperatorKind relation, ConstantValue value)
{
if (value.IsBad)
return;
var filtered = fac.Related(relation, value);
if (!sense)
filtered = filtered.Complement();
remainingValues = remainingValues.Intersect(filtered);
}
}

IValueSet remainingValues = computeRemainingValues(fac, constraints);
if (remainingValues.Complement().IsEmpty)
return "_";

Expand All @@ -320,6 +297,36 @@ void addRelation(BinaryOperatorKind relation, ConstantValue value)
return null;
}

static IValueSet computeRemainingValues(IValueSetFactory fac, ImmutableArray<(BoundDagTest test, bool sense)> constraints)
{
var remainingValues = fac.AllValues;
foreach (var constraint in constraints)
{
var (test, sense) = constraint;
switch (test)
{
case BoundDagValueTest v:
addRelation(BinaryOperatorKind.Equal, v.Value);
break;
case BoundDagRelationalTest r:
addRelation(r.Relation, r.Value);
break;
}

void addRelation(BinaryOperatorKind relation, ConstantValue value)
{
if (value.IsBad)
return;
var filtered = fac.Related(relation, value);
if (!sense)
filtered = filtered.Complement();
remainingValues = remainingValues.Intersect(filtered);
}
}

return remainingValues;
}

// Handle the special case of a list pattern
string tryHandleListPattern(ref bool unnamedEnumValue)
{
Expand Down Expand Up @@ -347,13 +354,9 @@ string tryHandleListPattern(ref bool unnamedEnumValue)
case BoundDagPropertyEvaluation e when e.IsLengthOrCount:
{
Debug.Assert(length == -1);
var subInput = new BoundDagTemp(e.Syntax, e.Property.Type, e);
var lengthValue = SamplePatternForTemp(subInput, constraintMap, evaluationMap, false, ref unnamedEnumValue);
if (int.TryParse(lengthValue, out var parsedLength))
{
Debug.Assert(parsedLength >= 0);
length = parsedLength;
}
var lengthTemp = new BoundDagTemp(e.Syntax, e.Property.Type, e);
var lengthValues = (IValueSet<int>)computeRemainingValues(ValueSetFactory.ForLength, getArray(constraintMap, lengthTemp));
length = lengthValues.Sample.Int32Value;
}
break;
case BoundDagIndexerEvaluation e:
Expand Down

0 comments on commit 7c98d01

Please sign in to comment.