Open cston opened 5 years ago
There should be a clearified state of "if reference type then nullable", in both source and analysis, aka <[MaybeNull]T>
.
The above design note contains important info on nullability of unconstrained generic T, which is IMO important for the community to read, as many have opinions on this matter. However, the issue title is a bit subtle, which reads like an internal issue for the roslyn team.
Maybe it is better to create a separate issue for the design note and link back?
Improve analysis of
[MaybeNull]T
values by adding a third possible flow state.Additional flow state
A third flow state is added that represents maybe null even when substituted with a non-nullable reference type. The additional state applies only to values of type parameters that are not constrained not nullable. Flow analysis will use two bits for each tracked value.
The merging rules are unchanged:
Join()
uses bitwise|
andMeet()
uses bitwise&
.Member signatures affect method analysis
Attributes on members, including attributes on the containing method signature, are considered when analyzing the method body.
No W warnings
Locals cannot use
[MaybeNull]
and neither can explicit casts. Because of that limitation, W warnings are not reported for unconstrained types.Warnings are produced for compound types though:
No warning for null expressions
Expressions (of a type parameter type not constrained to not nullable) that may produce null values are treated as
MaybeDefault
. Warnings are not reported for the expressions directly.Type inference
[MaybeNull]
is ignored in method type inference.Best type algorithm relies on the merging of states to choose [MaybeNull]T over T.
LDM Notes
[MaybeNull] T
)