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

t is null with LangVersion 7 should produce a diagnostic #34695

Merged
merged 1 commit into from
Apr 6, 2019
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
19 changes: 16 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ internal BoundExpression ConvertPatternExpression(

// If we are pattern-matching against an open type, we do not convert the constant to the type of the input.
// This permits us to match a value of type `IComparable<T>` with a pattern of type `int`.
bool inputContainsTypeParameter = inputType.ContainsTypeParameter();
if (inputContainsTypeParameter)
if (inputType.ContainsTypeParameter())
{
convertedExpression = expression;
if (!hasErrors)
Expand All @@ -198,11 +197,25 @@ internal BoundExpression ConvertPatternExpression(
{
// We do not permit matching null against a struct type.
diagnostics.Add(ErrorCode.ERR_ValueCantBeNull, expression.Syntax.Location, inputType);
hasErrors = true;
}
}
else if (ExpressionOfTypeMatchesPatternType(Conversions, inputType, expression.Type, ref useSiteDiagnostics, out _, operandConstantValue: null) == false)
{
diagnostics.Add(ErrorCode.ERR_PatternWrongType, expression.Syntax.Location, inputType, expression.Display);
hasErrors = true;
}

if (!hasErrors)
{
var requiredVersion = MessageID.IDS_FeatureRecursivePatterns.RequiredVersion();
if (Compilation.LanguageVersion < requiredVersion &&
// A null pattern can be tested against a type that can be assigned null, even in C# 7.3
!(expression.ConstantValue == ConstantValue.Null && inputType.CanBeAssignedNull()))
{
diagnostics.Add(ErrorCode.ERR_ConstantPatternVsOpenType,
expression.Syntax.Location, inputType, expression.Display, new CSharpRequiredLanguageVersion(requiredVersion));
}
}

diagnostics.Add(node, useSiteDiagnostics);
Expand All @@ -222,7 +235,7 @@ internal BoundExpression ConvertPatternExpression(
if (inputType.IsNullableType() && (convertedExpression.ConstantValue == null || !convertedExpression.ConstantValue.IsNull))
{
// Null is a special case here because we want to compare null to the Nullable<T> itself, not to the underlying type.
var discardedDiagnostics = DiagnosticBag.GetInstance(); // We are not intested in the diagnostic that get created here
var discardedDiagnostics = DiagnosticBag.GetInstance(); // We are not interested in the diagnostic that get created here
convertedExpression = CreateConversion(operand, inputType.GetNullableUnderlyingType(), discardedDiagnostics);
discardedDiagnostics.Free();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ protected NamespaceOrTypeOrAliasSymbolWithAnnotations BindNonGenericSimpleNamesp
SyntaxFacts.IsInTypeOnlyContext(node)) &&
node.Identifier.ValueText == "dynamic" &&
!IsViableType(result) &&
((CSharpParseOptions)node.SyntaxTree.Options).LanguageVersion >= MessageID.IDS_FeatureDynamic.RequiredVersion())
Compilation.LanguageVersion >= MessageID.IDS_FeatureDynamic.RequiredVersion())
{
bindingResult = Compilation.DynamicType;
ReportUseSiteDiagnosticForDynamic(diagnostics, node);
Expand Down
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5076,6 +5076,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_PatternWrongType" xml:space="preserve">
<value>An expression of type '{0}' cannot be handled by a pattern of type '{1}'.</value>
</data>
<data name="ERR_ConstantPatternVsOpenType" xml:space="preserve">
<value>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</value>
</data>
<data name="WRN_AttributeIgnoredWhenPublicSigning" xml:space="preserve">
<value>Attribute '{0}' is ignored when public signing is specified.</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ internal enum ErrorCode
ERR_VarMayNotBindToType = 8508,
WRN_SwitchExpressionNotExhaustive = 8509,
ERR_SwitchArmSubsumed = 8510,
// 8511, // available
ERR_ConstantPatternVsOpenType = 8511,
WRN_CaseConstantNamedUnderscore = 8512,
WRN_IsTypeNamedUnderscore = 8513,
ERR_ExpressionTreeContainsSwitchExpression = 8514,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private bool IsTraditionalSwitch(BoundSwitchStatement node)

// If we are in a recent enough language version, we treat the switch as a fully pattern-based switch
// for the purposes of flow analysis.
if (((CSharpParseOptions)node.Syntax.SyntaxTree.Options).LanguageVersion >= MessageID.IDS_FeatureRecursivePatterns.RequiredVersion())
if (compilation.LanguageVersion >= MessageID.IDS_FeatureRecursivePatterns.RequiredVersion())
{
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">Parametr typu {1} má omezení unmanaged, takže není možné používat {1} jako omezení pro {0}.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">Název {0} neodpovídá příslušnému parametru Deconstruct {1}.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">Der {1}-Typparameter enthält die Einschränkung "unmanaged". "{1}" kann daher nicht als Einschränkung für "{0}" verwendet werden.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">Der Name "{0}" stimmt nicht mit dem entsprechenden Deconstruct-Parameter "{1}" überein.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">El parámetro de tipo "{1}" tiene la restricción "unmanaged"; por tanto, "{1}" no se puede usar como restricción para "{0}"</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">El nombre "{0}" no coincide con el parámetro de "Deconstruct" correspondiente, "{1}".</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">Le paramètre de type '{1}' a la contrainte 'unmanaged'. '{1}' ne peut donc pas être utilisé comme contrainte pour '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">Le nom '{0}' ne correspond pas au paramètre 'Deconstruct' correspondant '{1}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">Il parametro di tipo '{1}' ha il vincolo 'managed'. Non è quindi possibile usare '{1}' come vincolo per '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">Il nome '{0}' non corrisponde al parametro '{1}' di 'Deconstruct' corrispondente.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">型パラメーター '{1}' は 'unmanaged' 制約を含むので、'{0}' の制約として '{1}' を使用することはできません</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">名前 '{0}' は対応する 'Deconstruct' パラメーター '{1}' と一致しません。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">형식 매개 변수 '{1}'에 'unmanaged' 제약 조건이 있으므로 '{1}'은(는) '{0}'에 대한 제약 조건으로 사용할 수 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">'{0}' 이름이 해당 'Deconstruct' 매개 변수 '{1}'과(와) 일치하지 않습니다.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">Parametr typu „{1}” ma ograniczenie „unmanaged”, dlatego elementu „{1}” nie można użyć jako ograniczenia dla elementu „{0}”</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">Nazwa „{0}” nie jest zgodna z odpowiednim parametrem „Deconstruct” „{1}”.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">O parâmetro de tipo '{1}' tem a restrição 'unmanaged' e, por isso, '{1}' não pode ser usado como uma restrição de '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">O nome '{0}' não corresponde ao parâmetro 'Deconstruct' '{1}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">Параметр типа "{1}" имеет ограничение "unmanaged", поэтому "{1}" не может использоваться в качестве ограничения для "{0}".</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">Имя "{0}" не соответствует указанному параметру "Deconstruct" "{1}".</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">'{1}' tür parametresinde 'unmanaged' kısıtlaması olduğundan '{1}', '{0}' için kısıtlama olarak kullanılamaz</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">'{0}' adı ilgili '{1}' 'Deconstruct' parametresiyle eşleşmiyor.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">类型参数“{1}”具有 "unmanaged" 约束,因此“{1}”不能用作“{0}”的约束</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">名称“{0}”与相应 "Deconstruct" 参数“{1}”不匹配。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<target state="translated">類型參數 '{1}' 有 'unmanaged' 條件約束,因此 '{1}' 不可作為 '{0}' 的條件約束</target>
<note />
</trans-unit>
<trans-unit id="ERR_ConstantPatternVsOpenType">
<source>An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</source>
<target state="new">An expression of type '{0}' cannot be handled by a pattern of type '{1}'. Please use language version '{2}' or greater to match an open type with a constant pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DeconstructParameterNameMismatch">
<source>The name '{0}' does not match the corresponding 'Deconstruct' parameter '{1}'.</source>
<target state="translated">名稱 '{0}' 與對應的 'Deconstruct' 參數 '{1}' 不相符。</target>
Expand Down
Loading