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

typeof(a[0]) in generic proc return type gives: Error: type mismatch #14877

Open
timotheecour opened this issue Jul 2, 2020 · 1 comment
Open

Comments

@timotheecour
Copy link
Member

timotheecour commented Jul 2, 2020

typeof(a[0]) in proc return type gives: Error: type mismatch

Example

# proc fn[T](a: T, index: int): typeof(a.x) = a.x # ok
proc fn2(a: seq[int], index: int): typeof(a[0]) = a[index] # ok
# proc fn[T](a: T, index: int): typeof(a[0]) = a[index] # Error: type mismatch: got <T, int literal(0)>

Current Output

Error: type mismatch: got <T, int literal(0)>

Expected Output

works

Additional Information

05384ef 1.3.5 devel

note

I'm wondering if it's due to the way [] is hardcoded in compiler in a few places eg:

semArrGet:
x[0] = newIdentNode(getIdent(c.cache, "[]"), n.info)

  of nkCallKinds:
    # builtin slice keeps lvalue-ness:
    if getMagic(n) in {mArrGet, mSlice}:
      result = isAssignable(owner, n[1], isUnsafeAddr)

proc matches*(c: PContext, n, nOrig: PNode, m: var TCandidate) =
  if m.magic in {mArrGet, mArrPut}:

  elif op.name.s == "[]" and op.fromSystem:
    let slice = n[2].skipStmtList
    c.addSlice(n, n[1], slice[1], slice[2])
    analyseSons(c, n)
  elif op.name.s == "[]=" and op.fromSystem:
    let slice = n[2].skipStmtList
    c.addSlice(n, n[1], slice[1], slice[2])
    analyseSons(c, n)

    if ident != nil and ident.s == "[]":
@timotheecour timotheecour changed the title typeof(a[0]) in proc return type gives: Error: type mismatch typeof(a[0]) in generic proc return type gives: Error: type mismatch Jul 2, 2020
@metagn metagn self-assigned this Aug 20, 2024
@metagn
Copy link
Collaborator

metagn commented Sep 18, 2024

Fixed in #24067

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants