提交 0da2331a 编写于 作者: A angocke

Fix Watson crashes in 1024401.

At least some of the cab files indicate that the compiler is crashing while emitting custom attributes. This appears to be due to the compiler-generated DebuggerDisplayAttribute which is emitted on anonymous types. The current synthesis of the attribute proceeds without checking if a properly formed DebuggerDisplayAttribute type is present in the compilation.

This changes the attribute synthesis helpers to check if the well-known members exist before synthesizing the attribute. (changeset 1376039)
上级 c186a6dd
......@@ -88,7 +88,7 @@ internal sealed override string ModuleName
internal sealed override Cci.ICustomAttribute SynthesizeAttribute(WellKnownMember attributeConstructor)
{
return Compilation.SynthesizeAttribute(attributeConstructor);
return Compilation.TrySynthesizeAttribute(attributeConstructor);
}
internal sealed override IEnumerable<Cci.ICustomAttribute> GetSourceAssemblyAttributes()
......
......@@ -203,7 +203,7 @@ protected override CSharpAttributeData CreateCompilerGeneratedAttribute()
{
Debug.Assert(WellKnownMembers.IsSynthesizedAttributeOptional(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
var compilation = TypeManager.ModuleBeingBuilt.Compilation;
return compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor);
return compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor);
}
protected override CSharpAttributeData CreateTypeIdentifierAttribute(bool hasGuid, CSharpSyntaxNode syntaxNodeOpt, DiagnosticBag diagnostics)
......
......@@ -27,7 +27,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
base.AddSynthesizedAttributes(compilationState, ref attributes);
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor));
}
public override ImmutableArray<ParameterSymbol> Parameters
......
......@@ -70,7 +70,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes,
compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
}
public sealed override ImmutableArray<TypeParameterSymbol> TypeParameters
......
......@@ -142,7 +142,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
AnonymousTypeManager manager = ((AnonymousTypeTemplateSymbol)this.ContainingSymbol).Manager;
AddSynthesizedAttribute(ref attributes, manager.Compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggerBrowsableAttribute__ctor,
ImmutableArray.Create(
new TypedConstant(manager.System_Diagnostics_DebuggerBrowsableState, TypedConstantKind.Enum, DebuggerBrowsableState.Never))));
......
......@@ -154,7 +154,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
{
base.AddSynthesizedAttributes(compilationState, ref attributes);
AddSynthesizedAttribute(ref attributes, Manager.Compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, Manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor));
}
......
......@@ -438,16 +438,20 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
{
base.AddSynthesizedAttributes(compilationState, ref attributes);
AddSynthesizedAttribute(ref attributes, Manager.Compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, Manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
if (Manager.Compilation.Options.OptimizationLevel == OptimizationLevel.Debug)
{
AddSynthesizedAttribute(ref attributes, SynthesizeDebuggerDisplayAttribute());
AddSynthesizedAttribute(ref attributes, TrySynthesizeDebuggerDisplayAttribute());
}
}
private SynthesizedAttributeData SynthesizeDebuggerDisplayAttribute()
/// <summary>
/// Returns a synthesized debugger display attribute or null if one
/// could not be synthesized.
/// </summary>
private SynthesizedAttributeData TrySynthesizeDebuggerDisplayAttribute()
{
string displayString;
if (this.Properties.Length == 0)
......@@ -486,11 +490,12 @@ private SynthesizedAttributeData SynthesizeDebuggerDisplayAttribute()
displayString = builder.ToStringAndFree();
}
return Manager.Compilation.SynthesizeAttribute(
return Manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__ctor,
arguments: ImmutableArray.Create(new TypedConstant(Manager.System_String, TypedConstantKind.Primitive, displayString)),
namedArguments: ImmutableArray.Create(new KeyValuePair<string, TypedConstant>(
"Type", new TypedConstant(Manager.System_String, TypedConstantKind.Primitive, "<Anonymous Type>"))));
namedArguments: ImmutableArray.Create(new KeyValuePair<WellKnownMember, TypedConstant>(
WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__Type,
new TypedConstant(Manager.System_String, TypedConstantKind.Primitive, "<Anonymous Type>"))));
}
}
}
......
......@@ -272,12 +272,24 @@ internal static Symbol GetRuntimeMember(NamedTypeSymbol declaringType, ref Membe
/// <summary>
/// Synthesizes a custom attribute.
/// Returns null if the <paramref name="constructor"/> symbol is missing and the attribute is synthesized only if present.
/// Returns null if the <paramref name="constructor"/> symbol is missing,
/// or any of the members in <paramref name="namedArguments" /> are missing.
/// The attribute is synthesized only if present.
/// </summary>
internal SynthesizedAttributeData SynthesizeAttribute(
/// <param name="constructor">
/// Constructor of the attribute. If it doesn't exist, the attribute is not created.
/// </param>
/// <param name="arguments">Arguments to the attribute constructor.</param>
/// <param name="namedArguments">
/// Takes a list of pairs of well-known members and constants. The constants
/// will be passed to the field/property referenced by the well-known member.
/// If the well-known member does not exist in the compilation then no attribute
/// will be synthesized.
/// </param>
internal SynthesizedAttributeData TrySynthesizeAttribute(
WellKnownMember constructor,
ImmutableArray<TypedConstant> arguments = default(ImmutableArray<TypedConstant>),
ImmutableArray<KeyValuePair<string, TypedConstant>> namedArguments = default(ImmutableArray<KeyValuePair<string, TypedConstant>>))
ImmutableArray<KeyValuePair<WellKnownMember, TypedConstant>> namedArguments = default(ImmutableArray<KeyValuePair<WellKnownMember, TypedConstant>>))
{
var ctorSymbol = (MethodSymbol)GetWellKnownTypeMember(constructor);
if ((object)ctorSymbol == null)
......@@ -292,12 +304,33 @@ internal static Symbol GetRuntimeMember(NamedTypeSymbol declaringType, ref Membe
arguments = ImmutableArray<TypedConstant>.Empty;
}
ImmutableArray<KeyValuePair<string, TypedConstant>> namedStringArguments;
if (namedArguments.IsDefault)
{
namedArguments = ImmutableArray<KeyValuePair<string, TypedConstant>>.Empty;
namedStringArguments = ImmutableArray<KeyValuePair<string, TypedConstant>>.Empty;
}
else
{
var builder = new ArrayBuilder<KeyValuePair<string, TypedConstant>>(namedArguments.Length);
foreach (var arg in namedArguments)
{
var wellKnownMember = GetWellKnownTypeMember(arg.Key);
if (wellKnownMember == null || wellKnownMember is ErrorTypeSymbol)
{
// if this assert fails, UseSiteErrors for "member" have not been checked before emitting ...
Debug.Assert(WellKnownMembers.IsSynthesizedAttributeOptional(constructor));
return null;
}
else
{
builder.Add(new KeyValuePair<string, TypedConstant>(
wellKnownMember.Name, arg.Value));
}
}
namedStringArguments = builder.ToImmutableAndFree();
}
return new SynthesizedAttributeData(ctorSymbol, arguments, namedArguments);
return new SynthesizedAttributeData(ctorSymbol, arguments, namedStringArguments);
}
internal SynthesizedAttributeData SynthesizeDecimalConstantAttribute(decimal value)
......@@ -309,7 +342,7 @@ internal SynthesizedAttributeData SynthesizeDecimalConstantAttribute(decimal val
var systemUnit32 = GetSpecialType(SpecialType.System_UInt32);
Debug.Assert(!systemUnit32.HasUseSiteError);
return SynthesizeAttribute(
return TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_DecimalConstantAttribute__ctor,
ImmutableArray.Create(
new TypedConstant(systemByte, TypedConstantKind.Primitive, decimalBits.Scale),
......@@ -327,7 +360,7 @@ internal SynthesizedAttributeData SynthesizeDebuggerBrowsableNeverAttribute()
return null;
}
return SynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerBrowsableAttribute__ctor,
return TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerBrowsableAttribute__ctor,
ImmutableArray.Create(new TypedConstant(
GetWellKnownType(WellKnownType.System_Diagnostics_DebuggerBrowsableState),
TypedConstantKind.Enum,
......@@ -401,7 +434,7 @@ internal SynthesizedAttributeData SynthesizeDebuggableAttribute()
var typedConstantDebugMode = new TypedConstant(debuggingModesType, TypedConstantKind.Enum, constantVal);
return SynthesizeAttribute(
return TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggableAttribute__ctorDebuggingModes,
ImmutableArray.Create(typedConstantDebugMode));
}
......@@ -418,7 +451,7 @@ internal SynthesizedAttributeData SynthesizeDynamicAttribute(TypeSymbol type, in
if (type.IsDynamic() && refKindOpt == RefKind.None && customModifiersCount == 0)
{
return SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_DynamicAttribute__ctor);
return TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_DynamicAttribute__ctor);
}
else
{
......@@ -427,7 +460,7 @@ internal SynthesizedAttributeData SynthesizeDynamicAttribute(TypeSymbol type, in
var transformFlags = DynamicTransformsEncoder.Encode(type, booleanType, customModifiersCount, refKindOpt);
var boolArray = new ArrayTypeSymbol(booleanType.ContainingAssembly, booleanType, customModifiers: ImmutableArray<CustomModifier>.Empty);
var arguments = ImmutableArray.Create<TypedConstant>(new TypedConstant(boolArray, transformFlags));
return SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_DynamicAttribute__ctorTransformFlags, arguments);
return TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_DynamicAttribute__ctorTransformFlags, arguments);
}
}
......
......@@ -1542,10 +1542,12 @@ internal IEnumerable<Cci.SecurityAttribute> GetSecurityAttributes()
var typedConstantTrue = new TypedConstant(boolType, TypedConstantKind.Primitive, value: true);
var attribute = compilation.SynthesizeAttribute(
var attribute = compilation.TrySynthesizeAttribute(
WellKnownMember.System_Security_Permissions_SecurityPermissionAttribute__ctor,
ImmutableArray.Create(typedConstantRequestMinimum),
ImmutableArray.Create(new KeyValuePair<string, TypedConstant>("SkipVerification", typedConstantTrue)));
ImmutableArray.Create(new KeyValuePair<WellKnownMember, TypedConstant>(
WellKnownMember.System_Security_Permissions_SecurityPermissionAttribute__SkipVerification,
typedConstantTrue)));
if (attribute != null)
{
......@@ -1639,7 +1641,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
{
// No need to check if [Extension] attribute was explicitly set since
// we'll issue CS1112 error in those cases and won't generate IL.
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_ExtensionAttribute__ctor));
}
......@@ -1663,7 +1665,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
var typedConstantNoStringInterning = new TypedConstant(int32Type, TypedConstantKind.Primitive, Cci.Constants.CompilationRelaxations_NoStringInterning);
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilationRelaxationsAttribute__ctorInt32,
ImmutableArray.Create(typedConstantNoStringInterning)));
}
......@@ -1682,10 +1684,12 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
var typedConstantTrue = new TypedConstant(boolType, TypedConstantKind.Primitive, value: true);
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_RuntimeCompatibilityAttribute__ctor,
ImmutableArray<TypedConstant>.Empty,
ImmutableArray.Create(new KeyValuePair<string, TypedConstant>("WrapNonExceptionThrows", typedConstantTrue))));
ImmutableArray.Create(new KeyValuePair<WellKnownMember, TypedConstant>(
WellKnownMember.System_Runtime_CompilerServices_RuntimeCompatibilityAttribute__WrapNonExceptionThrows,
typedConstantTrue))));
}
}
......@@ -1713,7 +1717,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
Debug.Assert(!stringType.HasUseSiteError, "Use site errors should have been checked ahead of time (type string).");
var typedConstant = new TypedConstant(stringType, TypedConstantKind.Primitive, compilation.Options.CryptoKeyContainer);
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyNameAttribute__ctor, ImmutableArray.Create(typedConstant)));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyNameAttribute__ctor, ImmutableArray.Create(typedConstant)));
}
if (!String.IsNullOrEmpty(compilation.Options.CryptoKeyFile) &&
......@@ -1723,7 +1727,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
Debug.Assert(!stringType.HasUseSiteError, "Use site errors should have been checked ahead of time (type string).");
var typedConstant = new TypedConstant(stringType, TypedConstantKind.Primitive, compilation.Options.CryptoKeyFile);
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyFileAttribute__ctor, ImmutableArray.Create(typedConstant)));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyFileAttribute__ctor, ImmutableArray.Create(typedConstant)));
}
}
}
......
......@@ -51,7 +51,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
base.AddSynthesizedAttributes(compilationState, ref attributes);
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
// Dev11 doesn't synthesize this attribute, the debugger has a knowledge
// of special name C# compiler uses for backing fields, which is not desirable.
......
......@@ -41,7 +41,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
var intType = compilation.GetSpecialType(SpecialType.System_Int32);
var item1 = new TypedConstant(systemType, TypedConstantKind.Type, ((PointerTypeSymbol)this.Type).PointedAtType);
var item2 = new TypedConstant(intType, TypedConstantKind.Primitive, this.FixedSize);
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_FixedBufferAttribute__ctor,
ImmutableArray.Create<TypedConstant>(item1, item2)));
}
......@@ -197,7 +197,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
{
base.AddSynthesizedAttributes(compilationState, ref attributes);
var compilation = ContainingSymbol.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_UnsafeValueTypeAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_UnsafeValueTypeAttribute__ctor));
}
public override IEnumerable<string> MemberNames
......
......@@ -975,7 +975,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
// we'll issue CS1112 error in those cases and won't generate IL.
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_ExtensionAttribute__ctor));
}
}
......
......@@ -1526,7 +1526,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
var arg = new TypedConstant(compilation.GetWellKnownType(WellKnownType.System_Type), TypedConstantKind.Type, stateMachineType.GetUnboundGenericTypeOrSelf());
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(ctor, ImmutableArray.Create(arg)));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(ctor, ImmutableArray.Create(arg)));
}
}
}
......
......@@ -513,7 +513,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
// NOTE: GlobalAttrBind::EmitCompilerGeneratedAttrs skips attribute if the well-known type isn't available.
if (!(compilation.GetWellKnownType(WellKnownType.System_Security_UnverifiableCodeAttribute) is MissingMetadataTypeSymbol))
{
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Security_UnverifiableCodeAttribute__ctor));
}
}
......
......@@ -1048,7 +1048,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
{
// No need to check if [Extension] attribute was explicitly set since
// we'll issue CS1112 error in those cases and won't generate IL.
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_ExtensionAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_ExtensionAttribute__ctor));
}
if (this.Indexers.Any())
......@@ -1056,7 +1056,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
string defaultMemberName = this.Indexers.First().MetadataName; // UNDONE: IndexerNameAttribute
var defaultMemberNameConstant = new TypedConstant(compilation.GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, defaultMemberName);
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Reflection_DefaultMemberAttribute__ctor,
ImmutableArray.Create(defaultMemberNameConstant)));
}
......
......@@ -65,7 +65,7 @@ internal sealed override void AddSynthesizedAttributes(ModuleCompilationState co
if (this.IsParams)
{
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_ParamArrayAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_ParamArrayAttribute__ctor));
}
// Synthesize DecimalConstantAttribute if we don't have an explicit custom attribute already:
......
......@@ -538,7 +538,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
if (isAutoPropertyAccessor)
{
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
}
}
}
......
......@@ -93,7 +93,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
// if this happens for whatever reason, we do not need "CompilerGenerated" anyways
Debug.Assert(compilation != null, "SynthesizedClass is not contained in a source module?");
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
}
......
......@@ -80,7 +80,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
base.AddSynthesizedAttributes(compilationState, ref attributes);
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
}
protected override object MethodChecksLockObject
......
......@@ -46,7 +46,7 @@ internal override void AddSynthesizedAttributes(ModuleCompilationState compilati
CSharpCompilation compilation = this.DeclaringCompilation;
// Assume that someone checked earlier that the attribute ctor is available and has no use-site errors.
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor));
// TODO (tomat): do we need to emit dynamic attribute on any synthesized field?
if (this.Type.ContainsDynamic())
......
......@@ -88,7 +88,7 @@ internal sealed override void AddSynthesizedAttributes(ModuleCompilationState co
if (debuggerHidden)
{
var compilation = this.DeclaringCompilation;
AddSynthesizedAttribute(ref attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor));
AddSynthesizedAttribute(ref attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor));
}
if (this.ReturnType.ContainsDynamic())
......
......@@ -1736,5 +1736,48 @@ static int Main()
// error CS0656: Missing compiler required member 'System.String.Format'
Diagnostic(ErrorCode.ERR_MissingPredefinedMember).WithArguments("System.String", "Format"));
}
[Fact]
[WorkItem(1024401)]
public void DebuggerDisplayAttributeWithNoTypeMember()
{
var src = @"
class Program
{
static void Main()
{
var v1 = new { Test1 = 1 };
}
}";
var comp = CreateCompilationWithMscorlib(src,
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication, optimizationLevel: OptimizationLevel.Debug));
// Expect both attributes when using a standard corlib
Action<ModuleSymbol> validator = m =>
{
var anonType = m.GlobalNamespace.GetMembers().Where(
sym => sym.Name.Contains("AnonymousType")).Single();
AssertEx.SetEqual(
anonType.GetAttributes().Select(attr => attr.AttributeClass),
comp.GetWellKnownType(WellKnownType.System_Diagnostics_DebuggerDisplayAttribute),
comp.GetWellKnownType(WellKnownType.System_Runtime_CompilerServices_CompilerGeneratedAttribute));
};
CompileAndVerify(comp, symbolValidator: validator);
// Expect no DebuggerDisplay when the member is missing
comp.MakeMemberMissing(WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__Type);
validator = m =>
{
var anonType = m.GlobalNamespace.GetMembers().Where(
sym => sym.Name.Contains("AnonymousType")).Single();
Assert.True(anonType.GetAttributes().Single().IsTargetAttribute(
"System.Runtime.CompilerServices",
"CompilerGeneratedAttribute"));
};
CompileAndVerify(comp, symbolValidator: validator);
}
}
}
\ No newline at end of file
......@@ -157,7 +157,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Emit.NoPia
Protected Overrides Function CreateCompilerGeneratedAttribute() As VisualBasicAttributeData
Debug.Assert(WellKnownMembers.IsSynthesizedAttributeOptional(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
Dim compilation = TypeManager.ModuleBeingBuilt.Compilation
Return compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor)
Return compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor)
End Function
Protected Overrides Function CreateTypeIdentifierAttribute(hasGuid As Boolean, syntaxNodeOpt As VisualBasicSyntaxNode, diagnostics As DiagnosticBag) As VisualBasicAttributeData
......
......@@ -125,7 +125,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Emit
End Sub
Friend NotOverridable Overrides Function SynthesizeAttribute(attributeConstructor As WellKnownMember) As Cci.ICustomAttribute
Return Me.Compilation.SynthesizeAttribute(attributeConstructor)
Return Me.Compilation.TrySynthesizeAttribute(attributeConstructor)
End Function
Friend NotOverridable Overrides Function GetSourceAssemblyAttributes() As IEnumerable(Of Cci.ICustomAttribute)
......
......@@ -219,7 +219,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim compilation = Me.DeclaringCompilation
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
' Dev11 emits DebuggerNonUserCode. We emit DebuggerHidden to hide the method even if JustMyCode is off.
AddSynthesizedAttribute(attributes, compilation.SynthesizeDebuggerHiddenAttribute())
......
......@@ -304,7 +304,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
WellKnownMembers.IsSynthesizedAttributeOptional(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
AddSynthesizedAttribute(attributes,
compilation.SynthesizeAttribute(
compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
End Sub
......
......@@ -77,7 +77,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
WellKnownMembers.IsSynthesizedAttributeOptional(
WellKnownMember.System_Diagnostics_DebuggerNonUserCodeAttribute__ctor))
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
End If
If (Me._debugAttributes And DebugAttributes.DebuggerHiddenAttribute) <> 0 Then
......
......@@ -34,10 +34,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
ReportErrorOnSymbol(System_IAsyncResult, diagnostics, hasErrors)
ReportErrorOnSymbol(System_AsyncCallback, diagnostics, hasErrors)
ReportErrorOnSymbol(System_MulticastDelegate, diagnostics, hasErrors)
ReportErrorOnWellKnownMember(System_Diagnostics_DebuggerDisplayAttribute__Type,
WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__Type,
diagnostics, hasErrors, vbEmbedRuntime)
End If
If hasClass Then
......
......@@ -174,15 +174,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
MyBase.AddSynthesizedAttributes(compilationState, attributes)
' Attribute: System.Runtime.CompilerServices.CompilerGeneratedAttribute()
AddSynthesizedAttribute(attributes, Manager.Compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, Manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
' Attribute: System.Diagnostics.DebuggerDisplayAttribute("<generated method>",Type := "<generated method>")
Dim value As New TypedConstant(Manager.System_String, TypedConstantKind.Primitive, "<generated method>")
AddSynthesizedAttribute(attributes, Manager.Compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, Manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__ctor,
ImmutableArray.Create(value),
ImmutableArray.Create(New KeyValuePair(Of String, TypedConstant)("Type", value))))
ImmutableArray.Create(New KeyValuePair(Of WellKnownMember, TypedConstant)(
WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__Type, value))))
End Sub
End Class
......
......@@ -144,7 +144,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
MyBase.AddSynthesizedAttributes(compilationState, attributes)
' Attribute: System.Runtime.CompilerServices.CompilerGeneratedAttribute()
AddSynthesizedAttribute(attributes, Manager.Compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, Manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
' VB emits this attribute regardless of /debug settings (unlike C#, which only emits it for /debug:full)
......@@ -176,7 +176,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
sb.Append(", ...")
End If
Return Manager.Compilation.SynthesizeAttribute(
Return Manager.Compilation.TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__ctor,
ImmutableArray.Create(New TypedConstant(Manager.System_String, TypedConstantKind.Primitive, builder.ToStringAndFree())))
End Function
......
......@@ -1401,7 +1401,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim emitEmbeddedAttribute As Boolean = Me.DeclaringCompilation.EmbeddedSymbolManager.IsAnySymbolReferenced
If emitEmbeddedAttribute Then
AddSynthesizedAttribute(attributes, DeclaringCompilation.SynthesizeAttribute(WellKnownMember.Microsoft_VisualBasic_Embedded__ctor))
AddSynthesizedAttribute(attributes, DeclaringCompilation.TrySynthesizeAttribute(WellKnownMember.Microsoft_VisualBasic_Embedded__ctor))
End If
' Synthesize CompilationRelaxationsAttribute only if all the following requirements are met:
......@@ -1422,7 +1422,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Debug.Assert(int32Type.GetUseSiteErrorInfo() Is Nothing, "Use site errors should have been checked ahead of time (type int).")
Dim typedConstantNoStringInterning = New TypedConstant(int32Type, TypedConstantKind.Primitive, Cci.Constants.CompilationRelaxations_NoStringInterning)
AddSynthesizedAttribute(attributes, DeclaringCompilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, DeclaringCompilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilationRelaxationsAttribute__ctorInt32,
ImmutableArray.Create(typedConstantNoStringInterning)))
End If
......@@ -1440,10 +1440,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Debug.Assert(boolType.GetUseSiteErrorInfo() Is Nothing, "Use site errors should have been checked ahead of time (type bool).")
Dim typedConstantTrue = New TypedConstant(boolType, TypedConstantKind.Primitive, True)
AddSynthesizedAttribute(attributes, DeclaringCompilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, DeclaringCompilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_RuntimeCompatibilityAttribute__ctor,
ImmutableArray(Of TypedConstant).Empty,
ImmutableArray.Create(New KeyValuePair(Of String, TypedConstant)("WrapNonExceptionThrows", typedConstantTrue))))
ImmutableArray.Create(New KeyValuePair(Of WellKnownMember, TypedConstant)(
WellKnownMember.System_Runtime_CompilerServices_RuntimeCompatibilityAttribute__WrapNonExceptionThrows, typedConstantTrue))))
End If
End If
......@@ -1477,7 +1478,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim typedConstantDebugMode = New TypedConstant(int32Type, TypedConstantKind.Enum, CInt(debuggingMode))
AddSynthesizedAttribute(attributes, DeclaringCompilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, DeclaringCompilation.TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggableAttribute__ctorDebuggingModes,
ImmutableArray.Create(typedConstantDebugMode)))
End If
......@@ -1493,7 +1494,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Debug.Assert(stringType.GetUseSiteErrorInfo Is Nothing, "Use site errors should have been checked ahead of time (type string).")
Dim typedConstant = New TypedConstant(stringType, TypedConstantKind.Primitive, m_Compilation.Options.CryptoKeyContainer)
AddSynthesizedAttribute(attributes, m_Compilation.SynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyNameAttribute__ctor, ImmutableArray.Create(typedConstant)))
AddSynthesizedAttribute(attributes, m_Compilation.TrySynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyNameAttribute__ctor, ImmutableArray.Create(typedConstant)))
End If
If Not String.IsNullOrEmpty(m_Compilation.Options.CryptoKeyFile) AndAlso
......@@ -1502,7 +1503,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Debug.Assert(stringType.GetUseSiteErrorInfo Is Nothing, "Use site errors should have been checked ahead of time (type string).")
Dim typedConstant = New TypedConstant(stringType, TypedConstantKind.Primitive, m_Compilation.Options.CryptoKeyFile)
AddSynthesizedAttribute(attributes, m_Compilation.SynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyFileAttribute__ctor, ImmutableArray.Create(typedConstant)))
AddSynthesizedAttribute(attributes, m_Compilation.TrySynthesizeAttribute(WellKnownMember.System_Reflection_AssemblyKeyFileAttribute__ctor, ImmutableArray.Create(typedConstant)))
End If
End If
End Sub
......
......@@ -262,7 +262,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim compilation = Me.DeclaringCompilation
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_DateTimeConstantAttribute__ctor,
ImmutableArray.Create(
New TypedConstant(specialTypeInt64, TypedConstantKind.Primitive, attributeValue.Ticks))))
......
......@@ -1435,7 +1435,7 @@ lReportErrorOnTwoTokens:
If Not Me.HasSTAThreadOrMTAThreadAttribute Then
' UNDONE: UV Support: Do not emit if using the starlite libraries.
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_STAThreadAttribute__ctor))
End If
End If
......
......@@ -2402,7 +2402,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
' NOTE: used from emit, so shouldn't have gotten here if there were errors
Debug.Assert(stringType.GetUseSiteErrorInfo() Is Nothing)
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Reflection_DefaultMemberAttribute__ctor,
ImmutableArray.Create(
New TypedConstant(stringType, TypedConstantKind.Primitive, DefaultPropertyName))))
......@@ -2411,19 +2411,19 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
If Me.TypeKind = TypeKind.Module Then
'TODO check that there's not a user supplied instance already. This attribute is AllowMultiple:=False.
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.Microsoft_VisualBasic_CompilerServices_StandardModuleAttribute__ctor))
End If
If m_comClassData IsNot Nothing Then
If m_comClassData.ClassId IsNot Nothing Then
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_GuidAttribute__ctor,
ImmutableArray.Create(
New TypedConstant(GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, m_comClassData.ClassId))))
End If
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_ClassInterfaceAttribute__ctorClassInterfaceType,
ImmutableArray.Create(
New TypedConstant(GetSpecialType(SpecialType.System_Int32), TypedConstantKind.Enum, CInt(ClassInterfaceType.None)))))
......@@ -2444,7 +2444,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
eventInterfaceName = container1.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat) & "+" & eventInterfaceName
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_ComSourceInterfacesAttribute__ctorString,
ImmutableArray.Create(
New TypedConstant(GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, eventInterfaceName))))
......
......@@ -964,14 +964,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim id As String = If(m_IsEventInterface, m_ComClass.m_comClassData.EventId, m_ComClass.m_comClassData.InterfaceId)
If id IsNot Nothing Then
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_GuidAttribute__ctor,
ImmutableArray.Create(
New TypedConstant(m_ComClass.GetSpecialType(SpecialType.System_String), TypedConstantKind.Primitive, id))))
End If
If m_IsEventInterface Then
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_InterfaceTypeAttribute__ctorInt16,
ImmutableArray.Create(
New TypedConstant(m_ComClass.GetSpecialType(SpecialType.System_Int16),
......@@ -979,14 +979,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
CShort(ComInterfaceType.InterfaceIsIDispatch)))))
End If
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_ComVisibleAttribute__ctor,
ImmutableArray.Create(New TypedConstant(m_ComClass.GetSpecialType(SpecialType.System_Boolean),
TypedConstantKind.Primitive,
value:=True))))
If m_DefaultMemberName IsNot Nothing Then
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Reflection_DefaultMemberAttribute__ctor,
ImmutableArray.Create(New TypedConstant(m_ComClass.GetSpecialType(SpecialType.System_String),
TypedConstantKind.Primitive,
......@@ -1297,7 +1297,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Return
End If
AddSynthesizedAttribute(attributes, m_Interface.ComClass.DeclaringCompilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, m_Interface.ComClass.DeclaringCompilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_DispIdAttribute__ctor,
ImmutableArray.Create(New TypedConstant(m_Interface.ComClass.GetSpecialType(Microsoft.CodeAnalysis.SpecialType.System_Int32),
TypedConstantKind.Primitive,
......@@ -1787,7 +1787,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Return
End If
AddSynthesizedAttribute(attributes, m_Interface.ComClass.DeclaringCompilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, m_Interface.ComClass.DeclaringCompilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_InteropServices_DispIdAttribute__ctor,
ImmutableArray.Create(New TypedConstant(m_Interface.ComClass.GetSpecialType(Microsoft.CodeAnalysis.SpecialType.System_Int32),
TypedConstantKind.Primitive,
......
......@@ -41,7 +41,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
' Create the ParamArrayAttribute
If IsParamArray AndAlso Not HasParamArrayAttribute Then
Dim compilation = Me.DeclaringCompilation
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_ParamArrayAttribute__ctor))
End If
......@@ -54,7 +54,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Select Case defaultValue.SpecialType
Case SpecialType.System_DateTime
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_DateTimeConstantAttribute__ctor,
ImmutableArray.Create(New TypedConstant(compilation.GetSpecialType(SpecialType.System_Int64),
TypedConstantKind.Primitive,
......
......@@ -496,7 +496,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim compilation = DeclaringCompilation
AddSynthesizedAttribute(attributes,
compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
' Dev11 adds DebuggerNonUserCode; there is no reason to do so since:
' - we emit no debug info for the body
......
......@@ -61,12 +61,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Debug.Assert(Not Me.ContainingType.IsImplicitlyDeclared)
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
AddSynthesizedAttribute(attributes, compilation.SynthesizeDebuggerBrowsableNeverAttribute())
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_AccessedThroughPropertyAttribute__ctor,
ImmutableArray.Create(New TypedConstant(compilation.GetSpecialType(SpecialType.System_String),
TypedConstantKind.Primitive,
......
......@@ -492,7 +492,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Debug.Assert(Not ContainingType.IsImplicitlyDeclared)
Dim compilation = Me.DeclaringCompilation
AddSynthesizedAttribute(attributes,
compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
' Dev11 adds DebuggerNonUserCode; there is no reason to do so since:
' - we emit no debug info for the body
......
......@@ -68,7 +68,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
End If
' Attribute: System.Runtime.CompilerServices.CompilerGeneratedAttribute()
AddSynthesizedAttribute(attributes, sourceType.DeclaringCompilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, sourceType.DeclaringCompilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
End Sub
......
......@@ -79,7 +79,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
MyBase.AddSynthesizedAttributes(compilationState, attributes)
AddSynthesizedAttribute(attributes,
DeclaringCompilation.SynthesizeAttribute(WellKnownMember.System_STAThreadAttribute__ctor))
DeclaringCompilation.TrySynthesizeAttribute(WellKnownMember.System_STAThreadAttribute__ctor))
End Sub
Friend Overrides ReadOnly Property GenerateDebugInfoImpl As Boolean
......
......@@ -89,7 +89,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Debug.Assert(Not ContainingType.IsImplicitlyDeclared)
Dim compilation = Me.DeclaringCompilation
AddSynthesizedAttribute(attributes,
compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
' Dev11 adds DebuggerNonUserCode; there is no reason to do so since:
' - we emit no debug info for the body
......
......@@ -126,7 +126,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
' Dev11 only synthesizes these attributes for backing field of auto-property, not for Events or WithEvents.
If Not Me.ContainingType.IsImplicitlyDeclared Then
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
End If
......
......@@ -182,7 +182,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim compilation = Me.DeclaringCompilation
AddSynthesizedAttribute(attributes, compilation.SynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
AddSynthesizedAttribute(attributes, compilation.TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
AddSynthesizedAttribute(attributes, compilation.SynthesizeDebuggerHiddenAttribute())
End Sub
......
......@@ -109,7 +109,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
End If
' Attribute: System.Runtime.CompilerServices.CompilerGeneratedAttribute()
AddSynthesizedAttribute(attributes, sourceType.DeclaringCompilation.SynthesizeAttribute(
AddSynthesizedAttribute(attributes, sourceType.DeclaringCompilation.TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor))
End Sub
......
......@@ -124,16 +124,60 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return DirectCast(m_LazyExtensionAttributeConstructor, MethodSymbol)
End Function
''' <summary>
''' Synthesizes a custom attribute.
''' Returns null if the <paramref name="constructor"/> symbol is missing and the attribute is synthesized only if present.
''' </summary>
Friend Function SynthesizeAttribute(constructor As WellKnownMember,
Optional arguments As ImmutableArray(Of TypedConstant) = Nothing,
Optional namedArguments As ImmutableArray(Of KeyValuePair(Of String, TypedConstant)) = Nothing) As SynthesizedAttributeData
Dim memberSymbol = TryCast(GetWellKnownTypeMember(constructor), MethodSymbol)
Return SynthesizedAttributeData.Create(memberSymbol, constructor, arguments, namedArguments)
''' <summary>
''' Synthesizes a custom attribute.
''' Returns null if the <paramref name="constructor"/> symbol is missing,
''' or any of the members in <paramref name="namedArguments" /> are missing.
''' The attribute is synthesized only if present.
''' </summary>
''' <param name="namedArguments">
''' Takes a list of pairs of well-known members and constants. The constants
''' will be passed to the field/property referenced by the well-known member.
''' If the well-known member does Not exist in the compilation then no attribute
''' will be synthesized.
''' </param>
Friend Function TrySynthesizeAttribute(
constructor As WellKnownMember,
Optional arguments As ImmutableArray(Of TypedConstant) = Nothing,
Optional namedArguments As ImmutableArray(Of KeyValuePair(Of WellKnownMember, TypedConstant)) = Nothing) As SynthesizedAttributeData
Dim constructorSymbol = TryCast(GetWellKnownTypeMember(constructor), MethodSymbol)
If constructorSymbol Is Nothing OrElse
Binder.GetUseSiteErrorForWellKnownTypeMember(constructorSymbol, constructor, False) IsNot Nothing Then
Return ReturnNothingOrThrowIfAttributeNonOptional(constructor)
End If
If arguments.IsDefault Then
arguments = ImmutableArray(Of TypedConstant).Empty
End If
Dim namedStringArguments As ImmutableArray(Of KeyValuePair(Of String, TypedConstant))
If namedArguments.IsDefault Then
namedStringArguments = ImmutableArray(Of KeyValuePair(Of String, TypedConstant)).Empty
Else
Dim builder = New ArrayBuilder(Of KeyValuePair(Of String, TypedConstant))(namedArguments.Length)
For Each arg In namedArguments
Dim wellKnownMember = GetWellKnownTypeMember(arg.Key)
If wellKnownMember Is Nothing OrElse
TypeOf wellKnownMember Is ErrorTypeSymbol OrElse
Binder.GetUseSiteErrorForWellKnownTypeMember(wellKnownMember, arg.Key, False) IsNot Nothing Then
Return ReturnNothingOrThrowIfAttributeNonOptional(constructor)
Else
builder.Add(New KeyValuePair(Of String, TypedConstant)(wellKnownMember.Name, arg.Value))
End If
Next
namedStringArguments = builder.ToImmutableAndFree()
End If
Return New SynthesizedAttributeData(constructorSymbol, arguments, namedStringArguments)
End Function
Private Function ReturnNothingOrThrowIfAttributeNonOptional(constructor As WellKnownMember) As SynthesizedAttributeData
If WellKnownMembers.IsSynthesizedAttributeOptional(constructor) Then
Return Nothing
Else
Throw ExceptionUtilities.Unreachable
End If
End Function
Friend Function SynthesizeExtensionAttribute() As SynthesizedAttributeData
......@@ -162,7 +206,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
TypedConstantKind.Type,
If(stateMachineType.IsGenericType, stateMachineType.ConstructUnboundGenericType(), stateMachineType))
Return SynthesizeAttribute(ctor, ImmutableArray.Create(arg))
Return TrySynthesizeAttribute(ctor, ImmutableArray.Create(arg))
End If
Return Nothing
......@@ -177,7 +221,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim specialTypeUInt32 = GetSpecialType(SpecialType.System_UInt32)
Debug.Assert(specialTypeUInt32.GetUseSiteErrorInfo() Is Nothing)
Return SynthesizeAttribute(
Return TrySynthesizeAttribute(
WellKnownMember.System_Runtime_CompilerServices_DecimalConstantAttribute__ctor,
ImmutableArray.Create(
New TypedConstant(specialTypeByte, TypedConstantKind.Primitive, decimalData.scale),
......@@ -193,7 +237,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return Nothing
End If
Return SynthesizeAttribute(
Return TrySynthesizeAttribute(
WellKnownMember.System_Diagnostics_DebuggerBrowsableAttribute__ctor,
ImmutableArray.Create(New TypedConstant(GetWellKnownType(WellKnownType.System_Diagnostics_DebuggerBrowsableState),
TypedConstantKind.Enum,
......@@ -205,11 +249,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return Nothing
End If
Return SynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor)
Return TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor)
End Function
Friend Function SynthesizeEditorBrowsableNeverAttribute() As SynthesizedAttributeData
Return SynthesizeAttribute(
Return TrySynthesizeAttribute(
WellKnownMember.System_ComponentModel_EditorBrowsableAttribute__ctor,
ImmutableArray.Create(New TypedConstant(GetWellKnownType(WellKnownType.System_ComponentModel_EditorBrowsableState),
TypedConstantKind.Enum,
......@@ -221,7 +265,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return Nothing
End If
Return SynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerNonUserCodeAttribute__ctor)
Return TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerNonUserCodeAttribute__ctor)
End Function
Friend Function SynthesizeOptionalDebuggerStepThroughAttribute() As SynthesizedAttributeData
......@@ -233,7 +277,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
WellKnownMembers.IsSynthesizedAttributeOptional(
WellKnownMember.System_Diagnostics_DebuggerStepThroughAttribute__ctor))
Return SynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerStepThroughAttribute__ctor)
Return TrySynthesizeAttribute(WellKnownMember.System_Diagnostics_DebuggerStepThroughAttribute__ctor)
End Function
#End Region
......
......@@ -14,6 +14,49 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.AnonymousDelegates
Public Class CreationAndEmit : Inherits BasicTestBase
<Fact>
<WorkItem(1024401)>
Public Sub DebuggerDisplayAttributeWithNoTypeMember()
Dim src = "
Module Test
Sub Main()
Dim x = Function(y) y + 1
End Sub
End Module"
Dim comp = CreateVisualBasicCompilation(src)
' Expect both attributes with a normal corlib
Dim validator As Action(Of ModuleSymbol) =
Sub(m As ModuleSymbol)
Dim anonDelegate = (From sym In m.GlobalNamespace.GetMembers()
Where sym.Name.Contains("AnonymousDelegate")).Single()
Dim expected =
{comp.GetWellKnownType(WellKnownType.System_Diagnostics_DebuggerDisplayAttribute),
comp.GetWellKnownType(WellKnownType.System_Runtime_CompilerServices_CompilerGeneratedAttribute)}
Dim actual = From attribute In anonDelegate.GetAttributes()
Select attribute.AttributeClass
AssertEx.SetEqual(actual, expected)
End Sub
CompileAndVerify(comp, symbolValidator:=validator)
' Expect no DebuggerDisplay with the type missing
comp.MakeMemberMissing(WellKnownMember.System_Diagnostics_DebuggerDisplayAttribute__Type)
validator =
Sub(m As ModuleSymbol)
Dim anonDelegate = (From sym In m.GlobalNamespace.GetMembers()
Where sym.Name.Contains("AnonymousDelegate")).Single()
Assert.True(anonDelegate.GetAttributes().Single().IsTargetAttribute(
"System.Runtime.CompilerServices",
"CompilerGeneratedAttribute"))
End Sub
CompileAndVerify(comp, symbolValidator:=validator)
End Sub
<Fact>
Public Sub InferenceMergeEmit1()
Dim compilationDef =
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册