Skip to content

Commit 94c06a1

Browse files
committed
Auto merge of #22026 - kmcallister:plugin, r=sfackler
```rust #[plugin] #[no_link] extern crate bleh; ``` becomes a crate attribute ```rust #![plugin(bleh)] ``` The feature gate is still required. It's almost never correct to link a plugin into the resulting library / executable, because it will bring all of libsyntax and librustc with it. However if you really want this behavior, you can get it with a separate `extern crate` item in addition to the `plugin` attribute. Fixes #21043. Fixes #20769. [breaking-change]
2 parents de8bc44 + 1aedc45 commit 94c06a1

37 files changed

+156
-165
lines changed

src/doc/reference.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,11 @@ type int8_t = i8;
20142014
- `no_start` - disable linking to the `native` crate, which specifies the
20152015
"start" language item.
20162016
- `no_std` - disable linking to the `std` crate.
2017+
- `plugin` — load a list of named crates as compiler plugins, e.g.
2018+
`#![plugin(foo, bar)]`. Optional arguments for each plugin,
2019+
i.e. `#![plugin(foo(... args ...))]`, are provided to the plugin's
2020+
registrar function. The `plugin` feature gate is required to use
2021+
this attribute.
20172022

20182023
### Module-only attributes
20192024

@@ -2082,7 +2087,7 @@ On `struct`s:
20822087
remove any padding between fields (note that this is very fragile and may
20832088
break platforms which require aligned access).
20842089

2085-
### Macro- and plugin-related attributes
2090+
### Macro-related attributes
20862091

20872092
- `macro_use` on a `mod` — macros defined in this module will be visible in the
20882093
module's parent, after this module has been included.
@@ -2097,13 +2102,8 @@ On `struct`s:
20972102

20982103
- `macro_export` - export a macro for cross-crate usage.
20992104

2100-
- `plugin` on an `extern crate` — load this crate as a [compiler
2101-
plugin][plugin]. The `plugin` feature gate is required. Any arguments to
2102-
the attribute, e.g. `#[plugin=...]` or `#[plugin(...)]`, are provided to the
2103-
plugin.
2104-
2105-
- `no_link` on an `extern crate` — even if we load this crate for macros or
2106-
compiler plugins, don't link it into the output.
2105+
- `no_link` on an `extern crate` — even if we load this crate for macros, don't
2106+
link it into the output.
21072107

21082108
See the [macros section of the
21092109
book](book/macros.html#scoping-and-macro-import/export) for more information on

src/doc/trpl/plugins.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ information.
3030
extend the compiler's behavior with new syntax extensions, lint checks, etc.
3131

3232
A plugin is a dynamic library crate with a designated *registrar* function that
33-
registers extensions with `rustc`. Other crates can use these extensions by
34-
loading the plugin crate with `#[plugin] extern crate`. See the
33+
registers extensions with `rustc`. Other crates can load these extensions using
34+
the crate attribute `#![plugin(...)]`. See the
3535
[`rustc::plugin`](../rustc/plugin/index.html) documentation for more about the
3636
mechanics of defining and loading a plugin.
3737

38-
Arguments passed as `#[plugin=...]` or `#[plugin(...)]` are not interpreted by
39-
rustc itself. They are provided to the plugin through the `Registry`'s [`args`
40-
method](../rustc/plugin/registry/struct.Registry.html#method.args).
38+
If present, arguments passed as `#![plugin(foo(... args ...))]` are not
39+
interpreted by rustc itself. They are provided to the plugin through the
40+
`Registry`'s [`args` method](../rustc/plugin/registry/struct.Registry.html#method.args).
4141

4242
# Syntax extensions
4343

@@ -110,8 +110,7 @@ Then we can use `rn!()` like any other macro:
110110

111111
```ignore
112112
#![feature(plugin)]
113-
114-
#[plugin] extern crate roman_numerals;
113+
#![plugin(roman_numerals)]
115114
116115
fn main() {
117116
assert_eq!(rn!(MMXV), 2015);
@@ -219,7 +218,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
219218
Then code like
220219

221220
```ignore
222-
#[plugin] extern crate lint_plugin_test;
221+
#![plugin(lint_plugin_test)]
223222
224223
fn lintme() { }
225224
```

src/librustc/metadata/creader.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use syntax::ast;
2626
use syntax::abi;
2727
use syntax::attr;
2828
use syntax::attr::AttrMetaMethods;
29-
use syntax::codemap::{COMMAND_LINE_SP, Span, mk_sp};
29+
use syntax::codemap::{Span, mk_sp};
3030
use syntax::parse;
3131
use syntax::parse::token::InternedString;
3232
use syntax::parse::token;
@@ -457,13 +457,13 @@ impl<'a> CrateReader<'a> {
457457
CrateOrString::Krate(c) => {
458458
(self.extract_crate_info(c).unwrap(), c.span)
459459
}
460-
CrateOrString::Str(s) => {
460+
CrateOrString::Str(sp, s) => {
461461
(CrateInfo {
462462
name: s.to_string(),
463463
ident: s.to_string(),
464464
id: ast::DUMMY_NODE_ID,
465465
should_link: true,
466-
}, COMMAND_LINE_SP)
466+
}, sp)
467467
}
468468
};
469469
let target_triple = &self.sess.opts.target_triple[];
@@ -531,7 +531,7 @@ impl<'a> CrateReader<'a> {
531531
#[derive(Copy)]
532532
pub enum CrateOrString<'a> {
533533
Krate(&'a ast::Item),
534-
Str(&'a str)
534+
Str(Span, &'a str)
535535
}
536536

537537
impl<'a> PluginMetadata<'a> {

src/librustc/plugin/load.rs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use std::mem;
1818
use std::env;
1919
use std::dynamic_lib::DynamicLibrary;
2020
use std::collections::HashSet;
21+
use std::borrow::ToOwned;
2122
use syntax::ast;
2223
use syntax::attr;
23-
use syntax::codemap::Span;
24+
use syntax::codemap::{Span, COMMAND_LINE_SP};
2425
use syntax::parse::token;
2526
use syntax::ptr::P;
2627
use syntax::visit;
@@ -33,7 +34,7 @@ pub type PluginRegistrarFun =
3334

3435
pub struct PluginRegistrar {
3536
pub fun: PluginRegistrarFun,
36-
pub args: P<ast::MetaItem>,
37+
pub args: Vec<P<ast::MetaItem>>,
3738
}
3839

3940
/// Information about loaded plugins.
@@ -81,10 +82,34 @@ pub fn load_plugins(sess: &Session, krate: &ast::Crate,
8182

8283
visit::walk_crate(&mut loader, krate);
8384

85+
for attr in &krate.attrs {
86+
if !attr.check_name("plugin") {
87+
continue;
88+
}
89+
90+
let plugins = match attr.meta_item_list() {
91+
Some(xs) => xs,
92+
None => {
93+
sess.span_err(attr.span, "malformed plugin attribute");
94+
continue;
95+
}
96+
};
97+
98+
for plugin in plugins {
99+
if plugin.value_str().is_some() {
100+
sess.span_err(attr.span, "malformed plugin attribute");
101+
continue;
102+
}
103+
104+
let args = plugin.meta_item_list().map(ToOwned::to_owned).unwrap_or_default();
105+
loader.load_plugin(CrateOrString::Str(plugin.span, &*plugin.name()),
106+
args);
107+
}
108+
}
109+
84110
if let Some(plugins) = addl_plugins {
85111
for plugin in plugins {
86-
loader.load_plugin(CrateOrString::Str(&plugin),
87-
None, None, None)
112+
loader.load_plugin(CrateOrString::Str(COMMAND_LINE_SP, &plugin), vec![]);
88113
}
89114
}
90115

@@ -104,21 +129,19 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
104129
}
105130

106131
// Parse the attributes relating to macro / plugin loading.
107-
let mut plugin_attr = None;
108132
let mut macro_selection = Some(HashSet::new()); // None => load all
109133
let mut reexport = HashSet::new();
110134
for attr in &item.attrs {
111135
let mut used = true;
112136
match &attr.name()[] {
113137
"phase" => {
114-
self.sess.span_err(attr.span, "#[phase] is deprecated; use \
115-
#[macro_use], #[plugin], and/or #[no_link]");
138+
self.sess.span_err(attr.span, "#[phase] is deprecated");
116139
}
117140
"plugin" => {
118-
if plugin_attr.is_some() {
119-
self.sess.span_err(attr.span, "#[plugin] specified multiple times");
120-
}
121-
plugin_attr = Some(attr.node.value.clone());
141+
self.sess.span_err(attr.span, "#[plugin] on `extern crate` is deprecated");
142+
self.sess.span_help(attr.span, &format!("use a crate attribute instead, \
143+
i.e. #![plugin({})]",
144+
item.ident.as_str())[]);
122145
}
123146
"macro_use" => {
124147
let names = attr.meta_item_list();
@@ -160,10 +183,7 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
160183
}
161184
}
162185

163-
self.load_plugin(CrateOrString::Krate(item),
164-
plugin_attr,
165-
macro_selection,
166-
Some(reexport))
186+
self.load_macros(item, macro_selection, Some(reexport))
167187
}
168188

169189
fn visit_mac(&mut self, _: &ast::Mac) {
@@ -173,38 +193,25 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
173193
}
174194

175195
impl<'a> PluginLoader<'a> {
176-
pub fn load_plugin<'b>(&mut self,
177-
c: CrateOrString<'b>,
178-
plugin_attr: Option<P<ast::MetaItem>>,
196+
pub fn load_macros<'b>(&mut self,
197+
vi: &ast::Item,
179198
macro_selection: Option<HashSet<token::InternedString>>,
180199
reexport: Option<HashSet<token::InternedString>>) {
181-
let mut macros = vec![];
182-
let mut registrar = None;
183-
184-
let load_macros = match (macro_selection.as_ref(), reexport.as_ref()) {
185-
(Some(sel), Some(re)) => sel.len() != 0 || re.len() != 0,
186-
_ => true,
187-
};
188-
let load_registrar = plugin_attr.is_some();
189-
190-
if let CrateOrString::Krate(vi) = c {
191-
if load_macros && !self.span_whitelist.contains(&vi.span) {
192-
self.sess.span_err(vi.span, "an `extern crate` loading macros must be at \
193-
the crate root");
200+
if let (Some(sel), Some(re)) = (macro_selection.as_ref(), reexport.as_ref()) {
201+
if sel.is_empty() && re.is_empty() {
202+
return;
194203
}
195-
}
204+
}
196205

197-
if load_macros || load_registrar {
198-
let pmd = self.reader.read_plugin_metadata(c);
199-
if load_macros {
200-
macros = pmd.exported_macros();
201-
}
202-
if load_registrar {
203-
registrar = pmd.plugin_registrar();
204-
}
206+
if !self.span_whitelist.contains(&vi.span) {
207+
self.sess.span_err(vi.span, "an `extern crate` loading macros must be at \
208+
the crate root");
209+
return;
205210
}
206211

207-
for mut def in macros {
212+
let pmd = self.reader.read_plugin_metadata(CrateOrString::Krate(vi));
213+
214+
for mut def in pmd.exported_macros() {
208215
let name = token::get_ident(def.ident);
209216
def.use_locally = match macro_selection.as_ref() {
210217
None => true,
@@ -217,12 +224,21 @@ impl<'a> PluginLoader<'a> {
217224
};
218225
self.plugins.macros.push(def);
219226
}
227+
}
228+
229+
pub fn load_plugin<'b>(&mut self,
230+
c: CrateOrString<'b>,
231+
args: Vec<P<ast::MetaItem>>) {
232+
let registrar = {
233+
let pmd = self.reader.read_plugin_metadata(c);
234+
pmd.plugin_registrar()
235+
};
220236

221237
if let Some((lib, symbol)) = registrar {
222238
let fun = self.dylink_registrar(c, lib, symbol);
223239
self.plugins.registrars.push(PluginRegistrar {
224240
fun: fun,
225-
args: plugin_attr.unwrap(),
241+
args: args,
226242
});
227243
}
228244
}

src/librustc/plugin/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,9 @@
4444
//!
4545
//! ```rust
4646
//! #![feature(plugin)]
47-
//!
48-
//! #[plugin]
49-
//! extern crate myplugin;
47+
//! #![plugin(myplugin)]
5048
//! ```
5149
//!
52-
//! If you don't need the plugin crate available at runtime, use
53-
//! `#[no_link]` as well.
54-
//!
5550
//! See [the compiler plugin guide](../../guide-plugin.html)
5651
//! for more examples.
5752

src/librustc/plugin/registry.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct Registry<'a> {
3737
pub sess: &'a Session,
3838

3939
#[doc(hidden)]
40-
pub args_hidden: Option<P<ast::MetaItem>>,
40+
pub args_hidden: Option<Vec<P<ast::MetaItem>>>,
4141

4242
#[doc(hidden)]
4343
pub krate_span: Span,
@@ -65,11 +65,14 @@ impl<'a> Registry<'a> {
6565
}
6666
}
6767

68-
/// Get the `#[plugin]` attribute used to load this plugin.
68+
/// Get the plugin's arguments, if any.
6969
///
70-
/// This gives access to arguments passed via `#[plugin=...]` or
71-
/// `#[plugin(...)]`.
72-
pub fn args<'b>(&'b self) -> &'b P<ast::MetaItem> {
70+
/// These are specified inside the `plugin` crate attribute as
71+
///
72+
/// ```no_run
73+
/// #![plugin(my_plugin_name(... args ...))]
74+
/// ```
75+
pub fn args<'b>(&'b self) -> &'b Vec<P<ast::MetaItem>> {
7376
self.args_hidden.as_ref().expect("args not set")
7477
}
7578

src/librustc_driver/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ use rustc::session::config::{Input, PrintRequest};
7070
use rustc::lint::Lint;
7171
use rustc::lint;
7272
use rustc::metadata;
73-
use rustc::metadata::creader::CrateOrString::Str;
7473
use rustc::util::common::time;
7574

7675
use std::cmp::Ordering::Equal;

src/libsyntax/feature_gate.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,7 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
284284
}
285285
match i.node {
286286
ast::ItemExternCrate(_) => {
287-
if attr::contains_name(&i.attrs[], "plugin") {
288-
self.gate_feature("plugin", i.span,
289-
"compiler plugins are experimental \
290-
and possibly buggy");
291-
} else if attr::contains_name(&i.attrs[], "macro_reexport") {
287+
if attr::contains_name(&i.attrs[], "macro_reexport") {
292288
self.gate_feature("macro_reexport", i.span,
293289
"macros reexports are experimental \
294290
and possibly buggy");
@@ -462,6 +458,10 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
462458
if attr.check_name("staged_api") {
463459
self.gate_feature("staged_api", attr.span,
464460
"staged_api is for use by rustc only");
461+
} else if attr.check_name("plugin") {
462+
self.gate_feature("plugin", attr.span,
463+
"compiler plugins are experimental \
464+
and possibly buggy");
465465
}
466466

467467
if attr::contains_name(slice::ref_slice(attr), "lang") {

src/test/auxiliary/plugin_args.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ use syntax::ptr::P;
2727
use rustc::plugin::Registry;
2828

2929
struct Expander {
30-
args: P<ast::MetaItem>,
30+
args: Vec<P<ast::MetaItem>>,
3131
}
3232

3333
impl TTMacroExpander for Expander {
3434
fn expand<'cx>(&self,
3535
ecx: &'cx mut ExtCtxt,
3636
sp: Span,
3737
_: &[ast::TokenTree]) -> Box<MacResult+'cx> {
38-
39-
let attr = ecx.attribute(sp, self.args.clone());
40-
let src = pprust::attribute_to_string(&attr);
41-
let interned = token::intern_and_get_ident(&src);
38+
let args = self.args.iter().map(|i| pprust::meta_item_to_string(&*i))
39+
.collect::<Vec<_>>().connect(", ");
40+
let interned = token::intern_and_get_ident(&args[]);
4241
MacExpr::new(ecx.expr_str(sp, interned))
4342
}
4443
}

src/test/auxiliary/syntax-extension-with-dll-deps-2.rs renamed to src/test/auxiliary/syntax_extension_with_dll_deps_2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#![crate_type = "dylib"]
1414
#![feature(plugin_registrar, quote)]
1515

16-
extern crate "syntax-extension-with-dll-deps-1" as other;
16+
extern crate "syntax_extension_with_dll_deps_1" as other;
1717
extern crate syntax;
1818
extern crate rustc;
1919

src/test/compile-fail-fulldeps/gated-plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// aux-build:macro_crate_test.rs
1212
// ignore-stage1
1313

14-
#[plugin] #[no_link] extern crate macro_crate_test;
14+
#![plugin(macro_crate_test)]
1515
//~^ ERROR compiler plugins are experimental and possibly buggy
1616

1717
fn main() {}

0 commit comments

Comments
 (0)