nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
279 stars 39 forks source link

sem: fix type inference for `static` parameters #1433

Closed zerbina closed 3 months ago

zerbina commented 3 months ago

Summary

Details

When matching an expression against a formal parameter that:

the expression was compile-time evaluated first, and on success, assigned a tyStatic type. If the tyStatic argument type matched the formal tyStatic type, the argument type was bound to the formal tyStatic as-is.

In case an implicit conversion is necessary, the conversion was applied only after the evaluated tyStatic was bound to the parameter type, leading to:

Solution

If the formal type is a static T, full argument matching (including implicit conversion and converter handling) for between the argument and T is performed first, and only in case of success is the expression compile-time evaluated, and the resulting tyStatic type bound to the parameter type.

Post-match fitting has to take place prior to const evaluation (in order to correctly type empty aggregates), so a new procedure tryEvalStaticArgument is introduced that handles the static arguments.

The new static handling renders the macro/template static special- casing obsolete; replacing the static argument with the evaluation result is now done in evalTemplateArgs.

Typed AST

For calls to routines with static arguments, the typed argument expression now stays in the AST as-is, instead of being replaced with the evaluated value.

zerbina commented 3 months ago

The failure of tgeneric_various.nim is due to proc symbols passed to static[proc()] parameters now being turned into closures (as they should). The evaluated result is an nkClosure, however, which can currently not pass through sem again.

zerbina commented 3 months ago

For making static[proc()] parameters work again, I've added handling for nkClosure to semExpr -- nkClosure AST is now turned back into an nkHiddenStdConv. I think it's an okay solution for now; the alternatives would have been to either:

saem commented 3 months ago

/merge

github-actions[bot] commented 3 months ago

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


## Notes for Reviewers * the typed argument expression staying in the typed AST makes #1192 a bit easier