提交 3cce82ab 编写于 作者: M Manish Vasani

Fix for internal bug 1132014: Implement interface generates innacessible...

Fix for internal bug 1132014: Implement interface generates innacessible attributes on implemented members

Fix is to remove inaccessible attributes from the generated members and its parameters.
上级 e4f6411d
......@@ -2641,5 +2641,50 @@ private static string DisposePattern(string disposeVisibility, string className,
}}
#endregion";
}
[WorkItem(1132014)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public void TestInaccesibleAttributes()
{
Test(
@"using System;
public class Foo : [|Holder.SomeInterface|]
{
}
public class Holder
{
public interface SomeInterface
{
void Something([SomeAttribute] string helloWorld);
}
private class SomeAttribute : Attribute
{
}
}",
@"using System;
public class Foo : Holder.SomeInterface
{
public void Something(string helloWorld)
{
throw new NotImplementedException();
}
}
public class Holder
{
public interface SomeInterface
{
void Something([SomeAttribute] string helloWorld);
}
private class SomeAttribute : Attribute
{
}
}");
}
}
}
......@@ -2127,5 +2127,45 @@ End Interface", index:=1, compareTokens:=False)
Return code
End Function
<WorkItem(1132014)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)>
Public Sub TestInaccesibleAttributes()
Test(
"Imports System
Public Class Foo
Implements [|Holder.SomeInterface|]
End Class
Public Class Holder
Public Interface SomeInterface
Sub Something(<SomeAttribute> helloWorld As String)
End Interface
Private Class SomeAttribute
Inherits Attribute
End Class
End Class",
"Imports System
Public Class Foo
Implements Holder.SomeInterface
Public Sub Something(helloWorld As String) Implements Holder.SomeInterface.Something
Throw New NotImplementedException()
End Sub
End Class
Public Class Holder
Public Interface SomeInterface
Sub Something(<SomeAttribute> helloWorld As String)
End Interface
Private Class SomeAttribute
Inherits Attribute
End Class
End Class", compareTokens:=False)
End Sub
End Class
End Namespace
......@@ -28,7 +28,9 @@ internal partial class ImplementInterfaceCodeAction
var updatedMethod = method.EnsureNonConflictingNames(
this.State.ClassOrStructType, syntaxFacts, cancellationToken);
updatedMethod = updatedMethod.RemoveAttributeFromParametersAndReturnType(compilation.ComAliasNameAttributeType());
updatedMethod = updatedMethod.RemoveInaccessibleAttributesAndAttributesOfType(
accessibleWithin: this.State.ClassOrStructType,
removeAttributeType: compilation.ComAliasNameAttributeType());
return CodeGenerationSymbolFactory.CreateMethodSymbol(
updatedMethod,
......
......@@ -32,7 +32,9 @@ internal partial class ImplementInterfaceCodeAction
var getAccessor = property.GetMethod == null
? null
: CodeGenerationSymbolFactory.CreateAccessorSymbol(
property.GetMethod.RemoveAttributeFromParametersAndReturnType(comAliasNameAttribute),
property.GetMethod.RemoveInaccessibleAttributesAndAttributesOfType(
accessibleWithin: this.State.ClassOrStructType,
removeAttributeType: comAliasNameAttribute),
attributes: null,
accessibility: accessibility,
explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.GetMethod : null,
......@@ -41,7 +43,9 @@ internal partial class ImplementInterfaceCodeAction
var setAccessor = property.SetMethod == null
? null
: CodeGenerationSymbolFactory.CreateAccessorSymbol(
property.SetMethod.RemoveAttributeFromParametersAndReturnType(comAliasNameAttribute),
property.SetMethod.RemoveInaccessibleAttributesAndAttributesOfType(
accessibleWithin: this.State.ClassOrStructType,
removeAttributeType: comAliasNameAttribute),
attributes: null,
accessibility: accessibility,
explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.SetMethod : null,
......
......@@ -168,29 +168,34 @@ public static IMethodSymbol RenameParameters(this IMethodSymbol method, IList<st
return updatedMethod.RenameParameters(parameterNames);
}
public static IMethodSymbol RemoveAttributeFromParametersAndReturnType(
this IMethodSymbol method, INamedTypeSymbol attributeType,
public static IMethodSymbol RemoveInaccessibleAttributesAndAttributesOfType(
this IMethodSymbol method, ISymbol accessibleWithin, INamedTypeSymbol removeAttributeType,
IList<SyntaxNode> statements = null, IList<SyntaxNode> handlesExpressions = null)
{
if (attributeType == null)
{
return method;
}
Func<AttributeData, bool> shouldRemoveAttribute = a =>
a.AttributeClass.Equals(removeAttributeType) || !a.AttributeClass.IsAccessibleWithin(accessibleWithin);
return method.RemoveAttributesCore(shouldRemoveAttribute, statements, handlesExpressions);
}
private static IMethodSymbol RemoveAttributesCore(
this IMethodSymbol method, Func<AttributeData, bool> shouldRemoveAttribute,
IList<SyntaxNode> statements, IList<SyntaxNode> handlesExpressions)
{
var methodHasAttribute = method.GetAttributes().Any(shouldRemoveAttribute);
var someParameterHasAttribute = method.Parameters
.Any(m => m.GetAttributes().Any(a => a.AttributeClass.Equals(attributeType)));
.Any(m => m.GetAttributes().Any(shouldRemoveAttribute));
var returnTypeHasAttribute = method.GetReturnTypeAttributes()
.Any(a => a.AttributeClass.Equals(attributeType));
var returnTypeHasAttribute = method.GetReturnTypeAttributes().Any(shouldRemoveAttribute);
if (!someParameterHasAttribute && !returnTypeHasAttribute)
if (!methodHasAttribute && !someParameterHasAttribute && !returnTypeHasAttribute)
{
return method;
}
return CodeGenerationSymbolFactory.CreateMethodSymbol(
method.ContainingType,
method.GetAttributes(),
method.GetAttributes().Where(a => !shouldRemoveAttribute(a)).ToList(),
method.DeclaredAccessibility,
method.GetSymbolModifiers(),
method.ReturnType,
......@@ -199,12 +204,12 @@ public static IMethodSymbol RenameParameters(this IMethodSymbol method, IList<st
method.TypeParameters,
method.Parameters.Select(p =>
CodeGenerationSymbolFactory.CreateParameterSymbol(
p.GetAttributes().Where(a => !a.AttributeClass.Equals(attributeType)).ToList(),
p.GetAttributes().Where(a => !shouldRemoveAttribute(a)).ToList(),
p.RefKind, p.IsParams, p.Type, p.Name, p.IsOptional,
p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)).ToList(),
statements,
handlesExpressions,
method.GetReturnTypeAttributes().Where(a => !a.AttributeClass.Equals(attributeType)).ToList());
method.GetReturnTypeAttributes().Where(a => !shouldRemoveAttribute(a)).ToList());
}
public static bool? IsMoreSpecificThan(this IMethodSymbol method1, IMethodSymbol method2)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册