From 22f788c64417523e838bf367c83f8a426102809a Mon Sep 17 00:00:00 2001 From: Josh Driver Date: Sat, 31 Dec 2016 16:02:06 +1030 Subject: [PATCH 1/2] Stop macro calls in structs for proc_macro_derive from panicing --- src/librustc_resolve/macros.rs | 5 ++++ src/libsyntax_ext/deriving/custom.rs | 5 +++- .../proc-macro/auxiliary/derive-nothing.rs | 24 +++++++++++++++ .../proc-macro/struct-field-macro.rs | 29 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-nothing.rs create mode 100644 src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 44cc580ad12..c7e0b2733f0 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -29,6 +29,7 @@ use syntax::ext::hygiene::Mark; use syntax::ext::tt::macro_rules; use syntax::feature_gate::{emit_feature_err, GateIssue}; use syntax::fold::Folder; +use syntax::fold; use syntax::ptr::P; use syntax::symbol::keywords; use syntax::util::lev_distance::find_best_match_for_name; @@ -117,6 +118,10 @@ impl<'a> base::Resolver for Resolver<'a> { } path } + + fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { + fold::noop_fold_mac(mac, self) + } } EliminateCrateVar(self).fold_item(item).expect_one("") diff --git a/src/libsyntax_ext/deriving/custom.rs b/src/libsyntax_ext/deriving/custom.rs index 6f02a348f91..811aea4a9eb 100644 --- a/src/libsyntax_ext/deriving/custom.rs +++ b/src/libsyntax_ext/deriving/custom.rs @@ -12,7 +12,7 @@ use std::panic; use errors::FatalError; use proc_macro::{TokenStream, __internal}; -use syntax::ast::{self, ItemKind, Attribute}; +use syntax::ast::{self, ItemKind, Attribute, Mac}; use syntax::attr::{mark_used, mark_known}; use syntax::codemap::Span; use syntax::ext::base::*; @@ -28,6 +28,9 @@ impl<'a> Visitor<'a> for MarkAttrs<'a> { mark_known(attr); } } + + fn visit_mac(&mut self, _mac: &Mac) { + } } pub struct CustomDerive { diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-nothing.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-nothing.rs new file mode 100644 index 00000000000..a9257cb4c8b --- /dev/null +++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-nothing.rs @@ -0,0 +1,24 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// no-prefer-dynamic + +#![crate_type = "proc-macro"] +#![feature(proc_macro)] +#![feature(proc_macro_lib)] + +extern crate proc_macro; + +use proc_macro::TokenStream; + +#[proc_macro_derive(Nothing)] +pub fn nothing(input: TokenStream) -> TokenStream { + "".parse().unwrap() +} diff --git a/src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs b/src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs new file mode 100644 index 00000000000..68da011e5a5 --- /dev/null +++ b/src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs @@ -0,0 +1,29 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:derive-nothing.rs +// ignore-stage1 + +#![feature(proc_macro)] + +#[macro_use] +extern crate derive_nothing; + +macro_rules! int { + () => { i32 } +} + +#[derive(Nothing)] +struct S { + x: int!(), +} + +fn main() { +} From e9b5839918a1e36ef923a88f71c67da2d9f7b9e1 Mon Sep 17 00:00:00 2001 From: Josh Driver Date: Sat, 31 Dec 2016 17:55:59 +1030 Subject: [PATCH 2/2] Style fixes --- src/librustc_resolve/macros.rs | 3 +-- src/libsyntax_ext/deriving/custom.rs | 3 +-- src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index c7e0b2733f0..47033f74e0e 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -28,8 +28,7 @@ use syntax::ext::expand::{Expansion, mark_tts}; use syntax::ext::hygiene::Mark; use syntax::ext::tt::macro_rules; use syntax::feature_gate::{emit_feature_err, GateIssue}; -use syntax::fold::Folder; -use syntax::fold; +use syntax::fold::{self, Folder}; use syntax::ptr::P; use syntax::symbol::keywords; use syntax::util::lev_distance::find_best_match_for_name; diff --git a/src/libsyntax_ext/deriving/custom.rs b/src/libsyntax_ext/deriving/custom.rs index 811aea4a9eb..64ec460a524 100644 --- a/src/libsyntax_ext/deriving/custom.rs +++ b/src/libsyntax_ext/deriving/custom.rs @@ -29,8 +29,7 @@ impl<'a> Visitor<'a> for MarkAttrs<'a> { } } - fn visit_mac(&mut self, _mac: &Mac) { - } + fn visit_mac(&mut self, _mac: &Mac) {} } pub struct CustomDerive { diff --git a/src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs b/src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs index 68da011e5a5..818308b05a4 100644 --- a/src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs +++ b/src/test/run-pass-fulldeps/proc-macro/struct-field-macro.rs @@ -25,5 +25,4 @@ struct S { x: int!(), } -fn main() { -} +fn main() {}