Skip to content

Commit

Permalink
don't use binding of auto for inferred return type
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Jan 14, 2024
1 parent 62d8ca4 commit bb6aca0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 8 additions & 0 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,15 @@ proc semInferredLambda(c: PContext, pt: TIdTable, n: PNode): PNode =
#incl(s.flags, sfFromGeneric)
#s.owner = original

let originalReturnType = n.typ[0]
n = replaceTypesInBody(c, pt, n, original)
if originalReturnType != nil and originalReturnType.kind == tyAnything:
# the return type should not be set to a previous binding of `auto`
# this could alternatively be done by changing tyAnything to
# differentiate based on which place it's inferring the type of
# (i.e. a unique instance of tyAnything for every reference to `auto`),
# but for now this is sufficient as we only care to infer return types
n.typ[0] = originalReturnType
result = n
s.ast = result
n[namePos].sym = s
Expand Down
16 changes: 16 additions & 0 deletions tests/overload/t23200.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import std/[sugar, sequtils]

proc dosomething(iter: int -> (iterator: int)) =
discard

proc dosomething(iter: int -> seq[int]) =
discard

proc makeSeq(x: int): seq[int] =
@[x]

# Works fine with 1.6.12 and 1.6.14
dosomething(makeSeq)

# Works with 1.6.12, fails with 1.6.14
dosomething((y) => makeSeq(y))
5 changes: 0 additions & 5 deletions tests/types/t15836_2.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@

discard """
action: "compile"
disabled: true
"""
import std/sugar

type Tensor[T] = object
Expand Down

0 comments on commit bb6aca0

Please sign in to comment.