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

Java: Overriding record toString() can't be referred to as a dependency #17181

Closed
rfevang opened this issue Jan 10, 2023 · 5 comments
Closed
Assignees
Labels
stale Issues or PRs that are stale (no activity for 30 days) team-Rules-Java Issues for Java rules type: bug untriaged

Comments

@rfevang
Copy link

rfevang commented Jan 10, 2023

Description of the bug:

The compiler complains about an ambiguous reference when trying to refer to an overriden toString() method of a record. The error only happens when the record class is a dependency, not when it is listed in srcs.

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Empty WORKSPACE. (It also fails when loading rules_java 5.4.0

.bazelrc:

build --java_language_version=17 --java_runtime_version=remotejdk_17
build --tool_java_language_version=17 --tool_java_runtime_version=remotejdk_17

MyRecord.java:

public record MyRecord(String value) {
  public String toString() {
    return "Custom toString(): " + value;
  }
}

DoubleToString.java:

import java.util.function.Supplier;

public class DoubleToString {
  public Supplier<String> foo(MyRecord myRecord) {
    return myRecord::toString;
  }
}

BUILD.bazel:

java_library(
    name = "my_record",
    srcs = ["MyRecord.java"],
)

# Failing rule:
java_library(
    name = "record_to_string_fails",
    srcs = ["DoubleToString.java"],
    deps = [":my_record"],
)

# Working rule:
java_library(
    name = "no_dep_to_string_works",      
    srcs = ["MyRecord.java", "DoubleToString.java"],
)

Doing a bazel build :record_to_string_fails results in the following output:

$ bazel build :record_to_string_fails
INFO: Analyzed target //:record_to_string_fails (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /home/fevang/debug/bazel_bug/BUILD.bazel:6:13: Building librecord_to_string_fails.jar (1 source file) failed: (Exit 1): java failed: error executing command (from target //:record_to_string_fails) external/remotejdk17_linux/bin/java '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' ... (remaining 16 arguments skipped)
DoubleToString.java:5: error: incompatible types: invalid method reference
    return myRecord::toString;
           ^
    reference to toString is ambiguous
      both method toString() in MyRecord and method toString() in MyRecord match
Target //:record_to_string_fails failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.075s, Critical Path: 0.02s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

Building :no_dep_to_string_works works just fine.

Which operating system are you running Bazel on?

Ubuntu 22.04.1 LTS

What is the output of bazel info release?

release 6.0.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No

Any other information, logs, or outputs that you want to share?

Ran into this issue when trying to convert a maven project to bazel.

Building with javac works, even when MyRecord.java is removed after compiling MyRecord.class.

@rfevang
Copy link
Author

rfevang commented Jan 10, 2023

Similar (probably related) issue involving extra constructors:

MyRecord.java:

public record MyRecord(String value) {

  public MyRecord(String a, String b) {
    this(a + ", " + b);
  }  

  public String toString() {
    return "Custom toString(): " + value;
  }
}

ExtraConstructor.java:

public class ExtraConstructor {
  public void foo() {
    new MyRecord("string");
  }
}

Compiling both in a rule works. If MyRecord is a dependency when compiling ExtraConstructor, the following error is printed:

ExtraConstructor.java:3: error: constructor MyRecord in record MyRecord cannot be applied to given types;
    new MyRecord("string");
    ^
  required: String,String
  found:    String
  reason: actual and formal argument lists differ in length

@cushon cushon self-assigned this Jan 12, 2023
copybara-service bot pushed a commit to google/turbine that referenced this issue Jan 12, 2023
…records

These methods are only emitted if they don't already exist, per JLS 8.10.3.

bazelbuild/bazel#17181

PiperOrigin-RevId: 501571162
copybara-service bot pushed a commit to google/turbine that referenced this issue Jan 12, 2023
…records

These methods are only emitted if they don't already exist, per JLS 8.10.3.

bazelbuild/bazel#17181

PiperOrigin-RevId: 501618070
@rfevang
Copy link
Author

rfevang commented Jan 18, 2023

Thanks for looking at this @cushon!.

Are there more steps needed? I tried building with HEAD bazel and I still see the same error. Any way I can test this locally?

@cushon
Copy link
Contributor

cushon commented Jan 18, 2023

This will require a java_tools release, I think it's going to be included in bazelbuild/java_tools#60

Until then, testing this requires additional steps: bazelbuild/java_tools#43

@rfevang
Copy link
Author

rfevang commented Jan 18, 2023

Thanks, I was indeed looking at the outdated documentation on how to run a local java_tools.

Can confirm an updated java_tools fixes the issue in the bug. The related error in my first comment is still there, so opening up a separate issue for that (#17250).

@github-actions
Copy link

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 30 days. It will be closed in the next 7 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler".

@github-actions github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label Feb 18, 2023
@cushon cushon closed this as completed Feb 21, 2023
treblereel pushed a commit to treblereel/turbine that referenced this issue Sep 28, 2023
…records

These methods are only emitted if they don't already exist, per JLS 8.10.3.

bazelbuild/bazel#17181

PiperOrigin-RevId: 501618070
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issues or PRs that are stale (no activity for 30 days) team-Rules-Java Issues for Java rules type: bug untriaged
Projects
None yet
Development

No branches or pull requests

3 participants