Skip to content

Invalid IL when using ternaries #7

Description

@SurDno

A refactoring from an if/else to a ternary expression may cause the AssemblyPatcher to emit invalid IL. The generated ldftn operand retains a metadata token from what I assume is the temporary Roslyn-compiled assembly. At runtime this leads to the following exception:

InvalidProgramException: Invalid IL code in Mods.CustomScenarios.StartScenarioWindow:Fill (): IL_0087: ldftn     0x06009c75

The issue can be reproduced with following code (example from CustomScenarios mod):

           // **Following code works because we don't use ternaries:**
           // if (localizedName == techName)
           //     scenarioItem.SetName(scenario.Name);
           // else 
           //     scenarioItem.SetName(localizedName);

           // **This generates a runtime error:**
           scenarioItem.SetName(localizedName == techName ? scenario.Name : localizedName);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions