Skip to content
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

Redefine defs when constant and number in generic arguments are equal #12785

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Nov 27, 2022

This PR makes the following two defs redefinitions of each other:

class Foo(N); end
X = 1

def foo(x : Foo(1)); end
def foo(x : Foo(X)); end

The same already happens if X were an alias type:

class Foo(T); end
alias X = Int32

# redefinitions
def foo(x : Foo(Int32)); end
def foo(x : Foo(X)); end

Except that only exact matches are allowed here; X's initializer must be exactly the NumberLiteral 1, not an expression like 4 - 3. (Those expressions still work during method lookup, just not during overload ordering.)

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:generics labels Nov 27, 2022
@HertzDevil HertzDevil marked this pull request as draft November 27, 2022 13:35
@HertzDevil HertzDevil force-pushed the bug/restriction-number-vs-const branch from a61d7f8 to c71534f Compare November 27, 2022 13:54
@HertzDevil HertzDevil marked this pull request as ready for review November 27, 2022 13:54
@straight-shoota straight-shoota added this to the 1.7.0 milestone Nov 29, 2022
@@ -605,11 +605,59 @@ module Crystal
false
end

def restriction_of?(other : NumberLiteral, owner, self_free_vars = nil, other_free_vars = nil)
return false if self_free_vars && self.single_name?.try { |name| self_free_vars.includes?(name) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style related suggestion.

Suggested change
return false if self_free_vars && self.single_name?.try { |name| self_free_vars.includes?(name) }
return false if self_free_vars && self.single_name?.try &.in?(self_free_vars) }

@straight-shoota straight-shoota merged commit 926e89c into crystal-lang:master Dec 1, 2022
@HertzDevil HertzDevil deleted the bug/restriction-number-vs-const branch December 1, 2022 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:generics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants