godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.1k stars 20.2k forks source link

Ternary with sibling classes warns of incompatible types #62325

Open ashtonmeuser opened 2 years ago

ashtonmeuser commented 2 years ago

Godot version

3.4.4

System information

macOS 10.15.7

Issue description

Sibling classes extending the same base class report incompatible types when assigning using a ternary. A warning is produced stating the following.

Values of the ternary conditional are not mutually compatible

The same does not occur for a ternary producing type of superclass or subclass as follows.

var _dummy: DummySuper = DummySuper.new() if true else DummySub0.new()

Steps to reproduce

  1. Create a superclass.
    extends Node
    class_name DummySuper
  2. Create two subclasses that each extend the superclass.
    extends DummySuper
    class_name DummySub0
    extends DummySuper
    class_name DummySub1
  3. Instatiate a variable of type superclass as one or the other subclass in a ternary.
    var _dummy: DummySuper = DummySub0.new() if true else DummySub1.new()

Minimal reproduction project

ternary_repro.zip

salamandars commented 2 years ago

At first glance it looks like the type compatibility check in the gdscript parser only checks for parent-child relationships, not siblings?

You can avoid the warning by casting to the super class: var _dummy: DummySuper = DummySub0.new() as DummySuper if true else DummySub1.new() as DummySuper

PrinceMerluza commented 1 year ago

Still happens on Godot 4b12. The workaround works for now.