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

Regression of type inference when using templates and a proc with the same name as one of them #20807

Closed
diegomrsantos opened this issue Nov 10, 2022 · 4 comments · Fixed by #20818

Comments

@diegomrsantos
Copy link

diegomrsantos commented Nov 10, 2022

What happened?

The following code doesn't compile

var s: seq[string]

template fail =
  s = @[]

template test(body: untyped) =
  body

proc test(a: string) =
   discard

test:
  fail()

Note that the test proc must have the same name as the test template and at least one parameter in order to reproduce the issue. It compiles If we change s = @[] to s = newSeq[string]().

Nim Version

Nim Compiler Version 1.7.3 [MacOSX: amd64]
Compiled at 2022-11-10
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 82e99f2
active boot switches: -d:release

Current Standard Output Logs

test.nim(12, 6) template/generic instantiation of `test` from here
test.nim(4, 7) Error: type mismatch: got <array[0..-1, string]>
but expected one of:
proc `@`[IDX, T](a: sink array[IDX, T]): seq[T]
  first type mismatch at position: 1
  required type for a: sink array[0..-1, empty]
  but expression '[]' is of type: array[0..-1, string]

expression: @[]

Expected Standard Output Logs

No response

Possible Solution

No response

Additional Information

It works fine with Nim 1.6.8

@diegomrsantos diegomrsantos changed the title Regression of type inference when using templates and a proc with the same name Regression of type inference when using templates and a proc with the same name as one of them Nov 10, 2022
@Menduist
Copy link
Contributor

Seems to be introduced by #20091 cc @metagn

@ringabout
Copy link
Member

It is the same issue as #20450

@Araq
Copy link
Member

Araq commented Nov 10, 2022

Overloading between untyped and string never was supported properly anyway. The real solution is to get rid of untyped as it leads to non-composable DSLs. Easier said than done, I know.

@metagn
Copy link
Collaborator

metagn commented Nov 10, 2022

I came across this in #20091 but I thought it was an unrelated bug with calling templates with MCS instead.

Seq type inference currently replaces the [] part of @[] nodes with a typed node, but not the @ part which may be a symbol with a resolved generic parameter (in this case empty). The fix would be to either unresolve this symbol or replace the empty generic parameter. I'm not sure exactly how to do these, hopefully someone can understand my explanation and implement it. Relevant lines (deal with n[0] here):

Nim/compiler/semexprs.nim

Lines 2408 to 2411 in a15872b

# seq type inference
var arrayType = newType(tyOpenArray, nextTypeId(c.idgen), expected.owner)
arrayType.rawAddSon(expected[0])
n[1] = semExpr(c, n[1], flags, arrayType)

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 a pull request may close this issue.

5 participants