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

Don't skip generic aliases when reading a generic parameter #24822

Draft
wants to merge 4 commits into
base: devel
Choose a base branch
from
Draft
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
14 changes: 10 additions & 4 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,8 @@ const
tyDotOpTransparent = {tyVar, tyLent, tyPtr, tyRef, tyOwned, tyAlias, tySink}

proc readTypeParameter(c: PContext, typ: PType,
paramName: PIdent, info: TLineInfo): PNode =
paramName: PIdent, info: TLineInfo, skip = true): PNode =
## - **skip**: Skips generic aliases and tries to get the parameter from the aliased type.
# Note: This function will return emptyNode when attempting to read
# a static type parameter that is not yet resolved (e.g. this may
# happen in proc signatures such as `proc(x: T): array[T.sizeParam, U]`
Expand All @@ -1387,8 +1388,12 @@ proc readTypeParameter(c: PContext, typ: PType,
discard

if typ.kind != tyUserTypeClass:
let ty = if typ.kind == tyCompositeTypeClass: typ.firstGenericParam.skipGenericAlias
else: typ.skipGenericAlias
let ty = block:
let ty = if typ.kind == tyCompositeTypeClass: typ.firstGenericParam
else: typ
if skip: typ.skipGenericAlias()
else: typ

let tbody = ty[0]
for s in 0..<tbody.len-1:
let tParam = tbody[s]
Expand Down Expand Up @@ -1519,7 +1524,8 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
proc tryReadingGenericParam(c: PContext, n: PNode, i: PIdent, t: PType): PNode =
case t.kind
of tyGenericInst:
result = readTypeParameter(c, t, i, n.info)
# Parameter could exist on the alias, don't skip past it
result = readTypeParameter(c, t, i, n.info, skip=false)
if result == c.graph.emptyNode:
if c.inGenericContext > 0:
result = semGenericStmt(c, n)
Expand Down
14 changes: 14 additions & 0 deletions tests/generics/t24791.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std/options

type
Foo[D: static int; T] = T
Bar[D] = Option[D]
Bizz = Foo[1, string]
Buzz = Foo[2, Option[string]]

CheckPriority[T: static int; D] = Option[D]

assert Bar[string].D is string
assert Bizz.D == 1
assert Buzz.D == 2
assert CheckPriority[3, string].T is string
Loading