Skip to content

[nullsafety] errors or warnings for null checks on non-nullable types #7816

@Gama11

Description

@Gama11

Existing Haxe code usually doesn't use Null<T> (and even if it does, not consistently), which means that many externs and types for loading external data become incorrect when null safety is enabled. The compiler can't see if null is ever assigned in those cases.

Consider code like this:

typedef Data = {
	var foo:String;
}

@:nullSafety(Strict)
class Main {
	static function main() {
		var data:Data = haxe.Json.parse("{}");

		// ...

		trace(data.foo.length);

		// ...

		if (data.foo == null) {
			data.foo = "default";
		}
	}
}

The intention is clearly that foo is nullable, since there's a null check and default value handling, yet it wasn't declared as optional. If there was a warning or error on the null check, the user could notice that the type declaration is wrong and adjust it accordingly, which would then reveal that accessing foo.length is unsafe. But right now this would compile + crash, despite null safety being enabled.

Perhaps there should simply be an additional argument to --macro nullSafety() for this? It's kind of orthogonal to strictness levels.

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions