diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs index 411bf6d323e00..37e52ae7bbd23 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs @@ -1399,7 +1399,10 @@ private bool CheckIsVariable(CSharpSyntaxNode node, BoundExpression expr, BindVa if ((object)containing != null && fieldIsStatic == containing.IsStatic && (fieldIsStatic || fieldAccess.ReceiverOpt.Kind == BoundKind.ThisReference) && - fieldSymbol.ContainingType == containing.ContainingType) + (Compilation.FeatureStrictEnabled + ? fieldSymbol.ContainingType == containing.ContainingType + // We duplicate a bug in the native compiler for compatibility in non-strict mode + : fieldSymbol.ContainingType.OriginalDefinition == containing.ContainingType.OriginalDefinition)) { if (containing.Kind == SymbolKind.Method) { diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs index f01ac52c02048..6ca563b26eb32 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs @@ -6762,6 +6762,77 @@ static MyDerived() }); } + [Fact, WorkItem(990, "https://github.com/dotnet/roslyn/issues/990")] + public void WriteOfReadonlyStaticMemberOfAnotherInstatiation01() + { + var text = +@"public static class Foo +{ + static Foo() + { + Foo.X = 1; + Foo.Y = 2; + Foo.Y = 3; + } + + public static readonly int X; + public static int Y { get; } +}"; + CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseDll).VerifyDiagnostics( + // (6,9): error CS0200: Property or indexer 'Foo.Y' cannot be assigned to -- it is read only + // Foo.Y = 2; + Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "Foo.Y").WithArguments("Foo.Y").WithLocation(6, 9) + ); + CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseDll.WithStrictMode()).VerifyDiagnostics( + // (5,9): error CS0198: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer) + // Foo.X = 1; + Diagnostic(ErrorCode.ERR_AssgReadonlyStatic, "Foo.X").WithLocation(5, 9), + // (6,9): error CS0200: Property or indexer 'Foo.Y' cannot be assigned to -- it is read only + // Foo.Y = 2; + Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "Foo.Y").WithArguments("Foo.Y").WithLocation(6, 9) + ); + } + + [Fact, WorkItem(990, "https://github.com/dotnet/roslyn/issues/990")] + public void WriteOfReadonlyStaticMemberOfAnotherInstatiation02() + { + var text = +@"using System; +using System.Threading; +class Program +{ + static void Main(string[] args) + { + Console.WriteLine(Foo.x); + Console.WriteLine(Foo.x); + Console.WriteLine(Foo.x); + Console.WriteLine(Foo.x); + } +} + +public static class Foo +{ + static Foo() + { + Console.WriteLine(""initializing for "" + typeof(T)); + Foo.x = typeof(T).Name; + } + + public static readonly string x; +}"; + var expectedOutput = +@"initializing for System.Int64 +initializing for System.Int32 + +Int64 +initializing for System.String + +String +"; + // Although we accept this nasty code, it will not verify. + CompileAndVerify(text, expectedOutput: expectedOutput, verify: false); + } + [Fact] public void CS0199ERR_RefReadonlyStatic() {