Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local function attributes emit #39226

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,16 @@ private ImmutableArray<ParameterSymbol> MakeParameters()
int ordinal = 0;
var builder = ArrayBuilder<ParameterSymbol>.GetInstance();
var parameters = this.BaseMethodParameters;
var inheritAttributes = this.BaseMethod.SynthesizedMethodsInheritAttributes;
foreach (var p in parameters)
{
builder.Add(SynthesizedParameterSymbol.Create(this, this.TypeMap.SubstituteType(p.OriginalDefinition.TypeWithAnnotations), ordinal++, p.RefKind, p.Name));
builder.Add(SynthesizedParameterSymbol.Create(
this,
this.TypeMap.SubstituteType(p.OriginalDefinition.TypeWithAnnotations),
ordinal++,
p.RefKind,
p.Name,
attributes: inheritAttributes ? p.GetAttributes() : default));
}
var extraSynthed = ExtraSynthesizedRefParameters;
if (!extraSynthed.IsDefaultOrEmpty)
Expand All @@ -130,6 +137,20 @@ private ImmutableArray<ParameterSymbol> MakeParameters()
return builder.ToImmutableAndFree();
}

public override ImmutableArray<CSharpAttributeData> GetAttributes()
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 14, 2019

Choose a reason for hiding this comment

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

override [](start = 15, length = 8)

sealed? #Closed

{
Debug.Assert(base.GetAttributes().IsEmpty);
return BaseMethod.SynthesizedMethodsInheritAttributes
? BaseMethod.GetAttributes()
: ImmutableArray<CSharpAttributeData>.Empty;
}

public override ImmutableArray<CSharpAttributeData> GetReturnTypeAttributes()
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 14, 2019

Choose a reason for hiding this comment

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

override [](start = 15, length = 8)

sealed? #Closed

{
Debug.Assert(base.GetReturnTypeAttributes().IsEmpty);
return BaseMethod.SynthesizedMethodsInheritAttributes ? BaseMethod.GetReturnTypeAttributes() : ImmutableArray<CSharpAttributeData>.Empty;
}

public sealed override RefKind RefKind
{
get { return this.BaseMethod.RefKind; }
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ internal virtual bool IsExplicitInterfaceImplementation

protected bool IsValidReadOnlyTarget => !IsStatic && ContainingType.IsStructType() && MethodKind != MethodKind.Constructor;

/// <summary>
/// Indicates whether synthesized methods derived from this method should inherit the attributes of this method, its parameters, return type, and type parameters.
/// </summary>
internal virtual bool SynthesizedMethodsInheritAttributes => false;
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 14, 2019

Choose a reason for hiding this comment

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

internal virtual bool SynthesizedMethodsInheritAttributes => false; [](start = 8, length = 67)

I find this approach very fragile and error prone. There could be multiple synthesized methods of different nature associated with the same "base" method. The property whether attributes should be inherited belongs to the specific synthesized method instead. Please make the change. #Closed


/// <summary>
/// Returns interface methods explicitly implemented by this method.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ internal override TypeWithAnnotations IteratorElementTypeWithAnnotations

internal override bool IsDeclaredReadOnly => false;

internal override bool SynthesizedMethodsInheritAttributes => true;

IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner => this;

AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations => AttributeLocation.Method | AttributeLocation.Return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ private CustomAttributesBag<CSharpAttributeData> GetAttributesBag(ref CustomAttr
/// Gets the attributes applied on this symbol.
/// Returns an empty array if there are no attributes.
/// </summary>
public sealed override ImmutableArray<CSharpAttributeData> GetAttributes()
public override ImmutableArray<CSharpAttributeData> GetAttributes()
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
{
return this.GetAttributesBag().Attributes;
}
Expand All @@ -1063,7 +1063,7 @@ public sealed override ImmutableArray<CSharpAttributeData> GetAttributes()
/// Gets the attributes applied on the return value of this method symbol.
/// Returns an empty array if there are no attributes.
/// </summary>
public sealed override ImmutableArray<CSharpAttributeData> GetReturnTypeAttributes()
public override ImmutableArray<CSharpAttributeData> GetReturnTypeAttributes()
{
return this.GetReturnTypeAttributesBag().Attributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,15 @@ public static ParameterSymbol Create(
int ordinal,
RefKind refKind,
string name = "",
ImmutableArray<CustomModifier> refCustomModifiers = default(ImmutableArray<CustomModifier>))
ImmutableArray<CustomModifier> refCustomModifiers = default,
ImmutableArray<CSharpAttributeData> attributes = default)
{
if (refCustomModifiers.IsDefaultOrEmpty)
if (refCustomModifiers.IsDefaultOrEmpty && attributes.IsDefaultOrEmpty)
{
return new SynthesizedParameterSymbol(container, type, ordinal, refKind, name);
}

return new SynthesizedParameterSymbolWithCustomModifiers(container, type, ordinal, refKind, name, refCustomModifiers);
return new SynthesizedComplexParameterSymbol(container, type, ordinal, refKind, name, refCustomModifiers.NullToEmpty(), attributes.NullToEmpty());
}

/// <summary>
Expand Down Expand Up @@ -228,26 +229,37 @@ public override ImmutableArray<CustomModifier> RefCustomModifiers
get { return ImmutableArray<CustomModifier>.Empty; }
}

private sealed class SynthesizedParameterSymbolWithCustomModifiers : SynthesizedParameterSymbolBase
private sealed class SynthesizedComplexParameterSymbol : SynthesizedParameterSymbolBase
{
private readonly ImmutableArray<CustomModifier> _refCustomModifiers;
private readonly ImmutableArray<CSharpAttributeData> _attributes;

public SynthesizedParameterSymbolWithCustomModifiers(
public SynthesizedComplexParameterSymbol(
MethodSymbol container,
TypeWithAnnotations type,
int ordinal,
RefKind refKind,
string name,
ImmutableArray<CustomModifier> refCustomModifiers)
ImmutableArray<CustomModifier> refCustomModifiers,
ImmutableArray<CSharpAttributeData> attributes)
: base(container, type, ordinal, refKind, name)
{
_refCustomModifiers = refCustomModifiers.NullToEmpty();
Debug.Assert(!refCustomModifiers.IsDefault);
Debug.Assert(!attributes.IsDefault);

_refCustomModifiers = refCustomModifiers;
_attributes = attributes;
}

public override ImmutableArray<CustomModifier> RefCustomModifiers
{
get { return _refCustomModifiers; }
}

public override ImmutableArray<CSharpAttributeData> GetAttributes()
{
return _attributes;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,48 @@ public override async Task<int> GetIntAsync()
AssertEx.SetEqual(new[] { "CompilerGeneratedAttribute", "DebuggerHiddenAttribute" }, GetAttributeNames(attributes));
});
}

[Theory]
[MemberData(nameof(OptimizationLevelTheoryData))]
[WorkItem(431, "https://github.com/dotnet/roslyn/issues/431")]
public void BaseMethodWrapper_DoNotInheritAttributes(OptimizationLevel optimizationLevel)
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 14, 2019

Choose a reason for hiding this comment

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

BaseMethodWrapper_DoNotInheritAttributes [](start = 20, length = 40)

Please include coverage for attributes on type parameters as well. #Closed

{
string source = @"
using System.Threading.Tasks;

class Attr : System.Attribute { }

class A
{
[Attr]
[return: Attr]
public virtual async Task<int> GetIntAsync([Attr] int x)
{
return 42;
}
}
class B : A
{
public override async Task<int> GetIntAsync(int x)
{
return await base.GetIntAsync(x);
}
}
";
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithOptimizationLevel(optimizationLevel)
.WithMetadataImportOptions(MetadataImportOptions.All);

CompileAndVerify(CreateCompilationWithMscorlib45(source, options: options), symbolValidator: module =>
{
var baseMethodWrapper = module.GlobalNamespace.GetTypeMember("B").GetMember<MethodSymbol>("<>n__0");
AssertEx.SetEqual(new[] { "CompilerGeneratedAttribute", "DebuggerHiddenAttribute" }, GetAttributeNames(baseMethodWrapper.GetAttributes()));
Assert.Empty(baseMethodWrapper.GetReturnTypeAttributes());

var parameter = baseMethodWrapper.Parameters.Single();
Assert.Empty(parameter.GetAttributes());
});
}
#endregion

#region CompilationRelaxationsAttribute, RuntimeCompatibilityAttribute, DebuggableAttribute
Expand Down
Loading