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

Invalid generic type deduction in mapIt #23418

Closed
arnetheduck opened this issue Mar 18, 2024 · 6 comments
Closed

Invalid generic type deduction in mapIt #23418

arnetheduck opened this issue Mar 18, 2024 · 6 comments

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Mar 18, 2024

Description

import sequtils

type F[T, E] = object

proc start(v: int): F[void, (ValueError,)] = discard
proc stop(v: int): F[void, tuple[]] = discard

var a = mapIt([1, 2, 3], start(it))

assert $type(a) == "seq[F[system.void, (ValueError,)]]", $type(a)

var b = mapIt([1, 2, 3], stop(it))

assert $type(b) == "seq[F[system.void, tuple[]]]", $type(b)

The expectation is that the b type is derived from stop, yet the start type gets reused..

Nim Version

1.6, devel

Current Output

Error: unhandled exception: testit2.nim(14, 8) `$type(b) == "seq[F[system.void, tuple[]]]"` seq[F[system.void, (ValueError,)]] [AssertionDefect]

Expected Output

No response

Possible Solution

No response

Additional Information

No response

@arnetheduck
Copy link
Contributor Author

Notably, if a and its assert is removed, the correct type is deduced for b - the presence of the deduction for a breaks b.

@SirOlaf
Copy link
Contributor

SirOlaf commented Mar 18, 2024

Looks like a variation of #22479

@arnetheduck
Copy link
Contributor Author

ah, right but without destructor this time..

The type system just does not see a generic body without generic parameters as a different type.

this applies though - also in the real bug of which this is a reproduction

@arnetheduck
Copy link
Contributor Author

fwiw, we've seen this bug in many different places though we never teased out a repro as simple as here - the showstopper label on 22479 feels appropritate

@arnetheduck
Copy link
Contributor Author

workaround:

import sequtils

type F[T, E] = object
  dummy: array[0, E]

proc start(v: int): F[void, (ValueError,)] = discard
proc stop(v: int): F[void, tuple[]] = discard

var a = mapIt([1, 2, 3], start(it))

assert $type(a) == "seq[F[system.void, (ValueError,)]]", $type(a)

var b = mapIt([1, 2, 3], stop(it))

assert $type(b) == "seq[F[system.void, tuple[]]]", $type(b)

@Araq
Copy link
Member

Araq commented Jun 7, 2024

Reduced example:

template mapIt*(x: untyped): untyped =
  type OutType = typeof(x, typeOfProc)
  newSeq[OutType](5)

type F[T, E] = object

proc start(v: int): F[void, (ValueError,)] = discard
proc stop(v: int): F[void, tuple[]] = discard

var a = mapIt(start(9))
assert $type(a) == "seq[F[system.void, (ValueError,)]]", $type(a)

var b = mapIt(stop(9))
assert $type(b) == "seq[F[system.void, tuple[]]]", $type(b)

Araq added a commit that referenced this issue Jun 9, 2024
@Araq Araq closed this as completed in 56c9575 Jun 9, 2024
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

3 participants