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

Trimmer-safety analyzer misses RUC members on base types kept through DAM attributes #3111

Open
jkoritzinsky opened this issue Nov 11, 2022 · 3 comments

Comments

@jkoritzinsky
Copy link
Member

Given the following code, the trimmer will issue a warning at trim time, but the Roslyn analyzer won't warn.

class Foo
{
	[RequiresUnreferencedCode("")]
     public static void Bar() {}
}

class Bar<T> : Foo
{
}


class Test
{
    public static void Method()
	{
		Method2(typeof(Bar<>)); // Warns here in the linker, but not in the analyzer
	}

	private static void Method2([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type) {}
}

We traced this down to the behavior in Roslyn around "unbound constructed generic types", or more specifically Bar<>, as compared to Bar<T>. Bar<T> is the original definition and has full fidelity in the Roslyn type system. However, Bar<> is considered an "unbound constructed generic" and as a result has a few limitations in the Roslyn type system.

These limitations cause this particular case to be missed by the analyzer built on top of the Roslyn type system, whereas the Cecil type system captures this case.

@vitek-karas
Copy link
Member

Do you know if there are ways to solve this in the analyzer? The docs simply say that BaseType is unavailable on such symbol - but doesn't say what to do about it :-(.

@sbomer
Copy link
Member

sbomer commented Nov 14, 2022

@agocke was suggesting to use the generic type definition (not sure if I'm using the right terminology for Roslyn) in places where we encounter unbound generic types instead.

@jkoritzinsky
Copy link
Member Author

The idea from @agocke would be to have something like the following:

// Might be worth using ConstructedFrom instead of OriginalDefinition, not sure.
type = type.IsUnboundGenericType ? type.OriginalDefinition : type;

@agocke agocke added this to AppModel Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants