Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate rustc_plugin_impl to SessionDiagnostic #100768

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4321,6 +4321,7 @@ dependencies = [
"rustc_ast",
"rustc_errors",
"rustc_lint",
"rustc_macros",
"rustc_metadata",
"rustc_session",
"rustc_span",
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/plugin_impl.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugin_impl_load_plugin_error = {$msg}
plugin_impl_malformed_plugin_attribute = malformed `plugin` attribute
.label = malformed attribute
1 change: 1 addition & 0 deletions compiler/rustc_error_messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fluent_messages! {
lint => "../locales/en-US/lint.ftl",
parser => "../locales/en-US/parser.ftl",
passes => "../locales/en-US/passes.ftl",
plugin_impl => "../locales/en-US/plugin_impl.ftl",
privacy => "../locales/en-US/privacy.ftl",
typeck => "../locales/en-US/typeck.ftl",
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_plugin_impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ doctest = false
libloading = "0.7.1"
rustc_errors = { path = "../rustc_errors" }
rustc_lint = { path = "../rustc_lint" }
rustc_macros = { path = "../rustc_macros" }
rustc_metadata = { path = "../rustc_metadata" }
rustc_ast = { path = "../rustc_ast" }
rustc_session = { path = "../rustc_session" }
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_plugin_impl/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Errors emitted by plugin_impl

use rustc_macros::SessionDiagnostic;
use rustc_span::Span;

#[derive(SessionDiagnostic)]
#[diag(plugin_impl::load_plugin_error)]
pub struct LoadPluginError {
#[primary_span]
pub span: Span,
pub msg: String,
}

#[derive(SessionDiagnostic)]
#[diag(plugin_impl::malformed_plugin_attribute, code = "E0498")]
pub struct MalformedPluginAttribute {
#[primary_span]
#[label]
pub span: Span,
}
3 changes: 3 additions & 0 deletions compiler/rustc_plugin_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![recursion_limit = "256"]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]

use rustc_lint::LintStore;

mod errors;
pub mod load;

/// Structure used to register plugins.
Expand Down
16 changes: 5 additions & 11 deletions compiler/rustc_plugin_impl/src/load.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
//! Used by `rustc` when loading a plugin.

use crate::errors::{LoadPluginError, MalformedPluginAttribute};
use crate::Registry;
use libloading::Library;
use rustc_ast::Crate;
use rustc_errors::struct_span_err;
use rustc_metadata::locator;
use rustc_session::cstore::MetadataLoader;
use rustc_session::Session;
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;

use std::borrow::ToOwned;
use std::env;
use std::mem;
use std::path::PathBuf;

/// Pointer to a registrar function.
type PluginRegistrarFn = fn(&mut Registry<'_>);

fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
struct_span_err!(sess, span, E0498, "malformed `plugin` attribute")
.span_label(span, "malformed attribute")
.emit();
}

/// Read plugin metadata and dynamically load registrar functions.
pub fn load_plugins(
sess: &Session,
Expand All @@ -42,7 +34,9 @@ pub fn load_plugins(
Some(ident) if plugin.is_word() => {
load_plugin(&mut plugins, sess, metadata_loader, ident)
}
_ => call_malformed_plugin_attribute(sess, plugin.span()),
_ => {
sess.emit_err(MalformedPluginAttribute { span: plugin.span() });
}
}
}
}
Expand All @@ -60,7 +54,7 @@ fn load_plugin(
let fun = dylink_registrar(lib).unwrap_or_else(|err| {
// This is fatal: there are almost certainly macros we need inside this crate, so
// continuing would spew "macro undefined" errors.
sess.span_fatal(ident.span, &err.to_string());
sess.emit_fatal(LoadPluginError { span: ident.span, msg: err.to_string() });
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
});
plugins.push(fun);
}
Expand Down