Skip to content

Commit ae95e24

Browse files
authored
Rollup merge of rust-lang#65892 - pnkfelix:trim-special-derives, r=petrochenkov
Remove `PartialEq` and `Eq` from the `SpecialDerives`. Now that PR rust-lang#65519 landed, this is the follow-on work of removing `PartialEq` and `Eq` from the set of `SpecialDerives` .
2 parents 1423bec + 9924361 commit ae95e24

File tree

11 files changed

+14
-59
lines changed

11 files changed

+14
-59
lines changed

src/librustc/hir/lowering.rs

-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use syntax::ast;
6565
use syntax::ptr::P as AstP;
6666
use syntax::ast::*;
6767
use syntax::errors;
68-
use syntax::expand::SpecialDerives;
6968
use syntax::print::pprust;
7069
use syntax::parse::token::{self, Nonterminal, Token};
7170
use syntax::tokenstream::{TokenStream, TokenTree};
@@ -184,8 +183,6 @@ pub trait Resolver {
184183
ns: Namespace,
185184
) -> (ast::Path, Res<NodeId>);
186185

187-
fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool;
188-
189186
fn lint_buffer(&mut self) -> &mut lint::LintBuffer;
190187
}
191188

src/librustc/hir/lowering/item.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use smallvec::SmallVec;
1818
use syntax::attr;
1919
use syntax::ast::*;
2020
use syntax::visit::{self, Visitor};
21-
use syntax::expand::SpecialDerives;
2221
use syntax::source_map::{respan, DesugaringKind, Spanned};
2322
use syntax::symbol::{kw, sym};
2423
use syntax_pos::Span;
@@ -227,13 +226,7 @@ impl LoweringContext<'_> {
227226
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
228227
let mut ident = i.ident;
229228
let mut vis = self.lower_visibility(&i.vis, None);
230-
let mut attrs = self.lower_attrs_extendable(&i.attrs);
231-
if self.resolver.has_derives(i.id, SpecialDerives::PARTIAL_EQ | SpecialDerives::EQ) {
232-
// Add `#[structural_match]` if the item derived both `PartialEq` and `Eq`.
233-
let ident = Ident::new(sym::structural_match, i.span);
234-
attrs.push(attr::mk_attr_outer(attr::mk_word_item(ident)));
235-
}
236-
let attrs = attrs.into();
229+
let attrs = self.lower_attrs(&i.attrs);
237230

238231
if let ItemKind::MacroDef(ref def) = i.kind {
239232
if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) {

src/librustc_resolve/lib.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use rustc_metadata::creader::CrateLoader;
3636
use rustc_metadata::cstore::CStore;
3737

3838
use syntax::{struct_span_err, unwrap_or};
39-
use syntax::expand::SpecialDerives;
4039
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
4140
use syntax::ast::{CRATE_NODE_ID, Crate};
4241
use syntax::ast::{ItemKind, Path};
@@ -934,12 +933,10 @@ pub struct Resolver<'a> {
934933
multi_segment_macro_resolutions: Vec<(Vec<Segment>, Span, MacroKind, ParentScope<'a>,
935934
Option<Res>)>,
936935
builtin_attrs: Vec<(Ident, ParentScope<'a>)>,
937-
/// Some built-in derives mark items they are applied to so they are treated specially later.
936+
/// `derive(Copy)` marks items they are applied to so they are treated specially later.
938937
/// Derive macros cannot modify the item themselves and have to store the markers in the global
939938
/// context, so they attach the markers to derive container IDs using this resolver table.
940-
/// FIXME: Find a way for `PartialEq` and `Eq` to emulate `#[structural_match]`
941-
/// by marking the produced impls rather than the original items.
942-
special_derives: FxHashMap<ExpnId, SpecialDerives>,
939+
containers_deriving_copy: FxHashSet<ExpnId>,
943940
/// Parent scopes in which the macros were invoked.
944941
/// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere.
945942
invocation_parent_scopes: FxHashMap<ExpnId, ParentScope<'a>>,
@@ -1078,12 +1075,6 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
10781075
&mut self.definitions
10791076
}
10801077

1081-
fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool {
1082-
let def_id = self.definitions.local_def_id(node_id);
1083-
let expn_id = self.definitions.expansion_that_defined(def_id.index);
1084-
self.has_derives(expn_id, derives)
1085-
}
1086-
10871078
fn lint_buffer(&mut self) -> &mut lint::LintBuffer {
10881079
&mut self.lint_buffer
10891080
}
@@ -1228,7 +1219,7 @@ impl<'a> Resolver<'a> {
12281219
single_segment_macro_resolutions: Default::default(),
12291220
multi_segment_macro_resolutions: Default::default(),
12301221
builtin_attrs: Default::default(),
1231-
special_derives: Default::default(),
1222+
containers_deriving_copy: Default::default(),
12321223
active_features:
12331224
features.declared_lib_features.iter().map(|(feat, ..)| *feat)
12341225
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
@@ -1314,10 +1305,6 @@ impl<'a> Resolver<'a> {
13141305
}
13151306
}
13161307

1317-
fn has_derives(&self, expn_id: ExpnId, markers: SpecialDerives) -> bool {
1318-
self.special_derives.get(&expn_id).map_or(false, |m| m.contains(markers))
1319-
}
1320-
13211308
/// Entry point to crate resolution.
13221309
pub fn resolve_crate(&mut self, krate: &Crate) {
13231310
let _prof_timer =

src/librustc_resolve/macros.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc::{ty, lint, span_bug};
1414
use syntax::ast::{self, NodeId, Ident};
1515
use syntax::attr::StabilityLevel;
1616
use syntax::edition::Edition;
17-
use syntax::expand::SpecialDerives;
1817
use syntax::feature_gate::{emit_feature_err, is_builtin_attr_name};
1918
use syntax::feature_gate::GateIssue;
2019
use syntax::print::pprust;
@@ -255,12 +254,12 @@ impl<'a> base::Resolver for Resolver<'a> {
255254
}
256255
}
257256

258-
fn has_derives(&self, expn_id: ExpnId, derives: SpecialDerives) -> bool {
259-
self.has_derives(expn_id, derives)
257+
fn has_derive_copy(&self, expn_id: ExpnId) -> bool {
258+
self.containers_deriving_copy.contains(&expn_id)
260259
}
261260

262-
fn add_derives(&mut self, expn_id: ExpnId, derives: SpecialDerives) {
263-
*self.special_derives.entry(expn_id).or_default() |= derives;
261+
fn add_derive_copy(&mut self, expn_id: ExpnId) {
262+
self.containers_deriving_copy.insert(expn_id);
264263
}
265264
}
266265

src/libsyntax/expand/mod.rs

-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ use syntax_pos::symbol::sym;
55

66
pub mod allocator;
77

8-
bitflags::bitflags! {
9-
/// Built-in derives that need some extra tracking beyond the usual macro functionality.
10-
#[derive(Default)]
11-
pub struct SpecialDerives: u8 {
12-
const PARTIAL_EQ = 1 << 0;
13-
const EQ = 1 << 1;
14-
const COPY = 1 << 2;
15-
}
16-
}
17-
188
pub fn is_proc_macro_attr(attr: &Attribute) -> bool {
199
[sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
2010
.iter().any(|kind| attr.check_name(*kind))

src/libsyntax_expand/base.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use syntax::symbol::{kw, sym, Ident, Symbol};
1313
use syntax::{ThinVec, MACRO_ARGUMENTS};
1414
use syntax::tokenstream::{self, TokenStream};
1515
use syntax::visit::Visitor;
16-
crate use syntax::expand::SpecialDerives;
1716

1817
use errors::{DiagnosticBuilder, DiagnosticId};
1918
use smallvec::{smallvec, SmallVec};
@@ -860,8 +859,8 @@ pub trait Resolver {
860859

861860
fn check_unused_macros(&mut self);
862861

863-
fn has_derives(&self, expn_id: ExpnId, derives: SpecialDerives) -> bool;
864-
fn add_derives(&mut self, expn_id: ExpnId, derives: SpecialDerives);
862+
fn has_derive_copy(&self, expn_id: ExpnId) -> bool;
863+
fn add_derive_copy(&mut self, expn_id: ExpnId);
865864
}
866865

867866
#[derive(Clone)]

src/libsyntax_expand/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
432432
// can be in scope for all code produced by that container's expansion.
433433
item.visit_with(&mut MarkAttrs(&helper_attrs));
434434
if has_copy {
435-
self.cx.resolver.add_derives(invoc.expansion_data.id, SpecialDerives::COPY);
435+
self.cx.resolver.add_derive_copy(invoc.expansion_data.id);
436436
}
437437

438438
let mut derive_placeholders = Vec::with_capacity(derives.len());

src/libsyntax_ext/deriving/clone.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::deriving::generic::*;
33
use crate::deriving::generic::ty::*;
44

55
use syntax::ast::{self, Expr, GenericArg, Generics, ItemKind, MetaItem, VariantData};
6-
use syntax::expand::SpecialDerives;
76
use syntax_expand::base::{Annotatable, ExtCtxt};
87
use syntax::ptr::P;
98
use syntax::symbol::{kw, sym, Symbol};
@@ -37,7 +36,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt<'_>,
3736
ItemKind::Struct(_, Generics { ref params, .. }) |
3837
ItemKind::Enum(_, Generics { ref params, .. }) => {
3938
let container_id = cx.current_expansion.id.expn_data().parent;
40-
if cx.resolver.has_derives(container_id, SpecialDerives::COPY) &&
39+
if cx.resolver.has_derive_copy(container_id) &&
4140
!params.iter().any(|param| match param.kind {
4241
ast::GenericParamKind::Type { .. } => true,
4342
_ => false,

src/libsyntax_ext/deriving/cmp/eq.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::deriving::generic::*;
33
use crate::deriving::generic::ty::*;
44

55
use syntax::ast::{self, Ident, Expr, MetaItem, GenericArg};
6-
use syntax::expand::SpecialDerives;
76
use syntax::ptr::P;
87
use syntax::symbol::{sym, Symbol};
98
use syntax_expand::base::{Annotatable, ExtCtxt};
@@ -14,8 +13,6 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>,
1413
mitem: &MetaItem,
1514
item: &Annotatable,
1615
push: &mut dyn FnMut(Annotatable)) {
17-
cx.resolver.add_derives(cx.current_expansion.id.expn_data().parent, SpecialDerives::EQ);
18-
1916
let inline = cx.meta_word(span, sym::inline);
2017
let hidden = syntax::attr::mk_nested_word_item(Ident::new(sym::hidden, span));
2118
let doc = syntax::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]);

src/libsyntax_ext/deriving/cmp/partial_eq.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::deriving::generic::*;
33
use crate::deriving::generic::ty::*;
44

55
use syntax::ast::{BinOpKind, Expr, MetaItem};
6-
use syntax::expand::SpecialDerives;
76
use syntax::ptr::P;
87
use syntax::symbol::sym;
98
use syntax_expand::base::{Annotatable, ExtCtxt};
@@ -14,8 +13,6 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt<'_>,
1413
mitem: &MetaItem,
1514
item: &Annotatable,
1615
push: &mut dyn FnMut(Annotatable)) {
17-
cx.resolver.add_derives(cx.current_expansion.id.expn_data().parent, SpecialDerives::PARTIAL_EQ);
18-
1916
// structures are equal if all fields are equal, and non equal, if
2017
// any fields are not equal or if the enum variants are different
2118
fn cs_op(cx: &mut ExtCtxt<'_>,

src/libsyntax_ext/deriving/generic/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ use rustc_target::spec::abi::Abi;
186186
use syntax::ast::{self, BinOpKind, EnumDef, Expr, Generics, Ident, PatKind};
187187
use syntax::ast::{VariantData, GenericParamKind, GenericArg};
188188
use syntax::attr;
189-
use syntax::expand::SpecialDerives;
190189
use syntax::source_map::respan;
191190
use syntax::util::map_in_place::MapInPlace;
192191
use syntax::ptr::P;
@@ -427,10 +426,8 @@ impl<'a> TraitDef<'a> {
427426
}
428427
};
429428
let container_id = cx.current_expansion.id.expn_data().parent;
430-
let is_always_copy =
431-
cx.resolver.has_derives(container_id, SpecialDerives::COPY) &&
432-
has_no_type_params;
433-
let use_temporaries = is_packed && is_always_copy;
429+
let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
430+
let use_temporaries = is_packed && always_copy;
434431

435432
let newitem = match item.kind {
436433
ast::ItemKind::Struct(ref struct_def, ref generics) => {

0 commit comments

Comments
 (0)