Skip to content

Support conditional arguments in Sorbet compiler #593

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

Merged
merged 1 commit into from
Jun 23, 2025

Conversation

Mangara
Copy link
Contributor

@Mangara Mangara commented Jun 20, 2025

Fixes #591

When generating types for build_enumerator definitions with both required positional arguments and other types of arguments:

def build_enumerator(user_id, foo_id = nil, cursor:)

The compiler failed with:

/Users/sander/.gem/ruby/3.3.5/gems/job-iteration-1.10.0/lib/tapioca/dsl/compilers/job_iteration.rb:60:in `sort_by': ArgumentError: comparison of NilClass with 1 failed (Parallel::UndumpableException)
	from /Users/sander/.gem/ruby/3.3.5/gems/job-iteration-1.10.0/lib/tapioca/dsl/compilers/job_iteration.rb:60:in `sort_by!'
	from /Users/sander/.gem/ruby/3.3.5/gems/job-iteration-1.10.0/lib/tapioca/dsl/compilers/job_iteration.rb:60:in `block in decorate'
	from /Users/sander/.gem/ruby/3.3.5/gems/tapioca-0.16.5/lib/tapioca/rbi_ext/model.rb:40:in `block in create_class'
	from <internal:kernel>:90:in `tap'
	from /Users/sander/.gem/ruby/3.3.5/gems/tapioca-0.16.5/lib/tapioca/rbi_ext/model.rb:39:in `create_class'
	from /Users/sander/.gem/ruby/3.3.5/gems/tapioca-0.16.5/lib/tapioca/rbi_ext/model.rb:16:in `create_path'
	from /Users/sander/.gem/ruby/3.3.5/gems/job-iteration-1.10.0/lib/tapioca/dsl/compilers/job_iteration.rb:27:in `decorate'
	from /Users/sander/.gem/ruby/3.3.5/gems/sorbet-runtime-0.5.11834/lib/types/private/methods/_methods.rb:279:in `bind_call'
	from /Users/sander/.gem/ruby/3.3.5/gems/sorbet-runtime-0.5.11834/lib/types/private/methods/_methods.rb:279:in `block in _on_method_added'

Looking at the parameter types and indices, this becomes clear:

types: [RBI::ReqParam, RBI::OptParam]
indexes: [nil, 1]

Because RBI::ReqParam is not in PARAM_TYPES_IN_ORDER, its index in that list is nil, and sorting it in a list with integers doesn't work. (The existing tests used either all mandatory positional arguments, or no mandatory positional arguments, so they didn't hit this case, and sorting a list with all nil elements does work.)

It turns out that RBI::Param is the superclass, and doesn't tend to occur as the concrete parameter type, so we can just replace it with RBI::ReqParam to fix it.

I also added some more test cases with optional parameters.

@Mangara Mangara self-assigned this Jun 20, 2025
@Mangara Mangara merged commit 66472d2 into main Jun 23, 2025
45 checks passed
@Mangara Mangara deleted the mangara-fix-positional-args-compiler branch June 23, 2025 15:11
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.

dev rbi dsl is failing with the new version of job-iteration.
3 participants