Skip to content

Commit eab62f9

Browse files
agockejtschuster
andauthored
Remove special logic for TypeConverterAttribute (#2659) (#2695)
(cherry picked from commit b10e1bc) Co-authored-by: Jackson Schuster <[email protected]>
1 parent e9cfb54 commit eab62f9

File tree

2 files changed

+48
-66
lines changed

2 files changed

+48
-66
lines changed

src/linker/Linker.Steps/MarkStep.cs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,6 @@ void MarkCustomAttributes (ICustomAttributeProvider provider, in DependencyInfo
819819
continue;
820820

821821
MarkCustomAttribute (ca, reason);
822-
MarkSpecialCustomAttributeDependencies (ca, provider);
823822
}
824823
}
825824

@@ -1570,7 +1569,6 @@ bool ProcessLateMarkedAttributes ()
15701569
markOccurred = true;
15711570
using (ScopeStack.PushScope (scope)) {
15721571
MarkCustomAttribute (customAttribute, reason);
1573-
MarkSpecialCustomAttributeDependencies (customAttribute, provider);
15741572
}
15751573
}
15761574

@@ -2070,30 +2068,10 @@ void MarkTypeSpecialCustomAttributes (TypeDefinition type)
20702068
if (MarkMethodsIf (type.Methods, MethodDefinitionExtensions.IsPublicInstancePropertyMethod, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, type)))
20712069
Tracer.AddDirectDependency (attribute, new DependencyInfo (DependencyKind.CustomAttribute, type), marked: false);
20722070
break;
2073-
case "TypeDescriptionProviderAttribute" when attrType.Namespace == "System.ComponentModel":
2074-
MarkTypeConverterLikeDependency (attribute, l => l.IsDefaultConstructor (), type);
2075-
break;
20762071
}
20772072
}
20782073
}
20792074

2080-
//
2081-
// Used for known framework attributes which can be applied to any element
2082-
//
2083-
bool MarkSpecialCustomAttributeDependencies (CustomAttribute ca, ICustomAttributeProvider provider)
2084-
{
2085-
var dt = ca.Constructor.DeclaringType;
2086-
if (dt.Name == "TypeConverterAttribute" && dt.Namespace == "System.ComponentModel") {
2087-
MarkTypeConverterLikeDependency (ca, l =>
2088-
l.IsDefaultConstructor () ||
2089-
l.Parameters.Count == 1 && l.Parameters[0].ParameterType.IsTypeOf ("System", "Type"),
2090-
provider);
2091-
return true;
2092-
}
2093-
2094-
return false;
2095-
}
2096-
20972075
void MarkMethodSpecialCustomAttributes (MethodDefinition method)
20982076
{
20992077
if (!method.HasCustomAttributes)
@@ -2116,34 +2094,6 @@ void MarkXmlSchemaProvider (TypeDefinition type, CustomAttribute attribute)
21162094
}
21172095
}
21182096

2119-
protected virtual void MarkTypeConverterLikeDependency (CustomAttribute attribute, Func<MethodDefinition, bool> predicate, ICustomAttributeProvider provider)
2120-
{
2121-
var args = attribute.ConstructorArguments;
2122-
if (args.Count < 1)
2123-
return;
2124-
2125-
TypeDefinition? typeDefinition = null;
2126-
switch (attribute.ConstructorArguments[0].Value) {
2127-
case string s:
2128-
if (!Context.TypeNameResolver.TryResolveTypeName (s, ScopeStack.CurrentScope.Origin.Provider, out TypeReference? typeRef, out AssemblyDefinition? assemblyDefinition))
2129-
break;
2130-
typeDefinition = Context.TryResolve (typeRef);
2131-
if (typeDefinition != null)
2132-
MarkingHelpers.MarkMatchingExportedType (typeDefinition, assemblyDefinition, new DependencyInfo (DependencyKind.CustomAttribute, provider));
2133-
2134-
break;
2135-
case TypeReference type:
2136-
typeDefinition = Context.Resolve (type);
2137-
break;
2138-
}
2139-
2140-
if (typeDefinition == null)
2141-
return;
2142-
2143-
Tracer.AddDirectDependency (attribute, new DependencyInfo (DependencyKind.CustomAttribute, provider), marked: false);
2144-
MarkMethodsIf (typeDefinition.Methods, predicate, new DependencyInfo (DependencyKind.ReferencedBySpecialAttribute, attribute));
2145-
}
2146-
21472097
static readonly Regex DebuggerDisplayAttributeValueRegex = new Regex ("{[^{}]+}", RegexOptions.Compiled);
21482098

21492099
void MarkTypeWithDebuggerDisplayAttribute (TypeDefinition type, CustomAttribute attribute)
Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,69 @@
1+
using System;
12
using System.ComponentModel;
3+
using System.Diagnostics.CodeAnalysis;
24
using Mono.Linker.Tests.Cases.Expectations.Assertions;
35
using Mono.Linker.Tests.Cases.Expectations.Metadata;
46

7+
58
namespace Mono.Linker.Tests.Cases.ComponentModel
69
{
7-
[TypeDescriptionProvider (typeof (CustomTDP))]
8-
9-
[Kept]
10-
[KeptAttributeAttribute (typeof (TypeDescriptionProviderAttribute))]
11-
class CustomTypeDescriptionProvider_1
10+
[Reference ("System.dll")]
11+
[ExpectedNoWarnings]
12+
public class TypeDescriptionProviderAttributeOnType
1213
{
13-
[Kept]
14-
public CustomTypeDescriptionProvider_1 ()
14+
public static void Main ()
1515
{
16+
var r1 = new CustomTypeDescriptionProvider_1 ();
17+
IInterface v = InterfaceTypeConverter.CreateVisual (typeof (System.String));
1618
}
1719

20+
[TypeDescriptionProvider (typeof (CustomTDP))]
1821
[Kept]
19-
[KeptBaseType (typeof (TypeDescriptionProvider))]
20-
class CustomTDP : TypeDescriptionProvider
22+
[KeptAttributeAttribute (typeof (TypeDescriptionProviderAttribute))]
23+
class CustomTypeDescriptionProvider_1
2124
{
2225
[Kept]
23-
public CustomTDP ()
26+
public CustomTypeDescriptionProvider_1 ()
2427
{
2528
}
29+
30+
[Kept]
31+
[KeptBaseType (typeof (TypeDescriptionProvider))]
32+
class CustomTDP : TypeDescriptionProvider
33+
{
34+
[Kept]
35+
public CustomTDP ()
36+
{
37+
}
38+
}
2639
}
27-
}
2840

29-
[Reference ("System.dll")]
30-
public class TypeDescriptionProviderAttributeOnType
31-
{
32-
public static void Main ()
41+
[Kept]
42+
[KeptAttributeAttribute (typeof (TypeConverterAttribute))]
43+
[TypeConverter (typeof (InterfaceTypeConverter))]
44+
public interface IInterface
45+
{ }
46+
47+
[Kept]
48+
[KeptBaseType (typeof (TypeConverter))]
49+
public class InterfaceTypeConverter : TypeConverter
3350
{
34-
var r1 = new CustomTypeDescriptionProvider_1 ();
51+
[Kept]
52+
public static IInterface CreateVisual (
53+
[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
54+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
55+
Type visualType)
56+
{
57+
try {
58+
return (IInterface) Activator.CreateInstance (visualType);
59+
} catch {
60+
}
61+
62+
return null;
63+
}
64+
65+
[Kept]
66+
public InterfaceTypeConverter () { }
3567
}
3668
}
3769
}

0 commit comments

Comments
 (0)