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

The playground told me to report this bug #6907

Closed
Iainmon opened this issue Oct 4, 2018 · 5 comments
Closed

The playground told me to report this bug #6907

Iainmon opened this issue Oct 4, 2018 · 5 comments

Comments

@Iainmon
Copy link

Iainmon commented Oct 4, 2018

require "socket"


class IPv4
  
  @addr1 : Int16
  @addr2 : Int16
  @addr3 : Int16
  @addr4 : Int16
  @validated : Bool = false
  @isValid : Bool = false
  @isLocal : Bool
  @localIPType : Int16
  
  def initialize(addr1 : Int16 | String, addr2 : Int16 = 0, addr3 : Int16 = 0, addr4 : Int16 = 0)
     if typeof(addr1) == String
    		prefix : String = addr1.to_s
        begin
        	params = prefix.split('.')
    			raise "nil" if params.size > 4 #if there are more than 4 dots in the string
          addr1 = params[0].to_i16
          addr2 = params[1].to_i16
          addr3 = params[2].to_i16
          addr4 = params[3].to_i16
        rescue ex
          return nil
        end
    end
    @addr1 = addr1.to_i16
    @addr2 = addr2
    @addr3 = addr3
    @addr4 = addr4
		if addr1 == 192 || addr1 == 10 ||addr1 == 172
			@localIPType = addr1.to_i16
			@isLocal = true
		else
			@localIPType = 0
			@isLocal = false
		end
  end
  
  def validate() : Bool
    valid : Bool = Socket.ip? self.stringify
		@isValid = valid
		valid
  end
  
  def stringify() : String
    "#{@addr1}.#{@addr2}.#{@addr3}.#{@addr4}"
  end
end

ip : IPv4
validIPs = [] of IPv4

(0..255).each do |w|
  (0..255).each do |x|
    (0..255).each do |y|
      (0..255).each do |z|
  				ip = IPv4.new("#{w}.#{x}.#{y}.#{z}")
					validIPs.push ip if ip.validate
      end
    end
  end
end


spawn do
  puts "Hello!"
end

Fiber.yield

string = "43.42.24.42"
ints = string.split('.')
ints.each do |int|
  int = int.to_i16
end
Exception
Error in line 88: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 88: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 89: instantiating 'Range(Int32, Int32)#each()'

in line 89: instantiating 'Range(Int32, Int32)#each()'

in line 90: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 90: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 91: instantiating 'Range(Int32, Int32)#each()'

in line 91: instantiating 'Range(Int32, Int32)#each()'

in line 92: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 92: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 93: instantiating 'Range(Int32, Int32)#each()'

in line 93: instantiating 'Range(Int32, Int32)#each()'

in line 94: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 94: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 95: instantiating 'Range(Int32, Int32)#each()'

in line 95: instantiating 'Range(Int32, Int32)#each()'

in line 96: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 96: instantiating 'Crystal::Playground::Agent#i(Int32)'

in line 97: instantiating 'IPv4:Class#new(String)'

in line 61: instance variable '@localIPType' of IPv4 must be Int16, not Int32
@hanyuone
Copy link

hanyuone commented Oct 5, 2018

This is not a bug, but a problem in the following line:

@localIPType = 0

All integer literals default to Int32, which is why you see the final error in instance variable... must be Int16, not Int32. To fix this issue, just do this:

@localIPType = 0_i16

indicating that the literal should be treated as an Int16.

This should be a bug with the playground listing this as a possible bug when it is in fact a user error.

@straight-shoota
Copy link
Member

straight-shoota commented Oct 5, 2018

@Qwerp-Derp No, it's not a user error. The example code does compile both in the playground and with the regular CLI tool. The compiler is smart enough to cast the integer literal to Int16 because that's the type of the instance var.

@Iainmon Could you please check your Crystal version? (crystal --version). Your example should compile on all compiler versions beginning with 0.25.0.
What @asterite said

@asterite
Copy link
Member

asterite commented Oct 5, 2018

Related: #6083

I don't think this can be fixed.

@straight-shoota
Copy link
Member

Damn, I was sure I had tested the example in the playground and it worked... but yeah, it can't work with the applied instrumentation.

@jhass
Copy link
Member

jhass commented Nov 12, 2019

I think we can close this as a duplicate of #6083. Different symptoms but same problem.

@straight-shoota straight-shoota closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants