Skip to content

Intrinsically handle Expression.Property(Expression,MethodInfo) #1814

@eerhardt

Description

@eerhardt

With the annotations applied in dotnet/runtime#47938, Expression.Property(Expression,MethodInfo) is being marked as RequiresUnreferencedCode. This is because the linker doesn't see the Property being used - only the getter (or setter). See discussion in dotnet/runtime#47938 (comment). Thus, the linker may trim the property metadata, or the other side of the property accessor.

Because of this decision, users will now get an ILLink warning for the following code:

IQueryable<Customer> customers = GetCustomers();
foreach (var cust in customers.Where(c => c.Name == "Bob"))
{
}
C:\DotNetTest\Net6Hello\Program.cs(10,34): Trim analysis warning IL2026: Net6Hello.Program.Main(String[]): Calling 'System.Linq.Expressions.Expression.Property(Expression,MethodInfo)' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. The Property metadata or other accessor may be trimmed.. [C:\DotNetTest\Net6Hello\Net6Hello.csproj]

The IL generated by the C# compiler to handle the above code looks like the following:

	𝐈𝐋_𝟎𝟎𝟑𝟑: 𝐥𝐝𝐭𝐨𝐤𝐞𝐧 𝐦𝐞𝐭𝐡𝐨𝐝 𝐢𝐧𝐬𝐭𝐚𝐧𝐜𝐞 𝐬𝐭𝐫𝐢𝐧𝐠 𝐂𝐮𝐬𝐭𝐨𝐦𝐞𝐫::𝐠𝐞𝐭_𝐍𝐚𝐦𝐞()
	𝐈𝐋_𝟎𝟎𝟑𝟖: 𝐜𝐚𝐥𝐥 𝐜𝐥𝐚𝐬𝐬 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐮𝐧𝐭𝐢𝐦𝐞]𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐞𝐟𝐥𝐞𝐜𝐭𝐢𝐨𝐧.𝐌𝐞𝐭𝐡𝐨𝐝𝐁𝐚𝐬𝐞 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐮𝐧𝐭𝐢𝐦𝐞]𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐞𝐟𝐥𝐞𝐜𝐭𝐢𝐨𝐧.𝐌𝐞𝐭𝐡𝐨𝐝𝐁𝐚𝐬𝐞::𝐆𝐞𝐭𝐌𝐞𝐭𝐡𝐨𝐝𝐅𝐫𝐨𝐦𝐇𝐚𝐧𝐝𝐥𝐞(𝐯𝐚𝐥𝐮𝐞𝐭𝐲𝐩𝐞 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐮𝐧𝐭𝐢𝐦𝐞]𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐮𝐧𝐭𝐢𝐦𝐞𝐌𝐞𝐭𝐡𝐨𝐝𝐇𝐚𝐧𝐝𝐥𝐞)
	𝐈𝐋_𝟎𝟎𝟑𝐝: 𝐜𝐚𝐬𝐭𝐜𝐥𝐚𝐬𝐬 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐮𝐧𝐭𝐢𝐦𝐞]𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐞𝐟𝐥𝐞𝐜𝐭𝐢𝐨𝐧.𝐌𝐞𝐭𝐡𝐨𝐝𝐈𝐧𝐟𝐨
	𝐈𝐋_𝟎𝟎𝟒𝟐: 𝐜𝐚𝐥𝐥 𝐜𝐥𝐚𝐬𝐬 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐋𝐢𝐧𝐪.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬]𝐒𝐲𝐬𝐭𝐞𝐦.𝐋𝐢𝐧𝐪.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬.𝐌𝐞𝐦𝐛𝐞𝐫𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐋𝐢𝐧𝐪.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬]𝐒𝐲𝐬𝐭𝐞𝐦.𝐋𝐢𝐧𝐪.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧::𝐏𝐫𝐨𝐩𝐞𝐫𝐭𝐲(𝐜𝐥𝐚𝐬𝐬 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐋𝐢𝐧𝐪.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬]𝐒𝐲𝐬𝐭𝐞𝐦.𝐋𝐢𝐧𝐪.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬.𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧, 𝐜𝐥𝐚𝐬𝐬 [𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐮𝐧𝐭𝐢𝐦𝐞]𝐒𝐲𝐬𝐭𝐞𝐦.𝐑𝐞𝐟𝐥𝐞𝐜𝐭𝐢𝐨𝐧.𝐌𝐞𝐭𝐡𝐨𝐝𝐈𝐧𝐟𝐨)

As you can see, the compiler grabs the metadata token for the get_Name getter, and then uses this to call Expression.Property(Expression,MethodInfo).

We should teach the linker about this pattern so users don't get this warning.

cc @vitek-karas @MichalStrehovsky

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions