Merge branch 'bigbreak' of https://github.com/Araq/Nimrod into bigbreak
This commit is contained in:
commit
bd4e229464
5 changed files with 36 additions and 9 deletions
|
|
@ -350,9 +350,14 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||||
var def: PNode
|
var def: PNode
|
||||||
if a.sons[length-1].kind != nkEmpty:
|
if a.sons[length-1].kind != nkEmpty:
|
||||||
def = semExprWithType(c, a.sons[length-1], {efAllowDestructor})
|
def = semExprWithType(c, a.sons[length-1], {efAllowDestructor})
|
||||||
# BUGFIX: ``fitNode`` is needed here!
|
if typ != nil:
|
||||||
# check type compability between def.typ and typ:
|
if typ.isMetaType:
|
||||||
if typ != nil: def = fitNode(c, typ, def)
|
def = inferWithMetaType(c, typ, def)
|
||||||
|
typ = def.typ
|
||||||
|
else:
|
||||||
|
# BUGFIX: ``fitNode`` is needed here!
|
||||||
|
# check type compability between def.typ and typ
|
||||||
|
def = fitNode(c, typ, def)
|
||||||
else:
|
else:
|
||||||
typ = skipIntLit(def.typ)
|
typ = skipIntLit(def.typ)
|
||||||
if typ.kind in {tySequence, tyArray, tySet} and
|
if typ.kind in {tySequence, tyArray, tySet} and
|
||||||
|
|
|
||||||
|
|
@ -216,12 +216,16 @@ proc replaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym =
|
||||||
result.typ = replaceTypeVarsT(cl, s.typ)
|
result.typ = replaceTypeVarsT(cl, s.typ)
|
||||||
result.ast = replaceTypeVarsN(cl, s.ast)
|
result.ast = replaceTypeVarsN(cl, s.ast)
|
||||||
|
|
||||||
proc lookupTypeVar(cl: TReplTypeVars, t: PType): PType =
|
proc lookupTypeVar(cl: var TReplTypeVars, t: PType): PType =
|
||||||
result = PType(idTableGet(cl.typeMap, t))
|
result = PType(idTableGet(cl.typeMap, t))
|
||||||
if result == nil:
|
if result == nil:
|
||||||
if cl.allowMetaTypes or tfRetType in t.flags: return
|
if cl.allowMetaTypes or tfRetType in t.flags: return
|
||||||
localError(t.sym.info, errCannotInstantiateX, typeToString(t))
|
localError(t.sym.info, errCannotInstantiateX, typeToString(t))
|
||||||
result = errorType(cl.c)
|
result = errorType(cl.c)
|
||||||
|
# In order to prevent endless recursions, we must remember
|
||||||
|
# this bad lookup and replace it with errorType everywhere.
|
||||||
|
# These code paths are only active in nimrod check
|
||||||
|
idTablePut(cl.typeMap, t, result)
|
||||||
elif result.kind == tyGenericParam and not cl.allowMetaTypes:
|
elif result.kind == tyGenericParam and not cl.allowMetaTypes:
|
||||||
internalError(cl.info, "substitution with generic parameter")
|
internalError(cl.info, "substitution with generic parameter")
|
||||||
|
|
||||||
|
|
@ -353,7 +357,7 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType =
|
||||||
|
|
||||||
of tyGenericBody:
|
of tyGenericBody:
|
||||||
localError(cl.info, errCannotInstantiateX, typeToString(t))
|
localError(cl.info, errCannotInstantiateX, typeToString(t))
|
||||||
result = t
|
result = errorType(cl.c)
|
||||||
#result = replaceTypeVarsT(cl, lastSon(t))
|
#result = replaceTypeVarsT(cl, lastSon(t))
|
||||||
|
|
||||||
of tyFromExpr:
|
of tyFromExpr:
|
||||||
|
|
|
||||||
|
|
@ -913,9 +913,11 @@ proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =
|
||||||
result = sameTypeAux(a.sons[0], b.sons[0], c)
|
result = sameTypeAux(a.sons[0], b.sons[0], c)
|
||||||
else:
|
else:
|
||||||
result = sameTypeAux(a.sons[0], b.sons[0], c) and sameFlags(a, b)
|
result = sameTypeAux(a.sons[0], b.sons[0], c) and sameFlags(a, b)
|
||||||
of tyEnum, tyForward, tyProxy:
|
of tyEnum, tyForward:
|
||||||
# XXX generic enums do not make much sense, but require structural checking
|
# XXX generic enums do not make much sense, but require structural checking
|
||||||
result = a.id == b.id and sameFlags(a, b)
|
result = a.id == b.id and sameFlags(a, b)
|
||||||
|
of tyError:
|
||||||
|
result = b.kind == tyError
|
||||||
of tyTuple:
|
of tyTuple:
|
||||||
cycleCheck()
|
cycleCheck()
|
||||||
result = sameTuple(a, b, c) and sameFlags(a, b)
|
result = sameTuple(a, b, c) and sameFlags(a, b)
|
||||||
|
|
|
||||||
|
|
@ -3671,8 +3671,8 @@ once for each tested type and any static code included within them will also be
|
||||||
executed once.
|
executed once.
|
||||||
|
|
||||||
|
|
||||||
Return Type Inference
|
Type inference with type classes
|
||||||
---------------------
|
--------------------------------
|
||||||
|
|
||||||
If a type class is used as the return type of a proc and it won't be bound to
|
If a type class is used as the return type of a proc and it won't be bound to
|
||||||
a concrete type by some of the proc params, Nim will infer the return type
|
a concrete type by some of the proc params, Nim will infer the return type
|
||||||
|
|
@ -3681,13 +3681,18 @@ from the proc body. This is usually used with the ``auto`` type class:
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
proc makePair(a, b): auto = (first: a, second: b)
|
proc makePair(a, b): auto = (first: a, second: b)
|
||||||
|
|
||||||
The return type will be treated as additional generic param and can be
|
The return type will be treated as an additional generic param and can be
|
||||||
explicitly specified at call sites as any other generic param.
|
explicitly specified at call sites as any other generic param.
|
||||||
|
|
||||||
Future versions of Nim may also support overloading based on the return type
|
Future versions of Nim may also support overloading based on the return type
|
||||||
of the overloads. In such settings, the expected result type at call sites may
|
of the overloads. In such settings, the expected result type at call sites may
|
||||||
also influence the inferred return type.
|
also influence the inferred return type.
|
||||||
|
|
||||||
|
Likewise, if a type class is used in another position where Nim expects a
|
||||||
|
concrete type (e.g. a variable declaration or a type coercion), Nim will try to
|
||||||
|
infer the concrete type by applying the sane matching algorithm also used in
|
||||||
|
overload resolution.
|
||||||
|
|
||||||
|
|
||||||
Symbol lookup in generics
|
Symbol lookup in generics
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "type mismatch: got (string) but expected 'ptr'"
|
||||||
|
line: 20
|
||||||
|
"""
|
||||||
|
|
||||||
import typetraits
|
import typetraits
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -8,3 +13,9 @@ var x = Vec([1, 2, 3])
|
||||||
static:
|
static:
|
||||||
assert x.type.name == "Vec[static[int](3), int]"
|
assert x.type.name == "Vec[static[int](3), int]"
|
||||||
|
|
||||||
|
var str1: string = "hello, world!"
|
||||||
|
var ptr1: ptr = addr(str1)
|
||||||
|
|
||||||
|
var str2: string = "hello, world!"
|
||||||
|
var ptr2: ptr = str2
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue