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

libcore cmp.rs implementation #3302

Open
philberty opened this issue Dec 12, 2024 · 0 comments
Open

libcore cmp.rs implementation #3302

philberty opened this issue Dec 12, 2024 · 0 comments

Comments

@philberty
Copy link
Member

We need to handle the equality and partial equality lang items following this example from libcore 1.49

// taken from https://github.com/rust-lang/rust/blob/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/cmp.rs#L98

#[lang = "sized"]
pub trait Sized {}

#[lang = "eq"]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(alias = "==")]
#[doc(alias = "!=")]
pub trait PartialEq<Rhs: ?Sized = Self> {
    /// This method tests for `self` and `other` values to be equal, and is used
    /// by `==`.
    #[must_use]
    #[stable(feature = "rust1", since = "1.0.0")]
    fn eq(&self, other: &Rhs) -> bool;

    /// This method tests for `!=`.
    #[inline]
    #[must_use]
    #[stable(feature = "rust1", since = "1.0.0")]
    fn ne(&self, other: &Rhs) -> bool {
        !self.eq(other)
    }
}

enum BookFormat {
    Paperback,
    Hardback,
    Ebook,
}

impl PartialEq<BookFormat> for BookFormat {
    fn eq(&self, other: &BookFormat) -> bool {
        self == other
    }
}

struct Book {
    isbn: i32,
    format: BookFormat,
}

// Implement <Book> == <BookFormat> comparisons
impl PartialEq<BookFormat> for Book {
    fn eq(&self, other: &BookFormat) -> bool {
        self.format == *other
    }
}

// Implement <BookFormat> == <Book> comparisons
impl PartialEq<Book> for BookFormat {
    fn eq(&self, other: &Book) -> bool {
        *self == other.format
    }
}

// Implement <Book> == <Book> comparisons
impl PartialEq<Book> for Book {
    fn eq(&self, other: &Book) -> bool {
        *self == other.format
    }
}

pub fn test() {
    let b1 = Book {
        isbn: 1,
        format: BookFormat::Paperback,
    };
    let b2 = Book {
        isbn: 2,
        format: BookFormat::Paperback,
    };

    let c1: bool = b1 == BookFormat::Paperback;
    let c2: bool = BookFormat::Paperback == b2;
    let c3: bool = b1 == b2;
}

@philberty philberty added this to the Remaining typecheck issues milestone Dec 12, 2024
@philberty philberty self-assigned this Dec 12, 2024
philberty added a commit that referenced this issue Dec 19, 2024
TODO

Fixes #3302

gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::visit):
	* backend/rust-compile-expr.h:
	* hir/tree/rust-hir-expr.cc (OperatorExprMeta::OperatorExprMeta):
	* hir/tree/rust-hir-expr.h:
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
	(TypeCheckExpr::resolve_operator_overload):
	* typecheck/rust-hir-type-check-expr.h:
	* util/rust-lang-item.cc (LangItem::ComparisonToLangItem):
	(LangItem::ComparisonToSegment):
	* util/rust-lang-item.h:

Signed-off-by: Philip Herron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

1 participant