Skip to content

Commit 5f7aeaf

Browse files
committed
Auto merge of #47013 - topecongiro:issue-46655, r=petrochenkov
Do not expand a derive invocation when derive is not allowed Closes #46655. The first commit is what actually closes #46655. The second one is just a refactoring I have done while waiting on a test.
2 parents 9a8c753 + d882691 commit 5f7aeaf

File tree

16 files changed

+151
-108
lines changed

16 files changed

+151
-108
lines changed

src/librustc/hir/lowering.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ impl<'a> LoweringContext<'a> {
15711571
bounds,
15721572
default: tp.default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
15731573
span: tp.span,
1574-
pure_wrt_drop: tp.attrs.iter().any(|attr| attr.check_name("may_dangle")),
1574+
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
15751575
synthetic: tp.attrs.iter()
15761576
.filter(|attr| attr.check_name("rustc_synthetic"))
15771577
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
@@ -1611,7 +1611,7 @@ impl<'a> LoweringContext<'a> {
16111611
let def = hir::LifetimeDef {
16121612
lifetime: self.lower_lifetime(&l.lifetime),
16131613
bounds: self.lower_lifetimes(&l.bounds),
1614-
pure_wrt_drop: l.attrs.iter().any(|attr| attr.check_name("may_dangle")),
1614+
pure_wrt_drop: attr::contains_name(&l.attrs, "may_dangle"),
16151615
in_band: false,
16161616
};
16171617

@@ -2331,7 +2331,7 @@ impl<'a> LoweringContext<'a> {
23312331
let mut vis = self.lower_visibility(&i.vis, None);
23322332
let attrs = self.lower_attrs(&i.attrs);
23332333
if let ItemKind::MacroDef(ref def) = i.node {
2334-
if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
2334+
if !def.legacy || attr::contains_name(&i.attrs, "macro_export") {
23352335
let body = self.lower_token_stream(def.stream());
23362336
self.exported_macros.push(hir::MacroDef {
23372337
name,

src/librustc/traits/on_unimplemented.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
140140
{
141141
let attrs = tcx.get_attrs(impl_def_id);
142142

143-
let attr = if let Some(item) =
144-
attrs.into_iter().find(|a| a.check_name("rustc_on_unimplemented"))
145-
{
143+
let attr = if let Some(item) = attr::find_by_name(&attrs, "rustc_on_unimplemented") {
146144
item
147145
} else {
148146
return Ok(None);

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24022402

24032403
/// Determine whether an item is annotated with an attribute
24042404
pub fn has_attr(self, did: DefId, attr: &str) -> bool {
2405-
self.get_attrs(did).iter().any(|item| item.check_name(attr))
2405+
attr::contains_name(&self.get_attrs(did), attr)
24062406
}
24072407

24082408
/// Returns true if this is an `auto trait`.

src/librustc_driver/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ compiler as a whole, see
33
[the README.md file found in `librustc`](../librustc/README.md).
44

55
The `driver` crate is effectively the "main" function for the rust
6-
compiler. It orchstrates the compilation process and "knits together"
6+
compiler. It orchestrates the compilation process and "knits together"
77
the code from the other crates within rustc. This crate itself does
88
not contain any of the "main logic" of the compiler (though it does
99
have some code related to pretty printing or other minor compiler

src/librustc_lint/bad_style.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ impl LintPass for NonSnakeCase {
221221

222222
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
223223
fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) {
224-
let attr_crate_name = cr.attrs
225-
.iter()
226-
.find(|at| at.check_name("crate_name"))
224+
let attr_crate_name = attr::find_by_name(&cr.attrs, "crate_name")
227225
.and_then(|at| at.value_str().map(|s| (at, s)));
228226
if let Some(ref name) = cx.tcx.sess.opts.crate_name {
229227
self.check_snake_case(cx, "crate", name, None);

src/librustc_mir/hair/cx/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc::ty::subst::Subst;
2727
use rustc::ty::{self, Ty, TyCtxt};
2828
use rustc::ty::subst::Substs;
2929
use syntax::ast;
30+
use syntax::attr;
3031
use syntax::symbol::Symbol;
3132
use rustc::hir;
3233
use rustc_const_math::{ConstInt, ConstUsize};
@@ -78,8 +79,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
7879
// Some functions always have overflow checks enabled,
7980
// however, they may not get codegen'd, depending on
8081
// the settings for the crate they are translated in.
81-
let mut check_overflow = attrs.iter()
82-
.any(|item| item.check_name("rustc_inherit_overflow_checks"));
82+
let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
8383

8484
// Respect -C overflow-checks.
8585
check_overflow |= tcx.sess.overflow_checks();

src/librustc_passes/ast_validation.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ impl<'a> AstValidator<'a> {
5151
}
5252

5353
fn invalid_non_exhaustive_attribute(&self, variant: &Variant) {
54-
let has_non_exhaustive = variant.node.attrs.iter()
55-
.any(|attr| attr.check_name("non_exhaustive"));
54+
let has_non_exhaustive = attr::contains_name(&variant.node.attrs, "non_exhaustive");
5655
if has_non_exhaustive {
5756
self.err_handler().span_err(variant.span,
5857
"#[non_exhaustive] is not yet supported on variants");
@@ -308,7 +307,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
308307
ItemKind::Mod(_) => {
309308
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
310309
attr::first_attr_value_str_by_name(&item.attrs, "path");
311-
if item.attrs.iter().any(|attr| attr.check_name("warn_directory_ownership")) {
310+
if attr::contains_name(&item.attrs, "warn_directory_ownership") {
312311
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
313312
let msg = "cannot declare a new module at this location";
314313
self.session.buffer_lint(lint, item.id, item.span, msg);

src/librustc_resolve/build_reduced_graph.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ impl<'a> Resolver<'a> {
358358

359359
let mut ctor_vis = vis;
360360

361-
let has_non_exhaustive = item.attrs.iter()
362-
.any(|item| item.check_name("non_exhaustive"));
361+
let has_non_exhaustive = attr::contains_name(&item.attrs, "non_exhaustive");
363362

364363
// If the structure is marked as non_exhaustive then lower the visibility
365364
// to within the crate.

src/librustc_trans_utils/link.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc::session::Session;
1313
use rustc::middle::cstore::{self, LinkMeta};
1414
use rustc::hir::svh::Svh;
1515
use std::path::{Path, PathBuf};
16-
use syntax::ast;
16+
use syntax::{ast, attr};
1717
use syntax_pos::Span;
1818

1919
pub fn out_filename(sess: &Session,
@@ -69,8 +69,8 @@ pub fn find_crate_name(sess: Option<&Session>,
6969
// as used. After doing this, however, we still prioritize a crate name from
7070
// the command line over one found in the #[crate_name] attribute. If we
7171
// find both we ensure that they're the same later on as well.
72-
let attr_crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
73-
.and_then(|at| at.value_str().map(|s| (at, s)));
72+
let attr_crate_name = attr::find_by_name(attrs, "crate_name")
73+
.and_then(|at| at.value_str().map(|s| (at, s)));
7474

7575
if let Some(sess) = sess {
7676
if let Some(ref s) = sess.opts.crate_name {

src/libsyntax/ext/base.rs

+12
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ impl Annotatable {
9696
_ => panic!("expected Item")
9797
}
9898
}
99+
100+
pub fn derive_allowed(&self) -> bool {
101+
match *self {
102+
Annotatable::Item(ref item) => match item.node {
103+
ast::ItemKind::Struct(..) |
104+
ast::ItemKind::Enum(..) |
105+
ast::ItemKind::Union(..) => true,
106+
_ => false,
107+
},
108+
_ => false,
109+
}
110+
}
99111
}
100112

101113
// A more flexible ItemDecorator.

0 commit comments

Comments
 (0)