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

Methods called from toString need a readonly receiver type #59

Open
Shreyas-BS opened this issue Jun 30, 2024 · 3 comments
Open

Methods called from toString need a readonly receiver type #59

Shreyas-BS opened this issue Jun 30, 2024 · 3 comments

Comments

@Shreyas-BS
Copy link

Shreyas-BS commented Jun 30, 2024

Issue Description

Primitive int Readonly constant numbers when passed to the toString method as radix parameter creates this error. It is expecting the object to be also Readonly Type.

How to reproduce

Create an object of type Immutable or Mutable and add 2 toString functions (function overloading) differentiated by an int radix parameter. Call the toString with a constant number as a radix parameter inside the other toString function without.

Screen Shot / Recording

Screenshot 2024-06-30 at 3 43 11 PM Screenshot 2024-06-30 at 4 22 41 PM

Sample Minimum test case code

import qual.Immutable;

@Immutable
public final class Demo {

    // private final @Immutable int value = 123;

    public @Immutable String toString() {
        return toString(10);
    }

    public @Immutable String toString(int radix) {
        return "Hello";
        // Usually it would be calling a function like this
        // return Integer.toString(this.value, radix);
    }
}
@wmdietl
Copy link
Member

wmdietl commented Jun 30, 2024

Thanks for the bug report!

Note that the receiver for toString() is @Readonly, according to

String toString(@Readonly Object this);

The default receiver for the new method toString(int) is @Immutable, because the corresponding class is annotated as @Immutable.

So the actual receiver at the call site (@Readonly) is a supertype of the expected receiver type (@Immutable).
Therefore, you get an error.

So any method that you want to call from a @Readonly method also needs to have an @Readonly receiver.
Can you try doing that?

@wmdietl wmdietl changed the title toString(radix) not accepting Readonly type Methods called from toString need a readonly receiver type Jun 30, 2024
@Shreyas-BS
Copy link
Author

Shreyas-BS commented Jul 1, 2024

I am not able to annotate the class with @readonly type and I am getting this error as Invalid annotation

Screenshot 2024-07-01 at 12 37 18 AM

Here is my code that I tried:

import qual.Readonly;

@Readonly
public final class Demo {

    public @Readonly String toString() {
        return toString(10);
    }

    public @Readonly String toString(@Readonly int radix) {
        return "Hello";
    }
}

@wmdietl
Copy link
Member

wmdietl commented Jul 1, 2024

You should annotate the receiver of the method, not the class.
Change the class back to @Immutable and then use String toString(@Readonly Demo this, int radix). The annotations on String and int are not necessary.

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