Skip to content
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

fix object subtype-relation between generic instances #847

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions compiler/sem/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -475,19 +475,23 @@ proc isSubtypeOfGenericInstance(c: var TCandidate; a, f: PType, fGenericOrigin:
## where `f` is a ``tyGenericInst``. The inheritance depth is returned,
## or, if the types are not related, -1.
##
## In case of a match, the unresolved generic parameters of `f` are bound to
## the arguments taken from the base type of actual that matched.
## In case of a subtype relationship existing, the unbound generic parameters
## of `f` are bound to the respective parameters of `a`.
assert f.kind == tyGenericInst
var
askip = skippedNone
fskip = skippedNone

t = a.skipToObject(askip)
last = t ## the unskipped type
last = t ## the most recently compared unskipped type

assert t != nil, "'a' is not object-like type"
discard f.skipToObject(fskip) # only compute the skip kind

if fskip != askip:
# one is a ref|ptr object while the other is not -> no relationship
return -1

proc isEqual(c: var TCandidate, f, a: PType): bool {.nimcall.} =
if f.id == a.id: # fast equality check
result = true
Expand All @@ -507,15 +511,16 @@ proc isSubtypeOfGenericInstance(c: var TCandidate; a, f: PType, fGenericOrigin:

# traverse the type hieararchy until we either reach the end or find a type
# that is equal to `f`
while t != nil and askip == fskip and not isEqual(c, f, last):
while t != nil and not isEqual(c, f, last):
t = t.base
if t.isNil:
break # we reached the end
last = t
t = t.skipToObject(askip)
var skip: SkippedPtr # ignore skip
t = t.skipToObject(skip)
inc result

if t != nil and askip == fskip:
if t != nil:
genericParamPut(c, last, f)
else:
result = -1 # no relationship
Expand Down
23 changes: 22 additions & 1 deletion tests/typerel/tphantom_type_as_base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,25 @@ block with_ptr_object:

proc p(x: ptr Sub): int = 3
# the more precise overload matches:
doAssert p(addr obj) == 3
doAssert p(addr obj) == 3

block with_intermediate_non_object_base:
# test the case where an intermediate base type is a ``ref`` or
# ``ptr`` while formal type is not
type
Root[T] = object of RootObj

First = ref object of Root[int]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no change for this PR. musing outloud really, but I do wonder about this feature... maybe it's ok under the "what else could it be?" although those sorts of things preclude future extension of the language.

Second = object of First

GFirst[T] = ref object of Root[T]
GSecond = object of GFirst[float]

proc p(x: Root[int]): int = 1
proc p(x: Root[float]): int = 2

# test with non-generic intermediate base:
doAssert p(Second()) == 1

# test with generic intermediate base:
doAssert p(GSecond()) == 2
Loading