Skip to content

Commit

Permalink
Relax conversion requirements for pattern-matching involving type par…
Browse files Browse the repository at this point in the history
…ameters. (#18784)

Fixes #16195
This is a language change for 7.1. See dotnet/csharplang#154.
  • Loading branch information
gafter authored May 8, 2017
1 parent 8058ba4 commit 02a0942
Show file tree
Hide file tree
Showing 18 changed files with 1,597 additions and 375 deletions.
22 changes: 20 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,26 @@ private bool CheckValidPatternType(
//case ConversionKind.ImplicitConstant:
//case ConversionKind.ImplicitNumeric:
default:
Error(diagnostics, ErrorCode.ERR_PatternWrongType, typeSyntax, operandType, patternType);
return true;
if (operandType.ContainsTypeParameter() || patternType.ContainsTypeParameter())
{
LanguageVersion requiredVersion = MessageID.IDS_FeatureGenericPatternMatching.RequiredVersion();
if (requiredVersion > Compilation.LanguageVersion)
{
Error(diagnostics, ErrorCode.ERR_PatternWrongGenericTypeInVersion, typeSyntax,
operandType, patternType,
Compilation.LanguageVersion.ToDisplayString(),
new CSharpRequiredLanguageVersion(requiredVersion));
return true;
}

// permit pattern-matching when one of the types is an open type in C# 7.1.
break;
}
else
{
Error(diagnostics, ErrorCode.ERR_PatternWrongType, typeSyntax, operandType, patternType);
return true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ internal bool AddLabel(BoundPatternSwitchLabel label, DiagnosticBag diagnostics,
// For purposes of subsumption, we do not take into consideration the value
// of the input expression. Therefore we consider null possible if the type permits.
Syntax = label.Syntax;
var subsumedErrorCode = CheckSubsumed(label.Pattern, _subsumptionTree, inputCouldBeNull: true);
var inputCouldBeNull = _subsumptionTree.Type.CanContainNull();
var subsumedErrorCode = CheckSubsumed(label.Pattern, _subsumptionTree, inputCouldBeNull: inputCouldBeNull);
if (subsumedErrorCode != 0 && subsumedErrorCode != ErrorCode.ERR_NoImplicitConvCast)
{
if (!label.HasErrors)
Expand Down
4 changes: 1 addition & 3 deletions src/Compilers/CSharp/Portable/BoundTree/DecisionTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static DecisionTree Create(BoundExpression expression, TypeSymbol type, S
expression = new BoundLocal(expression.Syntax, temp, null, type);
}

if (expression.Type.CanContainNull())
if (type.CanContainNull() || type.SpecialType == SpecialType.None)
{
// We need the ByType decision tree to separate null from non-null values.
// Note that, for the purpose of the decision tree (and subsumption), we
Expand All @@ -104,8 +104,6 @@ public static DecisionTree Create(BoundExpression expression, TypeSymbol type, S
else
{
// If it is a (e.g. builtin) value type, we can switch on its (constant) values.
// If it isn't a builtin, in practice we will only use the Default part of the
// ByValue.
return new ByValue(expression, type, temp);
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/Compilers/CSharp/Portable/BoundTree/DecisionTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private DecisionTree AddByValue(DecisionTree.Guarded guarded, BoundConstantPatte

private DecisionTree AddByValue(DecisionTree.ByValue byValue, BoundConstantPattern value, DecisionMaker makeDecision)
{
Debug.Assert(value.Value.Type == byValue.Type);
Debug.Assert(value.Value.Type.Equals(byValue.Type, TypeCompareKind.IgnoreDynamicAndTupleNames));
if (byValue.Default != null)
{
return AddByValue(byValue.Default, value, makeDecision);
Expand Down Expand Up @@ -221,7 +221,7 @@ private DecisionTree AddByValue(DecisionTree.ByType byType, BoundConstantPattern
var kvp = byType.TypeAndDecision[i];
var matchedType = kvp.Key;
var decision = kvp.Value;
if (matchedType.TupleUnderlyingTypeOrSelf() == value.Value.Type.TupleUnderlyingTypeOrSelf())
if (matchedType.Equals(value.Value.Type, TypeCompareKind.IgnoreDynamicAndTupleNames))
{
forType = decision;
break;
Expand Down Expand Up @@ -260,21 +260,30 @@ private DecisionTree AddByType(DecisionTree decision, TypeSymbol type, DecisionM
case DecisionTree.DecisionKind.ByValue:
{
var byValue = (DecisionTree.ByValue)decision;
DecisionTree result;
if (byValue.Default == null)
{
byValue.Default = makeDecision(byValue.Expression, byValue.Type);
if (byValue.Default.MatchIsComplete)
if (byValue.Type.Equals(type, TypeCompareKind.IgnoreDynamicAndTupleNames))
{
byValue.MatchIsComplete = true;
result = byValue.Default = makeDecision(byValue.Expression, byValue.Type);
}
else
{
byValue.Default = new DecisionTree.ByType(byValue.Expression, byValue.Type, null);
result = AddByType(byValue.Default, type, makeDecision);
}

return byValue.Default;
}
else
{
Debug.Assert(byValue.Default.Type == type);
return Add(byValue.Default, makeDecision);
result = AddByType(byValue.Default, type, makeDecision);
}

if (byValue.Default.MatchIsComplete)
{
byValue.MatchIsComplete = true;
}

return result;
}
case DecisionTree.DecisionKind.Guarded:
return AddByType((DecisionTree.Guarded)decision, type, makeDecision);
Expand Down Expand Up @@ -321,7 +330,7 @@ private DecisionTree AddByType(DecisionTree.ByType byType, TypeSymbol type, Deci
if (byType.TypeAndDecision.Count != 0)
{
var lastTypeAndDecision = byType.TypeAndDecision.Last();
if (lastTypeAndDecision.Key.TupleUnderlyingTypeOrSelf() == type.TupleUnderlyingTypeOrSelf())
if (lastTypeAndDecision.Key.Equals(type, TypeCompareKind.IgnoreDynamicAndTupleNames))
{
result = Add(lastTypeAndDecision.Value, makeDecision);
}
Expand Down Expand Up @@ -533,7 +542,7 @@ private DecisionTree Add(DecisionTree.ByType byType, DecisionMaker makeDecision)
TypeSymbol patternType,
ref HashSet<DiagnosticInfo> useSiteDiagnostics)
{
if (expressionType == patternType)
if ((object)expressionType == (object)patternType)
{
return true;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4891,7 +4891,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>The switch case has already been handled by a previous case.</value>
</data>
<data name="ERR_PatternWrongType" xml:space="preserve">
<value>An expression of type {0} cannot be handled by a pattern of type {1}.</value>
<value>An expression of type '{0}' cannot be handled by a pattern of type '{1}'.</value>
</data>
<data name="WRN_AttributeIgnoredWhenPublicSigning" xml:space="preserve">
<value>Attribute '{0}' is ignored when public signing is specified.</value>
Expand Down Expand Up @@ -5070,4 +5070,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_VoidInTuple" xml:space="preserve">
<value>A tuple may not contain a value of type 'void'.</value>
</data>
<data name="ERR_PatternWrongGenericTypeInVersion" xml:space="preserve">
<value>An expression of type '{0}' cannot be handled by a pattern of type '{1}' in C# {2}. Please use language version {3} or greater.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1476,8 +1476,13 @@ internal enum ErrorCode
WRN_Experimental = 8305,
ERR_TupleInferredNamesNotAvailable = 8306,

#region diagnostics for C# 7.1

ERR_BadDynamicMethodArgDefaultLiteral = 9000,
ERR_DefaultLiteralNotValid = 9001,
WRN_DefaultInSwitch = 9002,
ERR_PatternWrongGenericTypeInVersion = 9003,

#endregion diagnostics for C# 7.1
}
}
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ internal enum MessageID
IDS_ThrowExpression = MessageBase + 12717,
IDS_FeatureDefaultLiteral = MessageBase + 12718,
IDS_FeatureInferredTupleNames = MessageBase + 12719,
IDS_FeatureGenericPatternMatching = MessageBase + 12720,
}

// Message IDs may refer to strings that need to be localized.
Expand Down Expand Up @@ -188,6 +189,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
// C# 7.1 features.
case MessageID.IDS_FeatureDefaultLiteral:
case MessageID.IDS_FeatureInferredTupleNames:
case MessageID.IDS_FeatureGenericPatternMatching:
return LanguageVersion.CSharp7_1;

// C# 7 features.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private DecisionTree LowerToDecisionTree(
}
}

if (defaultLabel != null)
if (defaultLabel != null && !loweredDecisionTree.MatchIsComplete)
{
Add(loweredDecisionTree, (e, t) => new DecisionTree.Guarded(loweredExpression, loweredExpression.Type, default(ImmutableArray<KeyValuePair<BoundExpression, BoundExpression>>), defaultSection, null, defaultLabel));
}
Expand Down Expand Up @@ -219,7 +219,8 @@ private void LowerDecisionTree(BoundExpression expression, DecisionTree decision
// Store the input expression into a temp
if (decisionTree.Expression != expression)
{
_loweredDecisionTree.Add(_factory.Assignment(decisionTree.Expression, expression));
var convertedExpression = _factory.Convert(decisionTree.Expression.Type, expression);
_loweredDecisionTree.Add(_factory.Assignment(decisionTree.Expression, convertedExpression));
}

if (_declaredTempSet.Add(decisionTree.Temp))
Expand Down
Loading

0 comments on commit 02a0942

Please sign in to comment.