Skip to content

Commit

Permalink
Implement designator restrictions for C# 9.0 patterns (#40543)
Browse files Browse the repository at this point in the history
Fixes #40533
Relates to dotnet/csharplang#2850
Relates to #40727 (test plan for C# 8 patterns)
  • Loading branch information
Neal Gafter authored Jan 6, 2020
1 parent 754874c commit c0e6baf
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 38 deletions.
95 changes: 60 additions & 35 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/SwitchBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void BuildSwitchLabels(SyntaxList<SwitchLabelSyntax> labelsSyntax, Binde
// bind the pattern, to cause its pattern variables to be inferred if necessary
var matchLabel = (CasePatternSwitchLabelSyntax)labelSyntax;
var pattern = sectionBinder.BindPattern(
matchLabel.Pattern, SwitchGoverningType, SwitchGoverningValEscape, labelSyntax.HasErrors, tempDiagnosticBag);
matchLabel.Pattern, SwitchGoverningType, SwitchGoverningValEscape, permitDesignations: true, labelSyntax.HasErrors, tempDiagnosticBag);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private BoundSwitchLabel BindSwitchSectionLabel(
{
var matchLabelSyntax = (CasePatternSwitchLabelSyntax)node;
BoundPattern pattern = sectionBinder.BindPattern(
matchLabelSyntax.Pattern, SwitchGoverningType, SwitchGoverningValEscape, node.HasErrors, diagnostics);
matchLabelSyntax.Pattern, SwitchGoverningType, SwitchGoverningValEscape, permitDesignations: true, node.HasErrors, diagnostics);
if (matchLabelSyntax.Pattern is ConstantPatternSyntax p)
reportIfConstantNamedUnderscore(pattern, p.Expression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal override BoundSwitchExpressionArm BindSwitchExpressionArm(SwitchExpress
Binder armBinder = this.GetBinder(node);
bool hasErrors = _switchExpressionBinder.SwitchGoverningType.IsErrorType();
ImmutableArray<LocalSymbol> locals = _armScopeBinder.Locals;
BoundPattern pattern = armBinder.BindPattern(node.Pattern, _switchExpressionBinder.SwitchGoverningType, _switchExpressionBinder.SwitchGoverningValEscape, hasErrors, diagnostics);
BoundPattern pattern = armBinder.BindPattern(node.Pattern, _switchExpressionBinder.SwitchGoverningType, _switchExpressionBinder.SwitchGoverningValEscape, permitDesignations: true, hasErrors, diagnostics);
BoundExpression whenClause = node.WhenClause != null
? armBinder.BindBooleanExpression(node.WhenClause.Condition, diagnostics)
: null;
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 @@ -5951,4 +5951,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_ImplicitRangeIndexerWithName" xml:space="preserve">
<value>Invocation of implicit Range Indexer cannot name the argument.</value>
</data>
<data name="ERR_DesignatorBeneathPatternCombinator" xml:space="preserve">
<value>A variable may not be declared within a 'not' or 'or' pattern.</value>
</data>
</root>
8 changes: 8 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,14 @@ internal enum ErrorCode

ERR_ExternEventInitializer = 8760,

// available 8761-8779

#region diagnostics introduced for C# 9.0

ERR_DesignatorBeneathPatternCombinator = 8780,

#endregion diagnostics introduced for C# 9.0

// Note: you will need to re-generate compiler code after adding warnings (eng\generate-compiler-code.cmd)
}
}
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 @@ -122,6 +122,11 @@
<target state="translated">Výchozí literál default není platný jako vzor. Podle potřeby použijte jiný literál (například 0 nebo null). Pokud chcete, aby odpovídalo vše, použijte vzor discard „_“.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">Tento vzor discard není povolený jako návěstí příkazu case v příkazu switch. Použijte „case var _:“ pro vzor discard nebo „case @_:“ pro konstantu s názvem „_“.</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 @@ -122,6 +122,11 @@
<target state="translated">Ein Standardliteral "default" ist als Muster ungültig. Verwenden Sie ggf. ein anderes Literal (z. B. 0 oder "null"). Verwenden Sie zum Abgleich aller Elemente ein discard-Muster "_".</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">Das discard-Muster ist als case-Bezeichnung in einer switch-Anweisung unzulässig. Verwenden Sie "case var _:" für ein discard-Muster oder "case @_:" für eine Konstante namens "_".</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 @@ -122,6 +122,11 @@
<target state="translated">Un literal predeterminado "default" no es válido como patrón. Use otro literal (por ejemplo, "0" o "null") según corresponda. Para hacer coincidir todo, use un patrón de descarte "_".</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">El patrón de descarte no se permite como etiqueta de caso en una instrucción switch. Use "case var _:" para un patrón de descarte o "case @_:" para una constante con el nombre '_'.</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 @@ -122,6 +122,11 @@
<target state="translated">Un littéral par défaut 'default' est non valide en tant que modèle. Utilisez un autre littéral (par exemple, '0' ou 'null') selon le cas. Pour correspondre à tout, utilisez un modèle d'abandon '_'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">Le modèle d'abandon n'est pas autorisé en tant qu'étiquette case dans une instruction switch. Utilisez 'case var _:' pour un modèle d'abandon, ou 'case @_:' pour une constante nommée '_'.</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 @@ -122,6 +122,11 @@
<target state="translated">Non è possibile usare un valore letterale predefinito 'default' come criterio. Usare un altro valore letterale, ad esempio '0' o 'null'. Per abbinare tutto, usare un criterio di rimozione '_'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">Il criterio di rimozione non è consentito come etichetta case in un'istruzione switch. Usare 'case var _:' per un criterio di rimozione oppure 'case @_:' per una costante denominata '_'.</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 @@ -122,6 +122,11 @@
<target state="translated">既定のリテラル 'default' はパターンとして無効です。必要に応じて別のリテラル (例: '0' または 'null') をご使用ください。すべてと一致させるには、破棄パターン '_' をご使用ください。</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">この破棄パターンは switch ステートメントの case ラベルとして許可されていません。破棄パターンに 'case var _:' を使用するか、'_' という定数に'case @_:' をご使用ください。</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 @@ -122,6 +122,11 @@
<target state="translated">기본 리터럴 'default'가 패턴으로 유효하지 않습니다. 다른 리터럴(예: '0' 또는 'null')을 적절하게 사용하세요. 모두 일치시키려면 무시 패턴 '_'을 사용하세요.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">무시 패턴은 switch 문의 case 레이블로 사용할 수 없습니다. 무시 패턴에 대해 'case var _:'을 사용하거나 이름이 '_'인 상수에 대해 'case @_:'을 사용하세요.</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 @@ -122,6 +122,11 @@
<target state="translated">Domyślny literał „default” nie jest prawidłowy jako wzorzec. Użyj innego odpowiedniego literału (np. „0” lub „null”). Aby dopasować wszystko, użyj wzorca odrzucania „_”.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">Wzorzec odrzucania nie jest dozwolony jako etykieta instrukcji case w instrukcji switch. Użyj instrukcji „case var _:” w przypadku wzorca odrzucania lub użyj instrukcji „case @_:” w przypadku stałej o nazwie „_”.</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 @@ -122,6 +122,11 @@
<target state="translated">Um literal padrão 'default' não é válido como um padrão. Use outro literal (por exemplo, '0' ou 'null') conforme o necessário. Para corresponder a tudo, use um padrão de descarte '_'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">O padrão de descarte não é permitido como um rótulo de caso em uma instrução switch. Use 'case var _:' para um padrão de descarte ou 'case @_:' para uma constante chamada '_'.</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 @@ -122,6 +122,11 @@
<target state="translated">Литерал по умолчанию "default" недопустимо использовать в качестве шаблона. Используйте другой литерал (например, "0" или "null") по мере необходимости. Чтобы задать полное совпадение, используйте шаблон отмены "_".</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">Шаблон отмены запрещено использовать как метку case в операторе switch. Используйте "case var _:" в качестве шаблона отмены или "case @_:" в качестве константы "_".</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 @@ -122,6 +122,11 @@
<target state="translated">'default' varsayılan sabit değeri bir desen olarak geçerli değil. Uygun olan başka bir sabit değeri (ör. '0' veya 'null') kullanın. Tüm öğeleri eşleştirmek için '_' atma desenini kullanın.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">Bir switch deyiminde case etiketi olarak atma desenine izin verilmez. Atma deseni için 'case var _:' veya '_' adlı bir sabit için 'case @_:' seçeneğini kullanın.</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 @@ -122,6 +122,11 @@
<target state="new">There is no target type for the default literal.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">在 switch 语句中,不允许将放弃模式作为大小写标签。对于放弃模式,使用 "case var _:";对于名为 "_" 的常量,使用 "case @_:"。</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 @@ -122,6 +122,11 @@
<target state="translated">預設常值 'default' 作為模式無效。請使用另一個適當的常值 (例如 '0' 或 'null')。若要比對所有項目,請使用捨棄模式 '_'。</target>
<note />
</trans-unit>
<trans-unit id="ERR_DesignatorBeneathPatternCombinator">
<source>A variable may not be declared within a 'not' or 'or' pattern.</source>
<target state="new">A variable may not be declared within a 'not' or 'or' pattern.</target>
<note />
</trans-unit>
<trans-unit id="ERR_DiscardPatternInSwitchStatement">
<source>The discard pattern is not permitted as a case label in a switch statement. Use 'case var _:' for a discard pattern, or 'case @_:' for a constant named '_'.</source>
<target state="translated">捨棄模式不可為 switch 陳述式中的 case 標籤。針對捨棄模式,請使用 'case var _:',針對名為 '_' 的常數,則請使用 'case @_:'。</target>
Expand Down
Loading

0 comments on commit c0e6baf

Please sign in to comment.