Skip to content
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

Do not expand a derive invocation when derive is not allowed #47013

Merged
merged 2 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ impl<'a> LoweringContext<'a> {
bounds,
default: tp.default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
span: tp.span,
pure_wrt_drop: tp.attrs.iter().any(|attr| attr.check_name("may_dangle")),
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
synthetic: tp.attrs.iter()
.filter(|attr| attr.check_name("rustc_synthetic"))
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
Expand Down Expand Up @@ -1611,7 +1611,7 @@ impl<'a> LoweringContext<'a> {
let def = hir::LifetimeDef {
lifetime: self.lower_lifetime(&l.lifetime),
bounds: self.lower_lifetimes(&l.bounds),
pure_wrt_drop: l.attrs.iter().any(|attr| attr.check_name("may_dangle")),
pure_wrt_drop: attr::contains_name(&l.attrs, "may_dangle"),
in_band: false,
};

Expand Down Expand Up @@ -2331,7 +2331,7 @@ impl<'a> LoweringContext<'a> {
let mut vis = self.lower_visibility(&i.vis, None);
let attrs = self.lower_attrs(&i.attrs);
if let ItemKind::MacroDef(ref def) = i.node {
if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
if !def.legacy || attr::contains_name(&i.attrs, "macro_export") {
let body = self.lower_token_stream(def.stream());
self.exported_macros.push(hir::MacroDef {
name,
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/traits/on_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
{
let attrs = tcx.get_attrs(impl_def_id);

let attr = if let Some(item) =
attrs.into_iter().find(|a| a.check_name("rustc_on_unimplemented"))
{
let attr = if let Some(item) = attr::find_by_name(&attrs, "rustc_on_unimplemented") {
item
} else {
return Ok(None);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2402,7 +2402,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

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

/// Returns true if this is an `auto trait`.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ compiler as a whole, see
[the README.md file found in `librustc`](../librustc/README.md).

The `driver` crate is effectively the "main" function for the rust
compiler. It orchstrates the compilation process and "knits together"
compiler. It orchestrates the compilation process and "knits together"
the code from the other crates within rustc. This crate itself does
not contain any of the "main logic" of the compiler (though it does
have some code related to pretty printing or other minor compiler
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_lint/bad_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ impl LintPass for NonSnakeCase {

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) {
let attr_crate_name = cr.attrs
.iter()
.find(|at| at.check_name("crate_name"))
let attr_crate_name = attr::find_by_name(&cr.attrs, "crate_name")
.and_then(|at| at.value_str().map(|s| (at, s)));
if let Some(ref name) = cx.tcx.sess.opts.crate_name {
self.check_snake_case(cx, "crate", name, None);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use rustc::ty::subst::Subst;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
use syntax::ast;
use syntax::attr;
use syntax::symbol::Symbol;
use rustc::hir;
use rustc_const_math::{ConstInt, ConstUsize};
Expand Down Expand Up @@ -78,8 +79,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
// Some functions always have overflow checks enabled,
// however, they may not get codegen'd, depending on
// the settings for the crate they are translated in.
let mut check_overflow = attrs.iter()
.any(|item| item.check_name("rustc_inherit_overflow_checks"));
let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");

// Respect -C overflow-checks.
check_overflow |= tcx.sess.overflow_checks();
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ impl<'a> AstValidator<'a> {
}

fn invalid_non_exhaustive_attribute(&self, variant: &Variant) {
let has_non_exhaustive = variant.node.attrs.iter()
.any(|attr| attr.check_name("non_exhaustive"));
let has_non_exhaustive = attr::contains_name(&variant.node.attrs, "non_exhaustive");
if has_non_exhaustive {
self.err_handler().span_err(variant.span,
"#[non_exhaustive] is not yet supported on variants");
Expand Down Expand Up @@ -308,7 +307,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
ItemKind::Mod(_) => {
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
attr::first_attr_value_str_by_name(&item.attrs, "path");
if item.attrs.iter().any(|attr| attr.check_name("warn_directory_ownership")) {
if attr::contains_name(&item.attrs, "warn_directory_ownership") {
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
let msg = "cannot declare a new module at this location";
self.session.buffer_lint(lint, item.id, item.span, msg);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ impl<'a> Resolver<'a> {

let mut ctor_vis = vis;

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

// If the structure is marked as non_exhaustive then lower the visibility
// to within the crate.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans_utils/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc::session::Session;
use rustc::middle::cstore::{self, LinkMeta};
use rustc::hir::svh::Svh;
use std::path::{Path, PathBuf};
use syntax::ast;
use syntax::{ast, attr};
use syntax_pos::Span;

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

if let Some(sess) = sess {
if let Some(ref s) = sess.opts.crate_name {
Expand Down
12 changes: 12 additions & 0 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ impl Annotatable {
_ => panic!("expected Item")
}
}

pub fn derive_allowed(&self) -> bool {
match *self {
Annotatable::Item(ref item) => match item.node {
ast::ItemKind::Struct(..) |
ast::ItemKind::Enum(..) |
ast::ItemKind::Union(..) => true,
_ => false,
},
_ => false,
}
}
}

// A more flexible ItemDecorator.
Expand Down
Loading