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

Interpreter: fix as? when there's no resulting type #12328

Merged
merged 2 commits into from
Jul 29, 2022

Conversation

asterite
Copy link
Member

Fixes #12327

The code was using is_a which required the filtered type to be not-nil. I checked codegen.cr and for a nilable cast the filtered type can be nil, and the logic is a bit different there. So I did the same here, essentially inlining part of the is_a method, but first doing a special logic if the filtered type is nil.

I also changed the code a bit to do an early return.

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:interpreter labels Jul 27, 2022
@straight-shoota
Copy link
Member

I'd suggest adding a spec where the object type is not already nil (for example 1.as?(String)) to ensure the nil casting works correctly.

@asterite
Copy link
Member Author

Thanks! I pushed another commit with such a spec, which didn't initially work, so I made it work.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! That's what those specs are for ;)

@straight-shoota straight-shoota added this to the 1.6.0 milestone Jul 29, 2022
@asterite asterite merged commit ce51fbb into master Jul 29, 2022
@asterite asterite deleted the interpreter/nilable-cast-no-resulting-type branch July 29, 2022 13:44
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:interpreter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interpreter: Nil assertion failed (NilAssertionError) from nil.as?({{type}})
2 participants