You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that Class#cast is not duplicated for each actual metaclass, so the last two lines are calling Parent+.cast, which always succeeds.
Instead Class#cast should reference {% @type %} so that it performs the correct casts:
classClassdefcast2(other)
{%@type%}
other.as(self)
endendParent.as(Parent.class).cast2(Parent.new) # Error: can't cast Parent to Child2Parent.as(Parent.class).cast2(Child1.new) # Error: can't cast Child1 to Child2Parent.as(Parent.class).cast2(Child2.new) # Error: can't cast Child2 to Child1
which doesn't work. I believe this is because the compiler tries to instantiate as calls that always fail and produce compile-time errors. Considering the contexts where Class#cast is likely to be used (the receiver is not known ahead of time, otherwise a plain as would have sufficed), I don't think Class#cast should ever fail to compile. So the implementation might look like this instead:
t.cast(x)
effectively returnsx.as(t)
. However the following suggests otherwise:It seems that
Class#cast
is not duplicated for each actual metaclass, so the last two lines are callingParent+.cast
, which always succeeds.Instead
Class#cast
should reference{% @type %}
so that it performs the correct casts:which doesn't work. I believe this is because the compiler tries to instantiate
as
calls that always fail and produce compile-time errors. Considering the contexts whereClass#cast
is likely to be used (the receiver is not known ahead of time, otherwise a plainas
would have sufficed), I don't thinkClass#cast
should ever fail to compile. So the implementation might look like this instead:Related: #8422
The text was updated successfully, but these errors were encountered: