Ticket Created
over 2 years ago

CIQTOOLS-128

Unused new expressions are discarded

Given:

    var wasCalled as Boolean = false;
    class C {
        function initialize(logger as Logger) {
            logger.debug("This should appear");
            wasCalled = true;
        }
    }

    (:test)
    function test(logger as Logger) as Boolean {
        new C(logger);
        return wasCalled;
    }

The code compiles with no warnings or errors with strict type checking enabled, but the test fails. If I change it to "var c = new C(logger);" instead, it passes.

It looks like the compiler completely ignores the "new C". I get the same behavior with every recent sdk, including 4.1.5, 4.1.6 and 4.2.0-beta (and with the last two, at any optimization level).

EDIT: There's more strange behavior. If I add a public foo() method to C, and a public x as Number:

(new C(logger)).foo(); // works as expected. A C is constructed, and foo is called
(new C(logger)).x; // compiles, but C is not constructed
(new C(logger)).x++; // syntax error
++(new C(logger)).x; // syntax error
(new C(logger)).x = 42; // syntax error

EDIT2: Sorry, too many moving parts.

It turns out that (new C(logger)).x does construct a C in 4.1.5, but doesn't in 4.1.6 or 4.2.0-beta (unless you compile at -O0)

Parents Comment Children
  • Can you point me to it, I can't seem to find it...

    I filed a bug months ago about issues compiling certain top level expressions, and there are similar issues for things like "x && foo();" which ought to be shorthand for "if (x) { foo(); }" but which actually tends to fail to compile (I've not checked if any of those were fixed recently).

    I also see that '[1, 2, foo(), 3]' as a top-level expression seems to get thrown away entirely (ie foo() does not get called). I guess the compiler decides that constructing the array is pointless, but forgets that the elements might have side effects. Maybe this is what the other issue referred to?