xtclang / xvm

Ecstasy and XVM
Other
195 stars 15 forks source link

Fix the compiler inference inconsistencies #206

Closed ggleyzer closed 2 months ago

ggleyzer commented 2 months ago

The compiler used to have a slightly different type inference logic between AssignmentStatement and VariableDeclarationStatement. While the former always called "updateLValueFromRValueTypes()" at the end of the validation, the later did not. As a result, as Cliff first discovered, processing of

Int? n = 0;
... // use n 

was quite different from

Int? n;
n = 0;
... // use n 

In the second scenario the compiler would "know" that "n" is not Null, while in the first one it did not, which is definitely wrong. The obvious solution was to add the following line at the end of VariableDeclarationStatement#updateLValueFromRValueTypes().

getLValueExpression().updateLValueFromRValueTypes(ctx, branch, fCond, aTypes);

However, this simple fix uncovered a slew of bugs we have never seen before (the obvious reason we have not stepped onto any of those was that that Int? n; n = 0; style of variable initialization is quite unusual.

The good news is that the fix in this PR (on top of resolving the compilation inconsistency) made the compiler to uncover a couple of bugs that were not yet known (e.g. this line was missing, the Array API was a nothch off, and a couple of other small issues)

ggleyzer commented 2 months ago

Reviewed w/ Cam. A couple of small fixes were made and pushed