HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.14k stars 653 forks source link

Null Safety hole with array access #11585

Open kLabz opened 7 months ago

kLabz commented 7 months ago

Array access will return null on index not found, yet return type is T, not Null<T>:

@:nullSafety(Strict)
class Test {
  static function main() {
    var s:String = foo(42);
    trace(s == null); // true
    // s = null; // Null safety: Cannot assign nullable value here.
  }

  static function foo(i:Int) {
    var arr:Array<String> = [];
    return arr[i];
  }
}

https://try.haxe.org/#1DeFA9FA

RealyUniqueName commented 7 months ago

There was a PR merged to address this issue: #6825 But it was reverted because of #6847

kLabz commented 7 months ago

That's unfortunate :confused:

Maybe now that null safety is in the compiler we could revive this but only when null safety is Strict? cc @ncannasse

Simn commented 7 months ago

From my understanding one of the main problems with that PR was that it introduced target differences.

9Morello commented 7 months ago

@kLabz that sounds like a good compromise to me