Skip to content

Conversation

@MichalStrehovsky
Copy link
Member

This should ensure the target members are kept when we're running the tests with trimming enabled.

Some tests use these attributes with framework types and framework is always considered trimmable. Example: https://github.com/dotnet/runtime/blob/aafe4d465f97fa3a0ae61b18c2887cf84085d25d/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Cancellation.cs#L15

Cc @dotnet/ilc-contrib

This should ensure the target members are kept when we're running the tests with trimming enabled.

Some tests use these attributes with framework types and framework is always considered trimmable. Example: https://github.com/dotnet/runtime/blob/aafe4d465f97fa3a0ae61b18c2887cf84085d25d/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.Cancellation.cs#L15
@MichalStrehovsky
Copy link
Member Author

@dotnet/ilc-contrib could someone have a look? This is just a couple DynamicallyAccessedMembers attributes.

These values are funneled to here if you want to check I got the MemberKinds right:

internal static Func<bool> LookupConditionalMember(Type t, string name)
{
if (t == null || name == null)
return null;
TypeInfo ti = t.GetTypeInfo();
MethodInfo mi = ti.GetDeclaredMethod(name);
if (mi != null && mi.IsStatic && mi.GetParameters().Length == 0 && mi.ReturnType == typeof(bool))
return () => (bool)mi.Invoke(null, null);
PropertyInfo pi = ti.GetDeclaredProperty(name);
if (pi != null && pi.PropertyType == typeof(bool) && pi.GetMethod != null && pi.GetMethod.IsStatic && pi.GetMethod.GetParameters().Length == 0)
return () => (bool)pi.GetValue(null);
FieldInfo fi = ti.GetDeclaredField(name);
if (fi != null && fi.FieldType == typeof(bool) && fi.IsStatic)
return () => (bool)fi.GetValue(null);
return LookupConditionalMember(ti.BaseType, name);
}

It's not possible to funnel/annotate this thorough because it passes through xUnit's reflection abstraction thingie. xUnit loves it's abstractions. We don't care about the dataflow warnings.

Comment on lines 10 to 12
internal const DynamicallyAccessedMemberTypes ConditionalMemberKinds = DynamicallyAccessedMemberTypes.PublicFields |
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicProperties;
Copy link
Member

Choose a reason for hiding this comment

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

Based on your description the code in the consumer calls TypeInfo.GetDeclaredMethod (for example):

That method is annotated with PublicMethods | NonPublicMethods so the annotation doesn't match. And to make things worse the code loops (recursively) over base types doing the same - so effectively private reflection on base types. The only annotation which supports that is All - so I think this should be All.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! Having GetMethod/GetDeclaredMethod differ in their visibilities was a top-notch decision on the API designer's part.

I saw it goes to the base type but I didn't want to bother with it because people's instinct is to spell out the type that has the property/method/field and I was worried about keeping too much. It does have side effects that are observable on MakeGeneric and we haven't done an exercise to get the framework RDC warning free yet and it might be useful to at least get the heavy hitters clean. But maybe All is better because then we don't need to introduce the const for it either. This is used rarely on framework types.

Copy link
Member

@vitek-karas vitek-karas left a comment

Choose a reason for hiding this comment

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

Lgtm

@MichalStrehovsky MichalStrehovsky merged commit 1832a15 into main Jul 14, 2022
@MichalStrehovsky MichalStrehovsky deleted the MichalStrehovsky-patch-1 branch July 14, 2022 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants