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

[0.9.27 regression] Parser gets confused by aref assignment #1420

Open
dazuma opened this issue Jan 1, 2022 · 2 comments
Open

[0.9.27 regression] Parser gets confused by aref assignment #1420

dazuma opened this issue Jan 1, 2022 · 2 comments

Comments

@dazuma
Copy link

dazuma commented Jan 1, 2022

Create the following file foo.rb:

module Foo
  a = {}

  # Earlier assignment
  a[:b] = 1

  # This constant fails to parse correctly
  X = a[:b]
end

Run yardoc with this file, using yard 0.9.27 on Ruby 3.0.3. It results in an exception similar to the following:

[error]: Exception occurred while generating 'Foo.html'
[error]: NoMethodError: undefined method `[]' for nil:NilClass
[error]: Stack trace:
	/Users/dazuma/opt/asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/yard-0.9.27/lib/yard/templates/helpers/method_helper.rb:69:in `format_constant'
	/Users/dazuma/opt/asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/yard-0.9.27/templates/default/module/html/constant_summary.erb:15:in `block (2 levels) in _erb_cache_18'
	/Users/dazuma/opt/asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/yard-0.9.27/templates/default/module/html/constant_summary.erb:11:in `each'
	/Users/dazuma/opt/asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/yard-0.9.27/templates/default/module/html/constant_summary.erb:11:in `block in _erb_cache_18'
	/Users/dazuma/opt/asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/yard-0.9.27/templates/default/module/setup.rb:148:in `groups'
	/Users/dazuma/opt/asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/yard-0.9.27/templates/default/module/html/constant_summary.erb:4:in `_erb_cache_18'

I've tracked this down as far as determining that the value for the constant node for X is empty due to the node having an invalid source_range where the ending position is less than the beginning. The ending position is actually coming from the previous aref from the line a[:b] = 1. The parser doesn't seem to consume that aref when it is on the LHS of an assignment operator, so when the X = a[:b] line gets parsed in RipperParser#on_aref, it's getting the ending position from the previous instance.

This appears to be a regression in 0.9.27. It does not happen in 0.9.26.
Ruby 3.0.3.
2019 Macbook Pro running Monterey 12.1, although I doubt that matters.

@notEthan
Copy link
Contributor

notEthan commented Feb 4, 2022

I am encountering this as well. without really grokking this code:
https://github.com/lsegal/yard/blob/v0.9.27/lib/yard/parser/ruby/ruby_parser.rb#L373
I tried flipping the shift to a pop, since the correct entry of @map[:aref] was in the last position, which resolved my errors.
but I see this change was made in #1389 and the test added there fails when it is a pop.
some other fix is needed.

@dduugg
Copy link
Contributor

dduugg commented Aug 25, 2022

👋 I'm the author of the PR that caused the regression, thanks for flagging it @notEthan.

Do one of you want to create a branch with a failing test? I'll take a look and see if I can land a fix things up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants