fix #5689
This commit is contained in:
parent
d578815963
commit
bf4ce87e5b
4 changed files with 82 additions and 11 deletions
|
|
@ -904,8 +904,9 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
|
||||||
allowMetaTypes = true)
|
allowMetaTypes = true)
|
||||||
result = liftingWalk(expanded, true)
|
result = liftingWalk(expanded, true)
|
||||||
|
|
||||||
of tyUserTypeClasses, tyBuiltInTypeClass, tyAnd, tyOr, tyNot:
|
of tyUserTypeClasses, tyBuiltInTypeClass, tyCompositeTypeClass,
|
||||||
result = addImplicitGeneric(copyType(paramType, getCurrOwner(c), true))
|
tyAnd, tyOr, tyNot:
|
||||||
|
result = addImplicitGeneric(copyType(paramType, getCurrOwner(c), false))
|
||||||
|
|
||||||
of tyGenericParam:
|
of tyGenericParam:
|
||||||
markUsed(info, paramType.sym, c.graph.usageSym)
|
markUsed(info, paramType.sym, c.graph.usageSym)
|
||||||
|
|
|
||||||
|
|
@ -1383,13 +1383,14 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
||||||
if f.isResolvedUserTypeClass:
|
if f.isResolvedUserTypeClass:
|
||||||
result = typeRel(c, f.lastSon, a)
|
result = typeRel(c, f.lastSon, a)
|
||||||
else:
|
else:
|
||||||
var matched = matchUserTypeClass(c.c, c, f, aOrig)
|
considerPreviousT:
|
||||||
if matched != nil:
|
var matched = matchUserTypeClass(c.c, c, f, aOrig)
|
||||||
bindConcreteTypeToUserTypeClass(matched, a)
|
if matched != nil:
|
||||||
put(c, f, matched)
|
bindConcreteTypeToUserTypeClass(matched, a)
|
||||||
result = isGeneric
|
if doBind: put(c, f, matched)
|
||||||
else:
|
result = isGeneric
|
||||||
result = isNone
|
else:
|
||||||
|
result = isNone
|
||||||
|
|
||||||
of tyCompositeTypeClass:
|
of tyCompositeTypeClass:
|
||||||
considerPreviousT:
|
considerPreviousT:
|
||||||
|
|
@ -1407,6 +1408,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
||||||
if result != isNone:
|
if result != isNone:
|
||||||
put(c, f, a)
|
put(c, f, a)
|
||||||
result = isGeneric
|
result = isGeneric
|
||||||
|
|
||||||
of tyGenericParam:
|
of tyGenericParam:
|
||||||
var x = PType(idTableGet(c.bindings, f))
|
var x = PType(idTableGet(c.bindings, f))
|
||||||
if x == nil:
|
if x == nil:
|
||||||
|
|
@ -1436,7 +1438,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
||||||
result = isNone
|
result = isNone
|
||||||
else:
|
else:
|
||||||
if f.sonsLen > 0 and f.sons[0].kind != tyNone:
|
if f.sonsLen > 0 and f.sons[0].kind != tyNone:
|
||||||
result = typeRel(c, f.lastSon, a)
|
result = typeRel(c, f.lastSon, a, false)
|
||||||
if doBind and result notin {isNone, isGeneric}:
|
if doBind and result notin {isNone, isGeneric}:
|
||||||
let concrete = concreteType(c, a)
|
let concrete = concreteType(c, a)
|
||||||
if concrete == nil: return isNone
|
if concrete == nil: return isNone
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ echo p2 is AbstractPointOfFloat # true
|
||||||
echo p2.x is float and p2.y is float # true
|
echo p2.x is float and p2.y is float # true
|
||||||
|
|
||||||
# https://github.com/nim-lang/Nim/issues/2018
|
# https://github.com/nim-lang/Nim/issues/2018
|
||||||
type ProtocolFollower = generic
|
type ProtocolFollower = concept
|
||||||
true # not a particularly involved protocol
|
true # not a particularly involved protocol
|
||||||
|
|
||||||
type ImplementorA = object
|
type ImplementorA = object
|
||||||
|
|
|
||||||
68
tests/generics/tbindoncevsbindmany.nim
Normal file
68
tests/generics/tbindoncevsbindmany.nim
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
template accept(x) =
|
||||||
|
static: assert(compiles(x))
|
||||||
|
|
||||||
|
template reject(x) =
|
||||||
|
static: assert(not compiles(x))
|
||||||
|
|
||||||
|
type
|
||||||
|
ObjectWithNumber = concept obj
|
||||||
|
obj.number is int
|
||||||
|
|
||||||
|
Foo[T] = object
|
||||||
|
x: T
|
||||||
|
|
||||||
|
type A = object
|
||||||
|
anumber: int
|
||||||
|
|
||||||
|
type B = object
|
||||||
|
bnumber: int
|
||||||
|
|
||||||
|
proc number(a: A): int = a.anumber
|
||||||
|
proc number(b: B): int = b.bnumber
|
||||||
|
|
||||||
|
proc notDistincConcept1(a: ObjectWithNumber, b: ObjectWithNumber) = discard
|
||||||
|
proc notDistincConcept2(a, b: ObjectWithNumber) = discard
|
||||||
|
proc distinctConcept1(a, b: distinct ObjectWithNumber) = discard
|
||||||
|
proc distinctConcept2(a: ObjectWithNumber, b: distinct ObjectWithNumber) = discard
|
||||||
|
proc distinctConcept3(a: distinct ObjectWithNumber, b: ObjectWithNumber) = discard
|
||||||
|
proc distinctConcept4(a: distinct ObjectWithNumber, b: distinct ObjectWithNumber) = discard
|
||||||
|
|
||||||
|
var a = A(anumber: 5)
|
||||||
|
var b = B(bnumber: 6)
|
||||||
|
|
||||||
|
accept notDistincConcept1(a, a)
|
||||||
|
accept notDistincConcept1(b, b)
|
||||||
|
reject notDistincConcept2(a, b)
|
||||||
|
|
||||||
|
accept notDistincConcept2(a, a)
|
||||||
|
accept notDistincConcept2(b, b)
|
||||||
|
reject notDistincConcept2(a, b)
|
||||||
|
|
||||||
|
accept distinctConcept1(a, b)
|
||||||
|
accept distinctConcept2(a, b)
|
||||||
|
accept distinctConcept3(a, b)
|
||||||
|
accept distinctConcept4(a, b)
|
||||||
|
|
||||||
|
proc nonDistincGeneric1(a: Foo, b: Foo) = discard
|
||||||
|
proc nonDistincGeneric2(a, b: Foo) = discard
|
||||||
|
proc distinctGeneric1(a, b: distinct Foo) = discard
|
||||||
|
proc distinctGeneric2(a: distinct Foo, b: Foo) = discard
|
||||||
|
proc distinctGeneric3(a: Foo, b: distinct Foo) = discard
|
||||||
|
proc distinctGeneric4(a: distinct Foo, b: distinct Foo) = discard
|
||||||
|
|
||||||
|
var f1 = Foo[int](x: 10)
|
||||||
|
var f2 = Foo[string](x: "x")
|
||||||
|
|
||||||
|
accept nonDistincGeneric1(f1, f1)
|
||||||
|
accept nonDistincGeneric1(f2, f2)
|
||||||
|
reject nonDistincGeneric1(f1, f2)
|
||||||
|
|
||||||
|
accept nonDistincGeneric2(f1, f1)
|
||||||
|
accept nonDistincGeneric2(f2, f2)
|
||||||
|
reject nonDistincGeneric2(f1, f2)
|
||||||
|
|
||||||
|
accept distinctGeneric1(f1, f1)
|
||||||
|
accept distinctGeneric2(f1, f1)
|
||||||
|
accept distinctGeneric3(f1, f1)
|
||||||
|
accept distinctGeneric4(f1, f1)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue