Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6127

VerifyError "Incompatible argument to function" when returning value from function after instanceof check in a function that should return Object (bug in inference base return type selection)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Fixed
    • 2.1.3
    • 2.1.4
    • Static compilation
    • None

    Description

      This code started failing after adding the "return value" after the "if (value instanceof CharSequence)" line.

      This fails:

          static Object convertValueToType(Object value, Class targetType) {
              if(targetType != null && value != null && !(value in targetType)) {
                  if (value instanceof CharSequence) {
                      value = value.toString()
                      if(value in targetType) {
                          return value
                      }
                  }
                  try {
                      if (value instanceof Number && (targetType==Long || targetType==Integer)) {
                          if(targetType == Long) {
                              value = ((Number)value).toLong()
                          } else {
                              value = ((Number)value).toInteger()
                          }
                      } else if (value instanceof String && targetType in Number) {
                          String strValue = value.trim()
                          if(targetType == Long) {
                              value = Long.parseLong(strValue)
                          } else if (targetType == Integer) {
                              value = Integer.parseInt(strValue)
                          } else {
                              value = StringGroovyMethods.asType(strValue, targetType)
                          }
                      } else {
                          value = new SimpleTypeConverter().convertIfNecessary(value, targetType)
                      }
                  } catch (e) {
                      // ignore
                  }
              }
              return value
          }
      

      This works:

          static Object convertValueToType(Object passedValue, Class targetType) {
              Object value = passedValue
              if(targetType != null && value != null && !(value in targetType)) {
                  if (value instanceof CharSequence) {
                      value = value.toString()
                      if(value in targetType) {
                          return value
                      }
                  }
                  try {
                      if (value instanceof Number && (targetType==Long || targetType==Integer)) {
                          if(targetType == Long) {
                              value = ((Number)value).toLong()
                          } else {
                              value = ((Number)value).toInteger()
                          }
                      } else if (value instanceof String && targetType in Number) {
                          String strValue = value.trim()
                          if(targetType == Long) {
                              value = Long.parseLong(strValue)
                          } else if (targetType == Integer) {
                              value = Integer.parseInt(strValue)
                          } else {
                              value = StringGroovyMethods.asType(strValue, targetType)
                          }
                      } else {
                          value = new SimpleTypeConverter().convertIfNecessary(value, targetType)
                      }
                  } catch (e) {
                      // ignore
                  }
              }
              return value
          }
      

      perhaps this has something to do with inference based return type selection.

      Attachments

        Activity

          People

            melix Cédric Champeau
            lhotari Lari Hotari
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: