custom operator++ yields InvalidProgramException

G

Guest

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
InvalidProgramException 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.Program
ErrorTest.Program::blush:p_Increment(class ErrorTest.Program)
IL_0006: ret
} // end of method Program::Main

This was all experienced using VC# 2005 Standard.

DZJr
 
T

Tom Spink

Dennis said:
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
InvalidProgramException 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.Program
ErrorTest.Program::blush:p_Increment(class ErrorTest.Program)
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?
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Use of event handling 6
interface 1
same assembly size for debug and release 2
? 7
inheritance problem 2
Is it based up polymorphy or just inheritance? 2
run time error 3
tostring 3

Top