Skip to content

Commit

Permalink
Auto merge of rust-lang#85296 - bjorn3:plugin_cleanup, r=petrochenkov
Browse files Browse the repository at this point in the history
Plugin interface cleanup

The first commit performs two uncontroversial cleanups. The second commit removes `#[plugin_registrar]` and instead requires you to export a `__rustc_plugin_registrar` function, this will require a change to servo's script_plugins (cc `@jdm)`
  • Loading branch information
bors committed Aug 12, 2021
2 parents 25d3e14 + a501308 commit eb2226b
Show file tree
Hide file tree
Showing 35 changed files with 287 additions and 556 deletions.
8 changes: 0 additions & 8 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

ast::ItemKind::Fn(..) => {
if self.sess.contains_name(&i.attrs[..], sym::plugin_registrar) {
gate_feature_post!(
&self,
plugin_registrar,
i.span,
"compiler plugins are experimental and possibly buggy"
);
}
if self.sess.contains_name(&i.attrs[..], sym::start) {
gate_feature_post!(
&self,
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
reachable_non_generics.insert(id.to_def_id(), SymbolExportLevel::C);
}

if let Some(id) = tcx.plugin_registrar_fn(()) {
reachable_non_generics.insert(id.to_def_id(), SymbolExportLevel::C);
}

reachable_non_generics
}

Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ declare_features! (
// feature-group-start: actual feature gates
// -------------------------------------------------------------------------

/// Allows using `#[plugin_registrar]` on functions.
(active, plugin_registrar, "1.0.0", Some(29597), None),

/// Allows using `#![plugin(myplugin)]`.
(active, plugin, "1.0.0", Some(29597), None),

Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,18 +291,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
),

// Plugins:
(
sym::plugin_registrar, Normal, template!(Word),
Gated(
Stability::Deprecated(
"https://github.com/rust-lang/rust/pull/64675",
Some("may be removed in a future compiler version"),
),
sym::plugin_registrar,
"compiler plugins are deprecated",
cfg_fn!(plugin_registrar)
)
),
(
sym::plugin, CrateLevel, template!(List: "name"),
Gated(
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@ declare_features! (
(removed, main, "1.53.0", Some(29634), None, None),
(removed, pub_macro_rules, "1.53.0", Some(78855), None,
Some("removed due to being incomplete, in particular it does not work across crates")),
/// Allows the definition of `const` functions with some advanced features.
/// Allows the definition of `const` functions with some advanced features.
(removed, const_fn, "1.54.0", Some(57563), None,
Some("split into finer-grained feature gates")),
/// Allows using `#[plugin_registrar]` on functions.
(removed, plugin_registrar, "1.54.0", Some(29597), None,
Some("a __rustc_plugin_registrar symbol must now be defined instead")),

/// Allows `#[doc(include = "some-file")]`.
(removed, external_doc, "1.54.0", Some(44732), None,
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,6 @@ pub static DEFAULT_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
let providers = &mut Providers::default();
providers.analysis = analysis;
proc_macro_decls::provide(providers);
plugin::build::provide(providers);
rustc_middle::hir::provide(providers);
mir::provide(providers);
mir_build::provide(providers);
Expand Down Expand Up @@ -856,8 +855,6 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
{
entry_point = sess.time("looking_for_entry_point", || tcx.entry_fn(()));

sess.time("looking_for_plugin_registrar", || tcx.ensure().plugin_registrar_fn(()));

sess.time("looking_for_derive_registrar", || {
tcx.ensure().proc_macro_decls_static(())
});
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ use rustc_session::config::{self, CrateType};
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
use rustc_session::search_paths::PathKind;
use rustc_session::utils::CanonicalizedPath;
use rustc_session::{Session, StableCrateId};
use rustc_session::Session;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
use rustc_target::spec::{Target, TargetTriple};
Expand Down Expand Up @@ -787,7 +787,7 @@ pub fn find_plugin_registrar(
metadata_loader: &dyn MetadataLoader,
span: Span,
name: Symbol,
) -> (PathBuf, StableCrateId) {
) -> PathBuf {
match find_plugin_registrar_impl(sess, metadata_loader, name) {
Ok(res) => res,
// `core` is always available if we got as far as loading plugins.
Expand All @@ -799,7 +799,7 @@ fn find_plugin_registrar_impl<'a>(
sess: &'a Session,
metadata_loader: &dyn MetadataLoader,
name: Symbol,
) -> Result<(PathBuf, StableCrateId), CrateError> {
) -> Result<PathBuf, CrateError> {
info!("find plugin registrar `{}`", name);
let mut locator = CrateLocator::new(
sess,
Expand All @@ -816,7 +816,7 @@ fn find_plugin_registrar_impl<'a>(

match locator.maybe_load_library_crate()? {
Some(library) => match library.source.dylib {
Some(dylib) => Ok((dylib.0, library.metadata.get_root().stable_crate_id())),
Some(dylib) => Ok(dylib.0),
None => Err(CrateError::NonDylibPlugin(name)),
},
None => Err(locator.into_error()),
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,9 +1252,6 @@ rustc_queries! {
query entry_fn(_: ()) -> Option<(DefId, EntryFnType)> {
desc { "looking up the entry function of a crate" }
}
query plugin_registrar_fn(_: ()) -> Option<LocalDefId> {
desc { "looking up the plugin registrar for a crate" }
}
query proc_macro_decls_static(_: ()) -> Option<LocalDefId> {
desc { "looking up the derive registrar for a crate" }
}
Expand Down
57 changes: 0 additions & 57 deletions compiler/rustc_plugin_impl/src/build.rs

This file was deleted.

1 change: 0 additions & 1 deletion compiler/rustc_plugin_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use rustc_lint::LintStore;

pub mod build;
pub mod load;

/// Structure used to register plugins.
Expand Down
17 changes: 5 additions & 12 deletions compiler/rustc_plugin_impl/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,13 @@ fn load_plugin(
metadata_loader: &dyn MetadataLoader,
ident: Ident,
) {
let (lib, disambiguator) =
locator::find_plugin_registrar(sess, metadata_loader, ident.span, ident.name);
let symbol = sess.generate_plugin_registrar_symbol(disambiguator);
let fun = dylink_registrar(sess, ident.span, lib, symbol);
let lib = locator::find_plugin_registrar(sess, metadata_loader, ident.span, ident.name);
let fun = dylink_registrar(sess, ident.span, lib);
plugins.push(fun);
}

// Dynamically link a registrar function into the compiler process.
fn dylink_registrar(
sess: &Session,
span: Span,
path: PathBuf,
symbol: String,
) -> PluginRegistrarFn {
fn dylink_registrar(sess: &Session, span: Span, path: PathBuf) -> PluginRegistrarFn {
use rustc_metadata::dynamic_lib::DynamicLibrary;

// Make sure the path contains a / or the linker will search for it.
Expand All @@ -83,15 +76,15 @@ fn dylink_registrar(
};

unsafe {
let registrar = match lib.symbol(&symbol) {
let registrar = match lib.symbol("__rustc_plugin_registrar") {
Ok(registrar) => mem::transmute::<*mut u8, PluginRegistrarFn>(registrar),
// again fatal if we can't register macros
Err(err) => sess.span_fatal(span, &err),
};

// Intentionally leak the dynamic library. We can't ever unload it
// since the library can make things that will live arbitrarily long
// (e.g., an @-box cycle or a thread).
// (e.g., an Rc cycle or a thread).
mem::forget(lib);

registrar
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,12 +792,6 @@ impl Session {
)
}

/// Returns the symbol name for the registrar function,
/// given the crate `Svh` and the function `DefIndex`.
pub fn generate_plugin_registrar_symbol(&self, stable_crate_id: StableCrateId) -> String {
format!("__rustc_plugin_registrar_{:08x}__", stable_crate_id.to_u64())
}

pub fn generate_proc_macro_decls_symbol(&self, stable_crate_id: StableCrateId) -> String {
format!("__rustc_proc_macro_decls_{:08x}__", stable_crate_id.to_u64())
}
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_symbol_mangling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ fn compute_symbol_name(

// FIXME(eddyb) Precompute a custom symbol name based on attributes.
let is_foreign = if let Some(def_id) = def_id.as_local() {
if tcx.plugin_registrar_fn(()) == Some(def_id) {
let stable_crate_id = tcx.sess.local_stable_crate_id();
return tcx.sess.generate_plugin_registrar_symbol(stable_crate_id);
}
if tcx.proc_macro_decls_static(()) == Some(def_id) {
let stable_crate_id = tcx.sess.local_stable_crate_id();
return tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
Expand Down
13 changes: 0 additions & 13 deletions src/doc/unstable-book/src/language-features/plugin-registrar.md

This file was deleted.

9 changes: 3 additions & 6 deletions src/doc/unstable-book/src/language-features/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ The tracking issue for this feature is: [#29597]


This feature is part of "compiler plugins." It will often be used with the
[`plugin_registrar`] and `rustc_private` features.

[`plugin_registrar`]: plugin-registrar.md
`rustc_private` feature.

------------------------

Expand Down Expand Up @@ -39,7 +37,6 @@ additional checks for code style, safety, etc. Now let's write a plugin
that warns about any item named `lintme`.

```rust,ignore (requires-stage-2)
#![feature(plugin_registrar)]
#![feature(box_syntax, rustc_private)]
extern crate rustc_ast;
Expand Down Expand Up @@ -68,8 +65,8 @@ impl EarlyLintPass for Pass {
}
}
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
#[no_mangle]
fn __rustc_plugin_registrar(reg: &mut Registry) {
reg.lint_store.register_lints(&[&TEST_LINT]);
reg.lint_store.register_early_pass(|| box Pass);
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/ui-fulldeps/auxiliary/empty-plugin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// force-host

#![feature(plugin_registrar)]
#![feature(rustc_private)]

extern crate rustc_driver;
use rustc_driver::plugin::Registry;

#[plugin_registrar]
pub fn plugin_registrar(_: &mut Registry) {}
#[no_mangle]
fn __rustc_plugin_registrar(_: &mut Registry) {}
9 changes: 4 additions & 5 deletions src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![feature(box_syntax, plugin, plugin_registrar, rustc_private)]
#![feature(box_syntax, plugin, rustc_private)]
#![crate_type = "dylib"]

extern crate rustc_ast_pretty;
extern crate rustc_driver;
extern crate rustc_hir;
#[macro_use]
extern crate rustc_lint;
#[macro_use]
extern crate rustc_session;
Expand All @@ -16,11 +15,11 @@ use rustc_driver::plugin::Registry;
use rustc_hir as hir;
use rustc_hir::intravisit;
use rustc_hir::Node;
use rustc_lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_span::source_map;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
#[no_mangle]
fn __rustc_plugin_registrar(reg: &mut Registry) {
reg.lint_store.register_lints(&[&MISSING_ALLOWED_ATTR]);
reg.lint_store.register_late_pass(|| box MissingAllowedAttrPass);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// force-host

#![feature(plugin_registrar, rustc_private)]
#![feature(rustc_private)]
#![feature(box_syntax)]

extern crate rustc_driver;
Expand Down Expand Up @@ -64,8 +64,8 @@ fake_lint_pass! {
Symbol::intern("crate_grey"), Symbol::intern("crate_green")
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
#[no_mangle]
fn __rustc_plugin_registrar(reg: &mut Registry) {
reg.lint_store.register_lints(&[
&CRATE_NOT_OKAY,
&CRATE_NOT_RED,
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui-fulldeps/auxiliary/lint-for-crate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// force-host

#![feature(plugin_registrar, rustc_private)]
#![feature(rustc_private)]
#![feature(box_syntax)]

extern crate rustc_driver;
Expand Down Expand Up @@ -38,8 +38,8 @@ impl<'tcx> LateLintPass<'tcx> for Pass {
}
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
#[no_mangle]
fn __rustc_plugin_registrar(reg: &mut Registry) {
reg.lint_store.register_lints(&[&CRATE_NOT_OKAY]);
reg.lint_store.register_late_pass(|| box Pass);
}
Loading

0 comments on commit eb2226b

Please sign in to comment.