Skip to content

[js] Analyzer fails to optimize constant conditional inside abstract when StringBuf is used #12466

@barisyild

Description

@barisyild

The analyzer optimizer fails to fold a constant conditional expression inside an abstract type when StringBuf is used in the same function. The condition this == 0 where this is known to be 0 at compile-time should be evaluated to true, but instead generates a runtime conditional 0 == 0 ? 65 : 66.

Removing the new StringBuf().addChar(10); line causes the analyzer to correctly optimize the conditional.

Minimal Reproduction

abstract W(Int) from Int {
	public inline function test() {
		trace(this == 0 ? 65 : 66);
		new StringBuf().addChar(10);
	}
}

class Test {
	static function main() {
		final w:W = 0;
		w.test();
	}
}

Actual Output (JS)

class Test {
	static main() {
		console.log("Test.hx:3:", 0 == 0 ? 65 : 66);  // Not optimized!
		let _this_b = "";
		_this_b += String.fromCodePoint(10);
	}
}

Expected Output (JS)

class Test {
	static main() {
		console.log("Test.hx:3:", 65);  // Should be constant-folded
		let _this_b = "";
		_this_b += String.fromCodePoint(10);
	}
}

Notes

  • Removing new StringBuf().addChar(10); causes the analyzer to correctly optimize to 65
  • The same code outside an abstract is optimized correctly
  • The value of w is known at compile-time (w:W = 0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions