Skip to content

ILC emits false-positive trim/AOT warnings when [FeatureSwitchDefinition] property is combined with || #129296

Closed

Description

@Nigusu-Allehu

Description

When a [FeatureSwitchDefinition] property is the left operand of ||, ILC emits IL2026/IL3050 for the else branch even though it's correctly trimmed at publish time.

// No warnings
if (FeatureSwitch) { Safe(); } else { Unsafe(); }

// False-positive IL2026/IL3050 on Unsafe()
if (FeatureSwitch || RuntimeCheck()) { Safe(); } else { Unsafe(); }

Reproduction Steps

https://github.com/Nigusu-Allehu/TestFeatureSwitch

Expected behavior

I would expect constant folding in the if condition as the value of the runtime check does not matter if the feature switch is true.

Actual behavior

IL2026 and IL3050 warnings from the code path that should have been trimmed out

Regression?

IDK

Known Workarounds

expand the if condition

if (feature switch)
else
{
  if (runtime check)
  else
}

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Jun 11, 2026
  2. added
    area-Tools-ILLink
    on Jun 11, 2026
  3. dotnet-policy-service commented on Jun 11, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/illink
    See info in area-owners.md if you want to be subscribed.

  4. MichalStrehovsky commented on Jun 12, 2026

    @MichalStrehovsky
    Member

    When I publish the App_OrPattern_SwitchOn project, I see this:

    D:\Temp\testfs\App_OrPattern_SwitchOn>dotnet publish
    Restore complete (0.8s)
      CoreLib_OrPattern net9.0 succeeded with 1 warning(s) (0.3s) → D:\Temp\testfs\CoreLib_OrPattern\bin\Release\net9.0\CoreLib_OrPattern.dll
        D:\Temp\testfs\CoreLib_OrPattern\Worker.cs(21,13): warning IL2026: Using member 'CoreLib_OrPattern.Worker.LegacyWork()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Legacy path uses reflection.
      App_OrPattern_SwitchOn net9.0 win-x64 succeeded (6.8s) → bin\Release\net9.0\win-x64\publish\
    
    Build succeeded with 1 warning(s) in 8.5s
    

    The warning is coming from the trimming analyzer that runs when CoreLib_OrPattern project is built. When CoreLib_OrPattern project is built, the analyzer doesn't know what value for the feature will you specify at publish time. You can silence the warning with #pragma warning suppression. There is no warning for the App_OrPattern_SwitchOn project (if there was a warning, you have a WarningAsError for it in the project, this would be an error).

    When I publish the SwitchOff, I get:

    D:\Temp\testfs\App_OrPattern_SwitchOff>dotnet publish
    Restore complete (0.8s)
      CoreLib_OrPattern net9.0 succeeded with 1 warning(s) (0.3s) → D:\Temp\testfs\CoreLib_OrPattern\bin\Release\net9.0\CoreLib_OrPattern.dll
        D:\Temp\testfs\CoreLib_OrPattern\Worker.cs(21,13): warning IL2026: Using member 'CoreLib_OrPattern.Worker.LegacyWork()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Legacy path uses reflection.
      App_OrPattern_SwitchOff net9.0 win-x64 failed with 2 error(s) (7.3s) → bin\Release\net9.0\win-x64\App_OrPattern_SwitchOff.dll
        D:\Temp\testfs\CoreLib_OrPattern\Worker.cs(21): Trim analysis error IL2026: CoreLib_OrPattern.Worker.DoWork(): Using member 'CoreLib_OrPattern.Worker.LegacyWork()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Legacy path uses reflection.
        C:\Users\michals\.nuget\packages\microsoft.dotnet.ilcompiler\9.0.17\build\Microsoft.NETCore.Native.targets(317,5): error MSB3077: The command ""C:\Users\michals\.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompiler\9.0.17\tools\\ilc" @"obj\Release\net9.0\win-x64\native\App_OrPattern_SwitchOff.ilc.rsp"" exited with return value 0, but errors were detected during execution. ExitCode was set to -1.
    

    The first warning is again from the analyzer. Next there is the error from ILC.

    So the solution is to #pragma suppress in CoreLib. Pragma suppressions are only active when building the source, so publish will still error out.

  5. self-assigned this
    on Jun 22, 2026
  6. sbomer commented on Jun 22, 2026

    @sbomer
    Member

    Agreed, the behavior you see is not due to the ||, but due to the analyzer not knowing the feature switch values when building the library. If you change App_SwitchOn to use a ProjectReference you'll see a similar build error in CoreLib:

    CoreLib/Worker.cs(16,13): warning IL2026: Using member 'CoreLib.Worker.LegacyWork()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Legacy path uses reflection.
    

    Note that there is a way to teach the analyzer that a particular feature switch is expected to be disabled when trimming. You can do this:

    public static class FeatureSwitch
    {
        internal const string SwitchName = "CoreLib_OrPattern.UseLegacyPath";
    
        [FeatureSwitchDefinition(SwitchName)]
        [FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))]
    #pragma warning disable IL4000
        public static bool UseLegacyPath => AppContext.TryGetSwitch(SwitchName, out bool value) && value;
    #pragma warning restore IL4000
    }

    The IL4000 is there to tell you that it's your responsibility to make sure UseLegacyPath is always false in a trimmed app.

    Then you can call it like this without suppressions at the use site:

    public static void DoWork()
    {
        if (FeatureSwitch.UseLegacyPath)
        {
            LegacyWork();
        }
        else
        {
            Console.WriteLine("[CoreLib_OrPattern] Running NEW path (AOT safe).");
        }
    }

    There is also a related issue about trimming support for boolean expressions: #102830, but that's not what your repro is hitting. @Nigusu-Allehu does that help? Please let us know if you hit further issues.

  7. added
    needs-author-action
    on Jun 22, 2026
  8. dotnet-policy-service commented on Jun 22, 2026

    @dotnet-policy-service
    Contributor

    This issue has been marked needs-author-action and may be missing some important information.

  9. removed
    untriaged
    on Jun 23, 2026
  10. dotnet-policy-service commented on Jun 23, 2026

    @dotnet-policy-service
    Contributor

    This issue has been marked needs-author-action and may be missing some important information.

  11. added this to the Future milestone on Jun 26, 2026
  12. dotnet-policy-service commented on Oct 14, 2026

    @dotnet-policy-service
    Contributor

    This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

  13. dotnet-policy-service commented on Nov 16, 2026

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  14. removed this from the Future milestone on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tools-ILLinkneeds-author-actionno-recent-activity

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions