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

Bytes[1, 2] is broken when used in a class #5343

Closed
bew opened this issue Dec 3, 2017 · 1 comment · Fixed by #5354
Closed

Bytes[1, 2] is broken when used in a class #5343

bew opened this issue Dec 3, 2017 · 1 comment · Fixed by #5354

Comments

@bew
Copy link
Contributor

bew commented Dec 3, 2017

https://carc.in/#/r/36qg

STDOUT.write Bytes[1, 2] # works OK

class Foo
  def self.bytes
    Bytes['a'.ord, 97]
  end
end

STDOUT.write Foo.bytes   # compile error

Gives:

Error in bytes_slice.cr:9: instantiating 'Foo:Class#bytes()'

STDOUT.write Foo.bytes
                 ^~~~~

in bytes_slice.cr:5: expanding macro

    Bytes['a'.ord, 97]
         ^

in /usr/lib/crystal/slice.cr:34: undefined constant T

    {% if @type.name != "Slice(T)" && T < Number %}
                                      ^

The macro expansion is looking for a T in Foo.

This is visible with this example (https://carc.in/#/r/36qi):

class Bar(T) # Here is a T !
  def self.bytes
    Bytes['a'.ord, 97]
  end
end

STDOUT.write Bar(UInt8).bytes # compiles OK, but this is wrong!

Going back in time with carc.in, it seems to break on 0.19.0 release.

@makenowjust
Copy link
Contributor

makenowjust commented Dec 5, 2017

Bytes free version:

class Foo(T)
  macro [](*foo)
    {% puts T %}
  end
end

alias FooInt32 = Foo(Int32)

class Bar
  def self.foo
    FooInt32[1, 2, 3]
  end
end

p FooInt32[1, 2, 3]
# => Int32
p Bar.foo
# compilation error: undefined constant T

It means there is a bug about type aliases and macros.

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

Successfully merging a pull request may close this issue.

2 participants