Skip to content

Trying to follow example provided on page but still getting "Trim analysis error IL2026" #26847

@perlun

Description

@perlun

Hi,

While preparing the migration to .NET 6 (RC2), I started seeing a bunch of IL2026 errors like these:

/home/per/git/perlang/src/Perlang.Interpreter/PerlangInterpreter.cs(120,34): Trim analysis error IL2026: Perlang.Interpreter.PerlangInterpreter.<>c.<RegisterGlobalClasses>b__16_0(Assembly): Using member 'System.Reflection.Assembly.GetTypes()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed. [/home/per/git/perlang/src/Perlang.ConsoleApp/Perlang.ConsoleApp.csproj]
/home/per/git/perlang/src/Perlang.Interpreter/PerlangInterpreter.cs(1010,21): Trim analysis error IL2026: Perlang.Interpreter.PerlangInterpreter.VisitBinaryExpr(Expr.Binary): Using member 'Microsoft.CSharp.RuntimeBinder.Binder.BinaryOperation(CSharpBinderFlags,ExpressionType,Type,IEnumerable<CSharpArgumentInfo>)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Using dynamic types might cause types or members to be removed by trimmer. [/home/per/git/perlang/src/Perlang.ConsoleApp/Perlang.ConsoleApp.csproj]

I found https://github.com/dotnet/designs/blob/main/accepted/2020/linking-libraries.md#trimming-compatibility and the examples on this page, but I am unable to get them working in my code; the trimming error still persists. What could I be doing wrong? 🤔 Here's the WIP PR which can be used to reproduce the problem: perlang-org/perlang#223.

(To perform the linking & trimming in that project, run make release on a Linux machine with` .NET 6 RC2 installed. The CI logs are also available in that PR in case anyone's interested.)

        /// <summary>
        /// Registers global classes defined in native .NET code.
        /// </summary>
        /// <exception cref="PerlangInterpreterException">Multiple classes with the same name was encountered.</exception>
        [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "Perlang.Stdlib is manually preserved")]
        private void RegisterGlobalClasses()
        {
            var globalClassesQueryable = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(a => a.GetTypes())
                .Select(t => new
                {
                    Type = t,
                    ClassAttribute = t.GetCustomAttribute<GlobalClassAttribute>()
                })
                .Where(t => t.ClassAttribute != null);

            // ...
        }

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions