Skip to content

Commit 0c5b21f

Browse files
committed
Auto merge of #22241 - kmcallister:macro-plugin-cleanup, r=sfackler
2 parents c5db290 + b7683fc commit 0c5b21f

File tree

14 files changed

+396
-243
lines changed

14 files changed

+396
-243
lines changed

src/doc/trpl/plugins.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ If present, arguments passed as `#![plugin(foo(... args ...))]` are not
3939
interpreted by rustc itself. They are provided to the plugin through the
4040
`Registry`'s [`args` method](../rustc/plugin/registry/struct.Registry.html#method.args).
4141

42+
In the vast majority of cases, a plugin should *only* be used through
43+
`#![plugin]` and not through an `extern crate` item. Linking a plugin would
44+
pull in all of libsyntax and librustc as dependencies of your crate. This is
45+
generally unwanted unless you are building another plugin. The
46+
`plugin_as_library` lint checks these guidelines.
47+
48+
The usual practice is to put compiler plugins in their own crate, separate from
49+
any `macro_rules!` macros or ordinary Rust code meant to be used by consumers
50+
of a library.
51+
4252
# Syntax extensions
4353

4454
Plugins can extend Rust's syntax in various ways. One kind of syntax extension

src/librustc/lint/builtin.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! a `pub fn new()`.
2727
use self::MethodContext::*;
2828

29-
use metadata::csearch;
29+
use metadata::{csearch, decoder};
3030
use middle::def::*;
3131
use middle::subst::Substs;
3232
use middle::ty::{self, Ty};
@@ -1964,6 +1964,48 @@ impl LintPass for UnconditionalRecursion {
19641964
}
19651965
}
19661966

1967+
declare_lint! {
1968+
PLUGIN_AS_LIBRARY,
1969+
Warn,
1970+
"compiler plugin used as ordinary library in non-plugin crate"
1971+
}
1972+
1973+
#[derive(Copy)]
1974+
pub struct PluginAsLibrary;
1975+
1976+
impl LintPass for PluginAsLibrary {
1977+
fn get_lints(&self) -> LintArray {
1978+
lint_array![PLUGIN_AS_LIBRARY]
1979+
}
1980+
1981+
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
1982+
if cx.sess().plugin_registrar_fn.get().is_some() {
1983+
// We're compiling a plugin; it's fine to link other plugins.
1984+
return;
1985+
}
1986+
1987+
match it.node {
1988+
ast::ItemExternCrate(..) => (),
1989+
_ => return,
1990+
};
1991+
1992+
let md = match cx.sess().cstore.find_extern_mod_stmt_cnum(it.id) {
1993+
Some(cnum) => cx.sess().cstore.get_crate_data(cnum),
1994+
None => {
1995+
// Probably means we aren't linking the crate for some reason.
1996+
//
1997+
// Not sure if / when this could happen.
1998+
return;
1999+
}
2000+
};
2001+
2002+
if decoder::get_plugin_registrar_fn(md.data()).is_some() {
2003+
cx.span_lint(PLUGIN_AS_LIBRARY, it.span,
2004+
"compiler plugin used as an ordinary library");
2005+
}
2006+
}
2007+
}
2008+
19672009
declare_lint! {
19682010
pub UNUSED_IMPORTS,
19692011
Warn,

src/librustc/lint/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl LintStore {
214214
Stability,
215215
UnconditionalRecursion,
216216
InvalidNoMangleItems,
217+
PluginAsLibrary,
217218
);
218219

219220
add_builtin_with_new!(sess,

src/librustc/metadata/creader.rs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,10 @@ fn register_native_lib(sess: &Session,
121121
sess.cstore.add_used_library(name, kind);
122122
}
123123

124-
pub struct PluginMetadata<'a> {
125-
sess: &'a Session,
124+
// Extra info about a crate loaded for plugins or exported macros.
125+
struct ExtensionCrate {
126126
metadata: PMDSource,
127127
dylib: Option<Path>,
128-
info: CrateInfo,
129-
vi_span: Span,
130128
target_only: bool,
131129
}
132130

@@ -451,21 +449,7 @@ impl<'a> CrateReader<'a> {
451449
}).collect()
452450
}
453451

454-
pub fn read_plugin_metadata<'b>(&'b mut self,
455-
krate: CrateOrString<'b>) -> PluginMetadata<'b> {
456-
let (info, span) = match krate {
457-
CrateOrString::Krate(c) => {
458-
(self.extract_crate_info(c).unwrap(), c.span)
459-
}
460-
CrateOrString::Str(sp, s) => {
461-
(CrateInfo {
462-
name: s.to_string(),
463-
ident: s.to_string(),
464-
id: ast::DUMMY_NODE_ID,
465-
should_link: false,
466-
}, sp)
467-
}
468-
};
452+
fn read_extension_crate(&mut self, span: Span, info: &CrateInfo) -> ExtensionCrate {
469453
let target_triple = &self.sess.opts.target_triple[];
470454
let is_cross = target_triple != config::host_triple();
471455
let mut should_link = info.should_link && !is_cross;
@@ -517,30 +501,21 @@ impl<'a> CrateReader<'a> {
517501
PMDSource::Owned(library.metadata)
518502
};
519503

520-
PluginMetadata {
521-
sess: self.sess,
504+
ExtensionCrate {
522505
metadata: metadata,
523506
dylib: dylib.map(|p| p.0),
524-
info: info,
525-
vi_span: span,
526507
target_only: target_only,
527508
}
528509
}
529-
}
530510

531-
#[derive(Copy)]
532-
pub enum CrateOrString<'a> {
533-
Krate(&'a ast::Item),
534-
Str(Span, &'a str)
535-
}
511+
/// Read exported macros.
512+
pub fn read_exported_macros(&mut self, krate: &ast::Item) -> Vec<ast::MacroDef> {
513+
let ci = self.extract_crate_info(krate).unwrap();
514+
let ekrate = self.read_extension_crate(krate.span, &ci);
536515

537-
impl<'a> PluginMetadata<'a> {
538-
/// Read exported macros
539-
pub fn exported_macros(&self) -> Vec<ast::MacroDef> {
540-
let imported_from = Some(token::intern(&self.info.ident[]).ident());
541-
let source_name = format!("<{} macros>", &self.info.ident[]);
516+
let source_name = format!("<{} macros>", krate.ident);
542517
let mut macros = vec![];
543-
decoder::each_exported_macro(self.metadata.as_slice(),
518+
decoder::each_exported_macro(ekrate.metadata.as_slice(),
544519
&*self.sess.cstore.intr,
545520
|name, attrs, body| {
546521
// NB: Don't use parse::parse_tts_from_source_str because it parses with
@@ -558,7 +533,7 @@ impl<'a> PluginMetadata<'a> {
558533
attrs: attrs,
559534
id: ast::DUMMY_NODE_ID,
560535
span: span,
561-
imported_from: imported_from,
536+
imported_from: Some(krate.ident),
562537
// overridden in plugin/load.rs
563538
export: false,
564539
use_locally: false,
@@ -572,28 +547,35 @@ impl<'a> PluginMetadata<'a> {
572547
}
573548

574549
/// Look for a plugin registrar. Returns library path and symbol name.
575-
pub fn plugin_registrar(&self) -> Option<(Path, String)> {
576-
if self.target_only {
550+
pub fn find_plugin_registrar(&mut self, span: Span, name: &str) -> Option<(Path, String)> {
551+
let ekrate = self.read_extension_crate(span, &CrateInfo {
552+
name: name.to_string(),
553+
ident: name.to_string(),
554+
id: ast::DUMMY_NODE_ID,
555+
should_link: false,
556+
});
557+
558+
if ekrate.target_only {
577559
// Need to abort before syntax expansion.
578-
let message = format!("plugin crate `{}` is not available for triple `{}` \
560+
let message = format!("plugin `{}` is not available for triple `{}` \
579561
(only found {})",
580-
self.info.ident,
562+
name,
581563
config::host_triple(),
582564
self.sess.opts.target_triple);
583-
self.sess.span_err(self.vi_span, &message[]);
565+
self.sess.span_err(span, &message[]);
584566
self.sess.abort_if_errors();
585567
}
586568

587-
let registrar = decoder::get_plugin_registrar_fn(self.metadata.as_slice())
588-
.map(|id| decoder::get_symbol(self.metadata.as_slice(), id));
569+
let registrar = decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice())
570+
.map(|id| decoder::get_symbol(ekrate.metadata.as_slice(), id));
589571

590-
match (self.dylib.as_ref(), registrar) {
572+
match (ekrate.dylib.as_ref(), registrar) {
591573
(Some(dylib), Some(reg)) => Some((dylib.clone(), reg)),
592574
(None, Some(_)) => {
593-
let message = format!("plugin crate `{}` only found in rlib format, \
575+
let message = format!("plugin `{}` only found in rlib format, \
594576
but must be available in dylib format",
595-
self.info.ident);
596-
self.sess.span_err(self.vi_span, &message[]);
577+
name);
578+
self.sess.span_err(span, &message[]);
597579
// No need to abort because the loading code will just ignore this
598580
// empty dylib.
599581
None

0 commit comments

Comments
 (0)