Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-112480

QmlCompiler does not care about shadowing of methods

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P1: Critical
    • 6.6.0 Beta1
    • 6.5.0 RC
    • QML: Compiler
    • None
    • 46cc70e2a (dev), f90dda3ea (dev)

    Description

      Consider:

      pragma Strict
      import QtQuick
      import QtQuick.Window
      
      Window {
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          component B: Item {
              function contains(point: point) : string {
                  console.log("b", point.x, point.y)
                  return "b"
              }
          }
      
      
          component C: Item {
              function contains(point: point) : string {
                  console.log("c", point.x, point.y)
                  return "c"
              }
          }
      
          property Item a: Item {}
          property B b: B {}
          property C c: C {}
      
          function doThing() { console.log(a.contains(Qt.point(0, 0))) }
      
          Component.onCompleted: {
              doThing();
              a = b;
              doThing();
              a = c;
              doThing();
              Qt.quit();
          }
      }
      

      Expected output:

      qml: false
      qml: b 0 0
      qml: b
      qml: c 0 0
      qml: c
      

      Output when compiled to C++:

      qml: false
      qml: b 0 0
      qml: true
      qml: c 0 0
      qml: true
      

      So, the call actually gets resolved to the right method and the engine will coerce the types as necessary. However, the generated code expects to receive a bool as the method on QQuickItem tells it.

      There are three ways out of this:

      • Disable calls to potentially shadowed methods
      • When calling potentially shadowed methods treat all parameters and return values as var, weakening type inference.
      • Document the difference and accept it.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ulherman Ulf Hermann
            ulherman Ulf Hermann
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes