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

Inheriting from generic class/struct ignoring the type causes segmentation fault #1944

Closed
porras opened this issue Dec 10, 2015 · 7 comments
Closed
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler

Comments

@porras
Copy link
Contributor

porras commented Dec 10, 2015

I'm guessing that is not allowed (although I run into a case where it would be useful), but should probably give a compilation error:

Code:

struct Generic(T)
end

struct IgnoreType < Generic(T)
end

p IgnoreType.new.is_a?(Generic(String))
p IgnoreType.new.is_a?(Generic(Int32))
$ crystal example.cr 
Segmentation fault: 11

The same happens if they are classes instead of structs.

(if this is allowed, I'd expect both lines to print true, and if it's not, a compilation error)

@lbguilherme
Copy link
Contributor

This code as-is just doesn't make much sense. Consider this:

struct Generic(T)
  def foo
    T.new
  end
end

struct IgnoreType < Generic(T)
end

p IgnoreType.new.foo # What is this? (segfaults)

This should fail compilation with a "Unknown identifier T".
I'm curious, where would this be useful and what would be the semantics of it?

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Dec 10, 2015
@asterite
Copy link
Member

Hm, that should give a compile error right at the definition of IgnoreType.

@porras
Copy link
Contributor Author

porras commented Dec 11, 2015

Of course that code only makes sense if no method that uses T is inherited.

A possible use case would be trying to define an Option type similar to Scala's. A possible implementation could be:

abstract struct Option(T)
end

struct Some(T) < Option(T)
  def initialize(@value : T)
  end
end

struct None < Option(T)
end

Now, I'm not saying that this should work, I could totally imagine this not being possible (and in fact this kind of programming is not very Crystal-idiomatic I think), but then it should be a compilation error.

An important detail that I forgot to add is that the segmentation fault happens in the compiler, that's why I thought it is a bug worth reporting:

$ crystal build example.cr 
Segmentation fault: 11

@Perelandric
Copy link

Seems fixed as of v16.

Error in line 4: can't extend non-abstract struct Generic(T)

@asterite
Copy link
Member

I'm sure it fails with a class though

@Perelandric
Copy link

Indeed it does.

@asterite
Copy link
Member

Closed in favor of #2665

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
Projects
None yet
Development

No branches or pull requests

4 participants