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

Fix Issue 22159 - == causes error for array of classes in safe method #14988

Closed
wants to merge 1 commit into from

Conversation

RazvanN7
Copy link
Contributor

If the type of classes that are compared does not override opEquals, Object.opEquals ends up, eventually, being called. Since Object.opEquals is @System, @safe user code is going to issue a confusing "Incompatible types for comparison: LeClass and LeClass". To fix this, I'm simply checking if Object.opEquals is being called and I'm trusting it (it's just doing an is comparison anyway).

"But why aren't you marking Object.opEquals as @safe?" -> that will impose the restriction that all classes define a @safe opEquals (which would normally make sense, but it would break tons of code).

"This makes the code ugly" -> I agree, but I have no other idea on how to fix this, other than ProtoObject. Maybe other better solutions exist, feel free to enlighten me.

"This does not fix the underlying issue that the compiler gags errors when analyzing the hook, therefore it's still possible to have a confusing error message, for example when a class defines a system opEquals that is called from safe code" -> Yes, but that matter can be explored in a different PR.

cc @adamdruppe as you have been looking at this.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
22159 regression "==" causeses error for array of classes in safe method

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "stable + dmd#14988"

@adamdruppe
Copy link
Contributor

It looks to me that the original bug is a diagnostic bug - the compiler is not giving a helpful error message, but the behavior is correct. The opEquals is system so it is not supposed to be called from safe code.

The cast in here is not correct, since the method may be overridden by child classes and violate the safety guarantees.

Add this as a unittest:

class C { int a; this(int) @safe {} }
class D : C {
        this(int a) @safe { super(a); }
        override bool opEquals(Object rhs) const {
                // obviously not safe
                *(cast(int*) 0x5afe) = 0xdead5afe;
                return true;
        }
}

@safe void main()
{
    C c = new D(1);
    C[] a = [c, c, c];
    assert(a == [c, c, c]);
}

It should definitely fail to compile. I believe it will compile with this PR.

@dkorpel
Copy link
Contributor

dkorpel commented May 2, 2023

@RazvanN7 Do you agree with Adam?

@RazvanN7
Copy link
Contributor Author

RazvanN7 commented May 4, 2023

Yes, it seems that @adamdruppe is right.

@RazvanN7 RazvanN7 closed this May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants