From 7d7fbcb3015521412ab17a814680cd1d9bb6cde9 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 28 Oct 2019 12:40:12 +0100 Subject: Remove `PartialEq` and `Eq` from the `SpecialDerives`. --- src/libsyntax_ext/deriving/cmp/eq.rs | 3 --- src/libsyntax_ext/deriving/cmp/partial_eq.rs | 3 --- 2 files changed, 6 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/deriving/cmp/eq.rs b/src/libsyntax_ext/deriving/cmp/eq.rs index eddf8eea1db..41189de7fa2 100644 --- a/src/libsyntax_ext/deriving/cmp/eq.rs +++ b/src/libsyntax_ext/deriving/cmp/eq.rs @@ -3,7 +3,6 @@ use crate::deriving::generic::*; use crate::deriving::generic::ty::*; use syntax::ast::{self, Ident, Expr, MetaItem, GenericArg}; -use syntax::expand::SpecialDerives; use syntax::ptr::P; use syntax::symbol::{sym, Symbol}; use syntax_expand::base::{Annotatable, ExtCtxt}; @@ -14,8 +13,6 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>, mitem: &MetaItem, item: &Annotatable, push: &mut dyn FnMut(Annotatable)) { - cx.resolver.add_derives(cx.current_expansion.id.expn_data().parent, SpecialDerives::EQ); - let inline = cx.meta_word(span, sym::inline); let hidden = syntax::attr::mk_nested_word_item(Ident::new(sym::hidden, span)); let doc = syntax::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]); diff --git a/src/libsyntax_ext/deriving/cmp/partial_eq.rs b/src/libsyntax_ext/deriving/cmp/partial_eq.rs index 2e2be91de8a..19562e350dd 100644 --- a/src/libsyntax_ext/deriving/cmp/partial_eq.rs +++ b/src/libsyntax_ext/deriving/cmp/partial_eq.rs @@ -3,7 +3,6 @@ use crate::deriving::generic::*; use crate::deriving::generic::ty::*; use syntax::ast::{BinOpKind, Expr, MetaItem}; -use syntax::expand::SpecialDerives; use syntax::ptr::P; use syntax::symbol::sym; use syntax_expand::base::{Annotatable, ExtCtxt}; @@ -14,8 +13,6 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt<'_>, mitem: &MetaItem, item: &Annotatable, push: &mut dyn FnMut(Annotatable)) { - cx.resolver.add_derives(cx.current_expansion.id.expn_data().parent, SpecialDerives::PARTIAL_EQ); - // structures are equal if all fields are equal, and non equal, if // any fields are not equal or if the enum variants are different fn cs_op(cx: &mut ExtCtxt<'_>, -- cgit 1.4.1-3-g733a5 From 0dfe0ed8e1b2b8596a8cabee0afdae23e343ee69 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 30 Oct 2019 11:13:00 +0100 Subject: Review feedback: Remove more stuff! Simplify simplify simplify! --- src/librustc/hir/lowering.rs | 3 --- src/librustc_resolve/lib.rs | 17 +++-------------- src/librustc_resolve/macros.rs | 9 ++++----- src/libsyntax/expand/mod.rs | 8 -------- src/libsyntax_expand/base.rs | 5 ++--- src/libsyntax_expand/expand.rs | 2 +- src/libsyntax_ext/deriving/clone.rs | 3 +-- src/libsyntax_ext/deriving/generic/mod.rs | 7 ++----- 8 files changed, 13 insertions(+), 41 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 6b6032516ca..c6acdf53de3 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -65,7 +65,6 @@ use syntax::ast; use syntax::ptr::P as AstP; use syntax::ast::*; use syntax::errors; -use syntax::expand::SpecialDerives; use syntax::print::pprust; use syntax::parse::token::{self, Nonterminal, Token}; use syntax::tokenstream::{TokenStream, TokenTree}; @@ -184,8 +183,6 @@ pub trait Resolver { ns: Namespace, ) -> (ast::Path, Res); - fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool; - fn lint_buffer(&mut self) -> &mut lint::LintBuffer; } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index ca450544558..17b62b3bda2 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -36,7 +36,6 @@ use rustc_metadata::creader::CrateLoader; use rustc_metadata::cstore::CStore; use syntax::{struct_span_err, unwrap_or}; -use syntax::expand::SpecialDerives; use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy}; use syntax::ast::{CRATE_NODE_ID, Crate}; use syntax::ast::{ItemKind, Path}; @@ -934,10 +933,10 @@ pub struct Resolver<'a> { multi_segment_macro_resolutions: Vec<(Vec, Span, MacroKind, ParentScope<'a>, Option)>, builtin_attrs: Vec<(Ident, ParentScope<'a>)>, - /// Some built-in derives mark items they are applied to so they are treated specially later. + /// `derive(Copy)` marks items they are applied to so they are treated specially later. /// Derive macros cannot modify the item themselves and have to store the markers in the global /// context, so they attach the markers to derive container IDs using this resolver table. - special_derives: FxHashMap, + copy_derives: FxHashSet, /// Parent scopes in which the macros were invoked. /// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere. invocation_parent_scopes: FxHashMap>, @@ -1076,12 +1075,6 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> { &mut self.definitions } - fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool { - let def_id = self.definitions.local_def_id(node_id); - let expn_id = self.definitions.expansion_that_defined(def_id.index); - self.has_derives(expn_id, derives) - } - fn lint_buffer(&mut self) -> &mut lint::LintBuffer { &mut self.lint_buffer } @@ -1226,7 +1219,7 @@ impl<'a> Resolver<'a> { single_segment_macro_resolutions: Default::default(), multi_segment_macro_resolutions: Default::default(), builtin_attrs: Default::default(), - special_derives: Default::default(), + copy_derives: Default::default(), active_features: features.declared_lib_features.iter().map(|(feat, ..)| *feat) .chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat)) @@ -1312,10 +1305,6 @@ impl<'a> Resolver<'a> { } } - fn has_derives(&self, expn_id: ExpnId, markers: SpecialDerives) -> bool { - self.special_derives.get(&expn_id).map_or(false, |m| m.contains(markers)) - } - /// Entry point to crate resolution. pub fn resolve_crate(&mut self, krate: &Crate) { let _prof_timer = diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 84d3d3a48b0..9997d0f946a 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -14,7 +14,6 @@ use rustc::{ty, lint, span_bug}; use syntax::ast::{self, NodeId, Ident}; use syntax::attr::StabilityLevel; use syntax::edition::Edition; -use syntax::expand::SpecialDerives; use syntax::feature_gate::{emit_feature_err, is_builtin_attr_name}; use syntax::feature_gate::GateIssue; use syntax::print::pprust; @@ -255,12 +254,12 @@ impl<'a> base::Resolver for Resolver<'a> { } } - fn has_derives(&self, expn_id: ExpnId, derives: SpecialDerives) -> bool { - self.has_derives(expn_id, derives) + fn has_derive_copy(&self, expn_id: ExpnId) -> bool { + self.copy_derives.contains(&expn_id) } - fn add_derives(&mut self, expn_id: ExpnId, derives: SpecialDerives) { - *self.special_derives.entry(expn_id).or_default() |= derives; + fn add_derive_copy(&mut self, expn_id: ExpnId) { + self.copy_derives.insert(expn_id); } } diff --git a/src/libsyntax/expand/mod.rs b/src/libsyntax/expand/mod.rs index 35729508197..03b30fda745 100644 --- a/src/libsyntax/expand/mod.rs +++ b/src/libsyntax/expand/mod.rs @@ -5,14 +5,6 @@ use syntax_pos::symbol::sym; pub mod allocator; -bitflags::bitflags! { - /// Built-in derives that need some extra tracking beyond the usual macro functionality. - #[derive(Default)] - pub struct SpecialDerives: u8 { - const COPY = 1 << 2; - } -} - pub fn is_proc_macro_attr(attr: &Attribute) -> bool { [sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive] .iter().any(|kind| attr.check_name(*kind)) diff --git a/src/libsyntax_expand/base.rs b/src/libsyntax_expand/base.rs index d79b6910587..6cc7b7da53b 100644 --- a/src/libsyntax_expand/base.rs +++ b/src/libsyntax_expand/base.rs @@ -13,7 +13,6 @@ use syntax::symbol::{kw, sym, Ident, Symbol}; use syntax::{ThinVec, MACRO_ARGUMENTS}; use syntax::tokenstream::{self, TokenStream}; use syntax::visit::Visitor; -crate use syntax::expand::SpecialDerives; use errors::{DiagnosticBuilder, DiagnosticId}; use smallvec::{smallvec, SmallVec}; @@ -860,8 +859,8 @@ pub trait Resolver { fn check_unused_macros(&mut self); - fn has_derives(&self, expn_id: ExpnId, derives: SpecialDerives) -> bool; - fn add_derives(&mut self, expn_id: ExpnId, derives: SpecialDerives); + fn has_derive_copy(&self, expn_id: ExpnId) -> bool; + fn add_derive_copy(&mut self, expn_id: ExpnId); } #[derive(Clone)] diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index bdb50dbfb4f..da70fdbb0f3 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -432,7 +432,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { // can be in scope for all code produced by that container's expansion. item.visit_with(&mut MarkAttrs(&helper_attrs)); if has_copy { - self.cx.resolver.add_derives(invoc.expansion_data.id, SpecialDerives::COPY); + self.cx.resolver.add_derive_copy(invoc.expansion_data.id); } let mut derive_placeholders = Vec::with_capacity(derives.len()); diff --git a/src/libsyntax_ext/deriving/clone.rs b/src/libsyntax_ext/deriving/clone.rs index 061afa379c6..c056d03614d 100644 --- a/src/libsyntax_ext/deriving/clone.rs +++ b/src/libsyntax_ext/deriving/clone.rs @@ -3,7 +3,6 @@ use crate::deriving::generic::*; use crate::deriving::generic::ty::*; use syntax::ast::{self, Expr, GenericArg, Generics, ItemKind, MetaItem, VariantData}; -use syntax::expand::SpecialDerives; use syntax_expand::base::{Annotatable, ExtCtxt}; use syntax::ptr::P; use syntax::symbol::{kw, sym, Symbol}; @@ -37,7 +36,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt<'_>, ItemKind::Struct(_, Generics { ref params, .. }) | ItemKind::Enum(_, Generics { ref params, .. }) => { let container_id = cx.current_expansion.id.expn_data().parent; - if cx.resolver.has_derives(container_id, SpecialDerives::COPY) && + if cx.resolver.has_derive_copy(container_id) && !params.iter().any(|param| match param.kind { ast::GenericParamKind::Type { .. } => true, _ => false, diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index c04b65245e1..2e5ae235893 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -186,7 +186,6 @@ use rustc_target::spec::abi::Abi; use syntax::ast::{self, BinOpKind, EnumDef, Expr, Generics, Ident, PatKind}; use syntax::ast::{VariantData, GenericParamKind, GenericArg}; use syntax::attr; -use syntax::expand::SpecialDerives; use syntax::source_map::respan; use syntax::util::map_in_place::MapInPlace; use syntax::ptr::P; @@ -427,10 +426,8 @@ impl<'a> TraitDef<'a> { } }; let container_id = cx.current_expansion.id.expn_data().parent; - let is_always_copy = - cx.resolver.has_derives(container_id, SpecialDerives::COPY) && - has_no_type_params; - let use_temporaries = is_packed && is_always_copy; + let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id); + let use_temporaries = is_packed && always_copy; let newitem = match item.kind { ast::ItemKind::Struct(ref struct_def, ref generics) => { -- cgit 1.4.1-3-g733a5 From bbd7f5c85b4c9f547490786a1453eaaf20dd0696 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Mon, 4 Nov 2019 10:57:41 -0800 Subject: Do not ICE whith a precision flag in formatting str and no format arguments --- src/libsyntax_ext/format.rs | 10 ++++++---- src/test/ui/if/ifmt-bad-arg.rs | 5 +++++ src/test/ui/if/ifmt-bad-arg.stderr | 13 ++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 37310f46f7e..3c7f80aa399 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -374,10 +374,12 @@ impl<'a, 'b> Context<'a, 'b> { format!("are {} arguments", count) }, )); - e.span_label( - self.args[pos].span, - "this parameter corresponds to the precision flag", - ); + if let Some(arg) = self.args.get(pos) { + e.span_label( + arg.span, + "this parameter corresponds to the precision flag", + ); + } zero_based_note = true; } _ => {} diff --git a/src/test/ui/if/ifmt-bad-arg.rs b/src/test/ui/if/ifmt-bad-arg.rs index ba897f171af..a0b0a8fb985 100644 --- a/src/test/ui/if/ifmt-bad-arg.rs +++ b/src/test/ui/if/ifmt-bad-arg.rs @@ -86,4 +86,9 @@ tenth number: {}", println!("{:foo}", 1); //~ ERROR unknown format trait `foo` println!("{5} {:4$} {6:7$}", 1); //~^ ERROR invalid reference to positional arguments 4, 5, 6 and 7 (there is 1 argument) + + // We used to ICE here because we tried to unconditionally access the first argument, which + // doesn't exist. + println!("{:.*}"); + //~^ ERROR 2 positional arguments in format string, but no arguments were given } diff --git a/src/test/ui/if/ifmt-bad-arg.stderr b/src/test/ui/if/ifmt-bad-arg.stderr index c58cbc31233..11dcc3a6d23 100644 --- a/src/test/ui/if/ifmt-bad-arg.stderr +++ b/src/test/ui/if/ifmt-bad-arg.stderr @@ -285,6 +285,17 @@ LL | println!("{5} {:4$} {6:7$}", 1); = note: positional arguments are zero-based = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html +error: 2 positional arguments in format string, but no arguments were given + --> $DIR/ifmt-bad-arg.rs:92:15 + | +LL | println!("{:.*}"); + | ^^--^ + | | + | this precision flag adds an extra required argument at position 0, which is why there are 2 arguments expected + | + = note: positional arguments are zero-based + = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html + error[E0308]: mismatched types --> $DIR/ifmt-bad-arg.rs:78:32 | @@ -303,6 +314,6 @@ LL | println!("{} {:07$.*} {}", 1, 3.2, 4); = note: expected type `&usize` found type `&{float}` -error: aborting due to 35 previous errors +error: aborting due to 36 previous errors For more information about this error, try `rustc --explain E0308`. -- cgit 1.4.1-3-g733a5