Extend contextual typing of array literals
- Truncate descriptions
Extend contextual typing of array literals
When typifying an array literal, the typifier currently creates a type context spanning the array's entries and tries to figure out their type. If that fails, it tries to apply the type of the enclosing context to the array, which only works if a type is already known for the enclosing context. This means that in many cases, enough type information is available for typifying an array, but it is not propagated correctly.
There are two situations:
Array declarations: In a declaration of a numeric array, such as <unknown> arr[] = {1, 2, 3, 4};
,
the array elements should receive the context's default data type,
and the arr
symbol should get the PsArrayType(ctx.default_dtype, <length-of-array>)
.
Inline array literals: If an array literal of unknown type is embedded into a larger expression, such as {1, 2, 3}[idx] + x
, and the outer expression's type is unknown when the typifier visits the array but may become known later, typification of the array should be deferred until that type is known.
This requires the ability to defer the resolution of a nested type context by hooking it to its enclosing context. E.g. in this case, when the type of x
, and therefore of _ + x
, evaluates to float32
, the type of {...}[idx]
must be float32
, so the type of the literal must be Arr(float32, 3)
, and float32
must in turn be propagated to the array entries.
- Show labels
- Show closed items