digital-asset / daml

The Daml smart contract language
https://www.digitalasset.com/developers
797 stars 201 forks source link

Missing symbols from Java codegen #13308

Closed stefanobaghino-da closed 2 years ago

stefanobaghino-da commented 2 years ago

Steps to reproduce:

git clone -b logic_for_observers2 git@github.com:digital-asset/contingent-claims.git
cd contingent-claims
git submodule init && git submodule update
mkdir -p src/main/java && mkdir target
sed -i '0,/^\([[:space:]]*sdk-version: *\).*/s//\12.0.0/;' daml.yaml
daml build
daml codegen java .daml/dist/contingent-claims-0.0.1.dar -o src/main/java
cs fetch com.daml:bindings-java:2.0.0
javac \
  -cp ~/.cache/coursier/v1/https/repo1.maven.org/maven2/com/daml/bindings-java/2.0.0/bindings-java-2.0.0.jar \
  -d target -sourcepath src/main/java @<(find src/main/java -name "*.java")

This will cause the following errors to be reported:

src/main/java/contingentclaims/claim/serializable/claimf/ScaleF.java:33: error: cannot find symbol
    fields.add(new DamlRecord.Field("k", this.k.toValue(v$0 -> toValuet.apply(v$0),v$1 -> new Numeric(v$1))));
                                                               ^
  symbol:   variable toValuet
  location: class ScaleF<t,a,x>
  where t,a,x are type-variables:
    t extends Object declared in class ScaleF
    a extends Object declared in class ScaleF
    x extends Object declared in class ScaleF
src/main/java/contingentclaims/claim/serializable/claimf/ScaleF.java:49: error: cannot find symbol
    Observation<t, BigDecimal> k = Observation.<t, java.math.BigDecimal>fromValue(fields$.get(0).getValue(), v$0 -> fromValuet.apply(v$0), v$1 -> v$1.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected k to be of type com.daml.ledger.javaapi.data.Numeric")).getValue());
                                                                                                                    ^
  symbol:   variable fromValuet
  location: class ScaleF<t,a,x>
  where t,a,x are type-variables:
    t extends Object declared in class ScaleF
    a extends Object declared in class ScaleF
    x extends Object declared in class ScaleF
src/main/java/contingentclaims/observation/observation/DecimalNeg.java:25: error: cannot find symbol
    return new Variant("DecimalNeg", this.observationValue.toValue(v$0 -> toValuet.apply(v$0),v$1 -> new Numeric(v$1)));
                                                                          ^
  symbol:   variable toValuet
  location: class DecimalNeg<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalNeg
    a extends Object declared in class DecimalNeg
src/main/java/contingentclaims/observation/observation/DecimalNeg.java:32: error: cannot find symbol
    Observation<t, BigDecimal> body = Observation.<t, java.math.BigDecimal>fromValue(variantValue$, v$0 -> fromValuet.apply(v$0), v$1 -> v$1.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue());
                                                                                                           ^
  symbol:   variable fromValuet
  location: class DecimalNeg<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalNeg
    a extends Object declared in class DecimalNeg
src/main/java/contingentclaims/observation/observation/DecimalMul.java:26: error: cannot find symbol
    return new Variant("DecimalMul", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                        ^
  symbol:   variable toValuet
  location: class DecimalMul<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalMul
    a extends Object declared in class DecimalMul
src/main/java/contingentclaims/observation/observation/DecimalMul.java:26: error: cannot find symbol
    return new Variant("DecimalMul", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                                                                                               ^
  symbol:   variable toValuet
  location: class DecimalMul<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalMul
    a extends Object declared in class DecimalMul
src/main/java/contingentclaims/observation/observation/DecimalMul.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                  ^
  symbol:   variable fromValuet
  location: class DecimalMul<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalMul
    a extends Object declared in class DecimalMul
src/main/java/contingentclaims/observation/observation/DecimalMul.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
  symbol:   variable fromValuet
  location: class DecimalMul<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalMul
    a extends Object declared in class DecimalMul
src/main/java/contingentclaims/observation/observation/DecimalLte.java:26: error: cannot find symbol
    return new Variant("DecimalLte", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                        ^
  symbol:   variable toValuet
  location: class DecimalLte<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalLte
    a extends Object declared in class DecimalLte
src/main/java/contingentclaims/observation/observation/DecimalLte.java:26: error: cannot find symbol
    return new Variant("DecimalLte", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                                                                                               ^
  symbol:   variable toValuet
  location: class DecimalLte<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalLte
    a extends Object declared in class DecimalLte
src/main/java/contingentclaims/observation/observation/DecimalLte.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                  ^
  symbol:   variable fromValuet
  location: class DecimalLte<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalLte
    a extends Object declared in class DecimalLte
src/main/java/contingentclaims/observation/observation/DecimalLte.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
  symbol:   variable fromValuet
  location: class DecimalLte<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalLte
    a extends Object declared in class DecimalLte
src/main/java/contingentclaims/observation/observation/DecimalEqu.java:26: error: cannot find symbol
    return new Variant("DecimalEqu", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                        ^
  symbol:   variable toValuet
  location: class DecimalEqu<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalEqu
    a extends Object declared in class DecimalEqu
src/main/java/contingentclaims/observation/observation/DecimalEqu.java:26: error: cannot find symbol
    return new Variant("DecimalEqu", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                                                                                               ^
  symbol:   variable toValuet
  location: class DecimalEqu<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalEqu
    a extends Object declared in class DecimalEqu
src/main/java/contingentclaims/observation/observation/DecimalEqu.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                  ^
  symbol:   variable fromValuet
  location: class DecimalEqu<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalEqu
    a extends Object declared in class DecimalEqu
src/main/java/contingentclaims/observation/observation/DecimalEqu.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
  symbol:   variable fromValuet
  location: class DecimalEqu<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalEqu
    a extends Object declared in class DecimalEqu
src/main/java/contingentclaims/observation/observation/DecimalAdd.java:26: error: cannot find symbol
    return new Variant("DecimalAdd", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                        ^
  symbol:   variable toValuet
  location: class DecimalAdd<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalAdd
    a extends Object declared in class DecimalAdd
src/main/java/contingentclaims/observation/observation/DecimalAdd.java:26: error: cannot find symbol
    return new Variant("DecimalAdd", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                                                                                               ^
  symbol:   variable toValuet
  location: class DecimalAdd<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalAdd
    a extends Object declared in class DecimalAdd
src/main/java/contingentclaims/observation/observation/DecimalAdd.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                  ^
  symbol:   variable fromValuet
  location: class DecimalAdd<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalAdd
    a extends Object declared in class DecimalAdd
src/main/java/contingentclaims/observation/observation/DecimalAdd.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
  symbol:   variable fromValuet
  location: class DecimalAdd<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalAdd
    a extends Object declared in class DecimalAdd
src/main/java/contingentclaims/observation/observation/DecimalDiv.java:26: error: cannot find symbol
    return new Variant("DecimalDiv", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                        ^
  symbol:   variable toValuet
  location: class DecimalDiv<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalDiv
    a extends Object declared in class DecimalDiv
src/main/java/contingentclaims/observation/observation/DecimalDiv.java:26: error: cannot find symbol
    return new Variant("DecimalDiv", this.tuple2Value.toValue(v$0 -> v$0.toValue(v$1 -> toValuet.apply(v$1),v$2 -> new Numeric(v$2)),v$3 -> v$3.toValue(v$4 -> toValuet.apply(v$4),v$5 -> new Numeric(v$5))));
                                                                                                                                                               ^
  symbol:   variable toValuet
  location: class DecimalDiv<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalDiv
    a extends Object declared in class DecimalDiv
src/main/java/contingentclaims/observation/observation/DecimalDiv.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                  ^
  symbol:   variable fromValuet
  location: class DecimalDiv<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalDiv
    a extends Object declared in class DecimalDiv
src/main/java/contingentclaims/observation/observation/DecimalDiv.java:33: error: cannot find symbol
    Tuple2<Observation<t, BigDecimal>, Observation<t, BigDecimal>> body = Tuple2.<contingentclaims.observation.Observation<t, java.math.BigDecimal>, contingentclaims.observation.Observation<t, java.math.BigDecimal>>fromValue(variantValue$, v$0 -> Observation.<t, java.math.BigDecimal>fromValue(v$0, v$1 -> fromValuet.apply(v$1), v$2 -> v$2.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()), v$3 -> Observation.<t, java.math.BigDecimal>fromValue(v$3, v$4 -> fromValuet.apply(v$4), v$5 -> v$5.asNumeric().orElseThrow(() -> new IllegalArgumentException("Expected body to be of type com.daml.ledger.javaapi.data.Numeric")).getValue()));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
  symbol:   variable fromValuet
  location: class DecimalDiv<t,a>
  where t,a are type-variables:
    t extends Object declared in class DecimalDiv
    a extends Object declared in class DecimalDiv
24 errors

Support case: https://digitalasset1.lightning.force.com/lightning/r/Case/5004x00000AZaYTAA1/view

stefanobaghino-da commented 2 years ago

I had a look in past version to check whether this is some kind of regression. The earliest SDK version which which the library compiles is 1.11.0. The error was already there at that point. I also tried to use the latest version of the library instead of an old branch. The error is still there. I'll try to create a minimal repro now.

stefanobaghino-da commented 2 years ago

https://github.com/digital-asset/daml/pull/13571 "hides" the problem on the library itself but the occurrence on this bug can still be verified by using it as part of a project where the contingent claims are part of a template definition. Investigating it now.

stefanobaghino-da commented 2 years ago

Next steps:

  1. use all the serializable type definitions in the contingent-claims library in a template
  2. run the Java codegen on the DAR
  3. verify that the output code compiles

If it compiles: the ticket can be closed. If it doesn't compile: continue the investigation.

@matevarga-da If the customer just ran the codegen on library the next version should fix the problem. If the customer is actually using this as a library and running the codegen on the overall artifact using the library, please stand by and watch this ticket for further updates. The relevant ticket is https://digitalasset1.lightning.force.com/lightning/r/Case/5004x00000AZaYTAA1/view

S11001001 commented 2 years ago

@cocreature I confirmed that this does not demonstrate serializable being set on any nonserializable data type definitions in the dalf. ContingentClaims.Claim.Serializable.ClaimF is serializable.

Some investigation notes: generateToValueForRecordLike seems to do the right thing. At a glance escapedNestedTypeVarNames seems the most likely to be missing the t type variable in its traversal, or possibly distinctTypeVars is excluding it for being "equal" to x somehow.

cocreature commented 2 years ago

thanks for the confirmation.

stefanobaghino-da commented 2 years ago

I just realized that https://github.com/digital-asset/daml/pull/13571 performs the transitive closure computation at a DAR level (unless I'm mistaken). Is this enough? Could the codegen be run with two DARs, one with data declarations which are used in templates defined in another?

S11001001 commented 2 years ago

I just realized that #13571 performs the transitive closure computation at a DAR level (unless I'm mistaken).

I believe you're mistaken. configureCodeGenScope makes a Seq[Interface] from all darFiles and an EnvironmentInterface from that (all as intended), and finds the dependencies from that.

S11001001 commented 2 years ago

@matevarga-da #13658 follows up the above with a fix to the original noncompiling code, so it will work regardless of whether ClaimF is used in a template.