Disallow null coalescing assignment in expression trees.

上级 e87464c0
...@@ -19,7 +19,7 @@ namespace Microsoft.CodeAnalysis.CSharp { ...@@ -19,7 +19,7 @@ namespace Microsoft.CodeAnalysis.CSharp {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class CSharpResources { internal class CSharpResources {
...@@ -4425,6 +4425,15 @@ internal class CSharpResources { ...@@ -4425,6 +4425,15 @@ internal class CSharpResources {
} }
} }
/// <summary>
/// Looks up a localized string similar to An expression tree may not contain a null coalescing assignment.
/// </summary>
internal static string ERR_ExpressionTreeCantContainNullCoalescingAssignment {
get {
return ResourceManager.GetString("ERR_ExpressionTreeCantContainNullCoalescingAssignment", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Expression tree cannot contain value of ref struct or restricted type &apos;{0}&apos;.. /// Looks up a localized string similar to Expression tree cannot contain value of ref struct or restricted type &apos;{0}&apos;..
/// </summary> /// </summary>
......
...@@ -5635,4 +5635,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ ...@@ -5635,4 +5635,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_ElseCannotStartStatement" xml:space="preserve"> <data name="ERR_ElseCannotStartStatement" xml:space="preserve">
<value>'else' cannot start a statement.</value> <value>'else' cannot start a statement.</value>
</data> </data>
<data name="ERR_ExpressionTreeCantContainNullCoalescingAssignment" xml:space="preserve">
<value>An expression tree may not contain a null coalescing assignment</value>
</data>
</root> </root>
\ No newline at end of file
...@@ -1636,7 +1636,8 @@ internal enum ErrorCode ...@@ -1636,7 +1636,8 @@ internal enum ErrorCode
WRN_CantInferNullabilityOfMethodTypeArgs = 8638, WRN_CantInferNullabilityOfMethodTypeArgs = 8638,
WRN_NoBestNullabilityArrayElements = 8639, WRN_NoBestNullabilityArrayElements = 8639,
ERR_ExpressionTreeCantContainRefStruct = 8640, ERR_ExpressionTreeCantContainRefStruct = 8640,
ERR_ElseCannotStartStatement = 8641 ERR_ElseCannotStartStatement = 8641,
ERR_ExpressionTreeCantContainNullCoalescingAssignment = 8642
#endregion diagnostics introduced for C# 8.0 #endregion diagnostics introduced for C# 8.0
......
...@@ -614,6 +614,16 @@ public override BoundNode VisitNullCoalescingOperator(BoundNullCoalescingOperato ...@@ -614,6 +614,16 @@ public override BoundNode VisitNullCoalescingOperator(BoundNullCoalescingOperato
return base.VisitNullCoalescingOperator(node); return base.VisitNullCoalescingOperator(node);
} }
public override BoundNode VisitNullCoalescingAssignmentOperator(BoundNullCoalescingAssignmentOperator node)
{
if (_inExpressionLambda)
{
Error(ErrorCode.ERR_ExpressionTreeCantContainNullCoalescingAssignment, node);
}
return base.VisitNullCoalescingAssignmentOperator(node);
}
public override BoundNode VisitDynamicInvocation(BoundDynamicInvocation node) public override BoundNode VisitDynamicInvocation(BoundDynamicInvocation node)
{ {
if (_inExpressionLambda) if (_inExpressionLambda)
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -62,6 +62,11 @@ ...@@ -62,6 +62,11 @@
<target state="new">The given expression cannot be used in a fixed statement</target> <target state="new">The given expression cannot be used in a fixed statement</target>
<note /> <note />
</trans-unit> </trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainNullCoalescingAssignment">
<source>An expression tree may not contain a null coalescing assignment</source>
<target state="new">An expression tree may not contain a null coalescing assignment</target>
<note />
</trans-unit>
<trans-unit id="ERR_ExpressionTreeCantContainRefStruct"> <trans-unit id="ERR_ExpressionTreeCantContainRefStruct">
<source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source> <source>Expression tree cannot contain value of ref struct or restricted type '{0}'.</source>
<target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target> <target state="new">Expression tree cannot contain value of ref struct or restricted type '{0}'.</target>
......
...@@ -2351,5 +2351,23 @@ void M() ...@@ -2351,5 +2351,23 @@ void M()
// Span<byte>? s2 = null; // Span<byte>? s2 = null;
Diagnostic(ErrorCode.ERR_BadTypeArgument, "Span<byte>?").WithArguments("System.Span<byte>").WithLocation(9, 9)); Diagnostic(ErrorCode.ERR_BadTypeArgument, "Span<byte>?").WithArguments("System.Span<byte>").WithLocation(9, 9));
} }
[Fact]
[WorkItem(32138, "https://github.com/dotnet/roslyn/issues/31238")]
public void ExpressionTreeNotAllowed()
{
CreateCompilation(@"
using System;
using System.Linq.Expressions;
public class C {
public void M() {
String x = null;
Expression<Func<string>> e0 = () => x ??= null;
}
}").VerifyDiagnostics(
// (7,45): error CS8642: An expression tree may not contain a null coalescing assignment
// Expression<Func<string>> e0 = () => x ??= null;
Diagnostic(ErrorCode.ERR_ExpressionTreeCantContainNullCoalescingAssignment, "x ??= null").WithLocation(7, 45));
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册