capnproto / capnproto-java

Cap'n Proto in pure Java
Other
391 stars 86 forks source link

Nested generics don't compile properly #110

Closed c-aug closed 3 years ago

c-aug commented 3 years ago

The following code doesn't compile in capnp java v0.1.9:

@0xe49bca963ed0a9f3;

using Java = import "/java.capnp";
$Java.package("com.foo");
$Java.outerClassname("MapOuter");

struct Bar {
    messages @0 :Map(Text, List(Text));
}

struct Map(Key, Value) {
  entries @0 :List(Entry);
  struct Entry {
    key @0 :Key;
    value @1 :Value;
  }
}

It gets errors like

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project foo: Compilation failure: Compilation failure: 
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[106,102] cannot find symbol
[ERROR]   symbol:   class Key_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[106,131] cannot find symbol
[ERROR]   symbol:   class Value_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[104,202] cannot find symbol
[ERROR]   symbol:   class Key_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[104,231] cannot find symbol
[ERROR]   symbol:   class Value_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[107,195] cannot find symbol
[ERROR]   symbol:   class Key_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[107,224] cannot find symbol
[ERROR]   symbol:   class Value_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[110,203] cannot find symbol
[ERROR]   symbol:   class Key_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[110,232] cannot find symbol
[ERROR]   symbol:   class Value_eab2aec14a30ae3e_Reader
[ERROR]   location: class com.foo.MapOuter.Map.Builder<Key_eab2aec14a30ae3e_Builder,Value_eab2aec14a30ae3e_Builder>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[127,105] cannot find symbol
[ERROR]   symbol:   class Key_eab2aec14a30ae3e_Builder
[ERROR]   location: class com.foo.MapOuter.Map.Reader<Key_eab2aec14a30ae3e_Reader,Value_eab2aec14a30ae3e_Reader>
[ERROR] /home/IdeaProjects/foo/target/generated-sources/capnp/MapOuter.java:[127,135] cannot find symbol
[ERROR]   symbol:   class Value_eab2aec14a30ae3e_Builder
[ERROR]   location: class com.foo.MapOuter.Map.Reader<Key_eab2aec14a30ae3e_Reader,Value_eab2aec14a30ae3e_Reader>

The code compiles correctly in the C++ capnp. Note that the Map implementation is copied directly from https://capnproto.org/language.html#generic-types , so this should work.

I think it's the nested generics causing trouble, e.g. this works:

@0xcaebee21746adf65;

using Java = import "/java.capnp";
$Java.package("com.foo");
$Java.outerClassname("PairOuter");

struct Baz {
    messages @0 :List(Pair(Text, List(Text)));
}

struct Pair(Key, Value) {
  key @0 :Key;
  value @1 :Value;
}

Using :Map(Text, Text), or moving Entry to be a top level struct (i.e. moving it outside of Map) still fail.

dwrensha commented 3 years ago

Hm... that example seems to work for me.

Are you sure that you are using the latest version of the capnpc-java plugin? How did you install it?

c-aug commented 3 years ago

This is with the following setup in Maven:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.capnproto</groupId>
            <artifactId>runtime</artifactId>
            <version>0.1.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.expretio.maven.plugins</groupId>
                <artifactId>capnp-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Compiling is all done via mvn install.

dwrensha commented 3 years ago

Ah, looks like you are using https://github.com/expretio/capnp-maven-plugin, which has not been kept up to date with recent releases. You could open an issue on that repo to request an update. (I'm not sure whether it's being actively maintained.) Meanwhile, you're probably going to need to install canpnc-java more manually.

What I'd like to do eventually is rewrite capnpc-java in Java, to avoid this kind of distribution problem. I opened #111 to track that issue.

dwrensha commented 3 years ago

My guess is that capnp-maven-plugin does not include this fix: https://github.com/capnproto/capnproto-java/commit/7da1dc8159cecc8995c88e025394e22606406f1b

c-aug commented 3 years ago

Thanks for this. I can confirm that the schema compiles correctly with the latest version of capnproto-java; the problem was the old version in the plugin.

I am happy for you to close this issue if you want.