-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sem: fix type inference for
static
parameters (#1433)
## Summary * fix compiler crash when passing empty aggregate values to `static` parameters * fix `static T` parameter values not being of type `T`, when the argument is not directly of type `T` but requires a conversion * fix converters not being considered for arguments to `static` parameters ## Details When matching an expression against a formal parameter that: * is a `static` parameter * contains a `static` type somewhere (e.g., `int or static[float]`) 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: * the actual value inlined at usages of the `static` parameter having the wrong type * type inference for empty aggregate types not working ### 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.
- Loading branch information
Showing
7 changed files
with
106 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/lang_callable/converter/tconverter_for_static_param.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
discard """ | ||
description: ''' | ||
Ensure that converters are considered for arguments to static parameters | ||
" | ||
""" | ||
|
||
converter toInt(x: float): int = int(x) | ||
|
||
proc test(x: static int) = | ||
doAssert x == 1 | ||
|
||
test(1.5) # wouldn't work without a converter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters