From b0cba5b6a5ad378bd5eac356c8b4257c9106e894 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 11 Jun 2020 17:42:49 -0700 Subject: [PATCH 1/2] Disallow 'with' in expression trees --- .../CSharp/Portable/CSharpResources.resx | 3 +++ .../CSharp/Portable/Errors/ErrorCode.cs | 1 + .../DiagnosticsPass_ExpressionTrees.cs | 10 +++++++++ .../Portable/xlf/CSharpResources.cs.xlf | 5 +++++ .../Portable/xlf/CSharpResources.de.xlf | 5 +++++ .../Portable/xlf/CSharpResources.es.xlf | 5 +++++ .../Portable/xlf/CSharpResources.fr.xlf | 5 +++++ .../Portable/xlf/CSharpResources.it.xlf | 5 +++++ .../Portable/xlf/CSharpResources.ja.xlf | 5 +++++ .../Portable/xlf/CSharpResources.ko.xlf | 5 +++++ .../Portable/xlf/CSharpResources.pl.xlf | 5 +++++ .../Portable/xlf/CSharpResources.pt-BR.xlf | 5 +++++ .../Portable/xlf/CSharpResources.ru.xlf | 5 +++++ .../Portable/xlf/CSharpResources.tr.xlf | 5 +++++ .../Portable/xlf/CSharpResources.zh-Hans.xlf | 5 +++++ .../Portable/xlf/CSharpResources.zh-Hant.xlf | 5 +++++ .../Test/Semantic/Semantics/RecordTests.cs | 21 +++++++++++++++++++ 17 files changed, 100 insertions(+) diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index e7a17e886a9d6..6b3b109894761 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -5230,6 +5230,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ An expression tree may not contain a throw-expression. + + An expression tree may not contain a with-expression. + Invalid assembly name: {0} diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index 0b17327a6883b..003d230422ab5 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -1818,6 +1818,7 @@ internal enum ErrorCode ERR_CannotConvertAddressOfToDelegate = 8811, ERR_AddressOfToNonFunctionPointer = 8812, + ERR_ExpressionTreeContainsWithExpression = 8849, ERR_BadRecordDeclaration = 8850, ERR_DuplicateRecordConstructor = 8851, ERR_AssignmentInitOnly = 8852, diff --git a/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs b/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs index 17878195bd824..7067c4eaa38a5 100644 --- a/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs +++ b/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs @@ -860,5 +860,15 @@ public override BoundNode VisitThrowExpression(BoundThrowExpression node) return base.VisitThrowExpression(node); } + + public override BoundNode VisitWithExpression(BoundWithExpression node) + { + if (_inExpressionLambda) + { + Error(ErrorCode.ERR_ExpressionTreeContainsWithExpression, node); + } + + return base.VisitWithExpression(node); + } } } diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index 4acdd55a0ad14..60bb1eb1b86c1 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -307,6 +307,11 @@ Strom výrazů nesmí obsahovat operátor řazené kolekce členů == nebo !=. + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer {0}: Externí událost nemůže mít inicializátor. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index 2574672f0af24..6075a494e47f5 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -307,6 +307,11 @@ Eine Ausdrucksbaumstruktur darf keinen ==- oder !=-Tupeloperator enthalten. + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer "{0}": Externes Ereignis darf keinen Initialisierer aufweisen. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index 3ad8f16f0441a..db4d1cc263a13 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -307,6 +307,11 @@ Un árbol de expresión no puede contener un operador de tupla == o !=. + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer "{0}": un evento externo no puede tener un inicializador diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index c7a880a9f9679..df583fc84b7d3 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -307,6 +307,11 @@ Une arborescence de l'expression ne peut pas contenir un opérateur de tuple == ou != + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '{0}' : un événement extern ne peut pas avoir d'initialiseur diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index 1baae7bfbc6c8..f9a70287c1122 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -307,6 +307,11 @@ Un albero delle espressioni non può contenere un operatore == o != di tupla + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '{0}': l'evento extern non può avere inizializzatori diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index 83d58c4008f8c..86ef783ccc946 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -307,6 +307,11 @@ 式ツリーにタプルの == または != 演算子を含めることはできません + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '{0}': extern イベントは初期化子を持つことができません diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index 9cf38b3f9a3e3..0b92cccf47160 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -307,6 +307,11 @@ 식 트리에는 튜플 == 또는 != 연산자를 사용할 수 없습니다. + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '{0}': extern 이벤트에는 이니셜라이저를 사용할 수 없습니다. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index 1b27bc474a1f2..1b1a290bfdfd9 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -307,6 +307,11 @@ Drzewo wyrażenia nie może zawierać operatora == ani != krotki. + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '„{0}”: zdarzenie extern nie może mieć inicjatora diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index 588ccea98fe04..6a9b91ca104cf 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -307,6 +307,11 @@ Uma árvore de expressão não pode conter um operador == ou != de tupla + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '{0}': o evento externo não pode ter inicializador diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index f7618d3be4ff9..ad6f54c39700b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -307,6 +307,11 @@ Дерево выражений не может содержать оператор == или != кортежа. + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer "{0}": внешнее событие не может иметь инициализатор diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index de972dc0acb12..d954ec7a8779e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -307,6 +307,11 @@ İfade ağacı, demetin == veya != işlecini içeremez. + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '{0}': dış etkinliğin başlatıcısı olamaz diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 16050c1532eac..c9900edda24d0 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -307,6 +307,11 @@ 表达式树不能包含元组 == 或 != 运算符 + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer “{0}”: 外部事件不能有初始值设定项 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index c9ce42ddfbf0d..2df1a50fb7d14 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -307,6 +307,11 @@ 運算式樹狀架構不得包含元組 == 或 != 運算子。 + + An expression tree may not contain a with-expression. + An expression tree may not contain a with-expression. + + '{0}': extern event cannot have initializer '{0}': 外部事件不可有初始設定式 diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs index 9ed43309f63da..d5dcb21afaeda 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs @@ -145,6 +145,27 @@ record Point(int x, int y); comp.VerifyDiagnostics(); } + [Fact] + public void TestInExpressionTree() + { + var source = @" +using System; +using System.Linq.Expressions; +public record C(int i) +{ + public static void M() + { + Expression> expr = c => c with { i = 5 }; + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (8,44): error CS8849: An expression tree may not contain a with-expression. + // Expression> expr = c => c with { i = 5 }; + Diagnostic(ErrorCode.ERR_ExpressionTreeContainsWithExpression, "c with { i = 5 }").WithLocation(8, 44) + ); + } + [Fact] public void RecordProperties_01() { From 21aa335ebc0d2c1adf05589fd50e680f76d728b0 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Mon, 15 Jun 2020 07:11:17 -0700 Subject: [PATCH 2/2] Fix formatting --- src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs index 650f09d057996..362ff2e321d86 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs @@ -162,10 +162,10 @@ public static void M() comp.VerifyDiagnostics( // (8,44): error CS8849: An expression tree may not contain a with-expression. // Expression> expr = c => c with { i = 5 }; - Diagnostic(ErrorCode.ERR_ExpressionTreeContainsWithExpression, "c with { i = 5 }").WithLocation(8, 44) + Diagnostic(ErrorCode.ERR_ExpressionTreeContainsWithExpression, "c with { i = 5 }").WithLocation(8, 44) ); } - + [Fact] public void PartialRecordMixedWithClass() {