Skip to content

Implement negative line numbers - #1944

Merged
kddnewton merged 2 commits into
ruby:mainfrom
casperisfine:signed-varints
Nov 29, 2023
Merged

kddnewton merged 2 commits into
ruby:mainfrom
casperisfine:signed-varints

Conversation

@casperisfine

Copy link
Copy Markdown
Contributor

Fix: #1783

Ruby allows for 0 or negative line start, this is often used with eval calls to get a correct offset when prefixing a snippet.

e.g.

caller = caller_locations(1, 1).first
class_eval <<~RUBY, caller.path, caller.line - 2
  # frozen_string_literal: true
  def some_method
    #{caller_provided_code_snippet}
  end
RUBY

The first commit rename all varint stuff to varuint to avoid confusion.

The second commit introduce the varsint (signed) type, and change line to be signed.

@casperisfine

Copy link
Copy Markdown
Contributor Author

Looks like I broke JRuby and Truffle. I suppose there's some Java version of varints? I'll have a look.

Comment thread templates/lib/prism/serialize.rb.erb Resolved
Comment thread templates/lib/prism/serialize.rb.erb Resolved
@eregon

eregon commented Nov 29, 2023 •

Copy link
Copy Markdown
Member

Looks like I broke JRuby and Truffle. I suppose there's some Java version of varints? I'll have a look.

All the FFI backend CI jobs failed, including the one using CRuby.
Use PRISM_FFI_BACKEND=true to debug it locally on CRuby.
Probably an issue in encoding or decoding of varsint.

Interestingly currently there is no need to support encoding/decoding of varsint in Java yet because it's only used in the metadata for parse and not the result of parsing.
EDIT: I misread, it's needed.

Line numbers may be negative, so we need to introduce signed varint,
so renaming unsigned ones first avoid confusion.
@eregon

eregon commented Nov 29, 2023

Copy link
Copy Markdown
Member

In Loader.java.erb there is source.setStartLine(loadVarInt()); so that needs to change too.

@casperisfine
casperisfine force-pushed the signed-varints branch 2 times, most recently from 0222cda to 71f5e64 Compare November 29, 2023 11:49
@casperisfine

Copy link
Copy Markdown
Contributor Author

Alright, I implemented the missing Java part, and also switched to ZigZag endcoding as it's indeed much simpler.

CI is now green.

Some docs may need updating though? This isn't exactly my forte and I don't know much about prism.

@eregon eregon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

Comment thread docs/serialization.md Resolved
Comment thread src/prism.c Resolved
Comment thread src/util/pm_buffer.c Resolved
Ruby allows for 0 or negative line start, this is often used
with `eval` calls to get a correct offset when prefixing a snippet.

e.g.

```ruby
caller = caller_locations(1, 1).first
class_eval <<~RUBY, caller.path, caller.line - 2
  # frozen_string_literal: true
  def some_method
    #{caller_provided_code_snippet}
  end
RUBY
```

@kddnewton kddnewton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Jean!

We'll also need to update the deserializer for JS, but I'll handle that.

@kddnewton
kddnewton merged commit 244a0c7 into ruby:main Nov 29, 2023
@casperisfine
casperisfine deleted the signed-varints branch November 29, 2023 14:39
@eregon eregon mentioned this pull request Dec 8, 2023
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

Successfully merging this pull request may close these issues.

Accept negative start line number

4 participants