From d4b0556e331f1c9728a8a5b6864ea212cd490a49 Mon Sep 17 00:00:00 2001 From: lorcanmooney Date: Sun, 8 May 2016 21:03:23 +0100 Subject: [PATCH] Disallow sealed in structs --- .../Portable/Symbols/Source/SourceMemberMethodSymbol.cs | 5 +++++ .../CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs index d49b764b1ab..8c1b99c3f28 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs @@ -853,6 +853,11 @@ private void CheckModifiers(Location location, DiagnosticBag diagnostics) // '{0}' cannot be sealed because it is not an override diagnostics.Add(ErrorCode.ERR_SealedNonOverride, location, this); } + else if (IsSealed && ContainingType.TypeKind == TypeKind.Struct) + { + // The modifier '{0}' is not valid for this item + diagnostics.Add(ErrorCode.ERR_BadMemberFlag, location, SyntaxFacts.GetText(SyntaxKind.SealedKeyword)); + } else if (!ContainingType.IsInterfaceType() && _lazyReturnType.IsStatic) { // '{0}': static types cannot be used as return types diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs index 66deb0ad247..3f4c1ddea6c 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs @@ -1688,6 +1688,7 @@ struct Foo public virtual int this[int x] { get { return 1;} set {;} } // use long for to prevent signature clash public abstract int this[long x] { get; set; } + public sealed override string ToString() => null; } "; CreateCompilationWithMscorlib(text).VerifyDiagnostics( @@ -1714,7 +1715,10 @@ struct Foo Diagnostic(ErrorCode.ERR_BadMemberFlag, "Bar5").WithArguments("abstract").WithLocation(8, 47), // (9,46): error CS0106: The modifier 'virtual' is not valid for this item // public virtual event System.EventHandler Bar6; - Diagnostic(ErrorCode.ERR_BadMemberFlag, "Bar6").WithArguments("virtual").WithLocation(9, 46)); + Diagnostic(ErrorCode.ERR_BadMemberFlag, "Bar6").WithArguments("virtual").WithLocation(9, 46), + // (15,35): error CS0106: The modifier 'sealed' is not valid for this item + // public sealed override string ToString() => null; + Diagnostic(ErrorCode.ERR_BadMemberFlag, "ToString").WithArguments("sealed").WithLocation(15, 35)); } [Fact] -- GitLab