Merge branch 'devel' of https://github.com/Araq/Nim into devel
This commit is contained in:
commit
e751a0af57
6 changed files with 65 additions and 8 deletions
|
|
@ -926,7 +926,8 @@ const
|
||||||
proc readTypeParameter(c: PContext, typ: PType,
|
proc readTypeParameter(c: PContext, typ: PType,
|
||||||
paramName: PIdent, info: TLineInfo): PNode =
|
paramName: PIdent, info: TLineInfo): PNode =
|
||||||
let ty = if typ.kind == tyGenericInst: typ.skipGenericAlias
|
let ty = if typ.kind == tyGenericInst: typ.skipGenericAlias
|
||||||
else: (internalAssert(typ.kind == tyCompositeTypeClass); typ.sons[1])
|
else: (internalAssert(typ.kind == tyCompositeTypeClass);
|
||||||
|
typ.sons[1].skipGenericAlias)
|
||||||
#debug ty
|
#debug ty
|
||||||
let tbody = ty.sons[0]
|
let tbody = ty.sons[0]
|
||||||
for s in countup(0, tbody.len-2):
|
for s in countup(0, tbody.len-2):
|
||||||
|
|
@ -965,6 +966,7 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
var ty = n.sons[0].typ
|
var ty = n.sons[0].typ
|
||||||
var f: PSym = nil
|
var f: PSym = nil
|
||||||
result = nil
|
result = nil
|
||||||
|
|
||||||
if isTypeExpr(n.sons[0]) or (ty.kind == tyTypeDesc and ty.base.kind != tyNone):
|
if isTypeExpr(n.sons[0]) or (ty.kind == tyTypeDesc and ty.base.kind != tyNone):
|
||||||
if ty.kind == tyTypeDesc: ty = ty.base
|
if ty.kind == tyTypeDesc: ty = ty.base
|
||||||
ty = ty.skipTypes(tyDotOpTransparent)
|
ty = ty.skipTypes(tyDotOpTransparent)
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,8 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
|
||||||
result = n
|
result = n
|
||||||
styleCheckUse(n.info, s)
|
styleCheckUse(n.info, s)
|
||||||
of skType:
|
of skType:
|
||||||
if (s.typ != nil) and (s.typ.kind != tyGenericParam):
|
if (s.typ != nil) and
|
||||||
|
(s.typ.flags * {tfGenericTypeParam, tfImplicitTypeParam} == {}):
|
||||||
result = newSymNodeTypeDesc(s, n.info)
|
result = newSymNodeTypeDesc(s, n.info)
|
||||||
else:
|
else:
|
||||||
result = n
|
result = n
|
||||||
|
|
|
||||||
|
|
@ -979,6 +979,9 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
||||||
of tyStatic:
|
of tyStatic:
|
||||||
if aOrig.kind == tyStatic:
|
if aOrig.kind == tyStatic:
|
||||||
result = typeRel(c, f.lastSon, a)
|
result = typeRel(c, f.lastSon, a)
|
||||||
|
if result != isNone and f.n != nil:
|
||||||
|
if not exprStructuralEquivalent(f.n, a.n):
|
||||||
|
result = isNone
|
||||||
if result != isNone: put(c.bindings, f, aOrig)
|
if result != isNone: put(c.bindings, f, aOrig)
|
||||||
else:
|
else:
|
||||||
result = isNone
|
result = isNone
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ proc lastOrd*(t: PType): BiggestInt
|
||||||
proc lengthOrd*(t: PType): BiggestInt
|
proc lengthOrd*(t: PType): BiggestInt
|
||||||
type
|
type
|
||||||
TPreferedDesc* = enum
|
TPreferedDesc* = enum
|
||||||
preferName, preferDesc, preferExported, preferModuleInfo
|
preferName, preferDesc, preferExported, preferModuleInfo, preferGenericArg
|
||||||
|
|
||||||
proc typeToString*(typ: PType; prefer: TPreferedDesc = preferName): string
|
proc typeToString*(typ: PType; prefer: TPreferedDesc = preferName): string
|
||||||
proc base*(t: PType): PType
|
proc base*(t: PType): PType
|
||||||
|
|
@ -411,11 +411,13 @@ const
|
||||||
"UserTypeClassInst", "CompositeTypeClass",
|
"UserTypeClassInst", "CompositeTypeClass",
|
||||||
"and", "or", "not", "any", "static", "TypeFromExpr", "FieldAccessor"]
|
"and", "or", "not", "any", "static", "TypeFromExpr", "FieldAccessor"]
|
||||||
|
|
||||||
|
const preferToResolveSymbols = {preferName, preferModuleInfo, preferGenericArg}
|
||||||
|
|
||||||
proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
|
proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
|
||||||
var t = typ
|
var t = typ
|
||||||
result = ""
|
result = ""
|
||||||
if t == nil: return
|
if t == nil: return
|
||||||
if prefer in {preferName, preferModuleInfo} and t.sym != nil and
|
if prefer in preferToResolveSymbols and t.sym != nil and
|
||||||
sfAnon notin t.sym.flags:
|
sfAnon notin t.sym.flags:
|
||||||
if t.kind == tyInt and isIntLit(t):
|
if t.kind == tyInt and isIntLit(t):
|
||||||
return t.sym.name.s & " literal(" & $t.n.intVal & ")"
|
return t.sym.name.s & " literal(" & $t.n.intVal & ")"
|
||||||
|
|
@ -428,20 +430,26 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
|
||||||
if not isIntLit(t) or prefer == preferExported:
|
if not isIntLit(t) or prefer == preferExported:
|
||||||
result = typeToStr[t.kind]
|
result = typeToStr[t.kind]
|
||||||
else:
|
else:
|
||||||
result = "int literal(" & $t.n.intVal & ")"
|
if prefer == preferGenericArg:
|
||||||
|
result = $t.n.intVal
|
||||||
|
else:
|
||||||
|
result = "int literal(" & $t.n.intVal & ")"
|
||||||
of tyGenericBody, tyGenericInst, tyGenericInvokation:
|
of tyGenericBody, tyGenericInst, tyGenericInvokation:
|
||||||
result = typeToString(t.sons[0]) & '['
|
result = typeToString(t.sons[0]) & '['
|
||||||
for i in countup(1, sonsLen(t) -1 -ord(t.kind != tyGenericInvokation)):
|
for i in countup(1, sonsLen(t) -1 -ord(t.kind != tyGenericInvokation)):
|
||||||
if i > 1: add(result, ", ")
|
if i > 1: add(result, ", ")
|
||||||
add(result, typeToString(t.sons[i]))
|
add(result, typeToString(t.sons[i], preferGenericArg))
|
||||||
add(result, ']')
|
add(result, ']')
|
||||||
of tyTypeDesc:
|
of tyTypeDesc:
|
||||||
if t.base.kind == tyNone: result = "typedesc"
|
if t.base.kind == tyNone: result = "typedesc"
|
||||||
else: result = "typedesc[" & typeToString(t.base) & "]"
|
else: result = "typedesc[" & typeToString(t.base) & "]"
|
||||||
of tyStatic:
|
of tyStatic:
|
||||||
internalAssert t.len > 0
|
internalAssert t.len > 0
|
||||||
result = "static[" & typeToString(t.sons[0]) & "]"
|
if prefer == preferGenericArg and t.n != nil:
|
||||||
if t.n != nil: result.add "(" & renderTree(t.n) & ")"
|
result = t.n.renderTree
|
||||||
|
else:
|
||||||
|
result = "static[" & typeToString(t.sons[0]) & "]"
|
||||||
|
if t.n != nil: result.add "(" & renderTree(t.n) & ")"
|
||||||
of tyUserTypeClass:
|
of tyUserTypeClass:
|
||||||
internalAssert t.sym != nil and t.sym.owner != nil
|
internalAssert t.sym != nil and t.sym.owner != nil
|
||||||
return t.sym.owner.name.s
|
return t.sym.owner.name.s
|
||||||
|
|
|
||||||
26
tests/generics/t1056.nim
Normal file
26
tests/generics/t1056.nim
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
discard """
|
||||||
|
output: '''TMatrix[3, 3, system.int]
|
||||||
|
3
|
||||||
|
3'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
import typetraits
|
||||||
|
|
||||||
|
type
|
||||||
|
TMatrix*[N,M: static[int], T] = object
|
||||||
|
data*: array[0..N*M-1, T]
|
||||||
|
|
||||||
|
TMat2[T] = TMatrix[2,2,T]
|
||||||
|
|
||||||
|
proc echoMatrix(a: TMatrix) =
|
||||||
|
echo a.type.name
|
||||||
|
echo TMatrix.N
|
||||||
|
|
||||||
|
proc echoMat2(a: TMat2) =
|
||||||
|
echo TMat2.M
|
||||||
|
|
||||||
|
var m = TMatrix[3,3,int](data: [1,2,3,4,5,6,7,8,9])
|
||||||
|
|
||||||
|
echoMatrix m
|
||||||
|
echoMat2 m
|
||||||
|
|
||||||
|
|
@ -34,3 +34,20 @@ type
|
||||||
yes s.items is Iter[TNumber]
|
yes s.items is Iter[TNumber]
|
||||||
no s.items is Iter[float]
|
no s.items is Iter[float]
|
||||||
|
|
||||||
|
type
|
||||||
|
Foo[N: static[int], T] = object
|
||||||
|
field: array[1..N, T]
|
||||||
|
|
||||||
|
Bar[T] = Foo[4, T]
|
||||||
|
Baz[N: static[int]] = Foo[N, float]
|
||||||
|
|
||||||
|
no Foo[2, float] is Foo[3, float]
|
||||||
|
no Foo[2, float] is Foo[2, int]
|
||||||
|
|
||||||
|
yes Foo[4, string] is Foo[4, string]
|
||||||
|
yes Bar[int] is Foo[4, int]
|
||||||
|
yes Foo[4, int] is Bar[int]
|
||||||
|
|
||||||
|
no Foo[4, int] is Baz[4]
|
||||||
|
yes Foo[4, float] is Baz[4]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue