From ecc15dde9a51c1f85977ed084e07b92fbc902147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Thu, 24 Jan 2013 11:24:50 +0100 Subject: [PATCH] Fixed bug in type system conversion (interface members can shadow other members) & implement interface action bug. --- .../CodeActions/ImplementInterfaceAction.cs | 11 ++- .../TypeSystem/TypeSystemConvertVisitor.cs | 3 +- .../CodeActions/ImplementInterfaceTests.cs | 73 +++++++++++++++++-- 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceAction.cs index 9ee5c175f7..19053a397b 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceAction.cs @@ -158,9 +158,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring bool needsExplicitly = explicitly; alreadyImplemented = false; foreach (var t in implementingType.GetAllBaseTypeDefinitions ()) { - if (t.Kind == TypeKind.Interface) + if (t.Kind == TypeKind.Interface) { + foreach (var cprop in t.Properties) { + if (cprop.Name == prop.Name && cprop.IsShadowing) { + if (!needsExplicitly && !cprop.ReturnType.Equals(prop.ReturnType)) + needsExplicitly = true; + } + } continue; - foreach (IProperty cprop in t.Properties) { + } + foreach (var cprop in t.Properties) { if (cprop.Name == prop.Name) { if (!needsExplicitly && !cprop.ReturnType.Equals(prop.ReturnType)) needsExplicitly = true; diff --git a/ICSharpCode.NRefactory.CSharp/TypeSystem/TypeSystemConvertVisitor.cs b/ICSharpCode.NRefactory.CSharp/TypeSystem/TypeSystemConvertVisitor.cs index 735b1eeea7..94f5159151 100644 --- a/ICSharpCode.NRefactory.CSharp/TypeSystem/TypeSystemConvertVisitor.cs +++ b/ICSharpCode.NRefactory.CSharp/TypeSystem/TypeSystemConvertVisitor.cs @@ -811,10 +811,11 @@ namespace ICSharpCode.NRefactory.CSharp.TypeSystem static void ApplyModifiers(AbstractUnresolvedMember m, Modifiers modifiers) { - // members from interfaces are always Public+Abstract. + // members from interfaces are always Public+Abstract. (NOTE: 'new' modifier is valid in interfaces as well.) if (m.DeclaringTypeDefinition.Kind == TypeKind.Interface) { m.Accessibility = Accessibility.Public; m.IsAbstract = true; + m.IsShadowing = (modifiers & Modifiers.New) != 0; return; } m.Accessibility = GetAccessibility(modifiers) ?? Accessibility.Private; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.cs index b2c91599f2..687cf99915 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.cs @@ -32,7 +32,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions [TestFixture] public class ImplementInterfaceTests : ContextActionTestBase { - [Test()] + [Test] public void TestSimpleInterface() { Test(@"using System; @@ -56,7 +56,7 @@ class Foo : IDisposable /// /// Bug 663842 - Interface implementation does not include constraints /// - [Test()] + [Test] public void TestBug663842() { Test(@"using System; @@ -105,7 +105,7 @@ class Foo : ITest /// /// Bug 683007 - "Refactor/Implement implicit" creates explicit implementations of methods with same names /// - [Test()] + [Test] public void TestBug683007() { Test(@"interface ITest { @@ -138,7 +138,7 @@ class Foo : ITest /// /// Bug 243 - Implement implicit interface doesn't handle overloads correctly. /// - [Test()] + [Test] public void TestBug243() { Test(@"interface ITest { @@ -174,7 +174,7 @@ class Foo : ITest /// /// Bug 2074 - [Regression] Implement Interface implicitly does not check the methods already exist /// - [Test()] + [Test] public void TestBug2074() { Test(@"interface ITest { @@ -205,7 +205,7 @@ class Foo : ITest /// /// Bug 3365 - MD cannot implement IEnumerable interface correctly - MD cannot implement IEnumerable interface correctly /// - [Test()] + [Test] public void TestBug3365() { Test(@"using System; @@ -255,7 +255,7 @@ class Foo : ITest /// /// Bug 4818 - Implement implicit does not handle 'params' types /// - [Test()] + [Test] public void TestBug4818() { Test(@"using System; @@ -302,7 +302,7 @@ class Foo : $ITest /// /// Bug 9117 - [3.0.5] C#: Implementing interfaces inheriting from other interfaces /// - [Test()] + [Test] public void TestBug9117() { Test(@"using System; @@ -387,6 +387,63 @@ class Foo : $ITest } "); } + + /// + /// Bug 9603 - Implement interface cannot deal with member hidding + /// + [Test] + public void TestBug9603() + { + Test(@"using System; + +public interface IA +{ + string this[int index] { get; set; } +} + +public interface IB : IA +{ + new int this[int index] { get; set; } +} + +class M : $IB +{ +}", @"using System; + +public interface IA +{ + string this[int index] { get; set; } +} + +public interface IB : IA +{ + new int this[int index] { get; set; } +} + +class M : IB +{ + #region IB implementation + public int this [int index] { + get { + throw new NotImplementedException (); + } + set { + throw new NotImplementedException (); + } + } + #endregion + #region IA implementation + string IA.this [int index] { + get { + throw new NotImplementedException (); + } + set { + throw new NotImplementedException (); + } + } + #endregion +}"); + } } }