-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New rules for type inference #149
Labels
Comments
This was referenced May 14, 2020
metagn
added a commit
to metagn/Nim
that referenced
this issue
Jul 26, 2022
17 tasks
metagn
added a commit
to metagn/Nim
that referenced
this issue
Jul 31, 2022
metagn
added a commit
to metagn/Nim
that referenced
this issue
Aug 5, 2022
metagn
added a commit
to metagn/Nim
that referenced
this issue
Aug 9, 2022
metagn
added a commit
to metagn/Nim
that referenced
this issue
Aug 15, 2022
metagn
added a commit
to metagn/Nim
that referenced
this issue
Aug 15, 2022
Araq
added a commit
to nim-lang/Nim
that referenced
this issue
Aug 24, 2022
* micro implementation of rfc 149 refs nim-lang/RFCs#149 * number/array/seq literals, more statements * try fix number literal alias issue * renew expectedType with if/case/try branch types * fix (nerf) index type handling and float typed int * use typeAllowed * tweaks + const test (tested locally) [skip ci] * fill out more of the checklist * more literals, change @ order, type conversions Not copying the full call tree before the typedesc call check in `semIndirectOp` is also a small performance improvement. * disable self-conversion warning * revert type conversions (maybe separate op later) * deal with CI for now (seems unrelated), try enums * workaround CI different way * proper fix * again * see sizes * lol * overload selection, simplify int literal -> float * range, new @ solution, try use fitNode for nil * use new magic * try fix ranges, new magic, deal with #20193 * add documentation, support templates Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
capocasa
pushed a commit
to capocasa/Nim
that referenced
this issue
Mar 31, 2023
* micro implementation of rfc 149 refs nim-lang/RFCs#149 * number/array/seq literals, more statements * try fix number literal alias issue * renew expectedType with if/case/try branch types * fix (nerf) index type handling and float typed int * use typeAllowed * tweaks + const test (tested locally) [skip ci] * fill out more of the checklist * more literals, change @ order, type conversions Not copying the full call tree before the typedesc call check in `semIndirectOp` is also a small performance improvement. * disable self-conversion warning * revert type conversions (maybe separate op later) * deal with CI for now (seems unrelated), try enums * workaround CI different way * proper fix * again * see sizes * lol * overload selection, simplify int literal -> float * range, new @ solution, try use fitNode for nil * use new magic * try fix ranges, new magic, deal with nim-lang#20193 * add documentation, support templates Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
narimiran
pushed a commit
to nim-lang/Nim
that referenced
this issue
May 11, 2023
* micro implementation of rfc 149 refs nim-lang/RFCs#149 * number/array/seq literals, more statements * try fix number literal alias issue * renew expectedType with if/case/try branch types * fix (nerf) index type handling and float typed int * use typeAllowed * tweaks + const test (tested locally) [skip ci] * fill out more of the checklist * more literals, change @ order, type conversions Not copying the full call tree before the typedesc call check in `semIndirectOp` is also a small performance improvement. * disable self-conversion warning * revert type conversions (maybe separate op later) * deal with CI for now (seems unrelated), try enums * workaround CI different way * proper fix * again * see sizes * lol * overload selection, simplify int literal -> float * range, new @ solution, try use fitNode for nil * use new magic * try fix ranges, new magic, deal with #20193 * add documentation, support templates Co-authored-by: Andreas Rumpf <rumpf_a@web.de> (cherry picked from commit 0014b9c)
This RFC is stale because it has been open for 1095 days with no activity. Contribute a fix or comment on the issue, or it will be closed in 7 days. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example shows a problem with the current type inference algorithm.
In the compiler the expression
(echo "a"; {})
is semanticallychecked and the inferred type is
set[empty]
. And then when theassignment is checked, the type of the expression on the right hand
side is patched as an afterthought. This is the code in the compiler
that does it.
Basically this only works for very simple expressions. According to
Araq this function does: "uoh, two types here that we need to combine
somehow into a real type". I see this as a problematic hack in many
ways. First of all it is a special case that only takes the simple
case into consideration
x = {}
where x is fully typed. Then thesecond problem is, changes the type of the AST an a mutable
transformation as an afterthought (mutable changes might lead to
bugs). That is not a clean way to solve this problem, and there is a
better way to do it.
My proposal is the following:
Get rid of
fidNode
andfitNodePostMatch
entirely. These proceduresdon't work recursively on the AST, and making them traverse
recursively will introduce yet another AST traversal. This is costly
maybe incorrect and I don't think it is worth it. So I think the types
can be inferred in the first sem checking phase.
Currently when an expression like
var x : set[char] = (echo "a"; {})
is checked, we know that we expect the right hand side to be of type
set[char]
, but this expected type is not passed tosemExprWithType
. So I suggest to add another parameter tosemExprWithType
, the expected type. The expected type may always benil, but when it isn't it should be forwarded properly. With this
additional parameter the sem-checker will eventually do a sem-check on
{}
with with an expected type. This means that the expression{}
instantly get the correct type, it won't be inferred as an empty node
anymore.
This expected type can then be used recursively to have expected types
in other contexts, such as tuple constructors, arrays and sequences.
Here is an example on how the expected type should be used recursively
in other contexts (pseudo syntax):
Tuples like the example above might also be detected as correct code,
because the sem checker will be able to know that in the first element
of the tuple constructor an
uint8
is expected, not a string literal.steps that need to be done before this can be implemented
enforceVoidContext
is currentlytyTyped
. This needs to bechanged to
tyVoid
consistently though the compiler.nil
. Ithis is problematic as
typ.kind
cannot be accessed safely. Ithink the
nil
type can be used better as "no type expectation",or when seen in the AST: "type not yet inferred".
I hope that the expression flags
efWantStmt
andefDetermineType
can be superseded by this and therefore removed. As
efWantStmt
isrepresented as
efWantValue
.This could potentially improve the usability of pure enums, because
in contexts where a concrete enum type is expected, the owner of that
enum identifier is clear.
amb
is declared in both MyEnum and OtherEnum, but it is clear thatMyEnum.amb
is meant and notOtherEnum.amb
, because the expectedtype will be
MyEnum
.This RFC supersedes the following issues:
nim-lang/Nim#11109
The text was updated successfully, but these errors were encountered: