Include type_parameters declarations in signatures#2550
Conversation
Previously, when a compiler `used create_method_from_def` the generated signature did not include type parameters declarations. This created invalid signatures when type params were used in the method sig This commit moves out type param scanning logic from gem signature listener to rbi helpers and uses it to pass type params declarations through to `create_method_from_def` and `create_method` so that `RBI::Sig.new` will know about them and include them in the generated signature
| def extract_type_parameters(type_strings) | ||
| type_strings.join(", ").scan(TYPE_PARAMETER_MATCHER).flatten.uniq | ||
| end |
There was a problem hiding this comment.
I'd missed this PR earlier, so sorry for the delayed review, but it seems like this functionality should be a part of the inner working of create_method or RBI::Sig construction, since all the inputs to this method seem to always be params and return_type.
I suggest:
- Reverting the signature of
create_methodto remove thetype_paramsparameter - Adding a public method to
RBI::Sigfor doing this calculation and for settingtype_params. Something likeset_type_params_from_params. Btw, we don't need to passreturn_typeto that calculation since there is no way that we can have a type param only in the out position, that means the signature is broken in the first place. - Making
create_methodmethod call this method on the created sig after constructing it.
There was a problem hiding this comment.
Just saw you beat me to this, thank you! (+ thank you for the detailed feedback)
Btw, we don't need to pass return_type to that calculation since there is no way that we can have a type param only in the out position, that means the signature is broken in the first place.
Ah, that makes sense
There was a problem hiding this comment.
I still left the return type in, just in case, so that we don't end up generating broken signatures, even if the signature itself would be meaningless like that.
type_parametersdeclaration #2542Motivation
Previously, when a compiler used create_method_from_def (ex. ActiveSupportCurrentAttributes) any method signatures would be generated without
type_parametersdeclarations. This created invalid signatures when type params were used in the method sigImplementation
This PR moves out
TYPE_PARAMETER_MATCHERand the type param extraction logic from gemsorbet_signatures.rbfile torbi_helper.rbso that they can also be used by dsl. This waycreate_method_from_defcan pass any type params tocreate_methodso thatRBI::Sig.newwill be able to include them in the generated signatureTests
compiler_spec.rbcompiles a class with methods that have signatures:type_parametersdeclarationactive_support_current_attributes_spec.rbcopies types from instance methods to class methods