-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gccrs: add support for lang_item eq and PartialEq trait
The Eq and Partial Ord are very similar to the operator overloads we support for add/sub/etc... but they differ in that usually the function call name matches the name of the lang item. This time we need to have support to send in a new path for the method call on the lang item we want instead of just the name of the lang item. NOTE: this test case doesnt work correctly yet we need to support the derive of partial eq on enums to generate the correct comparison code for that. Fixes #3302 gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::visit): handle partial_eq possible call * backend/rust-compile-expr.h: handle case where lang item calls differ from name * hir/tree/rust-hir-expr.cc (OperatorExprMeta::OperatorExprMeta): new helper * hir/tree/rust-hir-expr.h: likewise * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): handle partial_eq (TypeCheckExpr::resolve_operator_overload): likewise * typecheck/rust-hir-type-check-expr.h: likewise * util/rust-lang-item.cc (LangItem::ComparisonToLangItem): map comparison to lang item (LangItem::ComparisonToSegment): likewise * util/rust-lang-item.h: new lang items PartialOrd and Eq * util/rust-operators.h (enum class): likewise gcc/testsuite/ChangeLog: * rust/compile/cmp1.rs: New test. Signed-off-by: Philip Herron <herron.philip@googlemail.com>
- Loading branch information
Showing
10 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// { dg-options "-w" } | ||
// 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 | ||
} | ||
} | ||
|
||
pub 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.isbn == other.isbn | ||
} | ||
} | ||
|
||
pub fn main() { | ||
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; | ||
} |