-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Error on deriving PartialEq on Foo and then implementing it for dyn Foo #78808
Comments
I have minimized the problem a bit more: trait T {}
impl PartialEq for dyn T + '_ {
fn eq (self: &'_ Self, _: &'_ Self) -> bool
{
loop {}
}
}
fn _check (it: &'_ Box<dyn T>)
{
*it == *it;
} fails with the same error message:
So, either that is a valid error, and the macro should be unsugaring to |
FWIW, I have implemented a fix for the "the macro should be unsugaring to …" case, although I think that it's rather diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
index 8e9f15743cc..faaba4bf4ef 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
@@ -5,7 +5,7 @@ use crate::deriving::{path_local, path_std};
use rustc_ast::ptr::P;
use rustc_ast::{BinOpKind, Expr, MetaItem};
use rustc_expand::base::{Annotatable, ExtCtxt};
-use rustc_span::symbol::sym;
+use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
pub fn expand_deriving_partial_eq(
@@ -21,7 +21,7 @@ pub fn expand_deriving_partial_eq(
cx: &mut ExtCtxt<'_>,
span: Span,
substr: &Substructure<'_>,
- op: BinOpKind,
+ op: Symbol,
combiner: BinOpKind,
base: bool,
) -> P<Expr> {
@@ -31,7 +31,15 @@ pub fn expand_deriving_partial_eq(
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
};
- cx.expr_binary(span, op, self_f, other_f.clone())
+ // Instead of `*at_field OP *at_other_field`, do:
+ // `::core::cmp::PartialEq::op(&*at_field, &*at_other_field)`
+ // so as to avoid a bug when `*at_field` is of type
+ // `impl !Copy + Deref<Target = impl !Sized + PartialEq>` (_e.g._,
+ // `Box<dyn Trait>` where `dyn Trait : PartialEq`. See #78808).
+ let self_f_ref = cx.expr_addr_of(span, self_f);
+ let other_f_ref = cx.expr_addr_of(span, other_f.clone());
+ let partial_eq_op = cx.std_path(&[sym::cmp, sym::PartialEq, op]);
+ cx.expr_call_global(span, partial_eq_op, vec![self_f_ref, other_f_ref])
};
cs_fold1(
@@ -57,10 +65,10 @@ pub fn expand_deriving_partial_eq(
}
fn cs_eq(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
- cs_op(cx, span, substr, BinOpKind::Eq, BinOpKind::And, true)
+ cs_op(cx, span, substr, sym::eq, BinOpKind::And, true)
}
fn cs_ne(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
- cs_op(cx, span, substr, BinOpKind::Ne, BinOpKind::Or, false)
+ cs_op(cx, span, substr, sym::ne, BinOpKind::Or, false)
}
macro_rules! md { |
I ran into the same bug today |
Current output:
|
Add extra tuple to workaround rust-lang/rust#78808 Rename CustomConst::(eq => equal_consts) to disambiguate
Add extra tuple to workaround rust-lang/rust#78808 Rename CustomConst::(eq => equal_consts) to disambiguate
I ran into the same problem today, and used this workaround. It's not pretty, but it's easy. Double wrap the trait object. This: #[derive(PartialEq)]
pub struct Foo(Box<dyn MyTrait>); ... becomes this: #[derive(PartialEq)]
pub struct Foo(Box<Box<dyn MyTrait>>); It should work with any combination of smart pointers, not just Box and Box. |
@dnut FWIW a less expensive workaround: trait Trait {}
impl PartialEq for dyn '_ + Trait { ... }
#[derive(PartialEq)]
struct Foo<DynTrait : ?Sized + PartialEq = dyn Trait>(
Box<DynTrait>,
);
|
I tried this code:
I expected to see this happen:
A working
PartialEq
derived implementation forBar
.Instead, this happened:
An error was thrown:
Meta
The bug is present on nightly and stable in the two versions I tried:
rustc --version --verbose
:rustc --version --verbose
:Backtrace doesn't show anything different than the error.
Here is the reproducable code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a4d7bd689ed0687e24533f956f7a5971
@danielhenrymantilla seemed interested in the issue.
The text was updated successfully, but these errors were encountered: