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

Array(T)#* of recursive type alias #8607

Closed
lbguilherme opened this issue Dec 22, 2019 · 5 comments
Closed

Array(T)#* of recursive type alias #8607

lbguilherme opened this issue Dec 22, 2019 · 5 comments

Comments

@lbguilherme
Copy link
Contributor

When updating an old project to the current Crystal release I found a case that used to work but now fails to build and looks like a compiler bug. Here is a reduced version:

alias Type = Array(Type) | String

arr = ["aa", ["23"]] of Type

p arr # this works
p arr * 2 # this doesn't compile

Here is the compiler output:

In test.cr:6:7

 6 | p arr * 2
           ^
Error: instantiating 'Array(Type)#*(Int32)'


In /usr/share/crystal/src/array.cr:337:23

 337 | return Array(T).new(times, first)
                       ^--
Error: instantiating 'Array(Type).class#new(Int32, (Array(String) | Array(Type) | String))'


In /usr/share/crystal/src/array.cr:116:7

 116 | @buffer = Pointer(T).malloc(size, value)
       ^------
Error: instance variable '@buffer' of Array(Type) must be Pointer(Array(String) | Array(Type) | String), not Pointer(Array(String) | Array(Type) | String)

In particular, notice that the last line says Pointer(Array(String) | Array(Type) | String) is not the same as Pointer(Array(String) | Array(Type) | String). But they look pretty much the same to me. Not sure what is going on.

This used to work on Crystal 0.30.1 (https://play.crystal-lang.org/#/r/88y1) but broke on Crystal 0.31.0 (https://play.crystal-lang.org/#/r/88y0)

@lbguilherme
Copy link
Contributor Author

arr.uniq also fails to compile for a recursive type.

May be worth to mention: #5155

@Sija
Copy link
Contributor

Sija commented Dec 22, 2019

Possibly related to #8087 and/or #8048.

@asterite
Copy link
Member

The first snippet shouldn't compile at all because an Array(String) is inserted into Array(Type). This is a bug.

I hope someone removes recursive aliases in the future...

@HertzDevil
Copy link
Contributor

The original snippet no longer compiles due to #10224.

@lbguilherme
Copy link
Contributor Author

lbguilherme commented Aug 6, 2021

The original snippet now doesn't compile with a correct error message. And this example correctly compiles:

alias Type = Array(Type) | String

arr = ["aa", ["23"] of Type] of Type

p arr
p arr * 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants