crystal-lang / crystal

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

Fully qualified name must be used inside a block when assigning to an ivar. #14827

Open sol-vin opened 1 month ago

sol-vin commented 1 month ago

Bug Report

It seems when assigning to an ivar and using a block at the same time any module references must use a FQN to be findable. The below minimal example illustrates the problem.

Thanks @Blacksmoke16 for the minimal example.

module A
  struct B
  end

  struct C
    @data : B = 1.try { B.new }
  end
end

A::C.new

This produces a compile error Error: undefined constant B

Temporary Fix

By changing @data : B = 1.try { B.new } to @data : B = 1.try { A::B.new } the program will compile again.

Crystal

Crystal 1.13.1 [0cef61e]

LLVM: 18.1.1
Default target: x86_64-pc-windows-msvc
HertzDevil commented 1 month ago

This actually works with class variables because of this line:

https://github.com/crystal-lang/crystal/blob/670543afc0e29ee4be88c654af1818ef1e3302b0/src/compiler/crystal/semantic/class_vars_initializer_visitor.cr#L70

The same pushing_type is missing in the corresponding part of Crystal::InstanceVarsInitializerVisitor.

cyangle commented 1 month ago

It seems like jennifer fails to build due to the same issue here.

https://github.com/imdrasil/jennifer.cr/issues/460

Blacksmoke16 commented 1 month ago

No, that failure is likely due to https://github.com/crystal-lang/crystal/issues/11181 given it's pointing at a method parameter.

cyangle commented 1 month ago

The jennifer failure is indeed caused by #11181, the tests passed after reverting the code change in #11208