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 in std/sets: len used before its definition #17385

Closed
ghost opened this issue Mar 15, 2021 · 8 comments
Closed

Regression in std/sets: len used before its definition #17385

ghost opened this issue Mar 15, 2021 · 8 comments

Comments

@ghost
Copy link

ghost commented Mar 15, 2021

The title might not be the most descriptive, but I have an example here. Found in https://github.com/mark-summerfield/diff (which uses std/sets)

Example

# first.nim
import std/sets

type
  Diff*[T] = object
    data: T

proc test*[T](diff: Diff[T]) =
  var bPopular = initHashSet[T]()
  for element in bPopular.items():
    echo element
# second.nim
import first

let a = Diff[int]()
a.test()

Current Output

/home/dian/Projects/ircord/tt2.nim(5, 2) template/generic instantiation of `test` from here
/home/dian/Projects/ircord/tt.nim(9, 26) template/generic instantiation of `items` from here
/home/dian/Things/Nim/lib/pure/collections/sets.nim(241, 17) Error: type mismatch: got <HashSet[system.int]>
but expected one of: 
func len(x: (type array) | array): int
  first type mismatch at position: 1
  required type for x: typedesc[array] or array
  but expression 's' is of type: HashSet[system.int]
func len(x: string): int
  first type mismatch at position: 1
  required type for x: string
  but expression 's' is of type: HashSet[system.int]
func len[TOpenArray: openArray | varargs](x: TOpenArray): int
  first type mismatch at position: 1
  required type for x: TOpenArray: openArray or varargs
  but expression 's' is of type: HashSet[system.int]
func len[T](x: seq[T]): int
  first type mismatch at position: 1
  required type for x: seq[T]
  but expression 's' is of type: HashSet[system.int]
func len[T](x: set[T]): int
  first type mismatch at position: 1
  required type for x: set[T]
  but expression 's' is of type: HashSet[system.int]
proc len(w: WideCString): int
  first type mismatch at position: 1
  required type for w: WideCString
  but expression 's' is of type: HashSet[system.int]
proc len(x: cstring): int
  first type mismatch at position: 1
  required type for x: cstring
  but expression 's' is of type: HashSet[system.int]
proc len[U: Ordinal; V: Ordinal](x: HSlice[U, V]): int
  first type mismatch at position: 1
  required type for x: HSlice[len.U, len.V]
  but expression 's' is of type: HashSet[system.int]

expression: len(s)

Expected Output

Successful compilation

Additional Information

Works in 1.4.4

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-03-15
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 51a04a3674d62707506774b11a4376a914735aeb
active boot switches: -d:release -d:danger
@ghost ghost added the Regression label Mar 15, 2021
@ghost
Copy link
Author

ghost commented Mar 15, 2021

Sorry, it's not a regression but rather a change in the sets module I think.

@ghost
Copy link
Author

ghost commented Mar 15, 2021

updated the example

@narimiran
Copy link
Member

Since this seems to be len-related, this might be the PR which introduced the change in the behaviour:

#16959

@ghost
Copy link
Author

ghost commented Mar 15, 2021

Caused by #16959

@ghost
Copy link
Author

ghost commented Mar 15, 2021

0cf3ba159c97d3aa00bcb408e99cd4bdef33d64f is the first bad commit
commit 0cf3ba159c97d3aa00bcb408e99cd4bdef33d64f
Author: flywind <43030857+xflywind@users.noreply.github.com>
Date:   Mon Feb 8 04:47:28 2021 -0600

    close #15767 (#16959)
    
    * fix some warnings
    
    * close #15767
    
    * Revert "fix some warnings"
    
    This reverts commit 39f2f23b0026d50c42af7be3ad80edf0f1f19610.

 lib/pure/collections/sets.nim | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
bisect run success

narimiran added a commit to narimiran/Nim that referenced this issue Mar 15, 2021
@timotheecour
Copy link
Member

timotheecour commented Mar 15, 2021

@Yardanico I tried the original example you had reported before editing it to add import std/sets etc, where you said it was working in 1.4.4; for me it didn't work on 1.4.4 either, can you double check?

furthermore:

for reference:

# first.nim
type
  HashSet*[T] = object
    data: T
    counter: int

iterator items*[T](s: HashSet[T]): int = 
  let length = s.len()
  yield length

proc len*[T](hs: HashSet[T]): int =
  result = hs.counter
# second.nim
import first

type
  Other*[T] = object
    data: T

proc test*[T](x: Other[T]) =
  var test = HashSet[T]()
  for x in test.items():
    echo x
# third.nim
import second

var other = Other[int]()
other.test()

@timotheecour timotheecour changed the title Regression when using a generic proc in an iterator that is defined after the proc Regression in std/sets: len used before its definition Mar 15, 2021
@ghost
Copy link
Author

ghost commented Mar 15, 2021

@timotheecour well I specifically edited the example because it was a wrong one, see #17385 (comment)

@timotheecour
Copy link
Member

ok, that clarifies, thanks! (updated the example didn't imply this :) )

ringabout pushed a commit to ringabout/Nim that referenced this issue Mar 22, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
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