Repository navigation
Attach comments above annotations to constant and global declarations - #3245
Merged
Merged
Conversation
parse_const_decl and parse_global_decl did not receive comment_pos, so
they looked up the comment only from the line of the name token. Other
declarations (class, module, interface, type alias, members) use the
start of the annotations as the base line, so a comment placed above
the annotations was silently dropped for constants and globals:
module M
# const doc
%a{deprecated}
C: bool # => comment was nil
end
# doc
%a{deprecated}
$g: String # => comment was nil
`rbs annotate` inserts new comments above existing annotations, so it
produced this layout itself and the result could not be read back.
Pass the annotation start position to both functions. When no comment
is found above the annotations, fall back to the line of the name so
that the existing `%a{...}` / `# comment` / `CONST: T` layout keeps
working.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fallback kept a comment placed between the annotations and the name of a constant or global. That layout only worked because comment_pos was not passed when annotations were added to constants and globals (1bc9bb4, 075d27c). Class, module, interface, type alias, and members have never accepted it. No annotated constant or global exists in core, stdlib, or gem_rbs_collection, so nothing relies on the old layout. Read the comment from the line above the annotations, the same as the other declarations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parse_const_declandparse_global_declinsrc/parser.cdid not takecomment_pos. They looked up the comment only from the line of the name token. Other declarations (class, module, interface, type alias, and members) use the start of the annotations as the base line. So a comment placed above the annotations was dropped for constants and globals:Annotations were added to constants and globals in 1bc9bb4 and 075d27c. Those commits passed
annotationsbut notcomment_pos, so these two declarations never followed the rule the other declarations use.I found this through the
commentsCI failure on #3242, which adds%a{deprecated}toOpenSSL::OPENSSL_FIPS.rbs annotateinserts new comments above existing annotations (Rewriter#add_comment), so it writes this layout itself. Then the parser cannot read the comment back, andrake annotate confirm_annotationis not stable.Fix
Pass the annotation start position (
annot_pos) toparse_const_declandparse_global_decl, from bothparse_decl(top level) andparse_nested_decl(class/module bodies). Then userbs_nonnull_pos_or(comment_pos, decl_range.start)as the base line, the same asparse_type_decl.Behavior change
A comment placed between the annotations and the name of a constant or global used to be attached. With this change it is not, which matches class, module, def, and type alias:
%a{deprecated} # doc FOO: String # comment was "doc", now nil (same as class/def)No constant or global in
core/,stdlib/, or gem_rbs_collection has annotations today, so I don't know of any signature that relies on the old layout.Verification
test/rbs/signature_parsing_test.rbcover top-level constants, nested constants, and globals. Each test checks three layouts: comment above the annotations (attached), comment below the annotations (nil, like other declarations), and no annotation (attached). The comment-above case fails before this change.rake testandrake validatepass.rake annotatewith Ruby v4.0.7 RDoc followed bygit diff --exit-code core stdlibshows no diff.%a{deprecated}toOPENSSL_FIPSin a copy ofstdlib/openssl/0/openssl.rbsand ranrbs annotatetwice. The second run produced no change.Related: #3246 fixes
RBS::Writerdropping annotations on the same declarations.🤖 Generated with Claude Code