Acknowledged
CIQQA-3175

Type checker fails to prevent nullable value from being assigned to array/dictionary which is typed to have non-nullable values

But the type checker is able to prevent null from being assigned in the same cases. And it is able to prevent a nullable value from being assigned to a scalar variable (not array/dictionary member) which is typed to be non-nullable.

- SDK 8.1.1

- type check level: 1, 2 or 3

import Toybox.Lang;

var floatVal as Float = 42.0;
var floatArray as Array<Float> = [ 42.0 ];
var floatDictionary as Dictionary<String, Float> = {} as Dictionary<String, Float>;

function bar(x as Float?) as Void {
    floatVal = null; // ✅ ERROR: fr955: Cannot assign value 'Null' to member ':floatVal'.
    floatVal = x; // ✅ ERROR: fr955: Assigned value type 'PolyType<Null or $.Toybox.Lang.Float>' to member '$.floatVal' of non-poly type '$.Toybox.Lang.Float'.

    floatArray[0] = null; // ✅ ERROR: fr955: Cannot assign type 'Null' to '$.Toybox.Lang.Array<$.Toybox.Lang.Float>'.
    floatArray[0] = x; // ❌ no error

    floatDictionary["foo"] = null; // ✅ ERROR: fr955: Cannot assign type 'Null' to '$.Toybox.Lang.Dictionary<$.Toybox.Lang.String,$.Toybox.Lang.Float>'.
    floatDictionary["foo"] = x; // ❌ no error
}

Expected behaviour:

- every line in bar() should have an error related to assigning a null / nullable value to something which isn't nullable

Notes:

- the type checker clearly knows that x is a Float or Null as per the error when trying to assign it to floatVal, and as per the type information that appears when you hover over any instance of x:

> var x as $.Toybox.Lang.Float or Null

- as per the comment below, the general issue seems to be that the checker only ensures that at least one type of a given polytype is valid when it's assigned to an element of an array/dictionary, while it should check that all types are valid.

  • Similar/same issue: if the type of x is changed to Float or String, the type checker doesn't take into account the fact that String shouldn't be assigned to either the array or dictionary.

    It seems that the type checker only checks that at least one type in a polytype is valid when assigning to an element of an array or dictionary, but it should be checking that all types are valid..