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

typRel and sumGeneric adjustments #23137

Merged
merged 2 commits into from
Dec 31, 2023
Merged

typRel and sumGeneric adjustments #23137

merged 2 commits into from
Dec 31, 2023

Conversation

Graveflo
Copy link
Contributor

Filling in some more logic in typeRel that I came across when poking the compiler in another PR. Some of the cases where typeRel returns an "incorrect" result are actually common, but sumGeneric ends up breaking the tie correctly. There isn't anything wrong with that necessarily, but I assume that it's preferred these functions behave just as well in isolation as they do when integrated.

I will be following up this description with specific examples.

@Graveflo
Copy link
Contributor Author

Graveflo commented Dec 28, 2023

These examples do not actually translate to bugs in the current version of the compiler! This is just to demonstrate the output of the relevant functions.

Ptr and Ref

type A[T] = ref object
proc p(M: ref object): bool = false
proc p(M: A): bool = true
var s = new(A[int])
discard p(s)

OK:

f a output
tyCompositeTypeClass(A) tyRef(ref object) isNone
tyRef(A) tyRef(ref object) isNone
tyObject(A:ObjectType) tyBuiltInTypeClass(object) isNone

not OK:

f a output
tyRef(ref object) tyCompositeTypeClass(A) isNone

ref object should have a generic relationship with A. Adjustment in getObjectType and in the tyPtr, tyRef branch of typeRel

Array

type IntArray[N: static[int]] = array[N, int]
proc p(a: IntArray): bool= true
proc p[T;N](a: array[N,T]): bool= false

var s: IntArray[5]
discard s.p()

not OK:

f a output
tyArray(array[N, T]) tyCompositeTypeClass(IntArray) isNone

OK:

f a output
tyCompositeTypeClass(IntArray) tyArray(array[N, T]) isNone
tyArray(IntArray) tyArray(array[N, T]) isNone
tyInt(int) tyGenericParam(T) isNone

Same idea as above

Need to devalue isInferred

proc foo[T](x: proc(): T): bool = return true
proc foo(x: proc(): int): bool = return false
discard foo(proc (): auto {.nimcall.} = 88)
f a output
tyProc(proc (): int{.closure.}) tyProc(proc (): T{.closure.}) isInferred
tyGenericParam(T) tyInt(int) isGeneric
f a output
tyProc(proc (): T{.closure.}) tyProc(proc (): int{.closure.}) isGeneric
tyGenericParam(T) tyInt(int) isGeneric

Both of these are okay, but one of them ultimately returns isInferred and the other isGeneric. In this situation the generic match should be preferred to the inferred one. Adjustment in checkGeneric

Obscured base class

type
X = ref object
Z[T] = X

proc p[H](x:Z[H]):bool = false
proc p(x: ref object): bool = true
echo p(new(Z[int]))

not OK:

f a output
tyRef(ref object) tyGenericInvocation(Z[p.H]) isNone

OK:

f a output
tyGenericInvocation(Z[p.H]) tyRef(ref object) isNone
tyGenericBody(Z) tyRef(ref object) isNone
tyRef(Z) tyRef(ref object) isNone
tyObject(X:ObjectType) tyBuiltInTypeClass(object) isNone

Changing in getObjectType:

of tyGenericInvocation:
  result = getObjectType(f.baseClass)

Edit: I should also mention #22143 for organizational purposes, since that is the PR that lead to finding these

@Graveflo Graveflo marked this pull request as ready for review December 28, 2023 07:54
@Araq Araq merged commit ccc7c45 into nim-lang:devel Dec 31, 2023
19 checks passed
Copy link
Contributor

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from ccc7c45

Hint: mm: orc; opt: speed; options: -d:release
177580 lines; 7.500s; 767.785MiB peakmem

@Graveflo Graveflo deleted the sumg-ovrb branch December 31, 2023 18:33
narimiran pushed a commit that referenced this pull request Aug 31, 2024
Filling in some more logic in `typeRel` that I came across when poking
the compiler in another PR. Some of the cases where `typeRel` returns an
"incorrect" result are actually common, but `sumGeneric` ends up
breaking the tie correctly. There isn't anything wrong with that
necessarily, but I assume that it's preferred these functions behave
just as well in isolation as they do when integrated.

I will be following up this description with specific examples.

(cherry picked from commit ccc7c45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants