Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4695847

generics: problem with inference of primitive return values

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 1.4.2
    • 5.0
    • tools
    • mantis
    • generic
    • solaris_8

    Description

      Compiling this class:
      ---- begin BugTernary.java ---
      public class BugTernary<T> {
          BugTernary<T> next;
          static <T> int size(BugTernary<T> bt) {
      if (bt==null)
      return 0;
      else
      return 1+size(bt.next);
          }
          static <T> int size2(BugTernary<T> bt) {
      return (bt==null) ? 0 : 1+size(bt.next);
          }
      }
      ---- end bugTernary.java ---

      yields the following error messages from the 1.2 prototype compiler:

      ---
      $ /home/cananian/jsr14_adding_generics-1_2-ea/scripts/javac -d . -g -gj BugTernary.java
      BugTernary.java:7: incompatible.types
      found : <T>int
      required: java.lang.Object
                  return 1+size(bt.next);
                               ^
      BugTernary.java:7: incompatible types
      found : java.lang.String
      required: int
                  return 1+size(bt.next);
                          ^
      2 errors
      $
      ---

      Note that the type inference algorithm seems to be incorrectly trying to
      co-erce the size() method to an Object, rather than an int.
      For some reason, the version in size2() escapes unscathed; note that
      size2() calls size() recursively. Changing the call from size() to
      size2() in the size2 method will cause the compiler to emit the same
      erroneous error that it does for the size() method --- so perhaps the fact
      that this is a recursive call is to blame?
        --scott

      Panama justice C4 SLBM Justice United Nations cryptographic Nazi DNC
      Castro immediate Attache Legion of Doom SDI Secretary MI5 NRA Bush
                               ( http://cscott.net/ )

      Attachments

        Activity

          People

            gafter Neal Gafter
            gafter Neal Gafter
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: