crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.2k stars 1.61k forks source link

Internal error when using `sizeof` as generic type argument in inferred ivar type #14731

Open HertzDevil opened 1 week ago

HertzDevil commented 1 week ago

The following is currently a compilation error until #5427 is resolved:

class Foo(N)
end

class Bar(T)
  @x : Foo(sizeof(T)) # Error: can't use sizeof(T) as a generic type argument
end

The following, however, produces an internal IndexError:

class Foo(N)
end

class Bar(T)
  @x = Foo(sizeof(T)).new
end
Index out of bounds (IndexError)
  from src/indexable.cr:89:20 in '[]'
  from src/compiler/crystal/types.cr:1602:11 in 'instantiate'
  from src/compiler/crystal/semantic/type_lookup.cr:296:11 in 'lookup'
  from src/compiler/crystal/semantic/type_lookup.cr:50:5 in 'lookup_type?:self_type:allow_typeof:find_root_generic_type_parameters'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:1168:14 in 'lookup_type?'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:1128:5 in 'lookup_type?'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:644:9 in 'guess_type'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:357:14 in 'process_assign_instance_var'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:321:9 in 'process_assign_instance_var'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:222:11 in 'process_assign'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:205:7 in 'process_assign'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:127:7 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/semantic/semantic_visitor.cr:110:7 in 'visit'
  from src/compiler/crystal/semantic/type_guess_visitor.cr:1208:7 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/syntax/ast.cr:184:27 in 'accept_children'
  from src/compiler/crystal/syntax/visitor.cr:28:11 in 'accept'
  from src/compiler/crystal/semantic/hooks.cr:25:7 in 'visit_with_finished_hooks'
  from src/compiler/crystal/semantic/type_declaration_processor.cr:144:5 in 'process'
  from src/compiler/crystal/semantic.cr:77:7 in 'top_level_semantic'
  from src/compiler/crystal/semantic.cr:22:23 in 'semantic:cleanup'
  from src/compiler/crystal/compiler.cr:210:14 in 'compile'
  from src/compiler/crystal/command.cr:359:3 in 'compile'
  from src/compiler/crystal/command.cr:239:5 in 'run_command'
  from src/compiler/crystal/command.cr:112:7 in 'run'
  from src/compiler/crystal/command.cr:55:5 in 'run'
  from src/compiler/crystal/command.cr:54:3 in 'run'
  from src/compiler/crystal.cr:11:1 in '__crystal_main'
  from src/crystal/main.cr:118:5 in 'main_user_code'
  from src/crystal/main.cr:104:7 in 'main'
  from src/crystal/main.cr:130:3 in 'main'

The same happens if @x is assigned inside #initialize instead.