Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment would be really useful here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

? 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)
{
Expand Down
71 changes: 71 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
{
static Foo()
{
Foo<int>.X = 1;
Foo<int>.Y = 2;
Foo<T>.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<int>.Y' cannot be assigned to -- it is read only
// Foo<int>.Y = 2;
Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "Foo<int>.Y").WithArguments("Foo<int>.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<int>.X = 1;
Diagnostic(ErrorCode.ERR_AssgReadonlyStatic, "Foo<int>.X").WithLocation(5, 9),
// (6,9): error CS0200: Property or indexer 'Foo<int>.Y' cannot be assigned to -- it is read only
// Foo<int>.Y = 2;
Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "Foo<int>.Y").WithArguments("Foo<int>.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<long>.x);
Console.WriteLine(Foo<int>.x);
Console.WriteLine(Foo<string>.x);
Console.WriteLine(Foo<int>.x);
}
}

public static class Foo<T>
{
static Foo()
{
Console.WriteLine(""initializing for "" + typeof(T));
Foo<int>.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()
{
Expand Down