-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: don't precompute sizeof on abstract structs and modules #7801
Conversation
Some explanation: at some point we made |
@@ -2568,7 +2568,8 @@ module Crystal | |||
# Try to resolve the sizeof right now to a number literal | |||
# (useful for sizeof inside as a generic type argument, but also | |||
# to make it easier for LLVM to optimize things) | |||
if type && !node.exp.is_a?(TypeOf) | |||
if type && !node.exp.is_a?(TypeOf) && | |||
!(type.module? || (type.abstract? && type.struct?)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be moved upwards together with the check of type.is_a?(GenericType)
? I thought the notion of an instantiable type was already defined somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here in MainVisitor
we compute it if we can. Later in CleanupTransformer
we check that it's only a class. So, doing sizeof
an abstract struct or module can be done and works, just not eagerly.
To be honest, now I'm not convinced that sizeof
should work eagerly at all. I did that so one can do, for example, StaticArray(Int32, sizeof(Int32))
, but that's a pretty contrived example. But for now I'm just fixing broken things, not changing the semantics of the language or introducing breaking changes. But eventually we can define the semantics we want for this and fix it.
And no, unfortunately there's no type.can_be_instantiated?
method.
Can confirm this fixed my issue. Thanks again. |
Fixes #7741
And a few other fixes/improvements too.
I'm marking it as a breaking change too because
instance_sizeof
could be incorrectly invoked with non-class stuff (like structs) and so this will make compiling (but incorrect) code to stop compiling... but I think it's fine.