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

Internal compiler error when explicitly instantiating Union of types in same hierarchy #11891

Open
HertzDevil opened this issue Mar 12, 2022 · 0 comments
Labels
kind:bug kind:regression Something that used to correctly work but no longer works topic:compiler:codegen

Comments

@HertzDevil
Copy link
Contributor

HertzDevil commented Mar 12, 2022

The following produces an internal compiler error since 1.2.0:

class Foo; end
class Bar < Foo; end
Union(Foo, Bar)
BUG: called type_id for Foo+.class (Crystal::VirtualMetaclassType) (Exception)
  from src/compiler/crystal/codegen/llvm_id.cr:43:5 in 'type_id'
  from src/compiler/crystal/codegen/type_id.cr:68:9 in 'type_id_impl'
  from src/compiler/crystal/codegen/type_id.cr:9:5 in 'type_id'
  from src/compiler/crystal/codegen/codegen.cr:1491:15 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:2279:7 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:665:9 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:2279:7 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:69:7 in 'codegen'
  from src/compiler/crystal/codegen/codegen.cr:67:5 in 'codegen:debug:single_module'
  from src/compiler/crystal/progress_tracker.cr:22:7 in 'codegen'
  from src/compiler/crystal/compiler.cr:173:16 in 'compile'
  from src/compiler/crystal/command.cr:309:3 in 'compile'
  from src/compiler/crystal/command.cr:213:14 in 'run_command'
  from src/compiler/crystal/command.cr:122:7 in 'run'
  from src/compiler/crystal/command.cr:51:5 in 'run'
  from src/compiler/crystal/command.cr:50:3 in 'run'
  from src/compiler/crystal.cr:11:1 in '__crystal_main'
  from src/crystal/main.cr:115:5 in 'main_user_code'
  from src/crystal/main.cr:101:7 in 'main'
  from src/crystal/main.cr:127:3 in 'main'
  from /lib/x86_64-linux-gnu/libc.so.6 in '__libc_start_main'
  from .build/crystal in '_start'
  from ???

Foo | Bar is okay. This is most likely a regression of #11188. The solution seems to be devirtualizing the Generic's type ID if it is exactly Union; alternatively, it might involve copying this part:

when Type
# We devirtualize the type because in an expression like
#
# T.new
#
# even if T is a virtual type that resulted from a generic
# type argument, creating an instance or invoking methods
# on the type itself don't need to resolve virtually.
#
# It's different if from a virtual type we do `v.class.new`
# because the class could be any in the hierarchy.
node.type = check_type_in_type_args(type.remove_alias_if_simple).devirtualize

to here:

generic_type = generic_type.metaclass unless @in_type_args
self.type = generic_type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug kind:regression Something that used to correctly work but no longer works topic:compiler:codegen
Projects
None yet
Development

No branches or pull requests

1 participant