custom operator++ yields InvalidProgramException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis Zickefoose

    custom operator++ yields InvalidProgramException

    The following code yields error CS0131: "The left-hand side of an assignment
    must be a variable, property or indexer": (new int())++;

    However, when using a class with a custom operator++, an
    InvalidProgramE xception is thrown. I assume this is a compiler bug of some
    sort, and that error CS0131 should be raised instead.

    Here's a complete program that illustrates the issue:

    namespace ErrorTest
    {
    class Program
    {
    public Program(int _) { i = _; }

    public static Program operator ++(Program b) {
    Program tmp = new Program(b.i);
    tmp.i++;
    return tmp;
    }

    public int i;

    static void Main(string[] args) {
    (new Program(0))++;
    }
    }
    }

    Running PEVerify on the above code yields a stack underflow in Main at
    Offset 0x1. The dissassembly of Main is as follows:

    ..method private hidebysig static void Main(string[] args) cil managed
    {
    .entrypoint
    // Code size 7 (0x7)
    .maxstack 8
    IL_0000: nop
    IL_0001: call class ErrorTest.Progr am
    ErrorTest.Progr am::op_Incremen t(class ErrorTest.Progr am)
    IL_0006: ret
    } // end of method Program::Main

    This was all experienced using VC# 2005 Standard.

    DZJr
  • Tom Spink

    #2
    Re: custom operator++ yields InvalidProgramE xception

    Dennis Zickefoose wrote:
    The following code yields error CS0131: "The left-hand side of an
    assignment
    must be a variable, property or indexer": (new int())++;
    >
    However, when using a class with a custom operator++, an
    InvalidProgramE xception is thrown. I assume this is a compiler bug of
    some sort, and that error CS0131 should be raised instead.
    >
    Here's a complete program that illustrates the issue:
    >
    namespace ErrorTest
    {
    class Program
    {
    public Program(int _) { i = _; }
    >
    public static Program operator ++(Program b) {
    Program tmp = new Program(b.i);
    tmp.i++;
    return tmp;
    }
    >
    public int i;
    >
    static void Main(string[] args) {
    (new Program(0))++;
    }
    }
    }
    >
    Running PEVerify on the above code yields a stack underflow in Main at
    Offset 0x1. The dissassembly of Main is as follows:
    >
    .method private hidebysig static void Main(string[] args) cil managed
    {
    .entrypoint
    // Code size 7 (0x7)
    .maxstack 8
    IL_0000: nop
    IL_0001: call class ErrorTest.Progr am
    ErrorTest.Progr am::op_Incremen t(class ErrorTest.Progr am)
    IL_0006: ret
    } // end of method Program::Main
    >
    This was all experienced using VC# 2005 Standard.
    >
    DZJr
    Hi,

    It certainly looks like that, since there's no new object initialisation
    before the call to op_Increment, which means that the stack would be empty
    when the call to op_Increment was made.

    Of course, I must ask, why would you do such a thing?

    --
    Hope this helps,
    Tom Spink

    Google first, ask later.

    Comment

    • Dennis Zickefoose

      #3
      Re: custom operator++ yields InvalidProgramE xception

      That's a gross simplification of the original code, which involved a factory
      of sorts. Sometimes, I'm not interested in what the factory provides, but in
      the incremented version of what the factory provides. I thought I could just
      wrap it all in one line, but apparently ++ doesn't work that way. None of
      that really matters, though...I can work around it easily enough.

      I marked my original post as a question, but I'm not sure why. I really
      just wanted to bring the issue up, since it appears like the compiler is
      acting up and not emmitting an error when it's supposed to.

      DZJr

      Comment

      Working...