Skip to content

Commit 93e4443

Browse files
committed
check for both partialeq and eq
1 parent 7f661ec commit 93e4443

File tree

4 files changed

+82
-38
lines changed

4 files changed

+82
-38
lines changed

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ declare_lint! {
146146
pub ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN,
147147
Deny,
148148
"constants of struct or enum type can only be used in a pattern if \
149-
the struct or enum has `#[derive(Eq)]`"
149+
the struct or enum has `#[derive(PartialEq, Eq)]`"
150150
}
151151

152152
declare_lint! {

src/librustc/middle/const_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pub fn const_expr_to_pat(tcx: &ty::TyCtxt, expr: &Expr, pat_id: ast::NodeId, spa
345345
span,
346346
format!("to use a constant of type `{}` \
347347
in a pattern, \
348-
`{}` must be annotated with `#[derive(Eq)]`",
348+
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
349349
tcx.item_path_str(adt_def.did),
350350
tcx.item_path_str(adt_def.did)));
351351
}

src/libsyntax_ext/deriving/mod.rs

+45-36
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ fn expand_derive(cx: &mut ExtCtxt,
9292
cx.span_warn(mitem.span, "empty trait list in `derive`");
9393
}
9494

95+
let mut found_partial_eq = false;
96+
let mut found_eq = false;
97+
9598
for titem in traits.iter().rev() {
9699
let tname = match titem.node {
97100
MetaItemKind::Word(ref tname) => tname,
@@ -110,50 +113,56 @@ fn expand_derive(cx: &mut ExtCtxt,
110113
continue;
111114
}
112115

113-
// RFC #1445. `#[derive(Eq)]` adds a (trusted)
114-
// `#[structural_match]` attribute.
115116
if &tname[..] == "Eq" {
116-
// This span is **very** sensitive and crucial to
117-
// getting the stability behavior we want. What we
118-
// are doing is marking `#[structural_match]` with
119-
// the span of the `#[deriving(Eq)]` attribute
120-
// (the entire attribute, not just the `Eq` part),
121-
// but with the current backtrace. The current
122-
// backtrace will contain a topmost entry that IS
123-
// this `#[deriving(Eq)]` attribute and with the
124-
// "allow-unstable" flag set to true.
125-
//
126-
// Note that we do NOT use the span of the `Eq`
127-
// text itself. You might think this is
128-
// equivalent, because the `Eq` appears within the
129-
// `#[deriving(Eq)]` attribute, and hence we would
130-
// inherit the "allows unstable" from the
131-
// backtrace. But in fact this is not always the
132-
// case. The actual source text that led to
133-
// deriving can be `#[$attr]`, for example, where
134-
// `$attr == deriving(Eq)`. In that case, the
135-
// "#[structural_match]" would be considered to
136-
// originate not from the deriving call but from
137-
// text outside the deriving call, and hence would
138-
// be forbidden from using unstable
139-
// content.
140-
//
141-
// See tests src/run-pass/rfc1445 for
142-
// examples. --nmatsakis
143-
let span = Span { expn_id: cx.backtrace(), .. span };
144-
assert!(cx.parse_sess.codemap().span_allows_unstable(span));
145-
debug!("inserting structural_match with span {:?}", span);
146-
let structural_match = intern_and_get_ident("structural_match");
147-
item.attrs.push(cx.attribute(span,
148-
cx.meta_word(span,
149-
structural_match)));
117+
found_eq = true;
118+
} else if &tname[..] == "PartialEq" {
119+
found_partial_eq = true;
150120
}
151121

152122
// #[derive(Foo, Bar)] expands to #[derive_Foo] #[derive_Bar]
153123
item.attrs.push(cx.attribute(titem.span, cx.meta_word(titem.span,
154124
intern_and_get_ident(&format!("derive_{}", tname)))));
155125
}
156126

127+
// RFC #1445. `#[derive(PartialEq, Eq)]` adds a (trusted)
128+
// `#[structural_match]` attribute.
129+
if found_partial_eq && found_eq {
130+
// This span is **very** sensitive and crucial to
131+
// getting the stability behavior we want. What we are
132+
// doing is marking `#[structural_match]` with the
133+
// span of the `#[deriving(...)]` attribute (the
134+
// entire attribute, not just the `PartialEq` or `Eq`
135+
// part), but with the current backtrace. The current
136+
// backtrace will contain a topmost entry that IS this
137+
// `#[deriving(...)]` attribute and with the
138+
// "allow-unstable" flag set to true.
139+
//
140+
// Note that we do NOT use the span of the `Eq`
141+
// text itself. You might think this is
142+
// equivalent, because the `Eq` appears within the
143+
// `#[deriving(Eq)]` attribute, and hence we would
144+
// inherit the "allows unstable" from the
145+
// backtrace. But in fact this is not always the
146+
// case. The actual source text that led to
147+
// deriving can be `#[$attr]`, for example, where
148+
// `$attr == deriving(Eq)`. In that case, the
149+
// "#[structural_match]" would be considered to
150+
// originate not from the deriving call but from
151+
// text outside the deriving call, and hence would
152+
// be forbidden from using unstable
153+
// content.
154+
//
155+
// See tests src/run-pass/rfc1445 for
156+
// examples. --nmatsakis
157+
let span = Span { expn_id: cx.backtrace(), .. span };
158+
assert!(cx.parse_sess.codemap().span_allows_unstable(span));
159+
debug!("inserting structural_match with span {:?}", span);
160+
let structural_match = intern_and_get_ident("structural_match");
161+
item.attrs.push(cx.attribute(span,
162+
cx.meta_word(span,
163+
structural_match)));
164+
}
165+
157166
item
158167
})
159168
}, |a| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(dead_code)]
12+
#![deny(future_incompatible)]
13+
14+
#[derive(Eq)]
15+
struct Foo {
16+
x: u32
17+
}
18+
19+
impl PartialEq for Foo {
20+
fn eq(&self, _: &Foo) -> bool {
21+
false // ha ha sucker!
22+
}
23+
}
24+
25+
const FOO: Foo = Foo { x: 0 };
26+
27+
fn main() {
28+
let y = Foo { x: 1 };
29+
match y {
30+
FOO => { }
31+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
32+
//~| WARNING will become a hard error
33+
_ => { }
34+
}
35+
}

0 commit comments

Comments
 (0)