Skip to content

Line numbers are off in for/of #1266

Closed

Description

@bnoordhuis

Observable when an iterator's .return method produces a stack trace.

var iter = {
    next() {
        return {value:"."[this.i], done:!!this.i++}
    },
    return() {
        print(new Error().stack)
        return {value:undefined, done:true}
    },
    i: 0,
}
var iterable = {[Symbol.iterator](){ return iter }}
for (var _ of iterable) break

Prints:

    at return (u.js:6:19)      // okay
    at <anonymous> (u.js:1:1)  // WRONG

The relevant bytecode with qjs --script -D2:

  111:  get_var         iterable
        for_of_start   
        goto            2:152
        label           5:127
  127:  put_var         _
        goto            3:142
        label           3:142
  142:  goto            4:165
        label           2:152
  152:  for_of_next     0
        if_false        5:127
        drop           
        label           4:165
  165:  iterator_close 
        get_loc         0    ; "<ret>"
        return         

Note how there are no source_loc opcodes anywhere to be seen. There should be one around iterator_close.

Change the body of the loop so it's not functionally empty...

for (var _ of iterable) { Math; break }

...and the right line numbers appear:

    at return (u.js:6:19)
    at <anonymous> (u.js:12:27)

Activity

  1. added
    bug
    on Dec 6, 2025
  2. mvanhorn commented on May 16, 2026

    @mvanhorn
    Contributor

    Implemented in #1473 - bracket iterator_close with source_loc ops carrying the for/of source line.

  3. added a commit that references this issue on May 19, 2026
    bcac5c2
  4. added a commit that references this issue on May 20, 2026
    c14d3b9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions