Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'illegal cyclic inheritance involving class' with Java enums #10639

Closed
404- opened this issue Dec 2, 2017 · 2 comments · Fixed by scala/scala#10349
Closed

'illegal cyclic inheritance involving class' with Java enums #10639

404- opened this issue Dec 2, 2017 · 2 comments · Fixed by scala/scala#10349

Comments

@404-
Copy link

404- commented Dec 2, 2017

Attempting to write Scala code that relies on Java enums which implement an interface with a static method that captures the enum type as a method parameter, causes the illegal cyclic inheritance involving class error to occur during compilation. The caveat is to make sure the Scala class is compiled on its own, e.g. the Java enum and interface could be compiled separately and packaged into a JAR file. Another way to induce consistent failures is to explicitly set compile order to JavaThenScala, i.e.: compileOrder := CompileOrder.JavaThenScala.

Reproducible using SBT 1.0.3 with Scala versions 2.11.8, 2.12.3, 2.12.4

//Identifiable.java
package com.example;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toMap;

public interface Identifiable {
    String name();

    static <E extends Enum<E> & Identifiable> Map<String, E> valuesByName(final E[] values) {
        return Stream.of(values).collect(toMap(Identifiable::name, Function.identity()));
    }
}
//Size.java
package com.example;

public enum Size implements Identifiable {
    SMALL, MEDIUM, LARGE;
}
//Broken.scala
package com.example

object Broken extends App {
  println(Size.SMALL)
}
//build.sbt
scalaVersion := "2.12.4" //or 2.11.8 or 2.12.3
compileOrder := CompileOrder.JavaThenScala

[error] /tmp/doyousee/src/main/scala/com/example/Broken.scala:4:10: illegal cyclic inheritance involving class Size
[error] println(Size.SMALL)
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

Possibly related #3809 (using -Ybreak-cycles option had no effect).

Originally asked on StackOverflow: How to resolve “Illegal cyclic inheritance” compilation error when using a Java enum in Scala?

@hrhino
Copy link
Member

hrhino commented Dec 4, 2017

I looked at this one a bit. Unclear so far what's going on, but it only happens when the signature of valuesByName mentions E[], so you may be able to work around this by not using an array.

I suspect the issue here is related to the generic array handling code in ClassfileParser.

@som-snytt
Copy link

Fixed since 2.12.9, so probably duplicates #11573

Adding code in test.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2023
@SethTisue SethTisue modified the milestones: Backlog, 2.12.9 Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants