Unsoundness when a type with wildcard is returned in closure

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      An example of an unsound program: https://github.com/scala/scala3/issues/16789#issuecomment-1410721692.
      Essentially, the bug is that Supplier<Magic<?>> (or more precisely Function<Integer,Magic<CAP#1>>) can be passed to a function that accepts Supplier<Magic<T>>.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Write code from link in description oк from test case, compile it and run program.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compilation error
      ACTUAL -
      The program compiles and a ClassNotFoundError error is thrown at runtime.

      ---------- BEGIN SOURCE ----------
      import java.util.function.Function;

      public class Main {
          public static void main(String[] args) {
              onestep(num -> {
                  Magic<?> m = num > 0 ? new IntMagic() : new StrMagic();
                  return m;
              });
          }

          static <T> T onestep(Function<Integer, Magic<T>> m) {
              return m.apply(1).step(m.apply(0).init());
          }
      }

      interface Magic<S> {
          S init();
          S step(S s);
      }

      class IntMagic implements Magic<Integer> {
          public Integer init() { return 0; }
          public Integer step(Integer s) { return s + 1; }
      }

      class StrMagic implements Magic<String> {
          public String init() { return ""; }
          public String step(String s) { return s + "0"; }
      }
      ---------- END SOURCE ----------

            Assignee:
            Unassigned
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: