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

Using properties in classes that extend a class implementing the same interface can lead to a Reference is ambiguous error #643

Open
EotT123 opened this issue Nov 21, 2024 · 5 comments

Comments

@EotT123
Copy link
Contributor

EotT123 commented Nov 21, 2024

Describe the bug
Using properties in classes that extend a class implementing the same interface can lead to a Reference is ambiguous error. Consider the following example:

public interface TestIntf {
   Object getObject();
}

public class Test implements TestIntf {
  @override @var Object object;
}

public class Test2 extends Test implements TestIntf {
    public void foo(){
        Object superObject = object; // Reference to 'object' is ambiguous, both 'Test. object' and 'TestIntf. object' match
    }
}

In this example, referencing object in Test2 results in an ambiguity. While only one actual object exists, both the field Test.object and the method TestIntf.getObject() are seen as possible references.

In this simple case, you could remove implements TestIntf from the Test2 class to resolve the conflict. However, in more complex scenarios, such as multiple inheritance or interface combinations, this approach may not be feasible.

To resolve the issue, the getter method getObject() can be used directly instead of the property reference. However, IntelliJ flags this as a warning: Use property 'object' here.

Desktop (please complete the following information):

  • OS Type & Version: Windows 10 22H2
  • Java/JDK version: openjdk 23.0.1
  • IDE version (IntelliJ IDEA or Android Studio): IntelliJ IDEA 2024.3
  • Manifold version: 2024.1.42 (latest version)
  • Manifold IntelliJ plugin version: 2024.1.13 (latest version)
@rsmckinney
Copy link
Member

By chance I am working on a set of properties changes that should fix this, will be ready in a week or so.

For the interim you can qualify the ambiguous reference with this.:

    public void foo(){
        Object superObject = this.object;
    }

@EotT123
Copy link
Contributor Author

EotT123 commented Nov 21, 2024

Adding this works as expected. However, this approach doesn't work when accessing the property on the object, as it is not possible to use this in that case. For example:

Test2 test2 = ....;
Object obj = test2.object; // error
Object obj = test2.this.object; // error, not valid to write this

I'm eagerly awaiting your solution!

@rsmckinney
Copy link
Member

Yes, I think the changes I have fix this. I whipped up a test reflecting yours here plus random other bits.

  public void testDoubleImplements()
  {
    MyClass2 myClass2 = new MyClass2();
    Object object = myClass2.object;
    assertNull(object);
    myClass2.object = "hello";
    assertEquals(myClass2.object, "hello");
    MyClass myClass = myClass2;
    myClass.object = "hi";
    assertEquals(myClass.object, "hi");
  }

  public interface TestIntf {
    Object getObject();
  }

  public static class MyClass implements TestIntf {
    @override @var Object object;

    void whatever()
    {
      object = "hey";
    }
  }

  public static class MyClass2 extends MyClass implements TestIntf {
    public void foo(){
      object = "hi"; // should not be ambiguous ref
      Object value = object; // should not be ambiguous ref

      MyClass2 mc2 = new MyClass2();
      mc2.object = value;
    }
  }

Please let me know if there are other cases you see that fail. Thanks!

@rsmckinney
Copy link
Member

Fix available with release 2024.1.43

@EotT123
Copy link
Contributor Author

EotT123 commented Dec 14, 2024

Thanks, this seems to have fixed the problem. However, IntelliJ is still marking it as an error (probably something that should be fixed in the IntelliJ plugin?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants