Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null-coalescing operator cast before test against null issue #11706

Closed
yuxiaomao opened this issue Jul 2, 2024 · 2 comments
Closed

Null-coalescing operator cast before test against null issue #11706

yuxiaomao opened this issue Jul 2, 2024 · 2 comments

Comments

@yuxiaomao
Copy link
Contributor

yuxiaomao commented Jul 2, 2024

Here is a code which works with 4.3.4 but not with 5.0 ( https://try.haxe.org/#3F583427 ): in 5.0, using ?? cause onEnd first being cast to EndFun before test against null.

Thanks to Rudy I found that the test fail after this commit 39642c9 ( #11425 )

class Test {
	var onEnd : Void -> Void = null;
	public function new() {
	}

	public function call(f : EndFun ) {
		f();
		trace("call " + f);
	}

	static function main()
	{
		var m = new Test();
		m.call(m.onEnd == null ? function() {trace("somefunc1");} : m.onEnd); // ok
		m.call(m.onEnd ?? function() {trace("somefunc2");}); // Runtime error null access f
	}
}

@:callable abstract EndFun(Void -> Void) {
	@:from public static function fromFun( f : Void -> Void ) : EndFun {
		return cast function() {
			f();
			f = () -> throw "double end";
		};
	}
}
@kLabz kLabz added this to the 4.3 Hotfix candidates milestone Jul 2, 2024
@kLabz
Copy link
Contributor

kLabz commented Jul 2, 2024

(added to hotfix candidates because #11425 is in there and this issues shouldn't be missed if #11425 is to be included in 4.3.x)

@Simn
Copy link
Member

Simn commented Jul 2, 2024

Hmm... the problem here is that the expected type is EndFun because that's what call wants as an argument, and then we pass a Void->Void to it and that causes a cast. That cast should clearly happen after the null-check though, not before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants