From b05dad9f314b48f86516f9833d40c3ef6f1e9a7d Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Fri, 29 Dec 2017 23:38:40 +0100 Subject: [PATCH] readonly struct this can be reassigned within ctor --- mcs/mcs/expression.cs | 2 +- mcs/tests/test-readonly-03.cs | 16 ++++++++++++++++ mcs/tests/ver-il-net_4_x.xml | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 mcs/tests/test-readonly-03.cs diff --git a/mcs/mcs/expression.cs b/mcs/mcs/expression.cs index 51f268737c0..183be4d8cb3 100644 --- a/mcs/mcs/expression.cs +++ b/mcs/mcs/expression.cs @@ -9059,7 +9059,7 @@ namespace Mono.CSharp if (eclass == ExprClass.Unresolved) ResolveBase (ec); - if (type.IsClass || type.IsReadOnly) { + if (type.IsClass || (type.IsReadOnly && !ec.HasSet (ResolveContext.Options.ConstructorScope))) { if (right_side == EmptyExpression.UnaryAddress) ec.Report.Error (459, loc, "Cannot take the address of `this' because it is read-only"); else if (right_side == EmptyExpression.OutAccess) diff --git a/mcs/tests/test-readonly-03.cs b/mcs/tests/test-readonly-03.cs new file mode 100644 index 00000000000..14cb958b41a --- /dev/null +++ b/mcs/tests/test-readonly-03.cs @@ -0,0 +1,16 @@ +// Compiler options: -langversion:latest +using System; + +readonly struct S +{ + static S shared = new S (); + + public S (int arg) + { + this = shared; + } + + public static void Main () + { + } +} \ No newline at end of file diff --git a/mcs/tests/ver-il-net_4_x.xml b/mcs/tests/ver-il-net_4_x.xml index 4e488c37bc4..c8010844ece 100644 --- a/mcs/tests/ver-il-net_4_x.xml +++ b/mcs/tests/ver-il-net_4_x.xml @@ -72889,6 +72889,19 @@ + + + + 2 + + + 13 + + + 15 + + + -- 2.11.4.GIT