Skip to content

Infer attr_accessor writer signatures#2667

Merged
Morriar merged 1 commit into
mainfrom
at-fix-attr-accessor-writer-sig
Jul 8, 2026
Merged

Infer attr_accessor writer signatures#2667
Morriar merged 1 commit into
mainfrom
at-fix-attr-accessor-writer-sig

Conversation

@Morriar

@Morriar Morriar commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR teaches gem RBI generation to infer signatures for writers generated by attr_accessor.

When code uses:

sig { returns(Integer) }
attr_accessor :foo

sorbet-runtime attaches the pending signature to the generated reader (foo) only. The generated writer (foo=) has no runtime signature, so Tapioca previously emitted it as untyped:

sig { returns(::Integer) }
def foo; end

def foo=(_arg0); end

With this change, Tapioca infers the writer signature from the matching reader signature:

sig { returns(::Integer) }
def foo; end

sig { params(foo: ::Integer).returns(::Integer) }
def foo=(foo); end

Details

The inference is intentionally conservative:

  • It only applies when the writer has no runtime signature.
  • The method must look like an attr-style writer:
    • method name ends with =
    • exactly one unnamed required parameter
  • A matching reader must exist on the same constant.
  • The reader must have a simple zero-argument signature.
  • The writer and reader’s original source locations must match, ensuring we only infer signatures for methods generated by the same attr_accessor call and avoid separately-defined or manually-defined writer methods.

@Morriar Morriar requested a review from a team as a code owner July 7, 2026 14:28
@Morriar Morriar self-assigned this Jul 7, 2026
@Morriar Morriar added the enhancement New feature or request label Jul 7, 2026
@Morriar Morriar force-pushed the at-fix-attr-accessor-writer-sig branch from ca25ff8 to af71b55 Compare July 7, 2026 14:33
@Morriar Morriar force-pushed the at-fix-attr-accessor-writer-sig branch from af71b55 to 3d448e9 Compare July 7, 2026 15:37
Comment thread lib/tapioca/gem/listeners/methods.rb Outdated
Comment thread spec/tapioca/gem/pipeline_spec.rb
Comment thread lib/tapioca/gem/listeners/methods.rb Outdated
@Morriar Morriar force-pushed the at-fix-attr-accessor-writer-sig branch from 3d448e9 to 4d4bae1 Compare July 7, 2026 19:38
@Morriar

Morriar commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Handled the review comments:

  • inferred writer signatures now return the reader type instead of void
  • moved the source-location check into reader matching
  • added coverage for separate attr_reader/attr_writer declarations

@Morriar Morriar requested a review from paracycle July 8, 2026 14:28
@Morriar Morriar force-pushed the at-fix-attr-accessor-writer-sig branch from 4d4bae1 to 2b10f49 Compare July 8, 2026 14:30
@Morriar Morriar merged commit 47f5e8f into main Jul 8, 2026
19 checks passed
@Morriar Morriar deleted the at-fix-attr-accessor-writer-sig branch July 8, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants