From 7506fe52696c5b3b628f173d74dc4a13b5ccc1f3 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 31 Dec 2014 17:25:18 +1300 Subject: [PATCH] Accept `derive` instead of `deriving` [breaking-change] `deriving is still accepted, but gives a deprecation warning --- src/librustc/lint/builtin.rs | 4 +-- src/libsyntax/ext/base.rs | 2 ++ src/libsyntax/ext/deriving/generic/mod.rs | 24 +++++++++--------- src/libsyntax/ext/deriving/mod.rs | 30 +++++++++++++++-------- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 692ecf75091..82aa40cb0ad 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -551,7 +551,7 @@ impl LintPass for BoxPointers { declare_lint! { RAW_POINTER_DERIVING, Warn, - "uses of #[deriving] with raw pointers are rarely correct" + "uses of #[derive] with raw pointers are rarely correct" } struct RawPtrDerivingVisitor<'a, 'tcx: 'a> { @@ -560,7 +560,7 @@ struct RawPtrDerivingVisitor<'a, 'tcx: 'a> { impl<'a, 'tcx, 'v> Visitor<'v> for RawPtrDerivingVisitor<'a, 'tcx> { fn visit_ty(&mut self, ty: &ast::Ty) { - static MSG: &'static str = "use of `#[deriving]` with a raw pointer"; + static MSG: &'static str = "use of `#[derive]` with a raw pointer"; if let ast::TyPtr(..) = ty.node { self.cx.span_lint(RAW_POINTER_DERIVING, ty.span, MSG); } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 5134897893f..efb4867a016 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -390,6 +390,8 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv { syntax_expanders.insert(intern("log_syntax"), builtin_normal_expander( ext::log_syntax::expand_syntax_ext)); + syntax_expanders.insert(intern("derive"), + Decorator(box ext::deriving::expand_meta_derive)); syntax_expanders.insert(intern("deriving"), Decorator(box ext::deriving::expand_meta_deriving)); diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 9149c20ce1b..acfb020fab6 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -9,7 +9,7 @@ // except according to those terms. //! Some code that abstracts away much of the boilerplate of writing -//! `deriving` instances for traits. Among other things it manages getting +//! `derive` instances for traits. Among other things it manages getting //! access to the fields of the 4 different sorts of structs and enum //! variants, as well as creating the method and impl ast instances. //! @@ -26,7 +26,7 @@ //! moment. (`TraitDef.additional_bounds`) //! //! Unsupported: FIXME #6257: calling methods on reference fields, -//! e.g. deriving Eq/Ord/Clone don't work on `struct A(&int)`, +//! e.g. derive Eq/Ord/Clone don't work on `struct A(&int)`, //! because of how the auto-dereferencing happens. //! //! The most important thing for implementers is the `Substructure` and @@ -209,7 +209,7 @@ use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self, Ty}; pub mod ty; pub struct TraitDef<'a> { - /// The span for the current #[deriving(Foo)] header. + /// The span for the current #[derive(Foo)] header. pub span: Span, pub attributes: Vec, @@ -354,7 +354,7 @@ impl<'a> TraitDef<'a> { generics) } _ => { - cx.span_err(mitem.span, "`deriving` may only be applied to structs and enums"); + cx.span_err(mitem.span, "`derive` may only be applied to structs and enums"); return; } }; @@ -718,7 +718,7 @@ impl<'a> MethodDef<'a> { } /// ``` - /// #[deriving(PartialEq)] + /// #[derive(PartialEq)] /// struct A { x: int, y: int } /// /// // equivalent to: @@ -782,7 +782,7 @@ impl<'a> MethodDef<'a> { } else { cx.span_bug(trait_.span, "no self arguments to non-static method in generic \ - `deriving`") + `derive`") }; // body of the inner most destructuring match @@ -822,7 +822,7 @@ impl<'a> MethodDef<'a> { } /// ``` - /// #[deriving(PartialEq)] + /// #[derive(PartialEq)] /// enum A { /// A1, /// A2(int) @@ -1185,7 +1185,7 @@ impl<'a> TraitDef<'a> { cx: &mut ExtCtxt, mut to_set: Span) -> Span { let trait_name = match self.path.path.last() { - None => cx.span_bug(self.span, "trait with empty path in generic `deriving`"), + None => cx.span_bug(self.span, "trait with empty path in generic `derive`"), Some(name) => *name }; to_set.expn_id = cx.codemap().record_expansion(codemap::ExpnInfo { @@ -1215,7 +1215,7 @@ impl<'a> TraitDef<'a> { match (just_spans.is_empty(), named_idents.is_empty()) { (false, false) => cx.span_bug(self.span, "a struct with named and unnamed \ - fields in generic `deriving`"), + fields in generic `derive`"), // named fields (_, false) => Named(named_idents), // tuple structs (includes empty structs) @@ -1263,7 +1263,7 @@ impl<'a> TraitDef<'a> { None } _ => { - cx.span_bug(sp, "a struct with named and unnamed fields in `deriving`"); + cx.span_bug(sp, "a struct with named and unnamed fields in `derive`"); } }; let ident = cx.ident_of(format!("{}_{}", prefix, i)[]); @@ -1371,7 +1371,7 @@ pub fn cs_fold(use_foldl: bool, enum_nonmatch_f(cx, trait_span, (all_args[], tuple), substructure.nonself_args), StaticEnum(..) | StaticStruct(..) => { - cx.span_bug(trait_span, "static function in `deriving`") + cx.span_bug(trait_span, "static function in `derive`") } } } @@ -1411,7 +1411,7 @@ pub fn cs_same_method(f: F, enum_nonmatch_f(cx, trait_span, (all_self_args[], tuple), substructure.nonself_args), StaticEnum(..) | StaticStruct(..) => { - cx.span_bug(trait_span, "static function in `deriving`") + cx.span_bug(trait_span, "static function in `derive`") } } } diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index 75f763b5c38..57d66f0e355 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! The compiler code necessary to implement the `#[deriving]` extensions. +//! The compiler code necessary to implement the `#[derive]` extensions. //! //! FIXME (#2810): hygiene. Search for "__" strings (in other files too). We also assume "extra" is //! the standard library, and "std" is the core library. @@ -45,16 +45,26 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt, _span: Span, mitem: &MetaItem, item: &Item, - mut push: Box)>) { + push: Box)>) { + cx.span_warn(mitem.span, "`deriving` is deprecated; use `derive`"); + + expand_meta_derive(cx, _span, mitem, item, push) +} + +pub fn expand_meta_derive(cx: &mut ExtCtxt, + _span: Span, + mitem: &MetaItem, + item: &Item, + mut push: Box)>) { match mitem.node { MetaNameValue(_, ref l) => { - cx.span_err(l.span, "unexpected value in `deriving`"); + cx.span_err(l.span, "unexpected value in `derive`"); } MetaWord(_) => { - cx.span_warn(mitem.span, "empty trait list in `deriving`"); + cx.span_warn(mitem.span, "empty trait list in `derive`"); } MetaList(_, ref titems) if titems.len() == 0 => { - cx.span_warn(mitem.span, "empty trait list in `deriving`"); + cx.span_warn(mitem.span, "empty trait list in `derive`"); } MetaList(_, ref titems) => { for titem in titems.iter().rev() { @@ -78,15 +88,15 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt, } "Encodable" => { cx.span_warn(titem.span, - "deriving(Encodable) is deprecated \ - in favor of deriving(RustcEncodable)"); + "derive(Encodable) is deprecated \ + in favor of derive(RustcEncodable)"); expand!(encodable::expand_deriving_encodable) } "Decodable" => { cx.span_warn(titem.span, - "deriving(Decodable) is deprecated \ - in favor of deriving(RustcDecodable)"); + "derive(Decodable) is deprecated \ + in favor of derive(RustcDecodable)"); expand!(decodable::expand_deriving_decodable) } @@ -111,7 +121,7 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt, ref tname => { cx.span_err(titem.span, - format!("unknown `deriving` \ + format!("unknown `derive` \ trait: `{}`", *tname)[]); }