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

Compiler confused about variable type inside while and block #13862

Open
HertzDevil opened this issue Oct 5, 2023 · 0 comments
Open

Compiler confused about variable type inside while and block #13862

HertzDevil opened this issue Oct 5, 2023 · 0 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic

Comments

@HertzDevil
Copy link
Contributor

HertzDevil commented Oct 5, 2023

The following snippet produces a compilation error:

def scope(&)
  yield
end

def foo : Int32?
  if x = foo
    scope do
      while x
        x = foo
      end
    end
  end
  0
end

foo # BUG: trying to downcast (Int32 | Nil) (Crystal::MixedUnionType) <- Int32 (Crystal::IntegerType) (Exception)

The above pattern can be seen in IO#gets_peek, where foo is IO::Buffered#peek and scope is String.build. This is the root cause behind #8163, #13720, and #13792.

Changing #foo's return type to Int32 or removing it altogether makes the internal error go away. In the case of IO::Buffered#peek this is indeed possible: #13863

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:semantic
Projects
None yet
Development

No branches or pull requests

1 participant