From 201141080c8ab67d53281c35cb1cacb82a368f95 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Fri, 14 Oct 2022 21:37:43 +0200 Subject: [PATCH 01/39] Clarify diagnostics --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 4 ++++ src/Compilers/CSharp/Portable/CSharpResources.resx | 3 +++ src/Compilers/CSharp/Portable/Errors/ErrorCode.cs | 1 + src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf | 5 +++++ src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf | 5 +++++ .../CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf | 5 +++++ .../CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf | 5 +++++ 16 files changed, 73 insertions(+) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 87aa77fca3797..5f0a82d47ade2 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -686,6 +686,10 @@ internal BoundExpression ConvertPatternExpression( { convertedExpression = operand; } + else if (conversion.ConversionKind == ConversionKind.ImplicitUserDefined) + { + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, convertedExpression.Syntax.Location, conversion.Operand.Type, conversion.Type); + } } } diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index 616853f44bc14..fae1d25b6286b 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -7382,4 +7382,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ A deconstruction variable cannot be declared as a ref local + + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + \ No newline at end of file diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index 4b71bce270a8f..99ac9db729b2b 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -2151,6 +2151,7 @@ internal enum ErrorCode ERR_RefAssignValEscapeWider = 9096, WRN_RefAssignValEscapeWider = 9097, + ERR_NonConstantConversionInConstantPattern = 9096 #endregion // Note: you will need to do the following after adding any code: diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index b2086c4938317..3abaab99d2856 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -1067,6 +1067,11 @@ Nepovedlo se určit výstupní adresář. + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. Člen záznamu {0} musí být privátní. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index 7b5c7556dc23d..67f0811b8f690 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -1067,6 +1067,11 @@ Das Ausgabeverzeichnis konnte nicht bestimmt werden. + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. Der Datensatzmember "{0}" muss privat sein. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index 6612728e2812a..c2b4525f7a637 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -1067,6 +1067,11 @@ No se pudo determinar el directorio de salida. + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. El miembro de registro "{0}" debe ser privado. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index 0264bd40fe1e7..8040284cf33ea 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -1067,6 +1067,11 @@ Impossible de déterminer le répertoire de sortie + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. Le membre d'enregistrement '{0}' doit être privé. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index f5813fd2a1641..32f61455b7a2e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -1067,6 +1067,11 @@ Non è stato possibile individuare la directory di output + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. Il membro del record '{0}' deve essere privato. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index 470eae4d9027c..b65a8dfe8225d 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -1067,6 +1067,11 @@ 出力ディレクトリを特定できませんでした + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. レコード メンバー '{0}' は private でなければなりません。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index d92f1f46c2024..2d375f586f93e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -1067,6 +1067,11 @@ 출력 디렉터리를 확인할 수 없습니다. + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. 레코드 멤버 '{0}'은(는) 프라이빗이어야 합니다. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index 24090dd9237e8..85395d9814852 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -1067,6 +1067,11 @@ Nie można było określić katalogu wyjściowego + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. Składowa rekordu „{0}” musi być prywatna. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index ba54938a62f00..adfac54623251 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -1067,6 +1067,11 @@ Não foi possível determinar o diretório de saída + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. O membro do registro '{0}' precisa ser privado. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index 7f034d5294183..28d6d8e13b117 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -1067,6 +1067,11 @@ Не удалось определить выходной каталог + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. Элемент записи "{0}" должен быть закрытым. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index 68078b1ceab59..e295fded48e7c 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -1067,6 +1067,11 @@ Çıkış dizini belirlenemedi + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. '{0}' kayıt üyesi özel olmalıdır. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 519c96f1b7830..9814a43cb6eea 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -1067,6 +1067,11 @@ 无法确定输出目录 + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. 记录成员“{0}”必须是非公开的。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index 457d6c2bf9e6e..f718d8c616bbd 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -1067,6 +1067,11 @@ 無法判斷輸出目錄 + + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + + Record member '{0}' must be private. 記錄成員 '{0}' 必須為私人。 From b00555dab84f16b5310d5790a5a7031b4bbfe224 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Fri, 14 Oct 2022 22:45:39 +0200 Subject: [PATCH 02/39] update localised resources with changed english text --- src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf | 4 ++-- 13 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index 3abaab99d2856..1021b288db8ae 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index 67f0811b8f690..e477d7f224df8 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index c2b4525f7a637..1b47c4c5c0e09 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index 8040284cf33ea..38d73c21908eb 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index 32f61455b7a2e..b5aabc805882b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index b65a8dfe8225d..f99d3adfcbea9 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index 2d375f586f93e..e00fcd57eacdf 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index 85395d9814852..2e47c98381e5b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index adfac54623251..0222dae1583f8 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index 28d6d8e13b117..678cfe96cccec 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index e295fded48e7c..96e146049f53b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 9814a43cb6eea..9c2247c3c2262 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index f718d8c616bbd..3af328e3e5291 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to input type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion From b1d900fc0b66caa90e5f3a2b6bd75ceaada3a897 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Fri, 14 Oct 2022 22:45:56 +0200 Subject: [PATCH 03/39] relocate statement to only add one errocode to diagnostics --- .../CSharp/Portable/Binder/Binder_Patterns.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 5f0a82d47ade2..a62ee1913649e 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -542,7 +542,12 @@ private BoundExpression BindExpressionForPatternContinued( if (!convertedExpression.HasErrors && !hasErrors) { - if (constantValueOpt == null) + var conversion = convertedExpression as BoundConversion; + if (conversion != null && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) + { + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, convertedExpression.Syntax.Location, conversion.Operand.Type, conversion.Type); + } + else if (constantValueOpt == null) { diagnostics.Add(ErrorCode.ERR_ConstantExpected, patternExpression.Location); hasErrors = true; @@ -686,10 +691,6 @@ internal BoundExpression ConvertPatternExpression( { convertedExpression = operand; } - else if (conversion.ConversionKind == ConversionKind.ImplicitUserDefined) - { - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, convertedExpression.Syntax.Location, conversion.Operand.Type, conversion.Type); - } } } From 4aad412111b76c4f264b00ead1252467374bbb9f Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Fri, 14 Oct 2022 22:52:37 +0200 Subject: [PATCH 04/39] change variable usage and supress null warning --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index a62ee1913649e..496df40d7a948 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -545,8 +545,8 @@ private BoundExpression BindExpressionForPatternContinued( var conversion = convertedExpression as BoundConversion; if (conversion != null && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) { - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, convertedExpression.Syntax.Location, conversion.Operand.Type, conversion.Type); - } + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, conversion.Syntax.Location, conversion.Operand.Type!, conversion.Type); + } else if (constantValueOpt == null) { diagnostics.Add(ErrorCode.ERR_ConstantExpected, patternExpression.Location); From 540293fccc37039a29d529762f1ec8d82ab8ab3c Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Fri, 14 Oct 2022 22:52:50 +0200 Subject: [PATCH 05/39] change variable usage and supress null warning --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 496df40d7a948..204e2e516cb71 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -545,7 +545,7 @@ private BoundExpression BindExpressionForPatternContinued( var conversion = convertedExpression as BoundConversion; if (conversion != null && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) { - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, conversion.Syntax.Location, conversion.Operand.Type!, conversion.Type); + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, conversion.Operand.Type!, conversion.Type); } else if (constantValueOpt == null) { From 6dc0cbf0c957fc660fe5b2f93d749bfe31c827f0 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Fri, 14 Oct 2022 23:38:15 +0200 Subject: [PATCH 06/39] resolve enum conflict --- src/Compilers/CSharp/Portable/Errors/ErrorCode.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index 99ac9db729b2b..d1faa3301186a 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -2151,7 +2151,7 @@ internal enum ErrorCode ERR_RefAssignValEscapeWider = 9096, WRN_RefAssignValEscapeWider = 9097, - ERR_NonConstantConversionInConstantPattern = 9096 + ERR_NonConstantConversionInConstantPattern = 9098 #endregion // Note: you will need to do the following after adding any code: From b6ff4d63315e90eb1834dad1e84f6428cecf77e4 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 12:21:17 +0200 Subject: [PATCH 07/39] re-add errorcode to nonbuildonly --- src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs index 22e689685f20d..32dcd202f56c9 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs @@ -2268,6 +2268,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code) case ErrorCode.WRN_RefReturnOnlyParameter2: case ErrorCode.ERR_RefAssignValEscapeWider: case ErrorCode.WRN_RefAssignValEscapeWider: + case ErrorCode.ERR_NonConstantConversionInConstantPattern: return false; default: // NOTE: All error codes must be explicitly handled in this switch statement From abc35ce082fe881ddc7b350e404a4bae89bbd7a8 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 12:22:26 +0200 Subject: [PATCH 08/39] rewrite binder to handle literal nulls and use correct variables for message types --- .../CSharp/Portable/Binder/Binder_Patterns.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 204e2e516cb71..2bbccfd90a380 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -542,14 +542,16 @@ private BoundExpression BindExpressionForPatternContinued( if (!convertedExpression.HasErrors && !hasErrors) { - var conversion = convertedExpression as BoundConversion; - if (conversion != null && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) + if (constantValueOpt == null) { - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, conversion.Operand.Type!, conversion.Type); - } - else if (constantValueOpt == null) - { - diagnostics.Add(ErrorCode.ERR_ConstantExpected, patternExpression.Location); + if (!expression.IsLiteralNull() && convertedExpression is BoundConversion conversion && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) + { + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, expression.Type!, inputType); + } + else + { + diagnostics.Add(ErrorCode.ERR_ConstantExpected, patternExpression.Location); + } hasErrors = true; } else if (inputType.IsPointerType()) From d06febffd8a33137b345591a028689217bcee125 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 12:28:38 +0200 Subject: [PATCH 09/39] clarify error message --- src/Compilers/CSharp/Portable/CSharpResources.resx | 2 +- src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf | 4 ++-- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index fae1d25b6286b..f49db3613f430 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -7383,6 +7383,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ A deconstruction variable cannot be declared as a ref local - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression \ No newline at end of file diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index 1021b288db8ae..c013a25a2b087 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index e477d7f224df8..f90fd4a3a0791 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index 1b47c4c5c0e09..340ff20b3619e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index 38d73c21908eb..6e832e2fc1ef1 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index b5aabc805882b..f55aac76297e7 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index f99d3adfcbea9..45c5207bd424f 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index e00fcd57eacdf..1beba705ebd20 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index 2e47c98381e5b..31748b64fe671 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index 0222dae1583f8..c87cf5d2cb084 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index 678cfe96cccec..c8aca92e62f3b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index 96e146049f53b..18dafb2a1b2ff 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 9c2247c3c2262..bda4f14775815 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index 3af328e3e5291..dd44008aa1964 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion - Cannot implicitly convert type '{0}' to type '{1}' in constant pattern using non-constant conversion + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression From 5e13b6c1900a9451171437054f520fac64850c8e Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 13:09:05 +0200 Subject: [PATCH 10/39] show null literal conversion error when relevant --- .../CSharp/Portable/Binder/Binder_Patterns.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 2bbccfd90a380..6d69d441fe5dd 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -543,10 +543,17 @@ private BoundExpression BindExpressionForPatternContinued( if (!convertedExpression.HasErrors && !hasErrors) { if (constantValueOpt == null) - { - if (!expression.IsLiteralNull() && convertedExpression is BoundConversion conversion && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) + { + if (convertedExpression is BoundConversion conversion && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) { - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, expression.Type!, inputType); + if(expression.IsLiteralNull() && !inputType.IsNullableType()) + { + diagnostics.Add(ErrorCode.ERR_ValueCantBeNull, patternExpression.Location, inputType); + } + else + { + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, expression.Type!, inputType); + } } else { From c9d58cc57c248e1cbf0252d19148a958a0286652 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 13:09:52 +0200 Subject: [PATCH 11/39] update unit tests about null literals to expect valuecantbenull error --- .../Test/Emit2/Semantics/PatternMatchingTests.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index 65a2b64379634..cdad34ec36e5a 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -8444,9 +8444,9 @@ class C }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview); comp.VerifyDiagnostics( - // (5,58): error CS0150: A constant value is expected + // (5,50): error CS0037: Cannot convert null to 'ReadOnlySpan' because it is a non-nullable value type // static bool M1(ReadOnlySpan chars) => chars is null; - Diagnostic(ErrorCode.ERR_ConstantExpected, "null").WithLocation(5, 58), + Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.ReadOnlySpan").WithLocation(5, 58), // (6,58): error CS8505: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'. // static bool M2(ReadOnlySpan chars) => chars is default; Diagnostic(ErrorCode.ERR_DefaultPattern, "default").WithLocation(6, 58)); @@ -8517,9 +8517,9 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (5,63): error CS0150: A constant value is expected + // (5,63): error CS0037: Cannot convert null to 'ReadOnlySpan' because it is a non-nullable value type // static bool M(ReadOnlySpan chars) => chars switch { null => true, _ => false }; - Diagnostic(ErrorCode.ERR_ConstantExpected, "null").WithLocation(5, 63)); + Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.ReadOnlySpan").WithLocation(5, 63)); } [Fact] @@ -9796,9 +9796,9 @@ class C }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview); comp.VerifyDiagnostics( - // (5,50): error CS0150: A constant value is expected + // (5,50): error CS0037: Cannot convert null to 'Span' because it is a non-nullable value type // static bool M1(Span chars) => chars is null; - Diagnostic(ErrorCode.ERR_ConstantExpected, "null").WithLocation(5, 50), + Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.Span").WithLocation(5, 50), // (6,50): error CS8505: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'. // static bool M2(Span chars) => chars is default; Diagnostic(ErrorCode.ERR_DefaultPattern, "default").WithLocation(6, 50)); @@ -9869,9 +9869,9 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (5,55): error CS0150: A constant value is expected + // (5,55): error CS0037: Cannot convert null to 'Span' because it is a non-nullable value type // static bool M(Span chars) => chars switch { null => true, _ => false }; - Diagnostic(ErrorCode.ERR_ConstantExpected, "null").WithLocation(5, 55)); + Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.Span").WithLocation(5, 55)); } [ConditionalFact(typeof(CoreClrOnly))] From ccbec6aebc1432993ca9145c56b6f4297c11f270 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 13:10:31 +0200 Subject: [PATCH 12/39] add unit tests related to workitem #63476 and implemented changes --- .../Emit2/Semantics/PatternMatchingTests.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index cdad34ec36e5a..b415ed8ab6491 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -11784,5 +11784,59 @@ public void Test2(G x2) Diagnostic(ErrorCode.ERR_BadBinaryOps, "x2 as T").WithArguments("as", "G", "T").WithLocation(11, 18) ); } + + [Fact] + [WorkItem(63476, "https://github.com/dotnet/roslyn/issues/63476")] + public void PatternNonConstant_UserDefined_ConvertionToInputType() + { + var source = +@" +class A { + public string S { get; set; } + public static implicit operator A(string s) { return new A { S = s }; } +} +class C +{ + static bool M(A a) => a switch { ""implicitA"" => true, _ => false }; +}"; + CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) + .VerifyDiagnostics( + // (8,38): error CS9098: Cannot implicitly convert type 'string' to type 'A' in constant pattern using non-constant conversion + // static bool M(A a) => a switch { "convertme" => true, _ => false }; + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, @"""implicitA""").WithArguments("string", "A").WithLocation(8, 38)); + } + + [Fact] + [WorkItem(63476, "https://github.com/dotnet/roslyn/issues/63476")] + public void PatternReadOnlySpanConvertionToString() + { + //cannot implicitly cast long to byte.. + var source = +@" +using System; +class C +{ + static bool M(ReadOnlySpan chars) => chars switch { """" => true, _ => false }; +}"; + CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) + .VerifyDiagnostics(); //this used to not work before work item related code changes + } + + [Fact] + public void PatternNoImplicitConvertionToInputType() + { + //cannot implicitly cast long to byte.. + var source = +@" +class C +{ + static bool M(byte b) => b switch { 1L => true, _ => false }; +}"; + CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) + .VerifyDiagnostics( + // (4,41): error CS0266: Cannot implicitly convert type 'long' to 'byte'. An explicit conversion exists (are you missing a cast?) + // static bool M(byte b) => b switch { 1l => true, _ => false }; + Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "1L").WithArguments("long", "byte").WithLocation(4, 41)); + } } } From 887bed096709127fe5cd11dad5f5b43ddc25861c Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 13:34:46 +0200 Subject: [PATCH 13/39] fix formatting --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 6d69d441fe5dd..991852aaee1b5 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -543,13 +543,13 @@ private BoundExpression BindExpressionForPatternContinued( if (!convertedExpression.HasErrors && !hasErrors) { if (constantValueOpt == null) - { + { if (convertedExpression is BoundConversion conversion && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) { - if(expression.IsLiteralNull() && !inputType.IsNullableType()) + if (expression.IsLiteralNull() && !inputType.IsNullableType()) { diagnostics.Add(ErrorCode.ERR_ValueCantBeNull, patternExpression.Location, inputType); - } + } else { diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, expression.Type!, inputType); From d8d71aae6bb408336baba3280171d06af52c8a03 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 14:58:56 +0200 Subject: [PATCH 14/39] fix overlooked test cases --- .../IOperation/IOperationTests_ISwitchOperation.cs | 4 ++-- .../CSharp/Test/Symbol/Symbols/ConversionTests.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index e3207923caad1..cc0e624ef15c4 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -1469,9 +1469,9 @@ void M(bool result, MyClass input) } "; var expectedDiagnostics = new[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9098: Cannot implicitly convert type 'int' to type 'MyClass' using user-defined conversion in pattern expression // case 1: - Diagnostic(ErrorCode.ERR_ConstantExpected, "1").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "1").WithArguments("int", "MyClass").WithLocation(8, 18) }; string expectedFlowGraph = @" diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs index ec02d2e86d81d..7c425891c35f4 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs @@ -1131,9 +1131,9 @@ public static implicit operator int(Convertible e) } }"; CreateCompilation(source).VerifyDiagnostics( - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9098: Cannot implicitly convert type 'Convertible' to type 'int' using user-defined conversion in pattern expression // case default(Convertible): return; - Diagnostic(ErrorCode.ERR_ConstantExpected, "default(Convertible)").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "default(Convertible)").WithArguments("Convertible", "int").WithLocation(8, 18) ); } @@ -1161,9 +1161,9 @@ public static implicit operator int(Convertible e) } }"; CreateCompilation(source).VerifyDiagnostics( - // (9,18): error CS0150: A constant value is expected + // (9,18): error CS9098: Cannot implicitly convert type 'Convertible' to type 'int' using user-defined conversion in pattern expression // case c: return; - Diagnostic(ErrorCode.ERR_ConstantExpected, "c").WithLocation(9, 18) + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "c").WithArguments("Convertible", "int").WithLocation(9, 18) ); } From 554394fecf866f02d49ec95ef8533ae031ff5ee2 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 15:22:57 +0200 Subject: [PATCH 15/39] update test to not relate to workitem but simply demonstrates built in conversion --- .../CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index b415ed8ab6491..1972fc90e50e3 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -11807,10 +11807,8 @@ class C } [Fact] - [WorkItem(63476, "https://github.com/dotnet/roslyn/issues/63476")] - public void PatternReadOnlySpanConvertionToString() + public void PatternReadOnlySpan_ImplicitBuiltInConvertion_ToString() { - //cannot implicitly cast long to byte.. var source = @" using System; @@ -11819,7 +11817,7 @@ class C static bool M(ReadOnlySpan chars) => chars switch { """" => true, _ => false }; }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) - .VerifyDiagnostics(); //this used to not work before work item related code changes + .VerifyDiagnostics(); //allowed due to built in conversion } [Fact] From 25000172a7809610e809d5b979f919c8b1095464 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 15 Oct 2022 15:45:27 +0200 Subject: [PATCH 16/39] correct message in comment --- .../CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index 1972fc90e50e3..fa7aaa2bc9a3a 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -11801,7 +11801,7 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (8,38): error CS9098: Cannot implicitly convert type 'string' to type 'A' in constant pattern using non-constant conversion + // (8,38): error CS9098: Cannot implicitly convert type 'string' to type 'A' using user-defined conversion in pattern expression // static bool M(A a) => a switch { "convertme" => true, _ => false }; Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, @"""implicitA""").WithArguments("string", "A").WithLocation(8, 38)); } From 9c3e2ff29c1cf37bba8b7baafa9c3e229a5384aa Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 26 Oct 2022 20:11:11 +0200 Subject: [PATCH 17/39] ErrorCode.cs formatting Co-authored-by: Rikki Gibson --- src/Compilers/CSharp/Portable/Errors/ErrorCode.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index d1faa3301186a..42594965c1b2a 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -2151,7 +2151,8 @@ internal enum ErrorCode ERR_RefAssignValEscapeWider = 9096, WRN_RefAssignValEscapeWider = 9097, - ERR_NonConstantConversionInConstantPattern = 9098 + ERR_NonConstantConversionInConstantPattern = 9098, + #endregion // Note: you will need to do the following after adding any code: From 84e660f0c216355aa6e19c0c02a3f6c767c4d660 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 26 Oct 2022 20:12:47 +0200 Subject: [PATCH 18/39] not null assert instead of ! suppression Co-authored-by: Rikki Gibson --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 991852aaee1b5..d133971a1dac4 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -552,7 +552,8 @@ private BoundExpression BindExpressionForPatternContinued( } else { - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, expression.Type!, inputType); + Debug.Assert(expression.Type is not null); + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, expression.Type, inputType); } } else From 5bc527a87cbe09d40457d5d8e7835c36439aa77f Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 26 Oct 2022 21:20:22 +0200 Subject: [PATCH 19/39] test for explicit conversions + use symboldistinguisher --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index d133971a1dac4..52482e3a0fca6 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -544,7 +544,7 @@ private BoundExpression BindExpressionForPatternContinued( { if (constantValueOpt == null) { - if (convertedExpression is BoundConversion conversion && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) + if (convertedExpression is BoundConversion conversion && conversion.ConversionKind is ConversionKind.ImplicitUserDefined or ConversionKind.ExplicitUserDefined) { if (expression.IsLiteralNull() && !inputType.IsNullableType()) { @@ -552,8 +552,9 @@ private BoundExpression BindExpressionForPatternContinued( } else { - Debug.Assert(expression.Type is not null); - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, expression.Type, inputType); + Debug.Assert(conversion.Operand is not null); + SymbolDistinguisher distinguisher1 = new SymbolDistinguisher(this.Compilation, conversion.Operand.Type, inputType); + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, distinguisher1.First, distinguisher1.Second); } } else From 6391b38fbc3f30b6033570430e08760dc5c50715 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 26 Oct 2022 21:21:23 +0200 Subject: [PATCH 20/39] generalize error message --- src/Compilers/CSharp/Portable/CSharpResources.resx | 2 +- src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf | 4 ++-- src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf | 4 ++-- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index f49db3613f430..f18b953e86dcf 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -7383,6 +7383,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ A deconstruction variable cannot be declared as a ref local - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression \ No newline at end of file diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index c013a25a2b087..3855ca55d31f4 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index f90fd4a3a0791..197a4047eb73a 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index 340ff20b3619e..61c3b651edd61 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index 6e832e2fc1ef1..cbb438c862bce 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index f55aac76297e7..039f0e534e8a1 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index 45c5207bd424f..4cbd953fe1034 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index 1beba705ebd20..5a09ccc88bb1e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index 31748b64fe671..c314d303e9878 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index c87cf5d2cb084..b5606b794e33a 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index c8aca92e62f3b..12b817c47ec68 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index 18dafb2a1b2ff..4978da058c500 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index bda4f14775815..57b40b2b8192a 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index dd44008aa1964..765b9dbb05408 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -1068,8 +1068,8 @@ - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot implicitly convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression From 42e816e45f121d234b534bcd6dbf3f13d3fc15ed Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 26 Oct 2022 21:21:34 +0200 Subject: [PATCH 21/39] add unit test with explicit cast --- .../Emit2/Semantics/PatternMatchingTests.cs | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index fa7aaa2bc9a3a..e67b243679171 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -11787,7 +11787,7 @@ public void Test2(G x2) [Fact] [WorkItem(63476, "https://github.com/dotnet/roslyn/issues/63476")] - public void PatternNonConstant_UserDefined_ConvertionToInputType() + public void PatternNonConstant_UserDefinedImplicit_ConvertionToInputType() { var source = @" @@ -11801,9 +11801,32 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (8,38): error CS9098: Cannot implicitly convert type 'string' to type 'A' using user-defined conversion in pattern expression - // static bool M(A a) => a switch { "convertme" => true, _ => false }; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, @"""implicitA""").WithArguments("string", "A").WithLocation(8, 38)); + // (8,38): error CS9098: Cannot convert type 'string' to type 'A' using user-defined conversion in pattern expression + // static bool M(A a) => a switch { "implicitA" => true, _ => false }; + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, @"""implicitA""").WithArguments("string", "A").WithLocation(8, 38) + ); + } + + [Fact] + [WorkItem(63476, "https://github.com/dotnet/roslyn/issues/63476")] + public void PatternNonConstant_UserDefinedExplicit_ConvertionToInputType() + { + var source = +@" +class A { + public string S { get; set; } + public static implicit operator A(string s) { return new A { S = s }; } +} +class C +{ + static bool M(A a) => a switch { (A)""castedA"" => true, _ => false }; +}"; + CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) + .VerifyDiagnostics( + // (8,38): error CS9098: Cannot convert type 'string' to type 'A' using user-defined conversion in pattern expression + // static bool M(A a) => a switch { "implicitA" => true, _ => false }; + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, @"(A)""castedA""").WithArguments("string", "A").WithLocation(8, 38) + ); } [Fact] @@ -11817,13 +11840,13 @@ class C static bool M(ReadOnlySpan chars) => chars switch { """" => true, _ => false }; }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) - .VerifyDiagnostics(); //allowed due to built in conversion + .VerifyDiagnostics(); // Allowed due to built in conversion } [Fact] public void PatternNoImplicitConvertionToInputType() { - //cannot implicitly cast long to byte.. + // Cannot implicitly cast long to byte.. var source = @" class C From da83f74967436590b3371a6f3538e7560ff894c6 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 26 Oct 2022 22:43:27 +0200 Subject: [PATCH 22/39] fix unit test --- .../IOperation/IOperation/IOperationTests_ISwitchOperation.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index cc0e624ef15c4..05920cd9d2534 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -1469,9 +1469,9 @@ void M(bool result, MyClass input) } "; var expectedDiagnostics = new[] { - // (8,18): error CS9098: Cannot implicitly convert type 'int' to type 'MyClass' using user-defined conversion in pattern expression + // (8,18): error CS9098: Cannot implicitly convert type 'long' to type 'MyClass' using user-defined conversion in pattern expression // case 1: - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "1").WithArguments("int", "MyClass").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "1").WithArguments("long", "MyClass").WithLocation(8, 18) }; string expectedFlowGraph = @" From 767dd67ed5c271aafc4dba92bbd050f183d2c3c9 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Thu, 27 Oct 2022 20:39:37 +0200 Subject: [PATCH 23/39] dont unnecessarily test for null - symboldistinguisher handles it just fine --- .../CSharp/Portable/Binder/Binder_Patterns.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 52482e3a0fca6..08d17464b72db 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -546,16 +546,9 @@ private BoundExpression BindExpressionForPatternContinued( { if (convertedExpression is BoundConversion conversion && conversion.ConversionKind is ConversionKind.ImplicitUserDefined or ConversionKind.ExplicitUserDefined) { - if (expression.IsLiteralNull() && !inputType.IsNullableType()) - { - diagnostics.Add(ErrorCode.ERR_ValueCantBeNull, patternExpression.Location, inputType); - } - else - { - Debug.Assert(conversion.Operand is not null); - SymbolDistinguisher distinguisher1 = new SymbolDistinguisher(this.Compilation, conversion.Operand.Type, inputType); - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, distinguisher1.First, distinguisher1.Second); - } + Debug.Assert(conversion.Operand is not null); + SymbolDistinguisher distinguisher1 = new SymbolDistinguisher(this.Compilation, conversion.Operand.Type, inputType); + diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, distinguisher1.First, distinguisher1.Second); } else { From 4d67f8295f7e31bc89c4903fc252bc39f4ab6970 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Thu, 27 Oct 2022 20:40:22 +0200 Subject: [PATCH 24/39] update null literal tests --- .../Emit2/Semantics/PatternMatchingTests.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index e67b243679171..c4d622cc4231f 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -8444,9 +8444,9 @@ class C }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview); comp.VerifyDiagnostics( - // (5,50): error CS0037: Cannot convert null to 'ReadOnlySpan' because it is a non-nullable value type + // (5,58): error CS9098: Cannot convert type 'char[]' to type 'System.ReadOnlySpan' using user-defined conversion in pattern expression // static bool M1(ReadOnlySpan chars) => chars is null; - Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.ReadOnlySpan").WithLocation(5, 58), + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.ReadOnlySpan").WithLocation(5, 58), // (6,58): error CS8505: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'. // static bool M2(ReadOnlySpan chars) => chars is default; Diagnostic(ErrorCode.ERR_DefaultPattern, "default").WithLocation(6, 58)); @@ -8517,9 +8517,10 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (5,63): error CS0037: Cannot convert null to 'ReadOnlySpan' because it is a non-nullable value type - // static bool M(ReadOnlySpan chars) => chars switch { null => true, _ => false }; - Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.ReadOnlySpan").WithLocation(5, 63)); + // (5,63): error CS9098: Cannot convert type 'char[]' to type 'System.ReadOnlySpan' using user-defined conversion in pattern expression + // static bool M(ReadOnlySpan chars) => chars switch { null => true, _ => false }; + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.ReadOnlySpan").WithLocation(5, 63) + ); } [Fact] @@ -9796,9 +9797,9 @@ class C }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview); comp.VerifyDiagnostics( - // (5,50): error CS0037: Cannot convert null to 'Span' because it is a non-nullable value type + // (5,50): error CS9098: Cannot convert type 'char[]' to type 'System.Span' using user-defined conversion in pattern expression // static bool M1(Span chars) => chars is null; - Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.Span").WithLocation(5, 50), + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.Span").WithLocation(5, 50), // (6,50): error CS8505: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'. // static bool M2(Span chars) => chars is default; Diagnostic(ErrorCode.ERR_DefaultPattern, "default").WithLocation(6, 50)); @@ -9868,10 +9869,11 @@ class C static bool M(Span chars) => chars switch { null => true, _ => false }; }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) - .VerifyDiagnostics( - // (5,55): error CS0037: Cannot convert null to 'Span' because it is a non-nullable value type - // static bool M(Span chars) => chars switch { null => true, _ => false }; - Diagnostic(ErrorCode.ERR_ValueCantBeNull, "null").WithArguments("System.Span").WithLocation(5, 55)); + .VerifyDiagnostics( + // (5,55): error CS9098: Cannot convert type 'char[]' to type 'System.Span' using user-defined conversion in pattern expression + // static bool M(Span chars) => chars switch { null => true, _ => false }; + Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.Span").WithLocation(5, 55) + ); } [ConditionalFact(typeof(CoreClrOnly))] From 7082eb972e0aedc1b0dca48040139d2b8703b534 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Thu, 17 Nov 2022 21:04:52 +0100 Subject: [PATCH 25/39] move errorcode and fix number --- src/Compilers/CSharp/Portable/Errors/ErrorCode.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index d0fbbe60fc264..f97b1652b5db5 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -2150,15 +2150,13 @@ internal enum ErrorCode WRN_RefReturnOnlyParameter2 = 9095, ERR_RefAssignValEscapeWider = 9096, WRN_RefAssignValEscapeWider = 9097, - - ERR_NonConstantConversionInConstantPattern = 9098, - #endregion #region diagnostics introduced for C# 12.0 ERR_ImplicitlyTypedDefaultParameter = 9098, WRN_OptionalParamValueMismatch = 9099, WRN_ParamsArrayInLambdaOnly = 9100, + ERR_NonConstantConversionInConstantPattern = 9101, #endregion // Note: you will need to do the following after adding warnings: From f899ed8eba1f31c205df1cecb721329a0258457e Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 18 Mar 2023 15:54:40 +0100 Subject: [PATCH 26/39] fix errorcode, message and tests --- .../CSharp/Portable/Binder/Binder_Patterns.cs | 3 +- .../CSharp/Portable/CSharpResources.resx | 4 +-- .../CSharp/Portable/Errors/ErrorCode.cs | 2 +- .../CSharp/Portable/Errors/ErrorFacts.cs | 2 +- .../Portable/xlf/CSharpResources.cs.xlf | 10 +++---- .../Portable/xlf/CSharpResources.de.xlf | 10 +++---- .../Portable/xlf/CSharpResources.es.xlf | 10 +++---- .../Portable/xlf/CSharpResources.fr.xlf | 10 +++---- .../Portable/xlf/CSharpResources.it.xlf | 10 +++---- .../Portable/xlf/CSharpResources.ja.xlf | 10 +++---- .../Portable/xlf/CSharpResources.ko.xlf | 10 +++---- .../Portable/xlf/CSharpResources.pl.xlf | 10 +++---- .../Portable/xlf/CSharpResources.pt-BR.xlf | 10 +++---- .../Portable/xlf/CSharpResources.ru.xlf | 10 +++---- .../Portable/xlf/CSharpResources.tr.xlf | 10 +++---- .../Portable/xlf/CSharpResources.zh-Hans.xlf | 10 +++---- .../Portable/xlf/CSharpResources.zh-Hant.xlf | 10 +++---- .../Emit2/Semantics/PatternMatchingTests.cs | 30 +++++++++---------- .../IOperationTests_ISwitchOperation.cs | 2 +- .../Test/Symbol/Symbols/ConversionTests.cs | 4 +-- 20 files changed, 88 insertions(+), 89 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index ae1cb0d8f3074..e729530db787c 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -560,8 +560,7 @@ private BoundExpression BindExpressionForPatternContinued( if (convertedExpression is BoundConversion conversion && conversion.ConversionKind is ConversionKind.ImplicitUserDefined or ConversionKind.ExplicitUserDefined) { Debug.Assert(conversion.Operand is not null); - SymbolDistinguisher distinguisher1 = new SymbolDistinguisher(this.Compilation, conversion.Operand.Type, inputType); - diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, distinguisher1.First, distinguisher1.Second); + diagnostics.Add(ErrorCode.ERR_ConstantValueOfTypeExpected, patternExpression.Location, inputType); } else { diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index d68f328bb09fd..28e5ed248380f 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -7499,7 +7499,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ The '&' operator should not be used on parameters or local variables in async methods. - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression + + A constant value of type '{0}' is expected \ No newline at end of file diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index 52c11ee5f3fc3..d712270e34886 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -2185,7 +2185,7 @@ internal enum ErrorCode ERR_BadRefInUsingAlias = 9130, ERR_BadUnsafeInUsingDirective = 9131, ERR_BadNullableReferenceTypeInUsingAlias = 9132, - ERR_NonConstantConversionInConstantPattern = 9133, + ERR_ConstantValueOfTypeExpected = 9133, #endregion diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs index e9998e9707ffa..495028ddd924b 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs @@ -2307,7 +2307,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code) case ErrorCode.ERR_BadRefInUsingAlias: case ErrorCode.ERR_BadUnsafeInUsingDirective: case ErrorCode.ERR_BadNullableReferenceTypeInUsingAlias: - case ErrorCode.ERR_NonConstantConversionInConstantPattern: + case ErrorCode.ERR_ConstantValueOfTypeExpected: return false; default: // NOTE: All error codes must be explicitly handled in this switch statement diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index dec003a7e9a76..a760d561baa22 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -397,6 +397,11 @@ Výraz typu {0} nelze zpracovat vzorem typu {1}. Použijte prosím verzi jazyka {2} nebo vyšší, aby odpovídala otevřenému typu se vzorem konstanty. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Kopírovací konstruktor v záznamu musí volat kopírovací konstruktor základní třídy, případně konstruktor objektu bez parametrů, pokud záznam dědí z objektu. @@ -1127,11 +1132,6 @@ Nepovedlo se určit výstupní adresář. - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. Člen záznamu {0} musí být privátní. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index bea694c7a9d09..c062784d5be53 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -397,6 +397,11 @@ Ein Ausdruck vom Typ "{0}" kann nicht von einem Muster vom Typ "{1}" behandelt werden. Verwenden Sie Sprachversion {2} oder höher, um einen offenen Typ mit einem konstanten Muster abzugleichen. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Ein Kopierkonstruktor in einem Datensatz muss einen Kopierkonstruktor der Basis oder einen parameterlosen Objektkonstruktor aufrufen, wenn der Datensatz von einem Objekt erbt. @@ -1127,11 +1132,6 @@ Das Ausgabeverzeichnis konnte nicht bestimmt werden. - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. Der Datensatzmember "{0}" muss privat sein. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index d2e7702fde45f..b7d738c077fa8 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -397,6 +397,11 @@ Un patrón de tipo "{0}" no se puede controlar por un patrón de tipo "{1}". Use la versión de lenguaje "{2}" o superior para buscar un tipo abierto con un patrón constante. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Un constructor de copia de un registro debe llamar a un constructor de copia de la base, o un constructor de objeto sin parámetros si el registro se hereda del objeto. @@ -1127,11 +1132,6 @@ No se pudo determinar el directorio de salida. - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. El miembro de registro "{0}" debe ser privado. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index be82d16aa92bd..3a21bddcfa9ed 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -397,6 +397,11 @@ Une expression de type '{0}' ne peut pas être prise en charge par un modèle de type '{1}'. Utilisez la version de langage '{2}' ou une version ultérieure pour faire correspondre un type ouvert à un modèle de constante. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Un constructeur de copie dans un enregistrement doit appeler un constructeur de copie de la base ou un constructeur d'objet sans paramètre, si l'enregistrement hérite de l'objet. @@ -1127,11 +1132,6 @@ Impossible de déterminer le répertoire de sortie - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. Le membre d'enregistrement '{0}' doit être privé. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index bf2c1ab7d3f27..15c7a7c329336 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -397,6 +397,11 @@ Un'espressione di tipo '{0}' non può essere gestita da un criterio di tipo '{1}'. Usare la versione '{2}' o versioni successive del linguaggio per abbinare un tipo aperto a un criterio costante. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Un costruttore di copia in un record deve chiamare un costruttore di copia della base o un costruttore di oggetto senza parametri se il record eredita dall'oggetto. @@ -1127,11 +1132,6 @@ Non è stato possibile individuare la directory di output - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. Il membro del record '{0}' deve essere privato. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index 246d52fdcea87..896d62d2ed723 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -397,6 +397,11 @@ 型 '{0}' の式を型 '{1}' のパターンで処理することはできません。オープン型と定数パターンを一致させるには、言語バージョン '{2}' 以上をご使用ください。 + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. レコード内のコピー コンストラクターは、ベースのコピー コンストラクターまたはパラメーターなしのオブジェクト コンストラクター (レコードがオブジェクトから継承している場合) を呼び出す必要があります。 @@ -1127,11 +1132,6 @@ 出力ディレクトリを特定できませんでした - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. レコード メンバー '{0}' は private でなければなりません。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index cd0cf2bee9954..df6a5e518fc16 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -397,6 +397,11 @@ '{0}' 형식의 식은 '{1}' 형식의 패턴으로 처리할 수 없습니다. 언어 버전 '{2}' 이상을 사용하여 개방형 형식과 상수 패턴을 일치시키세요. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. 레코드의 복사 생성자는 기준의 복사 생성자를 호출하거나, 레코드가 개체에서 상속되는 경우 매개 변수 없는 개체 생성자를 호출해야 합니다. @@ -1127,11 +1132,6 @@ 출력 디렉터리를 확인할 수 없습니다. - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. 레코드 멤버 '{0}'은(는) 프라이빗이어야 합니다. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index c630ff5a20142..7cb9089a46a43 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -397,6 +397,11 @@ Wyrażenie typu „{0}” nie może być obsługiwane przez wzorzec typu „{1}”. Użyj wersji języka „{2}” lub nowszej, aby dopasować typ otwarty za pomocą wzorca stałej. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Konstruktor kopiujący w rekordzie musi wywoływać konstruktor kopiujący elementu podstawowego lub bezparametrowy konstruktor obiektu, jeśli rekord dziedziczy z obiektu. @@ -1127,11 +1132,6 @@ Nie można było określić katalogu wyjściowego - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. Składowa rekordu „{0}” musi być prywatna. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index 8e341d5828516..feb00fb89b686 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -397,6 +397,11 @@ Uma expressão do tipo '{0}' não pode ser manipulada por um padrão do tipo '{1}'. Use a versão de linguagem '{2}' ou superior para corresponder a um tipo aberto com um padrão constante. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Um construtor de cópia em um registro precisa chamar um construtor de cópia da base ou um construtor de objeto sem parâmetros, quando o registro é herdado do objeto. @@ -1127,11 +1132,6 @@ Não foi possível determinar o diretório de saída - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. O membro do registro '{0}' precisa ser privado. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index 456c1cc8630df..f9aa87c17ee64 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -397,6 +397,11 @@ Выражение типа "{0}" не может быть обработано шаблоном типа "{1}". Используйте версию языка "{2}" или более позднюю, чтобы сопоставить открытый тип с постоянным шаблоном. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Конструктор копий в записи должен вызвать конструктор копий базового класса или конструктор объекта без параметров, если запись наследуется от объекта. @@ -1127,11 +1132,6 @@ Не удалось определить выходной каталог - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. Элемент записи "{0}" должен быть закрытым. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index 5bd85d3ce157e..ac4cac9285b88 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -397,6 +397,11 @@ '{0}' türünde bir ifade, '{1}' türünde bir desen tarafından işlenemez. Lütfen açık bir türü sabit bir desenle eşleştirmek için '{2}' veya daha yüksek bir dil sürümü kullanın. + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. Bir kayıttaki kopya oluşturucu, temel öğenin bir kopya oluşturucusuna veya kaydın nesneden devraldığı durumlarda parametresiz bir nesne oluşturucusuna çağrı yapmalıdır. @@ -1127,11 +1132,6 @@ Çıkış dizini belirlenemedi - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. '{0}' kayıt üyesi özel olmalıdır. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 25b7fb29ec8a5..7de021eada987 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -397,6 +397,11 @@ "{0}" 类型的表达式不能由 "{1}" 类型的模式进行处理。请使用语言版本 "{2}" 或更高版本,将开放类型与常数模式进行匹配。 + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. 如果记录继承自 object,则记录中的复制构造函数必须调用基对象的复制构造函数,或者调用无参数的对象构造函数。 @@ -1127,11 +1132,6 @@ 无法确定输出目录 - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. 记录成员“{0}”必须是非公开的。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index 59368e13f21e0..206102bc68036 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -397,6 +397,11 @@ 類型 '{0}' 的運算式無法由類型 '{1}' 的模式處理。請使用語言 '{2}' 版或更新版本,以比對開放式類型與常數模式。 + + A constant value of type '{0}' is expected + A constant value of type '{0}' is expected + + A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. 若記錄繼承自物件,則記錄中的複製建構函式,必須呼叫基底的複製建構函式,或未設定任何參數的物件建構函式。 @@ -1127,11 +1132,6 @@ 無法判斷輸出目錄 - - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - Cannot convert type '{0}' to type '{1}' using user-defined conversion in pattern expression - - Record member '{0}' must be private. 記錄成員 '{0}' 必須為私人。 diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index fa62481158144..e0a3d6fd40006 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -8689,9 +8689,9 @@ class C }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview); comp.VerifyDiagnostics( - // (5,58): error CS9098: Cannot convert type 'char[]' to type 'System.ReadOnlySpan' using user-defined conversion in pattern expression + // (5,58): error CS9133: A constant value of type 'ReadOnlySpan' is expected // static bool M1(ReadOnlySpan chars) => chars is null; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.ReadOnlySpan").WithLocation(5, 58), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "null").WithArguments("System.ReadOnlySpan").WithLocation(5, 58), // (6,58): error CS8505: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'. // static bool M2(ReadOnlySpan chars) => chars is default; Diagnostic(ErrorCode.ERR_DefaultPattern, "default").WithLocation(6, 58)); @@ -8762,10 +8762,10 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (5,63): error CS9098: Cannot convert type 'char[]' to type 'System.ReadOnlySpan' using user-defined conversion in pattern expression - // static bool M(ReadOnlySpan chars) => chars switch { null => true, _ => false }; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.ReadOnlySpan").WithLocation(5, 63) - ); + // (5,63): error CS9133: A constant value of type 'ReadOnlySpan' is expected + // static bool M(ReadOnlySpan chars) => chars switch { null => true, _ => false }; + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "null").WithArguments("System.ReadOnlySpan").WithLocation(5, 63) + ); } [Fact] @@ -10290,9 +10290,9 @@ class C }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview); comp.VerifyDiagnostics( - // (5,50): error CS9098: Cannot convert type 'char[]' to type 'System.Span' using user-defined conversion in pattern expression + // (5,50): error CS9133: A constant value of type 'Span' is expected // static bool M1(Span chars) => chars is null; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.Span").WithLocation(5, 50), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "null").WithArguments("System.Span").WithLocation(5, 50), // (6,50): error CS8505: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'. // static bool M2(Span chars) => chars is default; Diagnostic(ErrorCode.ERR_DefaultPattern, "default").WithLocation(6, 50)); @@ -10363,9 +10363,9 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (5,55): error CS9098: Cannot convert type 'char[]' to type 'System.Span' using user-defined conversion in pattern expression + // (5,55): error CS9133: A constant value of type 'Span' is expected // static bool M(Span chars) => chars switch { null => true, _ => false }; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "null").WithArguments("char[]", "System.Span").WithLocation(5, 55) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "null").WithArguments("System.Span").WithLocation(5, 55) ); } @@ -12296,9 +12296,9 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (8,38): error CS9098: Cannot convert type 'string' to type 'A' using user-defined conversion in pattern expression + // (8,38): error CS9133: A constant value of type 'A' is expected // static bool M(A a) => a switch { "implicitA" => true, _ => false }; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, @"""implicitA""").WithArguments("string", "A").WithLocation(8, 38) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"""implicitA""").WithArguments("A").WithLocation(8, 38) ); } @@ -12318,9 +12318,9 @@ class C }"; CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.RegularPreview) .VerifyDiagnostics( - // (8,38): error CS9098: Cannot convert type 'string' to type 'A' using user-defined conversion in pattern expression - // static bool M(A a) => a switch { "implicitA" => true, _ => false }; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, @"(A)""castedA""").WithArguments("string", "A").WithLocation(8, 38) + // (8,38): error CS9133: A constant value of type 'A' is expected + // static bool M(A a) => a switch { (A)"castedA" => true, _ => false }; + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"(A)""castedA""").WithArguments("A").WithLocation(8, 38) ); } diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index eeaa4b43cfcdf..17812f725653d 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -1471,7 +1471,7 @@ void M(bool result, MyClass input) var expectedDiagnostics = new[] { // (8,18): error CS9098: Cannot implicitly convert type 'long' to type 'MyClass' using user-defined conversion in pattern expression // case 1: - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "1").WithArguments("long", "MyClass").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1").WithArguments("long", "MyClass").WithLocation(8, 18) }; string expectedFlowGraph = @" diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs index 45f7bd18679a2..d33ea822a2b68 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs @@ -1132,7 +1132,7 @@ public static implicit operator int(Convertible e) CreateCompilation(source).VerifyDiagnostics( // (8,18): error CS9098: Cannot implicitly convert type 'Convertible' to type 'int' using user-defined conversion in pattern expression // case default(Convertible): return; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "default(Convertible)").WithArguments("Convertible", "int").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "default(Convertible)").WithArguments("Convertible", "int").WithLocation(8, 18) ); } @@ -1162,7 +1162,7 @@ public static implicit operator int(Convertible e) CreateCompilation(source).VerifyDiagnostics( // (9,18): error CS9098: Cannot implicitly convert type 'Convertible' to type 'int' using user-defined conversion in pattern expression // case c: return; - Diagnostic(ErrorCode.ERR_NonConstantConversionInConstantPattern, "c").WithArguments("Convertible", "int").WithLocation(9, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "c").WithArguments("Convertible", "int").WithLocation(9, 18) ); } From e966df65d34468011a7df2e437d4b9c3622c5ee3 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sat, 18 Mar 2023 18:21:52 +0100 Subject: [PATCH 27/39] update tests --- .../IOperation/IOperationTests_ISwitchOperation.cs | 4 ++-- .../CSharp/Test/Symbol/Symbols/ConversionTests.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index 17812f725653d..5f06630201298 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -1469,9 +1469,9 @@ void M(bool result, MyClass input) } "; var expectedDiagnostics = new[] { - // (8,18): error CS9098: Cannot implicitly convert type 'long' to type 'MyClass' using user-defined conversion in pattern expression + // (8,18): error CS9133: A constant value of type 'MyClass' is expected // case 1: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1").WithArguments("long", "MyClass").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1").WithArguments("MyClass").WithLocation(8, 18) }; string expectedFlowGraph = @" diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs index d33ea822a2b68..427a562614f5c 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/ConversionTests.cs @@ -1130,9 +1130,9 @@ public static implicit operator int(Convertible e) } }"; CreateCompilation(source).VerifyDiagnostics( - // (8,18): error CS9098: Cannot implicitly convert type 'Convertible' to type 'int' using user-defined conversion in pattern expression + // (8,18): error CS9133: A constant value of type 'int' is expected // case default(Convertible): return; - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "default(Convertible)").WithArguments("Convertible", "int").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "default(Convertible)").WithArguments("int").WithLocation(8, 18) ); } @@ -1160,9 +1160,9 @@ public static implicit operator int(Convertible e) } }"; CreateCompilation(source).VerifyDiagnostics( - // (9,18): error CS9098: Cannot implicitly convert type 'Convertible' to type 'int' using user-defined conversion in pattern expression + // (9,18): error CS9133: A constant value of type 'int' is expected // case c: return; - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "c").WithArguments("Convertible", "int").WithLocation(9, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "c").WithArguments("int").WithLocation(9, 18) ); } From 64c95946645e857a6c01f22e5b147beb73300c95 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sun, 19 Mar 2023 19:35:29 +0100 Subject: [PATCH 28/39] set ERR_ConstantValueOfTypeExpected except when the inputtype is errortype --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index e729530db787c..53d6973a18c2e 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -557,14 +557,14 @@ private BoundExpression BindExpressionForPatternContinued( { if (constantValueOpt == null) { - if (convertedExpression is BoundConversion conversion && conversion.ConversionKind is ConversionKind.ImplicitUserDefined or ConversionKind.ExplicitUserDefined) + if(inputType.Kind is not SymbolKind.ErrorType) { - Debug.Assert(conversion.Operand is not null); diagnostics.Add(ErrorCode.ERR_ConstantValueOfTypeExpected, patternExpression.Location, inputType); } else { diagnostics.Add(ErrorCode.ERR_ConstantExpected, patternExpression.Location); + } hasErrors = true; } From c7047bb122a3f0f008a325d00c72eaab907b89bd Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sun, 19 Mar 2023 19:35:49 +0100 Subject: [PATCH 29/39] update tests --- .../Emit2/Semantics/PatternMatchingTests.cs | 52 +++++++++---------- .../Emit2/Semantics/PatternMatchingTests3.cs | 6 +-- .../Emit2/Semantics/PatternMatchingTests4.cs | 48 ++++++++--------- .../Emit2/Semantics/PatternMatchingTests5.cs | 6 +-- .../Emit2/Semantics/PatternSwitchTests.cs | 6 +-- .../IOperationTests_IIsPatternExpression.cs | 4 +- .../IOperationTests_IPatternSwitchCase.cs | 8 +-- .../IOperationTests_ISwitchExpression.cs | 6 +-- .../Semantics/ImplicitObjectCreationTests.cs | 14 ++--- .../NullableReferenceTypesVsPatterns.cs | 8 +-- .../Semantics/Utf8StringsLiteralsTests.cs | 4 +- 11 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs index e0a3d6fd40006..4f99b0a55c5aa 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests.cs @@ -3221,13 +3221,13 @@ public static void Main(string[] args) "; var compilation = CreateCompilation(source, options: TestOptions.DebugExe); compilation.VerifyDiagnostics( - // (9,18): error CS0150: A constant value is expected + // (9,18): error CS9133: A constant value of type 'Type' is expected // case typeof(string): - Diagnostic(ErrorCode.ERR_ConstantExpected, "typeof(string)").WithLocation(9, 18), - // (12,18): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "typeof(string)").WithArguments("System.Type").WithLocation(9, 18), + // (12,18): error CS9133: A constant value of type 'Type' is expected // case typeof(string[]): - Diagnostic(ErrorCode.ERR_ConstantExpected, "typeof(string[])").WithLocation(12, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "typeof(string[])").WithArguments("System.Type").WithLocation(12, 18) + ); // If we support switching on System.Type as proposed, the expectation would be // something like CompileAndVerify(compilation, expectedOutput: @"string[]"); } @@ -4378,13 +4378,13 @@ public static void Main() "; var compilation = CreateCompilationWithMscorlib40AndSystemCore(source, options: TestOptions.DebugDll); compilation.VerifyDiagnostics( - // (9,29): error CS0118: '_' is a variable but is used like a type - // Write($"is _: {i is _}, "); - Diagnostic(ErrorCode.ERR_BadSKknown, "_").WithArguments("_", "variable", "type").WithLocation(9, 29), - // (12,18): error CS0150: A constant value is expected - // case _: - Diagnostic(ErrorCode.ERR_ConstantExpected, "_").WithLocation(12, 18) - ); + // (9,29): error CS0118: '_' is a variable but is used like a type + // Write($"is _: {i is _}, "); + Diagnostic(ErrorCode.ERR_BadSKknown, "_").WithArguments("_", "variable", "type").WithLocation(9, 29), + // (12,18): error CS9133: A constant value of type 'int' is expected + // case _: + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "_").WithArguments("int").WithLocation(12, 18) + ); } [Fact] @@ -4815,9 +4815,9 @@ public static void Main(string[] args) private static object M() => null; }"; var compilation = CreateCompilation(program).VerifyDiagnostics( - // (9,18): error CS0150: A constant value is expected + // (9,18): error CS9133: A constant value of type 'int' is expected // case true ? x3 : 4: - Diagnostic(ErrorCode.ERR_ConstantExpected, "true ? x3 : 4").WithLocation(9, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "true ? x3 : 4").WithArguments("int").WithLocation(9, 18), // (9,25): error CS0165: Use of unassigned local variable 'x3' // case true ? x3 : 4: Diagnostic(ErrorCode.ERR_UseDefViolation, "x3").WithArguments("x3").WithLocation(9, 25) @@ -10700,12 +10700,12 @@ class Program }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.Regular11); comp.VerifyEmitDiagnostics( - // (5,56): error CS0150: A constant value is expected + // (5,56): error CS9133: A constant value of type 'ReadOnlySpan' is expected // static bool F1(ReadOnlySpan span) => span is $"{123}"; - Diagnostic(ErrorCode.ERR_ConstantExpected, @"$""{123}""").WithLocation(5, 56), - // (6,48): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"$""{123}""").WithArguments("System.ReadOnlySpan").WithLocation(5, 56), + // (6,48): error CS9133: A constant value of type 'Span' is expected // static bool F2(Span span) => span is $"{n}"; - Diagnostic(ErrorCode.ERR_ConstantExpected, @"$""{n}""").WithLocation(6, 48)); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"$""{n}""").WithArguments("System.Span").WithLocation(6, 48)); } [Fact] @@ -10749,12 +10749,12 @@ class Program }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.Regular11); comp.VerifyEmitDiagnostics( - // (4,65): error CS0150: A constant value is expected + // (4,65): error CS9133: A constant value of type 'ReadOnlySpan' is expected // static bool F1(ReadOnlySpan span, bool b) => span is (b ? "" : "ABC"); - Diagnostic(ErrorCode.ERR_ConstantExpected, @"b ? """" : ""ABC""").WithLocation(4, 65), - // (5,57): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"b ? """" : ""ABC""").WithArguments("System.ReadOnlySpan").WithLocation(4, 65), + // (5,57): error CS9133: A constant value of type 'Span' is expected // static bool F2(Span span, bool b) => span is (b ? "" : "123"); - Diagnostic(ErrorCode.ERR_ConstantExpected, @"b ? """" : ""123""").WithLocation(5, 57)); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"b ? """" : ""123""").WithArguments("System.Span").WithLocation(5, 57)); } [Fact] @@ -10797,12 +10797,12 @@ class Program }"; var comp = CreateCompilationWithSpanAndMemoryExtensions(source, parseOptions: TestOptions.Regular11); comp.VerifyEmitDiagnostics( - // (4,64): error CS0150: A constant value is expected + // (4,64): error CS9133: A constant value of type 'ReadOnlySpan' is expected // static bool F1(ReadOnlySpan span, bool b) => span is b switch { true => "", false => "ABC" }; - Diagnostic(ErrorCode.ERR_ConstantExpected, @"b switch { true => """", false => ""ABC"" }").WithLocation(4, 64), - // (5,56): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"b switch { true => """", false => ""ABC"" }").WithArguments("System.ReadOnlySpan").WithLocation(4, 64), + // (5,56): error CS9133: A constant value of type 'Span' is expected // static bool F2(Span span, bool b) => span is b switch { false => "", true => "123" }; - Diagnostic(ErrorCode.ERR_ConstantExpected, @"b switch { false => """", true => ""123"" }").WithLocation(5, 56)); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"b switch { false => """", true => ""123"" }").WithArguments("System.Span").WithLocation(5, 56)); } [Fact] diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests3.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests3.cs index d750f09c34afc..27bd784d69384 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests3.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests3.cs @@ -5873,9 +5873,9 @@ static void M(int a) }"; var compilation = CreateCompilation(source, parseOptions: TestOptions.RegularWithPatternCombinators); compilation.VerifyDiagnostics( - // (5,18): error CS0150: A constant value is expected + // (5,18): error CS9133: A constant value of type 'int' is expected // if (a is a is > 0 and < 500) { } - Diagnostic(ErrorCode.ERR_ConstantExpected, "a").WithLocation(5, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "a").WithArguments("int").WithLocation(5, 18), // (5,25): error CS0029: Cannot implicitly convert type 'int' to 'bool' // if (a is a is > 0 and < 500) { } Diagnostic(ErrorCode.ERR_NoImplicitConv, "0").WithArguments("int", "bool").WithLocation(5, 25), @@ -5891,7 +5891,7 @@ static void M(int a) // (7,21): error CS0029: Cannot implicitly convert type 'int' to 'bool' // if (true is 0) { } Diagnostic(ErrorCode.ERR_NoImplicitConv, "0").WithArguments("int", "bool").WithLocation(7, 21) - ); + ); } [Fact, WorkItem(44518, "https://github.com/dotnet/roslyn/issues/44518")] diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs index 2fcc94c8b23f4..e2d555dc38c30 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs @@ -55,9 +55,9 @@ public void M(T t) } }"); comp.VerifyDiagnostics( - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9133: A constant value of type 'T' is expected // case default(T): - Diagnostic(ErrorCode.ERR_ConstantExpected, "default(T)").WithLocation(8, 18)); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "default(T)").WithArguments("T").WithLocation(8, 18)); } [Fact] @@ -95,33 +95,33 @@ public void M2(T t) // (9,18): error CS0428: Cannot convert method group 'M1' to non-delegate type 'object'. Did you intend to invoke the method? // case M1: Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "M1").WithArguments("M1", "object").WithLocation(9, 18), - // (15,18): error CS0150: A constant value is expected + // (15,18): error CS9133: A constant value of type 'T' is expected // _ = t is M2; - Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(15, 18), - // (18,18): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(15, 18), + // (18,18): error CS9133: A constant value of type 'T' is expected // case M2: - Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(18, 18)); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(18, 18)); comp = CreateCompilation(source); comp.VerifyDiagnostics( // (6,18): warning CS8974: Converting method group 'M1' to non-delegate type 'object'. Did you intend to invoke the method? // _ = o is M1; Diagnostic(ErrorCode.WRN_MethGrpToNonDel, "M1").WithArguments("M1", "object").WithLocation(6, 18), - // (6,18): error CS0150: A constant value is expected + // (6,18): error CS9133: A constant value of type 'object' is expected // _ = o is M1; - Diagnostic(ErrorCode.ERR_ConstantExpected, "M1").WithLocation(6, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M1").WithArguments("object").WithLocation(6, 18), // (9,18): warning CS8974: Converting method group 'M1' to non-delegate type 'object'. Did you intend to invoke the method? // case M1: Diagnostic(ErrorCode.WRN_MethGrpToNonDel, "M1").WithArguments("M1", "object").WithLocation(9, 18), - // (9,18): error CS0150: A constant value is expected + // (9,18): error CS9133: A constant value of type 'object' is expected // case M1: - Diagnostic(ErrorCode.ERR_ConstantExpected, "M1").WithLocation(9, 18), - // (15,18): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M1").WithArguments("object").WithLocation(9, 18), + // (15,18): error CS9133: A constant value of type 'T' is expected // _ = t is M2; - Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(15, 18), - // (18,18): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(15, 18), + // (18,18): error CS9133: A constant value of type 'T' is expected // case M2: - Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(18, 18)); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(18, 18)); } [Fact] @@ -157,13 +157,13 @@ public void M(T t) // (10,18): error CS0518: Predefined type 'System.Span`1' is not defined or imported // case stackalloc int[1] { 0 }: Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "stackalloc int[1] { 0 }").WithArguments("System.Span`1").WithLocation(10, 18), - // (10,18): error CS0150: A constant value is expected + // (10,18): error CS9133: A constant value of type 'T' is expected // case stackalloc int[1] { 0 }: - Diagnostic(ErrorCode.ERR_ConstantExpected, "stackalloc int[1] { 0 }").WithLocation(10, 18), - // (12,18): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "stackalloc int[1] { 0 }").WithArguments("T").WithLocation(10, 18), + // (12,18): error CS9133: A constant value of type 'T' is expected // case new { X = 0 }: - Diagnostic(ErrorCode.ERR_ConstantExpected, "new { X = 0 }").WithLocation(12, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new { X = 0 }").WithArguments("T").WithLocation(12, 18) + ); } [Fact] @@ -3430,16 +3430,16 @@ static unsafe void M(int* p) "; var compilation = CreatePatternCompilation(source, options: TestOptions.DebugDll.WithAllowUnsafe(true)); compilation.VerifyDiagnostics( - // (5,18): error CS8521: Pattern-matching is not permitted for pointer types. + // 0.cs(5,18): error CS8521: Pattern-matching is not permitted for pointer types. // if (p is {}) { } Diagnostic(ErrorCode.ERR_PointerTypeInPatternMatching, "{}").WithLocation(5, 18), - // (6,18): error CS0266: Cannot implicitly convert type 'int' to 'int*'. An explicit conversion exists (are you missing a cast?) + // 0.cs(6,18): error CS0266: Cannot implicitly convert type 'int' to 'int*'. An explicit conversion exists (are you missing a cast?) // if (p is 1) { } Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "1").WithArguments("int", "int*").WithLocation(6, 18), - // (6,18): error CS0150: A constant value is expected + // 0.cs(6,18): error CS9133: A constant value of type 'int*' is expected // if (p is 1) { } - Diagnostic(ErrorCode.ERR_ConstantExpected, "1").WithLocation(6, 18), - // (7,18): error CS8521: Pattern-matching is not permitted for pointer types. + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1").WithArguments("int*").WithLocation(6, 18), + // 0.cs(7,18): error CS8521: Pattern-matching is not permitted for pointer types. // if (p is var (x, y)) { } Diagnostic(ErrorCode.ERR_PointerTypeInPatternMatching, "var (x, y)").WithLocation(7, 18) ); diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs index d3b543b8c52a2..1b25372f158bc 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs @@ -1068,10 +1068,10 @@ public struct S "; var compilation = CreateCompilation(program, parseOptions: TestOptions.RegularWithExtendedPropertyPatterns, options: TestOptions.UnsafeDebugDll); compilation.VerifyEmitDiagnostics( - // (6,18): error CS0150: A constant value is expected + // (6,18): error CS9133: A constant value of type 'int' is expected // if (0 is s->X) {} - Diagnostic(ErrorCode.ERR_ConstantExpected, "s->X").WithLocation(6, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "s->X").WithArguments("int").WithLocation(6, 18) + ); } [Fact] diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternSwitchTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternSwitchTests.cs index 7bf8cbfa82ef5..e53ff87a55de4 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternSwitchTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternSwitchTests.cs @@ -1883,13 +1883,13 @@ static void Main(string[] args) var compilation = CreateCompilation(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); // The point of this test is that it should not crash. compilation.VerifyDiagnostics( - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9133: A constant value of type 'bool' is expected // case new object() is int x1: - Diagnostic(ErrorCode.ERR_ConstantExpected, "new object() is int x1").WithLocation(8, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new object() is int x1").WithArguments("bool").WithLocation(8, 18), // (9,42): error CS0165: Use of unassigned local variable 'x1' // System.Console.WriteLine(x1); Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(9, 42) - ); + ); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs index fc88acc33facf..fe7adeab9c91a 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs @@ -339,9 +339,9 @@ void M() ILocalReferenceOperation: y (OperationKind.LocalReference, Type: System.Int32?, IsInvalid) (Syntax: 'y') "; var expectedDiagnostics = new DiagnosticDescription[] { - // CS0150: A constant value is expected + // (8,28): error CS9133: A constant value of type 'int?' is expected // if (/**/x is y/**/) Console.WriteLine(y); - Diagnostic(ErrorCode.ERR_ConstantExpected, "y").WithLocation(8, 28) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "y").WithArguments("int?").WithLocation(8, 28) }; VerifyOperationTreeAndDiagnosticsForTest(source, expectedOperationTree, expectedDiagnostics); diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IPatternSwitchCase.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IPatternSwitchCase.cs index d51a4fc5ddb6d..1c09eeb843797 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IPatternSwitchCase.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IPatternSwitchCase.cs @@ -420,9 +420,9 @@ void M(bool x) ILiteralOperation (OperationKind.Literal, Type: System.Boolean, Constant: True, IsInvalid) (Syntax: 'true') "; var expectedDiagnostics = new DiagnosticDescription[] { - // CS0150: A constant value is expected + // (9,28): error CS9133: A constant value of type 'bool' is expected // case /**/x is true/**/: - Diagnostic(ErrorCode.ERR_ConstantExpected, "x is true").WithLocation(9, 28) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "x is true").WithArguments("bool").WithLocation(9, 28) }; VerifyOperationTreeAndDiagnosticsForTest(source, expectedOperationTree, expectedDiagnostics); @@ -482,9 +482,9 @@ void M(object x) TypeOperand: X "; var expectedDiagnostics = new DiagnosticDescription[] { - // CS0150: A constant value is expected + // (9,28): error CS9133: A constant value of type 'Type' is expected // /**/case typeof(X):/**/ - Diagnostic(ErrorCode.ERR_ConstantExpected, "typeof(X)").WithLocation(9, 28) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "typeof(X)").WithArguments("System.Type").WithLocation(9, 28) }; VerifyOperationTreeAndDiagnosticsForTest(source, expectedOperationTree, expectedDiagnostics); diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs index fc639257eb6f1..1e8e36905ac9d 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs @@ -1405,10 +1405,10 @@ void M(bool result, bool a, int input, int input1, int input2) } "; var expectedDiagnostics = new[] { - // file.cs(9,18): error CS0150: A constant value is expected + // (9,18): error CS9133: A constant value of type 'int' is expected // (a ? input1 : input2) => true - Diagnostic(ErrorCode.ERR_ConstantExpected, "a ? input1 : input2").WithLocation(9, 18) - }; + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "a ? input1 : input2").WithArguments("int").WithLocation(9, 18) + }; string expectedFlowGraph = @" Block[B0] - Entry Statements (0) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs index 39a6bbb42cf21..3cd132361b8b1 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs @@ -4846,19 +4846,19 @@ void M1(T t) "; var comp = CreateCompilation(source); comp.VerifyDiagnostics( - // (6,18): error CS0150: A constant value is expected + // (6,18): error CS9133: A constant value of type 'T' is expected // if (t is new()) { } // 1 - Diagnostic(ErrorCode.ERR_ConstantExpected, "new()").WithLocation(6, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new()").WithArguments("T").WithLocation(6, 18), // (7,18): error CS0304: Cannot create an instance of the variable type 'T' because it does not have the new() constraint // if (t is new T()) { } // 2 Diagnostic(ErrorCode.ERR_NoNewTyvar, "new T()").WithArguments("T").WithLocation(7, 18), - // (12,18): error CS0150: A constant value is expected + // (12,18): error CS9133: A constant value of type 'T' is expected // if (t is new()) { } // 3 - Diagnostic(ErrorCode.ERR_ConstantExpected, "new()").WithLocation(12, 18), - // (13,18): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new()").WithArguments("T").WithLocation(12, 18), + // (13,18): error CS9133: A constant value of type 'T' is expected // if (t is new T()) { } // 4 - Diagnostic(ErrorCode.ERR_ConstantExpected, "new T()").WithLocation(13, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new T()").WithArguments("T").WithLocation(13, 18) + ); } [Fact, WorkItem(60960, "https://github.com/dotnet/roslyn/issues/60960")] diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs index 2323d46317cc1..5a8288aabfc59 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs @@ -174,13 +174,13 @@ void Test(object? x) } "); c.VerifyDiagnostics( - // (7,18): error CS0150: A constant value is expected + // 0.cs(7,18): error CS9133: A constant value of type 'object' is expected // if (x is nonConstant) - Diagnostic(ErrorCode.ERR_ConstantExpected, "nonConstant").WithLocation(7, 18), - // (9,13): warning CS8602: Dereference of a possibly null reference. + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "nonConstant").WithArguments("object").WithLocation(7, 18), + // 0.cs(9,13): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(9, 13) - ); + ); } [Fact] diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/Utf8StringsLiteralsTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/Utf8StringsLiteralsTests.cs index 171500f4a21f5..14e49c888ded5 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/Utf8StringsLiteralsTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/Utf8StringsLiteralsTests.cs @@ -2944,9 +2944,9 @@ void Test(ReadOnlySpan s) // (7,18): error CS0029: Cannot implicitly convert type 'string' to 'System.ReadOnlySpan' // _ = s is "1"; Diagnostic(ErrorCode.ERR_NoImplicitConv, @"""1""").WithArguments("string", "System.ReadOnlySpan").WithLocation(7, 18), - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9133: A constant value of type 'ReadOnlySpan' is expected // _ = s is "2"u8; - Diagnostic(ErrorCode.ERR_ConstantExpected, @"""2""u8").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"""2""u8").WithArguments("System.ReadOnlySpan").WithLocation(8, 18) ); } From de4251e49a341899474dbc9a4f45bfbda3c4d2f0 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sun, 19 Mar 2023 19:41:33 +0100 Subject: [PATCH 30/39] formatting --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 53d6973a18c2e..39b1fba2aa24b 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -557,7 +557,7 @@ private BoundExpression BindExpressionForPatternContinued( { if (constantValueOpt == null) { - if(inputType.Kind is not SymbolKind.ErrorType) + if (inputType.Kind is not SymbolKind.ErrorType) { diagnostics.Add(ErrorCode.ERR_ConstantValueOfTypeExpected, patternExpression.Location, inputType); } From 8a996ab8a7b40995d39aa92df569d466067f08d7 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sun, 19 Mar 2023 19:43:20 +0100 Subject: [PATCH 31/39] formatting --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 39b1fba2aa24b..e2c70d25ad116 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -564,7 +564,6 @@ private BoundExpression BindExpressionForPatternContinued( else { diagnostics.Add(ErrorCode.ERR_ConstantExpected, patternExpression.Location); - } hasErrors = true; } From ab89a3aeed78da1fc4ca61bfa5aa3f61e15aabbe Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 22 Mar 2023 20:03:57 +0100 Subject: [PATCH 32/39] restore removed line --- src/Compilers/CSharp/Portable/Errors/ErrorCode.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index 5ea8daca044e4..8423565e79902 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -2150,6 +2150,7 @@ internal enum ErrorCode WRN_RefReturnOnlyParameter2 = 9095, ERR_RefAssignValEscapeWider = 9096, WRN_RefAssignValEscapeWider = 9097, + #endregion #region diagnostics introduced for C# 12.0 From 4698f28644cc6d697724008faedc53f2ff35aeef Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Wed, 22 Mar 2023 21:36:12 +0100 Subject: [PATCH 33/39] update tests --- .../CSharp/Test/Emit/CodeGen/SwitchTests.cs | 6 +-- .../IOperationTests_ISwitchOperation.cs | 38 +++++++++---------- .../Test/Semantic/Semantics/ConstantTests.cs | 8 ++-- .../Semantics/ImplicitObjectCreationTests.cs | 12 +++--- .../Semantics/NullableReferenceTypesTests.cs | 14 +++---- .../Test/Semantic/Semantics/OutVarTests.cs | 16 ++++---- .../Semantics/PrimaryConstructorTests.cs | 12 +++--- .../Test/Semantic/Semantics/SwitchTests.cs | 24 ++++++------ 8 files changed, 65 insertions(+), 65 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs index 3827a15d86948..1fc5dbfa6e703 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs @@ -2623,10 +2623,10 @@ static void Main() }"; var compilation = base.CreateCSharpCompilation(text); compilation.VerifyDiagnostics( - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'int?' is expected // case i: - Diagnostic(ErrorCode.ERR_ConstantExpected, "i").WithLocation(8, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "i").WithArguments("int?").WithLocation(8, 18) + ); } [Fact, WorkItem(7625, "https://github.com/dotnet/roslyn/issues/7625")] diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index 5f06630201298..2f5e23447fc50 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -1306,9 +1306,9 @@ public override int GetHashCode() } "; var expectedDiagnostics = new[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'MyClass' is expected // case other: - Diagnostic(ErrorCode.ERR_ConstantExpected, "other").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("MyClass").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -1813,9 +1813,9 @@ void M(bool result, int? input, int? other) } "; var expectedDiagnostics = new[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'int?' is expected // case other: - Diagnostic(ErrorCode.ERR_ConstantExpected, "other").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("int?").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -1884,12 +1884,12 @@ void M(bool result, int input, int? other) } "; var expectedDiagnostics = new[] { - // file.cs(8,18): error CS0266: Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?) + // (8,18): error CS0266: Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?) // case other: Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "other").WithArguments("int?", "int").WithLocation(8, 18), - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'int' is expected // case other: - Diagnostic(ErrorCode.ERR_ConstantExpected, "other").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("int").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -2076,9 +2076,9 @@ void M(bool result, dynamic input, dynamic other) } "; var expectedDiagnostics = new[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'dynamic' is expected // case other: - Diagnostic(ErrorCode.ERR_ConstantExpected, "other").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("dynamic").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -2251,9 +2251,9 @@ void M(bool result, int? input1, int input2, int input3) } "; var expectedDiagnostics = new[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'int' is expected // case input1 ?? input2: - Diagnostic(ErrorCode.ERR_ConstantExpected, "input1 ?? input2").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "input1 ?? input2").WithArguments("int").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -2528,9 +2528,9 @@ void M(bool result, object input, int? other) } "; var expectedDiagnostics = new[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'object' is expected // case other ?? 1: - Diagnostic(ErrorCode.ERR_ConstantExpected, "other ?? 1").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other ?? 1").WithArguments("object").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -3123,9 +3123,9 @@ int TakeOutParam(int a, out MyClass b) } "; var expectedDiagnostics = new DiagnosticDescription[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'int' is expected // case 1+TakeOutParam(3, out MyClass x1): - Diagnostic(ErrorCode.ERR_ConstantExpected, "1+TakeOutParam(3, out MyClass x1)").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+TakeOutParam(3, out MyClass x1)").WithArguments("int").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -3220,9 +3220,9 @@ void M(bool result, int? input) } "; var expectedDiagnostics = new DiagnosticDescription[] { - // file.cs(8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'int?' is expected // case 1+(input is int x1 ? x1 : 0): - Diagnostic(ErrorCode.ERR_ConstantExpected, "1+(input is int x1 ? x1 : 0)").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(input is int x1 ? x1 : 0)").WithArguments("int?").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -3479,9 +3479,9 @@ void M(bool result, int input, int other) } "; var expectedDiagnostics = new DiagnosticDescription[] { - // file.cs(8,20): error CS0150: A constant value is expected + // (8,20): error CS9135: A constant value of type 'int' is expected // case < other: - Diagnostic(ErrorCode.ERR_ConstantExpected, "other").WithLocation(8, 20) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("int").WithLocation(8, 20) }; string expectedFlowGraph = @" diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs index 36da7e6d98426..8e7813e0ee16e 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs @@ -3403,18 +3403,18 @@ static void Main(string[] args) // (13,27): error CS0134: 'y3' is of type 'object'. A const field of a reference type other than string can only be initialized with null. // const object y3 = "y"; Diagnostic(ErrorCode.ERR_NotNullConstRefField, @"""y""").WithArguments("y3", "object").WithLocation(13, 27), - // (19,18): error CS0150: A constant value is expected + // (19,18): error CS9135: A constant value of type 'string' is expected // case (string)(object)"b": - Diagnostic(ErrorCode.ERR_ConstantExpected, @"(string)(object)""b""").WithLocation(19, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"(string)(object)""b""").WithArguments("string").WithLocation(19, 18), // (21,18): error CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) // case (object)null: Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "(object)null").WithArguments("object", "string").WithLocation(21, 18), // (23,18): error CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) // case (object)"b": Diagnostic(ErrorCode.ERR_NoImplicitConvCast, @"(object)""b""").WithArguments("object", "string").WithLocation(23, 18), - // (23,18): error CS0150: A constant value is expected + // (23,18): error CS9135: A constant value of type 'string' is expected // case (object)"b": - Diagnostic(ErrorCode.ERR_ConstantExpected, @"(object)""b""").WithLocation(23, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, @"(object)""b""").WithArguments("string").WithLocation(23, 18), // (21,13): error CS0152: The switch statement contains multiple cases with the label value 'null' // case (object)null: Diagnostic(ErrorCode.ERR_DuplicateCaseLabel, "case (object)null:").WithArguments("null").WithLocation(21, 13)); diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs index 3cd132361b8b1..63f81ebb775a2 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs @@ -2619,12 +2619,12 @@ static void Main() "; var comp = CreateCompilation(source); comp.VerifyDiagnostics( - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'C' is expected // case new(): - Diagnostic(ErrorCode.ERR_ConstantExpected, "new()").WithLocation(8, 18), - // (9,19): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new()").WithArguments("C").WithLocation(8, 18), + // (9,19): error CS9135: A constant value of type 'C' is expected // case (new()): - Diagnostic(ErrorCode.ERR_ConstantExpected, "new()").WithLocation(9, 19) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new()").WithArguments("C").WithLocation(9, 19) ); } @@ -3717,9 +3717,9 @@ void M() // (8,19): error CS8754: There is no target type for 'new()' // bool v3 = new() is new(); Diagnostic(ErrorCode.ERR_ImplicitObjectCreationNoTargetType, "new()").WithArguments("new()").WithLocation(8, 19), - // (10,27): error CS0150: A constant value is expected + // (10,27): error CS9135: A constant value of type 'C' is expected // bool v5 = this is new(); - Diagnostic(ErrorCode.ERR_ConstantExpected, "new()").WithLocation(10, 27) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new()").WithArguments("C").WithLocation(10, 27) ); } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index f72cf1ee35a60..bcd565c2a6625 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -77480,13 +77480,13 @@ static void G(object? o) comp = CreateCompilation(new[] { source }, options: WithNullableEnable()); comp.VerifyDiagnostics( - // (6,18): warning CS8974: Converting method group 'F' to non-delegate type 'object'. Did you intend to invoke the method? + // 0.cs(6,18): warning CS8974: Converting method group 'F' to non-delegate type 'object'. Did you intend to invoke the method? // if (o is F) Diagnostic(ErrorCode.WRN_MethGrpToNonDel, "F").WithArguments("F", "object").WithLocation(6, 18), - // (6,18): error CS0150: A constant value is expected + // 0.cs(6,18): error CS9135: A constant value of type 'object' is expected // if (o is F) - Diagnostic(ErrorCode.ERR_ConstantExpected, "F").WithLocation(6, 18), - // (8,15): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'. + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "F").WithArguments("object").WithLocation(6, 18), + // 0.cs(8,15): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'. // F(o); Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o").WithArguments("o", "void C.F(object o)").WithLocation(8, 15)); } @@ -115790,13 +115790,13 @@ void M0(T x0) "; CreateCompilation(source, options: WithNullableEnable()).VerifyDiagnostics( - // (6,19): error CS0150: A constant value is expected + // (6,19): error CS9135: A constant value of type 'T' is expected // if (x0 is default(T)) return; - Diagnostic(ErrorCode.ERR_ConstantExpected, "default(T)").WithLocation(6, 19), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "default(T)").WithArguments("T").WithLocation(6, 19), // (7,9): warning CS8602: Dereference of a possibly null reference. // x0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x0").WithLocation(7, 9) - ); + ); } [Fact] diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs index f0f4d2bed1689..a0c3019bee015 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs @@ -32599,10 +32599,10 @@ static bool TakeOutParam(int y, out int x) var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); // The point of this test is that it should not crash. compilation.VerifyDiagnostics( - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'bool' is expected // case !TakeOutParam(3, out var x1): - Diagnostic(ErrorCode.ERR_ConstantExpected, "!TakeOutParam(3, out var x1)").WithLocation(8, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "!TakeOutParam(3, out var x1)").WithArguments("bool").WithLocation(8, 18) + ); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); @@ -34920,22 +34920,22 @@ public static void Main(string[] args) // (8,28): error CS8081: Expression does not have a name. // case !M(nameof(M(out int z1)), z1): Diagnostic(ErrorCode.ERR_ExpressionHasNoName, "M(out int z1)").WithLocation(8, 28), - // (8,18): error CS0150: A constant value is expected + // (8,18): error CS9135: A constant value of type 'object' is expected // case !M(nameof(M(out int z1)), z1): - Diagnostic(ErrorCode.ERR_ConstantExpected, "!M(nameof(M(out int z1)), z1)").WithLocation(8, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "!M(nameof(M(out int z1)), z1)").WithArguments("object").WithLocation(8, 18), // (11,28): error CS8081: Expression does not have a name. // case !M(nameof(M(out var z2)), z2): Diagnostic(ErrorCode.ERR_ExpressionHasNoName, "M(out var z2)").WithLocation(11, 28), - // (11,18): error CS0150: A constant value is expected + // (11,18): error CS9135: A constant value of type 'object' is expected // case !M(nameof(M(out var z2)), z2): - Diagnostic(ErrorCode.ERR_ConstantExpected, "!M(nameof(M(out var z2)), z2)").WithLocation(11, 18), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "!M(nameof(M(out var z2)), z2)").WithArguments("object").WithLocation(11, 18), // (8,44): error CS0165: Use of unassigned local variable 'z1' // case !M(nameof(M(out int z1)), z1): Diagnostic(ErrorCode.ERR_UseDefViolation, "z1").WithArguments("z1").WithLocation(8, 44), // (11,44): error CS0165: Use of unassigned local variable 'z2' // case !M(nameof(M(out var z2)), z2): Diagnostic(ErrorCode.ERR_UseDefViolation, "z2").WithArguments("z2").WithLocation(11, 44) - ); + ); var tree = compilation.SyntaxTrees[0]; var model = compilation.GetSemanticModel(tree); diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs index 43fcf41d7a08d..2eac3e8e12a47 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs @@ -8465,12 +8465,12 @@ class C2 // No unused warnings because we detected capturing comp.VerifyEmitDiagnostics( - // (6,21): error CS0150: A constant value is expected + // (6,21): error CS9135: A constant value of type 'int' is expected // return x is p1 || x is p2.F; - Diagnostic(ErrorCode.ERR_ConstantExpected, "p1").WithLocation(6, 21), - // (6,32): error CS0150: A constant value is expected + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "p1").WithArguments("int").WithLocation(6, 21), + // (6,32): error CS9135: A constant value of type 'int' is expected // return x is p1 || x is p2.F; - Diagnostic(ErrorCode.ERR_ConstantExpected, "p2.F").WithLocation(6, 32) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "p2.F").WithArguments("int").WithLocation(6, 32) ); } @@ -10501,9 +10501,9 @@ public void M1(object x) // (6,28): warning CS9113: Parameter 'Color' is unread. // public class C1 (Color Color) Diagnostic(ErrorCode.WRN_UnreadPrimaryConstructorParameter, "Color").WithArguments("Color").WithLocation(6, 28), - // (10,22): error CS0150: A constant value is expected + // (10,22): error CS9135: A constant value of type 'object' is expected // if (x is Color.Red) - Diagnostic(ErrorCode.ERR_ConstantExpected, "Color.Red").WithLocation(10, 22) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "Color.Red").WithArguments("object").WithLocation(10, 22) ); Assert.Empty(comp.GetTypeByMetadataName("Color+C1").InstanceConstructors.OfType().Single().GetCapturedParameters()); diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs index b3112d467e5cc..5fb71731a3aeb 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs @@ -148,15 +148,15 @@ public static int Main(string [] args) } }"; CreateCompilation(text, parseOptions: TestOptions.Regular6).VerifyDiagnostics( - // (11,18): error CS0150: A constant value is expected + // (11,18): error CS9135: A constant value of type 'int' is expected // case test: - Diagnostic(ErrorCode.ERR_ConstantExpected, "test").WithLocation(11, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "test").WithArguments("int").WithLocation(11, 18) + ); CreateCompilation(text).VerifyDiagnostics( - // (11,18): error CS0150: A constant value is expected + // (11,18): error CS9135: A constant value of type 'int' is expected // case test: - Diagnostic(ErrorCode.ERR_ConstantExpected, "test").WithLocation(11, 18) - ); + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "test").WithArguments("int").WithLocation(11, 18) + ); } [Fact] @@ -1038,27 +1038,27 @@ static void Main() { } // (6,17): error CS0151: A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type in C# 6 and earlier. // switch (o) Diagnostic(ErrorCode.ERR_V6SwitchGoverningTypeValueExpected, "o").WithLocation(6, 17), - // (8,19): error CS0150: A constant value is expected + // (8,19): error CS9135: A constant value of type 'object' is expected // case (1+(o.GetType().Name.Length)): - Diagnostic(ErrorCode.ERR_ConstantExpected, "1+(o.GetType().Name.Length)").WithLocation(8, 19), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(o.GetType().Name.Length)").WithArguments("object").WithLocation(8, 19), // (9,17): error CS7036: There is no argument given that corresponds to the required parameter 'o' of 'C.M(object)' // M(); Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "M").WithArguments("o", "C.M(object)").WithLocation(9, 17), // (12,13): error CS0152: The switch statement contains multiple cases with the label value '0' // case 0: Diagnostic(ErrorCode.ERR_DuplicateCaseLabel, "case 0:").WithArguments("0").WithLocation(12, 13) - ); + ); CreateCompilation(text).VerifyDiagnostics( - // (8,19): error CS0150: A constant value is expected + // (8,19): error CS9135: A constant value of type 'object' is expected // case (1+(o.GetType().Name.Length)): - Diagnostic(ErrorCode.ERR_ConstantExpected, "1+(o.GetType().Name.Length)").WithLocation(8, 19), + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(o.GetType().Name.Length)").WithArguments("object").WithLocation(8, 19), // (9,17): error CS7036: There is no argument given that corresponds to the required parameter 'o' of 'C.M(object)' // M(); Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "M").WithArguments("o", "C.M(object)").WithLocation(9, 17), // (12,13): error CS0152: The switch statement contains multiple cases with the label value '0' // case 0: Diagnostic(ErrorCode.ERR_DuplicateCaseLabel, "case 0:").WithArguments("0").WithLocation(12, 13) - ); + ); } [Fact] From feef4af6eaea5b9143a282a7eb42872e20eaf9c0 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sun, 26 Mar 2023 00:35:34 +0100 Subject: [PATCH 34/39] check for dynamic, object, valuetype and typeparams --- src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index e2c70d25ad116..487fd38978010 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -557,9 +557,11 @@ private BoundExpression BindExpressionForPatternContinued( { if (constantValueOpt == null) { - if (inputType.Kind is not SymbolKind.ErrorType) + var strippedInputType = inputType.StrippedType(); + if (strippedInputType.Kind is not SymbolKind.ErrorType and not SymbolKind.DynamicType and not SymbolKind.TypeParameter && + strippedInputType.SpecialType is not SpecialType.System_Object and not SpecialType.System_ValueType) { - diagnostics.Add(ErrorCode.ERR_ConstantValueOfTypeExpected, patternExpression.Location, inputType); + diagnostics.Add(ErrorCode.ERR_ConstantValueOfTypeExpected, patternExpression.Location, strippedInputType); } else { From 9aa432322187b8e46b1ff56c97eefa5cd3b4983b Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sun, 26 Mar 2023 00:36:31 +0100 Subject: [PATCH 35/39] update tests --- .../CSharp/Test/Emit/CodeGen/SwitchTests.cs | 4 +-- .../Emit2/Semantics/PatternMatchingTests4.cs | 36 +++++++++---------- .../IOperationTests_IIsPatternExpression.cs | 4 +-- .../IOperationTests_ISwitchOperation.cs | 12 +++---- .../Semantics/ImplicitObjectCreationTests.cs | 12 +++---- .../Semantics/NullableReferenceTypesTests.cs | 8 ++--- .../NullableReferenceTypesVsPatterns.cs | 4 +-- .../Test/Semantic/Semantics/OutVarTests.cs | 8 ++--- .../Semantics/PrimaryConstructorTests.cs | 4 +-- .../Test/Semantic/Semantics/SwitchTests.cs | 8 ++--- 10 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs index 1fc5dbfa6e703..9e114ce1aec4a 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/SwitchTests.cs @@ -2623,9 +2623,9 @@ static void Main() }"; var compilation = base.CreateCSharpCompilation(text); compilation.VerifyDiagnostics( - // (8,18): error CS9135: A constant value of type 'int?' is expected + // (8,18): error CS9135: A constant value of type 'int' is expected // case i: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "i").WithArguments("int?").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "i").WithArguments("int").WithLocation(8, 18) ); } diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs index e2d555dc38c30..76cca08480a2b 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests4.cs @@ -55,9 +55,9 @@ public void M(T t) } }"); comp.VerifyDiagnostics( - // (8,18): error CS9133: A constant value of type 'T' is expected + // (8,18): error CS0150: A constant value is expected // case default(T): - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "default(T)").WithArguments("T").WithLocation(8, 18)); + Diagnostic(ErrorCode.ERR_ConstantExpected, "default(T)").WithLocation(8, 18)); } [Fact] @@ -95,33 +95,33 @@ public void M2(T t) // (9,18): error CS0428: Cannot convert method group 'M1' to non-delegate type 'object'. Did you intend to invoke the method? // case M1: Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "M1").WithArguments("M1", "object").WithLocation(9, 18), - // (15,18): error CS9133: A constant value of type 'T' is expected + // (15,18): error CS0150: A constant value is expected // _ = t is M2; - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(15, 18), - // (18,18): error CS9133: A constant value of type 'T' is expected + Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(15, 18), + // (18,18): error CS0150: A constant value is expected // case M2: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(18, 18)); + Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(18, 18)); comp = CreateCompilation(source); comp.VerifyDiagnostics( // (6,18): warning CS8974: Converting method group 'M1' to non-delegate type 'object'. Did you intend to invoke the method? // _ = o is M1; Diagnostic(ErrorCode.WRN_MethGrpToNonDel, "M1").WithArguments("M1", "object").WithLocation(6, 18), - // (6,18): error CS9133: A constant value of type 'object' is expected + // (6,18): error CS0150: A constant value is expected // _ = o is M1; - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M1").WithArguments("object").WithLocation(6, 18), + Diagnostic(ErrorCode.ERR_ConstantExpected, "M1").WithLocation(6, 18), // (9,18): warning CS8974: Converting method group 'M1' to non-delegate type 'object'. Did you intend to invoke the method? // case M1: Diagnostic(ErrorCode.WRN_MethGrpToNonDel, "M1").WithArguments("M1", "object").WithLocation(9, 18), - // (9,18): error CS9133: A constant value of type 'object' is expected + // (9,18): error CS0150: A constant value is expected // case M1: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M1").WithArguments("object").WithLocation(9, 18), - // (15,18): error CS9133: A constant value of type 'T' is expected + Diagnostic(ErrorCode.ERR_ConstantExpected, "M1").WithLocation(9, 18), + // (15,18): error CS0150: A constant value is expected // _ = t is M2; - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(15, 18), - // (18,18): error CS9133: A constant value of type 'T' is expected + Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(15, 18), + // (18,18): error CS0150: A constant value is expected // case M2: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "M2").WithArguments("T").WithLocation(18, 18)); + Diagnostic(ErrorCode.ERR_ConstantExpected, "M2").WithLocation(18, 18)); } [Fact] @@ -157,12 +157,12 @@ public void M(T t) // (10,18): error CS0518: Predefined type 'System.Span`1' is not defined or imported // case stackalloc int[1] { 0 }: Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "stackalloc int[1] { 0 }").WithArguments("System.Span`1").WithLocation(10, 18), - // (10,18): error CS9133: A constant value of type 'T' is expected + // (10,18): error CS0150: A constant value is expected // case stackalloc int[1] { 0 }: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "stackalloc int[1] { 0 }").WithArguments("T").WithLocation(10, 18), - // (12,18): error CS9133: A constant value of type 'T' is expected + Diagnostic(ErrorCode.ERR_ConstantExpected, "stackalloc int[1] { 0 }").WithLocation(10, 18), + // (12,18): error CS0150: A constant value is expected // case new { X = 0 }: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new { X = 0 }").WithArguments("T").WithLocation(12, 18) + Diagnostic(ErrorCode.ERR_ConstantExpected, "new { X = 0 }").WithLocation(12, 18) ); } diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs index fe7adeab9c91a..68369ebb8e7a3 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IIsPatternExpression.cs @@ -339,9 +339,9 @@ void M() ILocalReferenceOperation: y (OperationKind.LocalReference, Type: System.Int32?, IsInvalid) (Syntax: 'y') "; var expectedDiagnostics = new DiagnosticDescription[] { - // (8,28): error CS9133: A constant value of type 'int?' is expected + // (8,28): error CS9135: A constant value of type 'int' is expected // if (/**/x is y/**/) Console.WriteLine(y); - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "y").WithArguments("int?").WithLocation(8, 28) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "y").WithArguments("int").WithLocation(8, 28) }; VerifyOperationTreeAndDiagnosticsForTest(source, expectedOperationTree, expectedDiagnostics); diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index 2f5e23447fc50..c1299fc6cca1e 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -1815,7 +1815,7 @@ void M(bool result, int? input, int? other) var expectedDiagnostics = new[] { // (8,18): error CS9135: A constant value of type 'int?' is expected // case other: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("int?").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("int").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -2076,9 +2076,9 @@ void M(bool result, dynamic input, dynamic other) } "; var expectedDiagnostics = new[] { - // (8,18): error CS9135: A constant value of type 'dynamic' is expected + // (8,18): error CS0150: A constant value is expected // case other: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("dynamic").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantExpected, "other").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -2528,9 +2528,9 @@ void M(bool result, object input, int? other) } "; var expectedDiagnostics = new[] { - // (8,18): error CS9135: A constant value of type 'object' is expected + // (8,18): error CS0150: A constant value is expected // case other ?? 1: - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other ?? 1").WithArguments("object").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantExpected, "other ?? 1").WithLocation(8, 18) }; string expectedFlowGraph = @" @@ -3222,7 +3222,7 @@ void M(bool result, int? input) var expectedDiagnostics = new DiagnosticDescription[] { // (8,18): error CS9135: A constant value of type 'int?' is expected // case 1+(input is int x1 ? x1 : 0): - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(input is int x1 ? x1 : 0)").WithArguments("int?").WithLocation(8, 18) + Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(input is int x1 ? x1 : 0)").WithArguments("int").WithLocation(8, 18) }; string expectedFlowGraph = @" diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs index 63f81ebb775a2..0dad5e99778f0 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/ImplicitObjectCreationTests.cs @@ -4846,18 +4846,18 @@ void M1(T t) "; var comp = CreateCompilation(source); comp.VerifyDiagnostics( - // (6,18): error CS9133: A constant value of type 'T' is expected + // (6,18): error CS0150: A constant value is expected // if (t is new()) { } // 1 - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new()").WithArguments("T").WithLocation(6, 18), + Diagnostic(ErrorCode.ERR_ConstantExpected, "new()").WithLocation(6, 18), // (7,18): error CS0304: Cannot create an instance of the variable type 'T' because it does not have the new() constraint // if (t is new T()) { } // 2 Diagnostic(ErrorCode.ERR_NoNewTyvar, "new T()").WithArguments("T").WithLocation(7, 18), - // (12,18): error CS9133: A constant value of type 'T' is expected + // (12,18): error CS0150: A constant value is expected // if (t is new()) { } // 3 - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new()").WithArguments("T").WithLocation(12, 18), - // (13,18): error CS9133: A constant value of type 'T' is expected + Diagnostic(ErrorCode.ERR_ConstantExpected, "new()").WithLocation(12, 18), + // (13,18): error CS0150: A constant value is expected // if (t is new T()) { } // 4 - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "new T()").WithArguments("T").WithLocation(13, 18) + Diagnostic(ErrorCode.ERR_ConstantExpected, "new T()").WithLocation(13, 18) ); } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index bcd565c2a6625..e6dafc8352a44 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -77483,9 +77483,9 @@ static void G(object? o) // 0.cs(6,18): warning CS8974: Converting method group 'F' to non-delegate type 'object'. Did you intend to invoke the method? // if (o is F) Diagnostic(ErrorCode.WRN_MethGrpToNonDel, "F").WithArguments("F", "object").WithLocation(6, 18), - // 0.cs(6,18): error CS9135: A constant value of type 'object' is expected + // 0.cs(6,18): error CS0150: A constant value is expected // if (o is F) - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "F").WithArguments("object").WithLocation(6, 18), + Diagnostic(ErrorCode.ERR_ConstantExpected, "F").WithLocation(6, 18), // 0.cs(8,15): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'. // F(o); Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o").WithArguments("o", "void C.F(object o)").WithLocation(8, 15)); @@ -115790,9 +115790,9 @@ void M0(T x0) "; CreateCompilation(source, options: WithNullableEnable()).VerifyDiagnostics( - // (6,19): error CS9135: A constant value of type 'T' is expected + // (6,19): error CS0150: A constant value is expected // if (x0 is default(T)) return; - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "default(T)").WithArguments("T").WithLocation(6, 19), + Diagnostic(ErrorCode.ERR_ConstantExpected, "default(T)").WithLocation(6, 19), // (7,9): warning CS8602: Dereference of a possibly null reference. // x0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x0").WithLocation(7, 9) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs index 5a8288aabfc59..f3daac961f96c 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs @@ -174,9 +174,9 @@ void Test(object? x) } "); c.VerifyDiagnostics( - // 0.cs(7,18): error CS9133: A constant value of type 'object' is expected + // 0.cs(7,18): error CS0150: A constant value is expected // if (x is nonConstant) - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "nonConstant").WithArguments("object").WithLocation(7, 18), + Diagnostic(ErrorCode.ERR_ConstantExpected, "nonConstant").WithLocation(7, 18), // 0.cs(9,13): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(9, 13) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs index a0c3019bee015..00a6cbb1eb21b 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs @@ -34920,15 +34920,15 @@ public static void Main(string[] args) // (8,28): error CS8081: Expression does not have a name. // case !M(nameof(M(out int z1)), z1): Diagnostic(ErrorCode.ERR_ExpressionHasNoName, "M(out int z1)").WithLocation(8, 28), - // (8,18): error CS9135: A constant value of type 'object' is expected + // (8,18): error CS0150: A constant value is expected // case !M(nameof(M(out int z1)), z1): - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "!M(nameof(M(out int z1)), z1)").WithArguments("object").WithLocation(8, 18), + Diagnostic(ErrorCode.ERR_ConstantExpected, "!M(nameof(M(out int z1)), z1)").WithLocation(8, 18), // (11,28): error CS8081: Expression does not have a name. // case !M(nameof(M(out var z2)), z2): Diagnostic(ErrorCode.ERR_ExpressionHasNoName, "M(out var z2)").WithLocation(11, 28), - // (11,18): error CS9135: A constant value of type 'object' is expected + // (11,18): error CS0150: A constant value is expected // case !M(nameof(M(out var z2)), z2): - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "!M(nameof(M(out var z2)), z2)").WithArguments("object").WithLocation(11, 18), + Diagnostic(ErrorCode.ERR_ConstantExpected, "!M(nameof(M(out var z2)), z2)").WithLocation(11, 18), // (8,44): error CS0165: Use of unassigned local variable 'z1' // case !M(nameof(M(out int z1)), z1): Diagnostic(ErrorCode.ERR_UseDefViolation, "z1").WithArguments("z1").WithLocation(8, 44), diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs index 2eac3e8e12a47..28654bc28f2b7 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs @@ -10501,9 +10501,9 @@ public void M1(object x) // (6,28): warning CS9113: Parameter 'Color' is unread. // public class C1 (Color Color) Diagnostic(ErrorCode.WRN_UnreadPrimaryConstructorParameter, "Color").WithArguments("Color").WithLocation(6, 28), - // (10,22): error CS9135: A constant value of type 'object' is expected + // (10,22): error CS0150: A constant value is expected // if (x is Color.Red) - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "Color.Red").WithArguments("object").WithLocation(10, 22) + Diagnostic(ErrorCode.ERR_ConstantExpected, "Color.Red").WithLocation(10, 22) ); Assert.Empty(comp.GetTypeByMetadataName("Color+C1").InstanceConstructors.OfType().Single().GetCapturedParameters()); diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs index 5fb71731a3aeb..4aa59e87ea4cc 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/SwitchTests.cs @@ -1038,9 +1038,9 @@ static void Main() { } // (6,17): error CS0151: A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type in C# 6 and earlier. // switch (o) Diagnostic(ErrorCode.ERR_V6SwitchGoverningTypeValueExpected, "o").WithLocation(6, 17), - // (8,19): error CS9135: A constant value of type 'object' is expected + // (8,19): error CS0150: A constant value is expected // case (1+(o.GetType().Name.Length)): - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(o.GetType().Name.Length)").WithArguments("object").WithLocation(8, 19), + Diagnostic(ErrorCode.ERR_ConstantExpected, "1+(o.GetType().Name.Length)").WithLocation(8, 19), // (9,17): error CS7036: There is no argument given that corresponds to the required parameter 'o' of 'C.M(object)' // M(); Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "M").WithArguments("o", "C.M(object)").WithLocation(9, 17), @@ -1049,9 +1049,9 @@ static void Main() { } Diagnostic(ErrorCode.ERR_DuplicateCaseLabel, "case 0:").WithArguments("0").WithLocation(12, 13) ); CreateCompilation(text).VerifyDiagnostics( - // (8,19): error CS9135: A constant value of type 'object' is expected + // (8,19): error CS0150: A constant value is expected // case (1+(o.GetType().Name.Length)): - Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(o.GetType().Name.Length)").WithArguments("object").WithLocation(8, 19), + Diagnostic(ErrorCode.ERR_ConstantExpected, "1+(o.GetType().Name.Length)").WithLocation(8, 19), // (9,17): error CS7036: There is no argument given that corresponds to the required parameter 'o' of 'C.M(object)' // M(); Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "M").WithArguments("o", "C.M(object)").WithLocation(9, 17), From 5fdf01069f5901073a71042e1019f40bf3a399f1 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Sun, 26 Mar 2023 00:37:30 +0100 Subject: [PATCH 36/39] add extra tests for diagnostic --- .../Emit2/FlowAnalysis/FlowDiagnosticTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs b/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs index 61eebf4976dd8..f0c58645a562f 100644 --- a/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs @@ -2759,5 +2759,27 @@ struct Empty Diagnostic(ErrorCode.ERR_ImplicitlyTypedOutVariableUsedInTheSameArgumentList, "x").WithArguments("x").WithLocation(6, 25) ); } + + [Theory] + [InlineData("object", "new {}")] + [InlineData("dynamic", "new {}")] + [InlineData("System.ValueType", "((int)x0)/2")] + public void ConstantsExpectedInPatternExpression(string type, string expression) + { + var source = @$" +class Outer +{{ + bool M0({type} x0) + {{ + return x0 is {expression}; + }} +}} +"; + CreateCompilation(source).VerifyDiagnostics( + // (6,22): error CS0150: A constant value is expected + // return x0 is new {}; + Diagnostic(ErrorCode.ERR_ConstantExpected, expression).WithLocation(6, 22) + ); + } } } From d17db0c320038a42db5d8649273940d49af44f88 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Tue, 28 Mar 2023 18:06:57 +0200 Subject: [PATCH 37/39] move test to more appropriate location --- .../Emit2/FlowAnalysis/FlowDiagnosticTests.cs | 22 ------------------- .../Emit2/Semantics/PatternMatchingTests5.cs | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs b/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs index f0c58645a562f..61eebf4976dd8 100644 --- a/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs @@ -2759,27 +2759,5 @@ struct Empty Diagnostic(ErrorCode.ERR_ImplicitlyTypedOutVariableUsedInTheSameArgumentList, "x").WithArguments("x").WithLocation(6, 25) ); } - - [Theory] - [InlineData("object", "new {}")] - [InlineData("dynamic", "new {}")] - [InlineData("System.ValueType", "((int)x0)/2")] - public void ConstantsExpectedInPatternExpression(string type, string expression) - { - var source = @$" -class Outer -{{ - bool M0({type} x0) - {{ - return x0 is {expression}; - }} -}} -"; - CreateCompilation(source).VerifyDiagnostics( - // (6,22): error CS0150: A constant value is expected - // return x0 is new {}; - Diagnostic(ErrorCode.ERR_ConstantExpected, expression).WithLocation(6, 22) - ); - } } } diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs index 1b25372f158bc..0120dc6e33d29 100644 --- a/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs +++ b/src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs @@ -3334,5 +3334,27 @@ .locals init (bool V_0, } """); } + + [Theory] + [InlineData("object", "new {}")] + [InlineData("dynamic", "new {}")] + [InlineData("System.ValueType", "((int)x0)/2")] + public void ConstantsExpectedInPatternExpression(string type, string expression) + { + var source = @$" +class Outer +{{ + bool M0({type} x0) + {{ + return x0 is {expression}; + }} +}} +"; + CreateCompilation(source).VerifyDiagnostics( + // (6,22): error CS0150: A constant value is expected + // return x0 is {expression}; + Diagnostic(ErrorCode.ERR_ConstantExpected, expression).WithLocation(6, 22) + ); + } } } From eb438633d1f7a83d27083fca7a6fa5f49213d6f8 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Tue, 28 Mar 2023 18:07:06 +0200 Subject: [PATCH 38/39] fix diagnostics comments --- .../IOperation/IOperation/IOperationTests_ISwitchOperation.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index c1299fc6cca1e..11b3e4e337073 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -1813,7 +1813,7 @@ void M(bool result, int? input, int? other) } "; var expectedDiagnostics = new[] { - // (8,18): error CS9135: A constant value of type 'int?' is expected + // (8,18): error CS9135: A constant value of type 'int' is expected // case other: Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "other").WithArguments("int").WithLocation(8, 18) }; @@ -3220,7 +3220,7 @@ void M(bool result, int? input) } "; var expectedDiagnostics = new DiagnosticDescription[] { - // (8,18): error CS9135: A constant value of type 'int?' is expected + // (8,18): error CS9135: A constant value of type 'int' is expected // case 1+(input is int x1 ? x1 : 0): Diagnostic(ErrorCode.ERR_ConstantValueOfTypeExpected, "1+(input is int x1 ? x1 : 0)").WithArguments("int").WithLocation(8, 18) }; From bc1114980b68f3304a020b8458b2c66acaf3acd2 Mon Sep 17 00:00:00 2001 From: Marc Zwart Date: Tue, 28 Mar 2023 18:07:23 +0200 Subject: [PATCH 39/39] undo file changes --- .../Semantic/Semantics/NullableReferenceTypesTests.cs | 8 ++++---- .../Semantics/NullableReferenceTypesVsPatterns.cs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index e6dafc8352a44..f72cf1ee35a60 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -77480,13 +77480,13 @@ static void G(object? o) comp = CreateCompilation(new[] { source }, options: WithNullableEnable()); comp.VerifyDiagnostics( - // 0.cs(6,18): warning CS8974: Converting method group 'F' to non-delegate type 'object'. Did you intend to invoke the method? + // (6,18): warning CS8974: Converting method group 'F' to non-delegate type 'object'. Did you intend to invoke the method? // if (o is F) Diagnostic(ErrorCode.WRN_MethGrpToNonDel, "F").WithArguments("F", "object").WithLocation(6, 18), - // 0.cs(6,18): error CS0150: A constant value is expected + // (6,18): error CS0150: A constant value is expected // if (o is F) Diagnostic(ErrorCode.ERR_ConstantExpected, "F").WithLocation(6, 18), - // 0.cs(8,15): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'. + // (8,15): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'. // F(o); Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o").WithArguments("o", "void C.F(object o)").WithLocation(8, 15)); } @@ -115796,7 +115796,7 @@ void M0(T x0) // (7,9): warning CS8602: Dereference of a possibly null reference. // x0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x0").WithLocation(7, 9) - ); + ); } [Fact] diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs index f3daac961f96c..2323d46317cc1 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs @@ -174,13 +174,13 @@ void Test(object? x) } "); c.VerifyDiagnostics( - // 0.cs(7,18): error CS0150: A constant value is expected + // (7,18): error CS0150: A constant value is expected // if (x is nonConstant) Diagnostic(ErrorCode.ERR_ConstantExpected, "nonConstant").WithLocation(7, 18), - // 0.cs(9,13): warning CS8602: Dereference of a possibly null reference. + // (9,13): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(9, 13) - ); + ); } [Fact]