You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ILC emits false-positive trim/AOT warnings when [FeatureSwitchDefinition] property is combined with || #129296
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 warningsif(FeatureSwitch){Safe();}else{Unsafe();}// False-positive IL2026/IL3050 on Unsafe()if(FeatureSwitch||RuntimeCheck()){Safe();}else{Unsafe();}
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.
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:
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:
publicstaticvoidDoWork(){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.
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.
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.
Activity
dotnet-policy-service commentedon Jun 11, 2026
Tagging subscribers to this area: @agocke, @dotnet/illink
See info in area-owners.md if you want to be subscribed.
MichalStrehovsky commentedon Jun 12, 2026
When I publish the App_OrPattern_SwitchOn project, I see this:
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 warningsuppression. 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:
The first warning is again from the analyzer. Next there is the error from ILC.
So the solution is to
#pragma suppressin CoreLib. Pragma suppressions are only active when building the source, sopublishwill still error out.sbomer commentedon Jun 22, 2026
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 changeApp_SwitchOnto use a ProjectReference you'll see a similar build error in CoreLib: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:
The IL4000 is there to tell you that it's your responsibility to make sure
UseLegacyPathis always false in a trimmed app.Then you can call it like this without suppressions at the use site:
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.
dotnet-policy-service commentedon Jun 22, 2026
This issue has been marked
needs-author-actionand may be missing some important information.dotnet-policy-service commentedon Jun 23, 2026
This issue has been marked
needs-author-actionand may be missing some important information.dotnet-policy-service commentedon Oct 14, 2026
This issue has been automatically marked
no-recent-activitybecause 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 removeno-recent-activity.dotnet-policy-service commentedon Nov 16, 2026
This issue will now be closed since it had been marked
no-recent-activitybut 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.