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

Formatter bug: expecting ), not 'IDENT, bar' #10693

Closed
Blacksmoke16 opened this issue May 9, 2021 · 2 comments · Fixed by #10842
Closed

Formatter bug: expecting ), not 'IDENT, bar' #10693

Blacksmoke16 opened this issue May 9, 2021 · 2 comments · Fixed by #10842
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:tools:formatter

Comments

@Blacksmoke16
Copy link
Member

Blacksmoke16 commented May 9, 2021

Error is similar to #10181, but it still reproduced with #10597 so I'm creating a dedicated issue.

Running the formatter on the code:

 def execute(foo = "foo", bar = "bar")

  pp(foo: foo bar: bar)
end

execute

Produces the error (I ran it with compiled compiler for the backtrace):

from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:5022:9 in 'check'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:2880:9 in 'finish_args'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:2721:9 in 'visit'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:20:7 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:4684:24 in 'indent'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:4708:7 in 'write_indent'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:1397:13 in 'format_nested'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:1388:5 in 'format_nested:write_end_line'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:1424:7 in 'format_nested_with_end'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:1409:5 in 'format_nested_with_end'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:1493:9 in 'visit'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:20:7 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:4684:24 in 'indent'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:262:11 in 'visit'
from /home/george/dev/git/crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:16:7 in 'format'
from /home/george/dev/git/crystal/src/compiler/crystal/tools/formatter.cr:5:5 in 'format'
from /home/george/dev/git/crystal/src/compiler/crystal/command/format.cr:159:7 in 'format'
from /home/george/dev/git/crystal/src/compiler/crystal/command/format.cr:127:16 in 'format_source'
from /home/george/dev/git/crystal/src/compiler/crystal/command/format.cr:123:7 in 'format_file'
from /home/george/dev/git/crystal/src/compiler/crystal/command/format.cr:110:11 in 'format_file_or_directory'
from /home/george/dev/git/crystal/src/compiler/crystal/command/format.cr:103:9 in 'format_many'
from /home/george/dev/git/crystal/src/compiler/crystal/command/format.cr:92:9 in 'run'
from /home/george/dev/git/crystal/src/compiler/crystal/command/format.cr:53:5 in 'format'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:160:7 in 'tool'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:108:7 in 'run'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:49:5 in 'run'
from /home/george/dev/git/crystal/src/compiler/crystal/command.cr:48:3 in 'run'
from /home/george/dev/git/crystal/src/compiler/crystal.cr:11:1 in '__crystal_main'
from /home/george/dev/git/crystal/src/crystal/main.cr:110:5 in 'main_user_code'
from /home/george/dev/git/crystal/src/crystal/main.cr:96:7 in 'main'
from /home/george/dev/git/crystal/src/crystal/main.cr:119:3 in 'main'
from __libc_start_main
from _start
from ???

What's also interesting is that the example code actually compiles fine.

@Blacksmoke16 Blacksmoke16 added the kind:bug A bug in the code. Does not apply to documentation, specs, etc. label May 9, 2021
@straight-shoota
Copy link
Member

the example code actually compiles fine.

😲

Interestingly, it prints {foo: "foo"}. The bar part is entirely vanished.

The same happens with a named tuple literal:

foo = "foo"
{foo: foo bar: foo} # => {foo: "foo"}

@straight-shoota
Copy link
Member

The fact that the example compiles reveales a more general parser bug: A call is incorrectly identified as reading from a local variable if such exists, even with a named argument (and no parenthesis).

The following code is a minimal reproduction:

foo = 1
foo b: 1

The parser entirely skips the named argument. And that actually causes the formatter to error because it traverses the AST and source input in sync and that doesn't line up when the AST presents a var but the source moves on to the named argument.

Fix is in #10842

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:tools:formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants