Non-null mode allows a constructor to have null assigned to a non-nullable generic field
@astavale
Submitted by Al Thomas Link to original bug (#793912)
Description
In the example below the int, string and class fields all need to marked nullable with '?' when compiling with --enable-experimental-non-null.
When the field is of a generic type non-null mode still picks the field is not nullable when a null assignment is made in the main() block. When this is done in the constructor, however, it is not picked up by the checker.
void main () {
var a = new Test<MyClass> ();
a.int_field = null;
a.string_field = null;
a.my_class_field = null;
// a.my_generic_field = null; // Produces "Assignment: Cannot convert from `null' to `MyClass'"
}
class Test<T> {
public int? int_field;
public string? string_field;
public MyClass? my_class_field;
public T my_generic_field; // Should be 'public T? my_generic_field' for this code to compile
public Test () {
int_field = null;
string_field = null;
my_class_field = null;
my_generic_field = null; // Should produce "Assignment: Cannot convert from `null' to `MyClass'"
}
}
class MyClass {}
Edited by Rico Tzschichholz