From a07d2c88bbeac21b78745ffc660ff2a98f1f09bb Mon Sep 17 00:00:00 2001 From: Neal Gafter Date: Thu, 17 Dec 2015 14:39:09 -0800 Subject: [PATCH] A method may have the same name as its type parameter. Fixes #7506 --- .../Symbols/Source/SourceMemberMethodSymbol.cs | 5 +---- .../Symbols/Source/TypeParameterBuilder.cs | 3 ++- .../Test/Symbol/Symbols/SymbolErrorTests.cs | 17 +++++++++-------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs index 7eae1e9bbba..00c5587c1b4 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs @@ -822,10 +822,7 @@ private ImmutableArray MakeTypeParameters(MethodDeclaration var location = identifier.GetLocation(); var name = identifier.ValueText; - if (name == this.Name) - { - diagnostics.Add(ErrorCode.ERR_TypeVariableSameAsParent, location, name); - } + // Note: It is not an error to have a type parameter named the same as its enclosing method: void M() {} for (int i = 0; i < result.Count; i++) { diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/TypeParameterBuilder.cs b/src/Compilers/CSharp/Portable/Symbols/Source/TypeParameterBuilder.cs index 84b14bd0fbb..673d3fbe097 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/TypeParameterBuilder.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/TypeParameterBuilder.cs @@ -10,7 +10,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Symbols { /// - /// A context for binding type parameter symbols. + /// A context for binding type parameter symbols of named types. /// internal sealed class TypeParameterBuilder { @@ -37,6 +37,7 @@ internal TypeParameterSymbol MakeSymbol(int ordinal, IList ToLocations(builders), ToSyntaxRefs(builders)); + // SPEC: A type parameter [of a type] cannot have the same name as the type itself. if (result.Name == result.ContainingSymbol.Name) { diagnostics.Add(ErrorCode.ERR_TypeVariableSameAsParent, result.Locations[0], result.Name); diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs index 58a8760959f..61e4c1cfdf2 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs @@ -11961,7 +11961,7 @@ public void CS0694ERR_TypeVariableSameAsParent01() { interface IFoo { - void M(M m); + void M(M m); // OK (constraint applies to types but not methods) } class C @@ -11972,13 +11972,14 @@ public struct S } } "; - var comp = DiagnosticsUtils.VerifyErrorsAndGetCompilationWithMscorlib(text, - new ErrorDescription { Code = (int)ErrorCode.ERR_TypeVariableSameAsParent, Line = 5, Column = 16 }, // Dev10 not report this if there next 2 errors - new ErrorDescription { Code = (int)ErrorCode.ERR_TypeVariableSameAsParent, Line = 8, Column = 13 }, - new ErrorDescription { Code = (int)ErrorCode.ERR_TypeVariableSameAsParent, Line = 10, Column = 28 }); - - var ns = comp.SourceModule.GlobalNamespace.GetMembers("NS").Single() as NamespaceSymbol; - // TODO... + CreateCompilationWithMscorlib(text).VerifyDiagnostics( + // (8,13): error CS0694: Type parameter 'C' has the same name as the containing type, or method + // class C + Diagnostic(ErrorCode.ERR_TypeVariableSameAsParent, "C").WithArguments("C").WithLocation(8, 13), + // (10,28): error CS0694: Type parameter 'S' has the same name as the containing type, or method + // public struct S + Diagnostic(ErrorCode.ERR_TypeVariableSameAsParent, "S").WithArguments("S").WithLocation(10, 28) + ); } [Fact] -- GitLab