rustc_lint: Reuse the set of registered tools from resolver
This commit is contained in:
parent
452aa81770
commit
51b2338611
10 changed files with 70 additions and 56 deletions
|
@ -16,10 +16,9 @@
|
|||
|
||||
use self::TargetLint::*;
|
||||
|
||||
use crate::levels::{is_known_lint_tool, LintLevelsBuilder};
|
||||
use crate::levels::LintLevelsBuilder;
|
||||
use crate::passes::{EarlyLintPassObject, LateLintPassObject};
|
||||
use ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
|
||||
use rustc_ast as ast;
|
||||
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::sync;
|
||||
use rustc_errors::{struct_span_err, Applicability, SuggestionStyle};
|
||||
|
@ -32,13 +31,14 @@ use rustc_middle::middle::privacy::AccessLevels;
|
|||
use rustc_middle::middle::stability;
|
||||
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
|
||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
|
||||
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, RegisteredTools, Ty, TyCtxt};
|
||||
use rustc_serialize::json::Json;
|
||||
use rustc_session::lint::{BuiltinLintDiagnostics, ExternDepSpec};
|
||||
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
|
||||
use rustc_session::Session;
|
||||
use rustc_span::lev_distance::find_best_match_for_name;
|
||||
use rustc_span::{symbol::Symbol, BytePos, MultiSpan, Span, DUMMY_SP};
|
||||
use rustc_span::symbol::{sym, Ident, Symbol};
|
||||
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};
|
||||
use rustc_target::abi;
|
||||
use tracing::debug;
|
||||
|
||||
|
@ -313,7 +313,7 @@ impl LintStore {
|
|||
sess: &Session,
|
||||
lint_name: &str,
|
||||
level: Level,
|
||||
crate_attrs: &[ast::Attribute],
|
||||
registered_tools: &RegisteredTools,
|
||||
) {
|
||||
let (tool_name, lint_name_only) = parse_lint_and_tool_name(lint_name);
|
||||
if lint_name_only == crate::WARNINGS.name_lower() && level == Level::ForceWarn {
|
||||
|
@ -326,7 +326,7 @@ impl LintStore {
|
|||
)
|
||||
.emit();
|
||||
}
|
||||
let db = match self.check_lint_name(sess, lint_name_only, tool_name, crate_attrs) {
|
||||
let db = match self.check_lint_name(lint_name_only, tool_name, registered_tools) {
|
||||
CheckLintNameResult::Ok(_) => None,
|
||||
CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
|
||||
CheckLintNameResult::NoLint(suggestion) => {
|
||||
|
@ -397,13 +397,16 @@ impl LintStore {
|
|||
/// printing duplicate warnings.
|
||||
pub fn check_lint_name(
|
||||
&self,
|
||||
sess: &Session,
|
||||
lint_name: &str,
|
||||
tool_name: Option<Symbol>,
|
||||
crate_attrs: &[ast::Attribute],
|
||||
registered_tools: &RegisteredTools,
|
||||
) -> CheckLintNameResult<'_> {
|
||||
if let Some(tool_name) = tool_name {
|
||||
if !is_known_lint_tool(tool_name, sess, crate_attrs) {
|
||||
// FIXME: rustc and rustdoc are considered tools for lints, but not for attributes.
|
||||
if tool_name != sym::rustc
|
||||
&& tool_name != sym::rustdoc
|
||||
&& !registered_tools.contains(&Ident::with_dummy_span(tool_name))
|
||||
{
|
||||
return CheckLintNameResult::NoTool;
|
||||
}
|
||||
}
|
||||
|
@ -794,11 +797,16 @@ impl<'a> EarlyContext<'a> {
|
|||
sess: &'a Session,
|
||||
warn_about_weird_lints: bool,
|
||||
lint_store: &'a LintStore,
|
||||
crate_attrs: &'a [ast::Attribute],
|
||||
registered_tools: &'a RegisteredTools,
|
||||
buffered: LintBuffer,
|
||||
) -> EarlyContext<'a> {
|
||||
EarlyContext {
|
||||
builder: LintLevelsBuilder::new(sess, warn_about_weird_lints, lint_store, crate_attrs),
|
||||
builder: LintLevelsBuilder::new(
|
||||
sess,
|
||||
warn_about_weird_lints,
|
||||
lint_store,
|
||||
registered_tools,
|
||||
),
|
||||
buffered,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ use crate::passes::{EarlyLintPass, EarlyLintPassObject};
|
|||
use rustc_ast as ast;
|
||||
use rustc_ast::visit as ast_visit;
|
||||
use rustc_ast::AstLike;
|
||||
use rustc_middle::ty::RegisteredTools;
|
||||
use rustc_session::lint::{BufferedEarlyLint, LintBuffer, LintPass};
|
||||
use rustc_session::Session;
|
||||
use rustc_span::symbol::Ident;
|
||||
|
@ -329,13 +330,19 @@ fn early_lint_node(
|
|||
sess: &Session,
|
||||
warn_about_weird_lints: bool,
|
||||
lint_store: &LintStore,
|
||||
crate_attrs: &[ast::Attribute],
|
||||
registered_tools: &RegisteredTools,
|
||||
buffered: LintBuffer,
|
||||
pass: impl EarlyLintPass,
|
||||
check_node: &ast::Crate,
|
||||
) -> LintBuffer {
|
||||
let mut cx = EarlyContextAndPass {
|
||||
context: EarlyContext::new(sess, warn_about_weird_lints, lint_store, crate_attrs, buffered),
|
||||
context: EarlyContext::new(
|
||||
sess,
|
||||
warn_about_weird_lints,
|
||||
lint_store,
|
||||
registered_tools,
|
||||
buffered,
|
||||
),
|
||||
pass,
|
||||
};
|
||||
|
||||
|
@ -351,7 +358,7 @@ pub fn check_ast_node(
|
|||
sess: &Session,
|
||||
pre_expansion: bool,
|
||||
lint_store: &LintStore,
|
||||
crate_attrs: &[ast::Attribute],
|
||||
registered_tools: &RegisteredTools,
|
||||
lint_buffer: Option<LintBuffer>,
|
||||
builtin_lints: impl EarlyLintPass,
|
||||
check_node: &ast::Crate,
|
||||
|
@ -366,7 +373,7 @@ pub fn check_ast_node(
|
|||
sess,
|
||||
pre_expansion,
|
||||
lint_store,
|
||||
crate_attrs,
|
||||
registered_tools,
|
||||
buffered,
|
||||
builtin_lints,
|
||||
check_node,
|
||||
|
@ -377,7 +384,7 @@ pub fn check_ast_node(
|
|||
sess,
|
||||
false,
|
||||
lint_store,
|
||||
crate_attrs,
|
||||
registered_tools,
|
||||
buffered,
|
||||
EarlyLintPassObjects { lints: &mut passes[..] },
|
||||
check_node,
|
||||
|
@ -391,7 +398,7 @@ pub fn check_ast_node(
|
|||
sess,
|
||||
pre_expansion && i == 0,
|
||||
lint_store,
|
||||
crate_attrs,
|
||||
registered_tools,
|
||||
buffered,
|
||||
EarlyLintPassObjects { lints: slice::from_mut(pass) },
|
||||
check_node,
|
||||
|
|
|
@ -14,7 +14,7 @@ use rustc_middle::lint::{
|
|||
COMMAND_LINE,
|
||||
};
|
||||
use rustc_middle::ty::query::Providers;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_middle::ty::{RegisteredTools, TyCtxt};
|
||||
use rustc_session::lint::{
|
||||
builtin::{self, FORBIDDEN_LINT_GROUPS},
|
||||
Level, Lint, LintId,
|
||||
|
@ -27,8 +27,8 @@ use tracing::debug;
|
|||
|
||||
fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
|
||||
let store = unerased_lint_store(tcx);
|
||||
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
|
||||
let levels = LintLevelsBuilder::new(tcx.sess, false, &store, crate_attrs);
|
||||
let levels =
|
||||
LintLevelsBuilder::new(tcx.sess, false, &store, &tcx.resolutions(()).registered_tools);
|
||||
let mut builder = LintLevelMapBuilder { levels, tcx };
|
||||
let krate = tcx.hir().krate();
|
||||
|
||||
|
@ -49,7 +49,7 @@ pub struct LintLevelsBuilder<'s> {
|
|||
cur: LintStackIndex,
|
||||
warn_about_weird_lints: bool,
|
||||
store: &'s LintStore,
|
||||
crate_attrs: &'s [ast::Attribute],
|
||||
registered_tools: &'s RegisteredTools,
|
||||
}
|
||||
|
||||
pub struct BuilderPush {
|
||||
|
@ -62,7 +62,7 @@ impl<'s> LintLevelsBuilder<'s> {
|
|||
sess: &'s Session,
|
||||
warn_about_weird_lints: bool,
|
||||
store: &'s LintStore,
|
||||
crate_attrs: &'s [ast::Attribute],
|
||||
registered_tools: &'s RegisteredTools,
|
||||
) -> Self {
|
||||
let mut builder = LintLevelsBuilder {
|
||||
sess,
|
||||
|
@ -71,7 +71,7 @@ impl<'s> LintLevelsBuilder<'s> {
|
|||
id_to_set: Default::default(),
|
||||
warn_about_weird_lints,
|
||||
store,
|
||||
crate_attrs,
|
||||
registered_tools,
|
||||
};
|
||||
builder.process_command_line(sess, store);
|
||||
assert_eq!(builder.sets.list.len(), 1);
|
||||
|
@ -91,7 +91,7 @@ impl<'s> LintLevelsBuilder<'s> {
|
|||
self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
|
||||
|
||||
for &(ref lint_name, level) in &sess.opts.lint_opts {
|
||||
store.check_lint_name_cmdline(sess, &lint_name, level, self.crate_attrs);
|
||||
store.check_lint_name_cmdline(sess, &lint_name, level, self.registered_tools);
|
||||
let orig_level = level;
|
||||
let lint_flag_val = Symbol::intern(lint_name);
|
||||
|
||||
|
@ -314,7 +314,7 @@ impl<'s> LintLevelsBuilder<'s> {
|
|||
let tool_name = tool_ident.map(|ident| ident.name);
|
||||
let name = pprust::path_to_string(&meta_item.path);
|
||||
let lint_result =
|
||||
self.store.check_lint_name(sess, &name, tool_name, self.crate_attrs);
|
||||
self.store.check_lint_name(&name, tool_name, self.registered_tools);
|
||||
match &lint_result {
|
||||
CheckLintNameResult::Ok(ids) => {
|
||||
let src = LintLevelSource::Node(
|
||||
|
@ -463,7 +463,7 @@ impl<'s> LintLevelsBuilder<'s> {
|
|||
// Ignore any errors or warnings that happen because the new name is inaccurate
|
||||
// NOTE: `new_name` already includes the tool name, so we don't have to add it again.
|
||||
if let CheckLintNameResult::Ok(ids) =
|
||||
self.store.check_lint_name(sess, &new_name, None, self.crate_attrs)
|
||||
self.store.check_lint_name(&new_name, None, self.registered_tools)
|
||||
{
|
||||
let src = LintLevelSource::Node(Symbol::intern(&new_name), sp, reason);
|
||||
for &id in ids {
|
||||
|
@ -566,20 +566,6 @@ impl<'s> LintLevelsBuilder<'s> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn is_known_lint_tool(m_item: Symbol, sess: &Session, attrs: &[ast::Attribute]) -> bool {
|
||||
if [sym::clippy, sym::rustc, sym::rustdoc].contains(&m_item) {
|
||||
return true;
|
||||
}
|
||||
// Look for registered tools
|
||||
// NOTE: does no error handling; error handling is done by rustc_resolve.
|
||||
sess.filter_by_name(attrs, sym::register_tool)
|
||||
.filter_map(|attr| attr.meta_item_list())
|
||||
.flatten()
|
||||
.filter_map(|nested_meta| nested_meta.ident())
|
||||
.map(|ident| ident.name)
|
||||
.any(|name| name == m_item)
|
||||
}
|
||||
|
||||
struct LintLevelMapBuilder<'tcx> {
|
||||
levels: LintLevelsBuilder<'tcx>,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
|
|
|
@ -96,7 +96,7 @@ use unused::*;
|
|||
pub use builtin::SoftLints;
|
||||
pub use context::{CheckLintNameResult, FindLintError, LintStore};
|
||||
pub use context::{EarlyContext, LateContext, LintContext};
|
||||
pub use early::check_ast_node;
|
||||
pub use early::{check_ast_node, EarlyCheckNode};
|
||||
pub use late::check_crate;
|
||||
pub use passes::{EarlyLintPass, LateLintPass};
|
||||
pub use rustc_session::lint::Level::{self, *};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue