Skip to content

Guess instance variable types from global method calls#16119

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/guess-global-call
Sep 8, 2025
Merged

Guess instance variable types from global method calls#16119
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/guess-global-call

Conversation

@HertzDevil
Copy link
Contributor

This allows the following to compile:

def foo(x : Int32) : Float64
  # ...
end

class Bar
  def initialize
    @foo = ::foo(1) # okay, `@foo` has the type `Float64`
  end
end

The call needs to be global; other than that, type guessing for top-level methods has the same limitations as for class methods.

Perhaps this would also apply to non-global calls as long as Bar and its ancestors do not define #foo?

@mverzilli
Copy link

The change looks good, my only concern here would be introducing compiler performance regressions which is the reason why the compiler's type inference capabilities were intentionally reduced many years ago

@straight-shoota
Copy link
Member

straight-shoota commented Sep 3, 2025

@mverzilli This doesn't add any more complexity than what's already there. It just adds the same logic we already have for calls to class method with explicit receiver to apply to calls to top-level methods with explicit namespace indicator (:: prefix).
So it's fine to add without concerns for performance regression.

@straight-shoota straight-shoota added this to the 1.18.0 milestone Sep 3, 2025
@straight-shoota straight-shoota merged commit d4e17d2 into crystal-lang:master Sep 8, 2025
47 of 48 checks passed
@HertzDevil HertzDevil deleted the feature/guess-global-call branch October 2, 2025 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants