implement the special treatment of explicit type params in concepts

This commit is contained in:
Zahary Karadjov 2016-08-12 16:45:28 +03:00
commit 644d645ea7
6 changed files with 20 additions and 5 deletions

View file

@ -494,6 +494,8 @@ type
tfGenericTypeParam tfGenericTypeParam
tfImplicitTypeParam tfImplicitTypeParam
tfInferrableStatic tfInferrableStatic
tfExplicit # for typedescs, marks types explicitly prefixed with the
# `type` operator (e.g. type int)
tfWildcard # consider a proc like foo[T, I](x: Type[T, I]) tfWildcard # consider a proc like foo[T, I](x: Type[T, I])
# T and I here can bind to both typedesc and static types # T and I here can bind to both typedesc and static types
# before this is determined, we'll consider them to be a # before this is determined, we'll consider them to be a

View file

@ -24,7 +24,7 @@ proc semTypeOf(c: PContext; n: PNode): PNode =
result = newNodeI(nkTypeOfExpr, n.info) result = newNodeI(nkTypeOfExpr, n.info)
let typExpr = semExprWithType(c, n, {efInTypeof}) let typExpr = semExprWithType(c, n, {efInTypeof})
result.add typExpr result.add typExpr
result.typ = makeTypeDesc(c, typExpr.typ.skipTypes({tyTypeDesc})) result.typ = makeTypeDesc(c, typExpr.typ)
type type
SemAsgnMode = enum asgnNormal, noOverloadedSubscript, noOverloadedAsgn SemAsgnMode = enum asgnNormal, noOverloadedSubscript, noOverloadedAsgn

View file

@ -1239,6 +1239,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
let typExpr = semExprWithType(c, n.sons[0], {efInTypeof}) let typExpr = semExprWithType(c, n.sons[0], {efInTypeof})
fixupTypeOf(c, prev, typExpr) fixupTypeOf(c, prev, typExpr)
result = typExpr.typ result = typExpr.typ
if result.kind == tyTypeDesc: result.flags.incl tfExplicit
of nkPar: of nkPar:
if sonsLen(n) == 1: result = semTypeNode(c, n.sons[0], prev) if sonsLen(n) == 1: result = semTypeNode(c, n.sons[0], prev)
else: else:

View file

@ -638,6 +638,7 @@ proc matchUserTypeClass*(c: PContext, m: var TCandidate,
if modifier != tyNone: if modifier != tyNone:
dummyName = param[0] dummyName = param[0]
dummyType = c.makeTypeWithModifier(modifier, a) dummyType = c.makeTypeWithModifier(modifier, a)
if modifier == tyTypeDesc: dummyType.flags.incl tfExplicit
else: else:
dummyName = param dummyName = param
dummyType = a dummyType = a
@ -833,7 +834,8 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
var var
useTypeLoweringRuleInTypeClass = c.c.inTypeClass > 0 and useTypeLoweringRuleInTypeClass = c.c.inTypeClass > 0 and
not c.isNoCall and not c.isNoCall and
f.kind != tyTypeDesc f.kind != tyTypeDesc and
tfExplicit notin aOrig.flags
aOrig = if useTypeLoweringRuleInTypeClass: aOrig = if useTypeLoweringRuleInTypeClass:
aOrig.skipTypes({tyTypeDesc, tyFieldAccessor}) aOrig.skipTypes({tyTypeDesc, tyFieldAccessor})

View file

@ -25,7 +25,7 @@ type
# more complicated static param inference cases # more complicated static param inference cases
m.data is array[TotalElements, T] m.data is array[TotalElements, T]
M.foo(array[0..FromFoo, type m[int, 10]]) m.foo(array[0..FromFoo, type m[int, 10]])
MyMatrix[M, K: static[int]; T] = object MyMatrix[M, K: static[int]; T] = object
data: array[M*K, T] data: array[M*K, T]

View file

@ -15,6 +15,9 @@ type
TObj = object TObj = object
x: int x: int
JSonValue = object
val: string
Sortable = concept x, y Sortable = concept x, y
(x < y) is bool (x < y) is bool
@ -49,9 +52,14 @@ type
staticproc(static[T]) staticproc(static[T])
typeproc T
T.typeproc
typeproc o.type typeproc o.type
o.type.typeproc o.type.typeproc
o.to(type string)
o.to(type JsonValue)
refproc(r, intref) refproc(r, intref)
varproc(v) varproc(v)
p.ptrproc(string) p.ptrproc(string)
@ -59,9 +67,9 @@ type
typeproc(T) typeproc(T)
const TypeName = T.name const TypeName = T.name
type MappedType = type(T.y) type MappedType = type(o.y)
intval T.y intval y(o)
let z = intval(o.y) let z = intval(o.y)
static: static:
@ -80,6 +88,8 @@ proc refproc(x: ref TObj, y: ref int) = discard
proc ptrproc(x: ptr TObj, y: string) = discard proc ptrproc(x: ptr TObj, y: string) = discard
proc staticproc(x: static[TObj]) = discard proc staticproc(x: static[TObj]) = discard
proc typeproc(t: type TObj) = discard proc typeproc(t: type TObj) = discard
proc to(x: TObj, t: type string) = discard
proc to(x: TObj, t: type JSonValue) = discard
proc testFoo(x: TFoo) = proc testFoo(x: TFoo) =
echo x.TypeName echo x.TypeName