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

Can't initialize nested StaticArrays at the top level #6536

Open
dawkot opened this issue Aug 13, 2018 · 3 comments
Open

Can't initialize nested StaticArrays at the top level #6536

dawkot opened this issue Aug 13, 2018 · 3 comments

Comments

@dawkot
Copy link

dawkot commented Aug 13, 2018

This code compiles:

x = StaticArray[1, 2, 3]
xs = StaticArray[x]

This one returns an error:

xs = StaticArray[StaticArray[1, 2, 3]]

Error in line 1: expanding macro

in line 1: expanding macro

in macro '[]' /usr/lib/crystal/static_array.cr:17, line 1:

  1. __temp_23 = uninitialized StaticArray(typeof(StaticArray[1, 2, 3]), 1)
    
  1.   __temp_23.to_unsafe[0] = StaticArray[1, 2, 3]
    
  2. __temp_23
    

macro '[]' must be defined before this point but is defined later

@makenowjust
Copy link
Contributor

It is bug because this code works fine:

def foo
  StaticArray[StaticArray[1, 2, 3]]
end

p foo

Wrapping StaticArray[StaticArray[...]] by method is important. This issue is happened only at top level.

And bonus, StaticArray-free version:

class Foo
  macro [](x)
    typeof({{x}})
  end
end

Foo[Foo[1]]

@dawkot
Copy link
Author

dawkot commented Aug 14, 2018

Interestingly enough, now I'm getting the same error message trying to manually do what the macro was supposed to do:

m : StaticArray(StaticArray(Float64, 3), 3) = begin
    a = StaticArray[1.0, 2.0, 3.0]
    b = StaticArray[4.0, 5.0, 6.0]
    c = StaticArray[7.0, 8.0, 9.0]
    StaticArray[a, b, c]
end

Error message:

Error in line 2: macro '[]' must be defined before this point but is defined later

Neither does this work:

m : StaticArray(StaticArray(Float64, 3), 3) = begin
    res = uninitialized StaticArray(StaticArray(Float64, 3), 3)
    res[0] = StaticArray[1.0, 2.0, 3.0]
    res[1] = StaticArray[4.0, 5.0, 6.0]
    res[2] = StaticArray[7.0, 8.0, 9.0]
    res
end

Error in m2.cr:3: macro '[]' must be defined before this point but is defined later

¯_(ツ)_/¯

@dawkot dawkot changed the title Can't create nested StaticArrays using the literal ("[]") macro Can't initialize nested StaticArrays Aug 14, 2018
@dawkot dawkot changed the title Can't initialize nested StaticArrays Can't initialize nested StaticArrays at the top level Aug 15, 2018
@RX14 RX14 modified the milestone: 0.26.1 Aug 16, 2018
@straight-shoota
Copy link
Member

straight-shoota commented Dec 8, 2020

Reduced:

macro foo
end

typeof(foo) # Error: macro 'foo' must be defined before this point but is defined later

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

5 participants