The core of this problem is that coerce_type() fails for Var of type UNKNOWNOID.
The comment for the function says that,
> * The caller should already have determined that the coercion is possible; > * see can_coerce_type.
But can_coerce_type() should say it's possible to convert from unknown to any type as it doesn't see the target node type. I think this as an inconsistency between can_coerce_type and coerce_type. So making this consistent would be right way.
You have two pieces of contradictory knowledge - how are you picking which one to "fix"?
Concerning only this issue, putting on-the-fly conversion for unkown nonconstant as attached patch worked for me. I'm not so confident on this, though..
Confident about what aspect - the safety of the patch itself or whether the conversion is even a good idea?