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

Variance with generic parent class #1943

Closed
actsasbuffoon opened this issue Dec 10, 2015 · 6 comments
Closed

Variance with generic parent class #1943

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

Comments

@actsasbuffoon
Copy link

I'm getting an unexpected error from the Crystal compiler. Here's an example:

class Parent(T)
end

class Child < Parent(String)
end

def example(value : Parent(String)+)
end

example Child.new

This results in a compiler error:

Bug: unsupported restriction: Child vs. Parent(String)+
[4523669323] *raise<String>:NoReturn +139
[4528370945] *Crystal::Type+@Crystal::Type#restrict<Crystal::Type+, Crystal::ASTNode+, Crystal::MatchContext>:NoReturn +529
[4528468570] *Crystal::MatchesLookup::match_arg<Crystal::Type+, Crystal::Arg, Crystal::MatchContext>:(Nil | Crystal::Type+) +1434
[4528465964] *Crystal::MatchesLookup::match_def<Crystal::CallSignature, Crystal::DefWithMetadata, Crystal::MatchContext>:Crystal::Match? +1100
[4529146351] *Crystal::Program@Crystal::MatchesLookup#lookup_matches_without_parents<Crystal::Program, Crystal::CallSignature, Crystal::Program, Crystal::Program, Nil>:Crystal::Matches +335
[4529143777] *Crystal::Program@Crystal::MatchesLookup#lookup_matches<Crystal::Program, Crystal::CallSignature, Crystal::Program, Crystal::Program, Nil>:Crystal::Matches +81
[4529239220] *Crystal::Call#lookup_matches_with_signature<Crystal::Call, Crystal::Program, Crystal::CallSignature, Bool>:Crystal::Matches +340
[4528419330] *Crystal::Call#lookup_matches_in_type<Crystal::Call, Crystal::Program, Array(Crystal::Type+), Nil, String, Bool>:Array(Crystal::Def+) +642
[4523957767] *Crystal::Call#recalculate<Crystal::Call>:Array(Crystal::Def+)? +2679
[4526783554] *Crystal::TypeVisitor#visit<Crystal::TypeVisitor, Crystal::Call>:Bool +6018
[4525078922] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::ASTNode+, Crystal::TypeVisitor>:Nil +2266
[4525168776] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::ASTNode+, Crystal::TypeVisitor>:Nil +92120
[4526662915] *Crystal::Program#infer_type<Crystal::Program, Crystal::Expressions>:Crystal::Expressions +179
[4526085987] *Crystal::Compiler#compile<Crystal::Compiler, Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result +2979
[4530222929] *Crystal::Command#run_command<Crystal::Command>:Nil +145
[4530052947] *Crystal::Command#run<Crystal::Command>:(Nil | Bool | Process::Status | Array(String) | IO::FileDescriptor+ | Crystal::Compiler::Result | Array(Crystal::ImplementationTrace) | Array(Crystal::Init::View+:Class)) +8563
[4530043276] main +17164

Error: you've found a bug in the Crystal compiler. Please open an issue, including source code that will allow us to reproduce the bug: https://github.com/manastech/crystal/issues

The code in my example seems weird, but that's because I've simplified it to get to the essence of the problem. I'm toying around with writing a parser combinator library, and this functionality would be really useful.

If Child is a subclass of Parent(String) then it seems like it should work with a Parent(String)+ type restriction.

@refi64
Copy link
Contributor

refi64 commented Dec 10, 2015

IIRC, @asterite said the + was supposed to be an internal thing; you should never have to use it explicitly.

@refi64
Copy link
Contributor

refi64 commented Dec 10, 2015

@actsasbuffoon
Copy link
Author

@kirbyfan64 Good to know, thank you.

I may have over-simplified my example code. Something a little closer shows why I thought including the + might help.

class Parent(T); end

class Child < Parent(String); end

class Example
  @value :: Parent(String)

  def initialize(@value)
  end
end

Example.new Child.new

This results in:

Error in ./failure.cr:12: instantiating 'Example:Class#new(Child)'

Example.new Child.new
        ^~~

instantiating 'Example#initialize(Child)'

in ./failure.cr:8: type must be Parent(String), not (Parent(String) | Child)

  def initialize(@value)
                 ^

================================================================================

Child trace:

  ./failure.cr:8

      def initialize(@value)
                     ^~~~~

  ./failure.cr:8

      def initialize(@value)

This seems to be saying that @value must be a Parent(String), and Child doesn't fulfill that requirement.

@asterite
Copy link
Member

Yes, inheriting from a generic class doesn't work pretty well :-(

@jhass jhass added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels May 27, 2016
@jhass
Copy link
Member

jhass commented May 27, 2016

The first example works now, the second example gives a compile time error when ported to new syntax. When adding a cast it gives a compile time exception instead of a proper error message: https://carc.in/#/r/zym

@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