Propagate block return types to method return types#112
Closed
dak2 wants to merge 1 commit into
Closed
Conversation
8b77fcb to
e1b6e44
Compare
Methods like `map` return generic types parameterized by block return values
(e.g., `[1,2].map { |x| x.to_s }` → `Array[String]`). Without this, the
type checker could not infer the element type of collections produced by
block-yielding methods, causing false positives on subsequent method calls.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
e1b6e44 to
d928289
Compare
There was a problem hiding this comment.
Pull request overview
This PR improves generic return inference for block-yielding methods so method results can reflect block body return types.
Changes:
- Adds generic RBS type parsing and uses it when loading cached RBS method signatures.
- Introduces
BlockReturnTypeBoxto substitute block return types into method return types. - Adds Ruby and Rust tests for generic parsing and block return propagation.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
test/range_test.rb |
Updates expected Range#to_a inference to resolved Array[Integer]. |
test/block_test.rb |
Adds coverage for map block return propagation and related cases. |
core/src/rbs/converter.rs |
Adds parsing for generic RBS types and tests. |
core/src/graph/mod.rs |
Re-exports BlockReturnTypeBox. |
core/src/graph/box.rs |
Adds return type variable resolution and block return propagation box. |
core/src/checker.rs |
Uses RbsTypeConverter::parse for cached return types. |
core/src/cache/rbs_cache.rs |
Removes the old simple return type parser usage. |
core/src/analyzer/dispatch.rs |
Wires block processing results into BlockReturnTypeBox. |
core/src/analyzer/blocks.rs |
Returns block parameter vertices plus the block body’s last expression vertex. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+497
to
+505
| for recv_ty in &recv_types { | ||
| let Some(info) = genv.resolve_method(recv_ty, &self.method_name) else { | ||
| continue; | ||
| }; | ||
|
|
||
| // Reuse resolve_return_type with block_return_union to substitute | ||
| // both receiver type variables and block output type variables in one pass. | ||
| if let Some(resolved) = | ||
| resolve_return_type(&info.return_type, recv_ty, Some(&block_return_union)) |
Comment on lines
+588
to
+598
| if let Some(body_vtx) = block_result.body_last_vtx { | ||
| let box_id = genv.alloc_box_id(); | ||
| let ret_box = BlockReturnTypeBox::new( | ||
| box_id, | ||
| recv_vtx, | ||
| method_name, | ||
| body_vtx, | ||
| ret_vtx, | ||
| ); | ||
| genv.register_box(box_id, Box::new(ret_box)); | ||
| } |
| type_args: resolved_args, | ||
| }) | ||
| } | ||
| _ => Some(return_type.clone()), |
Comment on lines
+56
to
+59
| let type_args: Vec<Type> = split_type_args(args_str) | ||
| .into_iter() | ||
| .map(Self::parse) | ||
| .collect(); |
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.
Motivation
Methods like
mapreturn generic types parameterized by block return values (e.g.,[1,2].map { |x| x.to_s }→Array[String]). Without this propagation, the type checker could not infer the element type of collections produced by block-yielding methods, causing false positives on subsequent method calls like.upcase.Changes
BlockResultstruct to return block body's last expression vertex fromblocks.rsBlockReturnTypeBoxthat observes block return type and substitutes type variables in method return typesRbsTypeConverter::parseto handle generic types (e.g.,Array[Elem],Hash[K, V])is_type_variable_name/is_block_type_variable_nameas module-level functions and addresolve_return_typefor shared type variable resolutionSerializableMethodInfo::return_type()method (replaced byRbsTypeConverter::parse)Checked
cargo test --libpassesbundle exec rake testpasses🤖 Generated with Claude Code