2014-06-06 22:49:48 +00:00
|
|
|
//! Implementation of lint checking.
|
|
|
|
//!
|
2018-05-08 13:10:16 +00:00
|
|
|
//! The lint checking is mostly consolidated into one pass which runs
|
|
|
|
//! after all other analyses. Throughout compilation, lint warnings
|
2014-06-06 22:49:48 +00:00
|
|
|
//! can be added via the `add_lint` method on the Session structure. This
|
2019-02-08 13:53:55 +00:00
|
|
|
//! requires a span and an ID of the node that the lint is being added to. The
|
2014-06-06 22:49:48 +00:00
|
|
|
//! lint isn't actually emitted at that time because it is unknown what the
|
|
|
|
//! actual lint level at that location is.
|
|
|
|
//!
|
2018-05-08 13:10:16 +00:00
|
|
|
//! To actually emit lint warnings/errors, a separate pass is used.
|
|
|
|
//! A context keeps track of the current state of all lint levels.
|
2014-06-06 22:49:48 +00:00
|
|
|
//! Upon entering a node of the ast which can modify the lint settings, the
|
|
|
|
//! previous lint state is pushed onto a stack and the ast is then recursed
|
2019-02-08 13:53:55 +00:00
|
|
|
//! upon. As the ast is traversed, this keeps track of the current lint level
|
2014-06-06 22:49:48 +00:00
|
|
|
//! for all lint attributes.
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
use self::TargetLint::*;
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2020-01-09 06:52:01 +00:00
|
|
|
use crate::levels::LintLevelsBuilder;
|
|
|
|
use crate::passes::{EarlyLintPassObject, LateLintPassObject};
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::util::lev_distance::find_best_match_for_name;
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-12-30 13:22:46 +00:00
|
|
|
use rustc_data_structures::sync;
|
2020-01-31 12:24:57 +00:00
|
|
|
use rustc_errors::{struct_span_err, Applicability};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2020-06-26 02:55:23 +00:00
|
|
|
use rustc_hir::def::Res;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId};
|
2020-03-21 03:39:48 +00:00
|
|
|
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::lint::LintDiagnosticBuilder;
|
|
|
|
use rustc_middle::middle::privacy::AccessLevels;
|
|
|
|
use rustc_middle::middle::stability;
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
|
2020-09-02 07:40:56 +00:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
|
2020-03-21 02:13:14 +00:00
|
|
|
use rustc_session::lint::{add_elided_lifetime_in_path_suggestion, BuiltinLintDiagnostics};
|
2020-01-05 08:40:16 +00:00
|
|
|
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
|
2020-01-05 09:58:44 +00:00
|
|
|
use rustc_session::Session;
|
2019-12-31 20:25:16 +00:00
|
|
|
use rustc_span::{symbol::Symbol, MultiSpan, Span, DUMMY_SP};
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_target::abi::LayoutOf;
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2020-06-25 23:56:23 +00:00
|
|
|
use std::cell::Cell;
|
2019-12-31 20:25:16 +00:00
|
|
|
use std::slice;
|
2019-11-11 21:46:56 +00:00
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Information about the registered lints.
|
|
|
|
///
|
|
|
|
/// This is basically the subset of `Context` that we can
|
|
|
|
/// build early in the compile pipeline.
|
|
|
|
pub struct LintStore {
|
2019-11-30 08:31:25 +00:00
|
|
|
/// Registered lints.
|
2019-10-07 20:52:53 +00:00
|
|
|
lints: Vec<&'static Lint>,
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2019-10-07 22:23:16 +00:00
|
|
|
/// Constructor functions for each variety of lint pass.
|
|
|
|
///
|
|
|
|
/// These should only be called once, but since we want to avoid locks or
|
|
|
|
/// interior mutability, we don't enforce this (and lints should, in theory,
|
|
|
|
/// be compatible with being constructed more than once, though not
|
|
|
|
/// necessarily in a sane manner. This is safe though.)
|
2019-12-30 13:11:49 +00:00
|
|
|
pub pre_expansion_passes: Vec<Box<dyn Fn() -> EarlyLintPassObject + sync::Send + sync::Sync>>,
|
|
|
|
pub early_passes: Vec<Box<dyn Fn() -> EarlyLintPassObject + sync::Send + sync::Sync>>,
|
2019-12-30 13:22:46 +00:00
|
|
|
pub late_passes: Vec<Box<dyn Fn() -> LateLintPassObject + sync::Send + sync::Sync>>,
|
2019-10-07 22:23:16 +00:00
|
|
|
/// This is unique in that we construct them per-module, so not once.
|
2019-12-30 13:22:46 +00:00
|
|
|
pub late_module_passes: Vec<Box<dyn Fn() -> LateLintPassObject + sync::Send + sync::Sync>>,
|
2014-06-06 22:49:48 +00:00
|
|
|
|
|
|
|
/// Lints indexed by name.
|
2016-11-08 03:02:55 +00:00
|
|
|
by_name: FxHashMap<String, TargetLint>,
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2018-09-15 16:32:24 +00:00
|
|
|
/// Map of registered lint groups to what lints they expand to.
|
|
|
|
lint_groups: FxHashMap<&'static str, LintGroup>,
|
2017-05-15 19:16:18 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 19:45:21 +00:00
|
|
|
/// The target of the `by_name` map, which accounts for renaming/deprecation.
|
2014-10-14 18:32:31 +00:00
|
|
|
enum TargetLint {
|
|
|
|
/// A direct lint target
|
|
|
|
Id(LintId),
|
|
|
|
|
|
|
|
/// Temporary renaming, used for easing migration pain; see #16545
|
|
|
|
Renamed(String, LintId),
|
2015-07-15 17:12:30 +00:00
|
|
|
|
|
|
|
/// Lint with this name existed previously, but has been removed/deprecated.
|
|
|
|
/// The string argument is the reason for removal.
|
|
|
|
Removed(String),
|
|
|
|
}
|
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
pub enum FindLintError {
|
2015-07-15 17:12:30 +00:00
|
|
|
NotFound,
|
2017-03-13 00:12:13 +00:00
|
|
|
Removed,
|
2014-10-14 18:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 16:32:24 +00:00
|
|
|
struct LintAlias {
|
|
|
|
name: &'static str,
|
|
|
|
/// Whether deprecation warnings should be suppressed for this alias.
|
|
|
|
silent: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LintGroup {
|
|
|
|
lint_ids: Vec<LintId>,
|
|
|
|
from_plugin: bool,
|
|
|
|
depr: Option<LintAlias>,
|
|
|
|
}
|
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
pub enum CheckLintNameResult<'a> {
|
|
|
|
Ok(&'a [LintId]),
|
2018-12-19 18:51:52 +00:00
|
|
|
/// Lint doesn't exist. Potentially contains a suggestion for a correct lint name.
|
|
|
|
NoLint(Option<Symbol>),
|
2017-08-17 14:15:40 +00:00
|
|
|
/// The lint is either renamed or removed. This is the warning
|
2018-07-15 03:52:40 +00:00
|
|
|
/// message, and an optional new name (`None` if removed).
|
|
|
|
Warning(String, Option<String>),
|
2018-07-30 09:29:23 +00:00
|
|
|
/// The lint is from a tool. If the Option is None, then either
|
|
|
|
/// the lint does not exist in the tool or the code was not
|
|
|
|
/// compiled with the tool and therefore the lint was never
|
|
|
|
/// added to the `LintStore`. Otherwise the `LintId` will be
|
|
|
|
/// returned as if it where a rustc lint.
|
2018-08-27 21:25:31 +00:00
|
|
|
Tool(Result<&'a [LintId], (Option<&'a [LintId]>, String)>),
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
impl LintStore {
|
|
|
|
pub fn new() -> LintStore {
|
|
|
|
LintStore {
|
2016-10-29 21:54:04 +00:00
|
|
|
lints: vec![],
|
2019-10-07 22:23:16 +00:00
|
|
|
pre_expansion_passes: vec![],
|
|
|
|
early_passes: vec![],
|
|
|
|
late_passes: vec![],
|
2019-01-31 03:36:37 +00:00
|
|
|
late_module_passes: vec![],
|
2018-10-16 14:57:53 +00:00
|
|
|
by_name: Default::default(),
|
|
|
|
lint_groups: Default::default(),
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:52:53 +00:00
|
|
|
pub fn get_lints<'t>(&'t self) -> &'t [&'static Lint] {
|
2015-02-20 19:08:14 +00:00
|
|
|
&self.lints
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-21 03:27:59 +00:00
|
|
|
pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec<LintId>, bool)> {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.lint_groups
|
|
|
|
.iter()
|
2018-09-15 16:32:24 +00:00
|
|
|
.filter(|(_, LintGroup { depr, .. })| {
|
|
|
|
// Don't display deprecated lint groups.
|
|
|
|
depr.is_none()
|
|
|
|
})
|
|
|
|
.map(|(k, LintGroup { lint_ids, from_plugin, .. })| {
|
|
|
|
(*k, lint_ids.clone(), *from_plugin)
|
|
|
|
})
|
|
|
|
.collect()
|
2014-07-21 03:27:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 20:53:28 +00:00
|
|
|
pub fn register_early_pass(
|
|
|
|
&mut self,
|
2019-12-22 22:42:04 +00:00
|
|
|
pass: impl Fn() -> EarlyLintPassObject + 'static + sync::Send + sync::Sync,
|
2019-10-22 20:53:28 +00:00
|
|
|
) {
|
2019-10-10 23:33:00 +00:00
|
|
|
self.early_passes.push(Box::new(pass));
|
2015-09-14 23:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 20:53:28 +00:00
|
|
|
pub fn register_pre_expansion_pass(
|
|
|
|
&mut self,
|
|
|
|
pass: impl Fn() -> EarlyLintPassObject + 'static + sync::Send + sync::Sync,
|
|
|
|
) {
|
2019-10-10 23:33:00 +00:00
|
|
|
self.pre_expansion_passes.push(Box::new(pass));
|
2018-07-14 14:40:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 20:53:28 +00:00
|
|
|
pub fn register_late_pass(
|
|
|
|
&mut self,
|
|
|
|
pass: impl Fn() -> LateLintPassObject + 'static + sync::Send + sync::Sync,
|
|
|
|
) {
|
2019-10-10 23:33:00 +00:00
|
|
|
self.late_passes.push(Box::new(pass));
|
2019-10-07 20:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 20:53:28 +00:00
|
|
|
pub fn register_late_mod_pass(
|
|
|
|
&mut self,
|
|
|
|
pass: impl Fn() -> LateLintPassObject + 'static + sync::Send + sync::Sync,
|
|
|
|
) {
|
2019-10-10 23:33:00 +00:00
|
|
|
self.late_module_passes.push(Box::new(pass));
|
2015-09-14 23:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method for register_early/late_pass
|
2019-10-07 21:21:50 +00:00
|
|
|
pub fn register_lints(&mut self, lints: &[&'static Lint]) {
|
2019-09-24 22:24:45 +00:00
|
|
|
for lint in lints {
|
2019-10-07 20:52:53 +00:00
|
|
|
self.lints.push(lint);
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2018-06-21 07:04:50 +00:00
|
|
|
let id = LintId::of(lint);
|
2014-11-06 17:25:16 +00:00
|
|
|
if self.by_name.insert(lint.name_lower(), Id(id)).is_some() {
|
2019-09-24 00:13:02 +00:00
|
|
|
bug!("duplicate specification of lint {}", lint.name_lower())
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:49:21 +00:00
|
|
|
if let Some(FutureIncompatibleInfo { edition, .. }) = lint.future_incompatible {
|
|
|
|
if let Some(edition) = edition {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.lint_groups
|
|
|
|
.entry(edition.lint_name())
|
2019-10-09 01:49:21 +00:00
|
|
|
.or_insert(LintGroup {
|
|
|
|
lint_ids: vec![],
|
|
|
|
from_plugin: lint.is_plugin,
|
|
|
|
depr: None,
|
|
|
|
})
|
2019-12-22 22:42:04 +00:00
|
|
|
.lint_ids
|
|
|
|
.push(id);
|
2019-10-09 01:49:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
self.lint_groups
|
|
|
|
.entry("future_incompatible")
|
2019-10-09 01:49:21 +00:00
|
|
|
.or_insert(LintGroup {
|
|
|
|
lint_ids: vec![],
|
|
|
|
from_plugin: lint.is_plugin,
|
|
|
|
depr: None,
|
|
|
|
})
|
2019-12-22 22:42:04 +00:00
|
|
|
.lint_ids
|
|
|
|
.push(id);
|
2018-02-23 00:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 22:53:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn register_group_alias(&mut self, lint_name: &'static str, alias: &'static str) {
|
|
|
|
self.lint_groups.insert(
|
|
|
|
alias,
|
|
|
|
LintGroup {
|
|
|
|
lint_ids: vec![],
|
|
|
|
from_plugin: false,
|
|
|
|
depr: Some(LintAlias { name: lint_name, silent: true }),
|
|
|
|
},
|
|
|
|
);
|
2018-09-15 14:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 21:24:42 +00:00
|
|
|
pub fn register_group(
|
|
|
|
&mut self,
|
|
|
|
from_plugin: bool,
|
|
|
|
name: &'static str,
|
|
|
|
deprecated_name: Option<&'static str>,
|
|
|
|
to: Vec<LintId>,
|
|
|
|
) {
|
|
|
|
let new = self
|
|
|
|
.lint_groups
|
2019-12-22 22:42:04 +00:00
|
|
|
.insert(name, LintGroup { lint_ids: to, from_plugin, depr: None })
|
2018-08-27 21:24:42 +00:00
|
|
|
.is_none();
|
|
|
|
if let Some(deprecated) = deprecated_name {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.lint_groups.insert(
|
|
|
|
deprecated,
|
|
|
|
LintGroup {
|
|
|
|
lint_ids: vec![],
|
|
|
|
from_plugin,
|
|
|
|
depr: Some(LintAlias { name, silent: false }),
|
|
|
|
},
|
|
|
|
);
|
2018-08-27 21:24:42 +00:00
|
|
|
}
|
2014-07-21 03:27:59 +00:00
|
|
|
|
|
|
|
if !new {
|
2019-09-24 00:13:02 +00:00
|
|
|
bug!("duplicate specification of lint group {}", name);
|
2014-07-21 03:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 11:44:44 +00:00
|
|
|
pub fn register_renamed(&mut self, old_name: &str, new_name: &str) {
|
2014-11-12 23:51:51 +00:00
|
|
|
let target = match self.by_name.get(new_name) {
|
2020-01-16 22:15:52 +00:00
|
|
|
Some(&Id(lint_id)) => lint_id,
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => bug!("invalid lint renaming of {} to {}", old_name, new_name),
|
2014-10-14 18:32:31 +00:00
|
|
|
};
|
|
|
|
self.by_name.insert(old_name.to_string(), Renamed(new_name.to_string(), target));
|
|
|
|
}
|
|
|
|
|
2015-07-15 17:12:30 +00:00
|
|
|
pub fn register_removed(&mut self, name: &str, reason: &str) {
|
|
|
|
self.by_name.insert(name.into(), Removed(reason.into()));
|
|
|
|
}
|
|
|
|
|
2018-09-15 16:05:52 +00:00
|
|
|
pub fn find_lints(&self, mut lint_name: &str) -> Result<Vec<LintId>, FindLintError> {
|
2014-11-12 23:51:51 +00:00
|
|
|
match self.by_name.get(lint_name) {
|
2017-07-27 04:51:09 +00:00
|
|
|
Some(&Id(lint_id)) => Ok(vec![lint_id]),
|
2019-12-22 22:42:04 +00:00
|
|
|
Some(&Renamed(_, lint_id)) => Ok(vec![lint_id]),
|
|
|
|
Some(&Removed(_)) => Err(FindLintError::Removed),
|
|
|
|
None => loop {
|
|
|
|
return match self.lint_groups.get(lint_name) {
|
|
|
|
Some(LintGroup { lint_ids, depr, .. }) => {
|
|
|
|
if let Some(LintAlias { name, .. }) = depr {
|
|
|
|
lint_name = name;
|
|
|
|
continue;
|
2018-09-15 16:05:52 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
Ok(lint_ids.clone())
|
|
|
|
}
|
|
|
|
None => Err(FindLintError::Removed),
|
|
|
|
};
|
|
|
|
},
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
2017-05-15 19:16:18 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 14:21:55 +00:00
|
|
|
/// Checks the validity of lint names derived from the command line
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn check_lint_name_cmdline(&self, sess: &Session, lint_name: &str, level: Level) {
|
2018-07-30 09:29:23 +00:00
|
|
|
let db = match self.check_lint_name(lint_name, None) {
|
2017-07-27 04:51:09 +00:00
|
|
|
CheckLintNameResult::Ok(_) => None,
|
2019-12-22 22:42:04 +00:00
|
|
|
CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
|
2018-12-19 18:51:52 +00:00
|
|
|
CheckLintNameResult::NoLint(suggestion) => {
|
2019-12-31 20:25:16 +00:00
|
|
|
let mut err =
|
|
|
|
struct_span_err!(sess, DUMMY_SP, E0602, "unknown lint: `{}`", lint_name);
|
2018-12-19 18:51:52 +00:00
|
|
|
|
|
|
|
if let Some(suggestion) = suggestion {
|
|
|
|
err.help(&format!("did you mean: `{}`", suggestion));
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(err)
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
2018-09-01 15:43:14 +00:00
|
|
|
CheckLintNameResult::Tool(result) => match result {
|
|
|
|
Err((Some(_), new_name)) => Some(sess.struct_warn(&format!(
|
2018-09-01 16:15:44 +00:00
|
|
|
"lint name `{}` is deprecated \
|
2018-09-01 15:43:14 +00:00
|
|
|
and does not have an effect anymore. \
|
|
|
|
Use: {}",
|
|
|
|
lint_name, new_name
|
|
|
|
))),
|
|
|
|
_ => None,
|
|
|
|
},
|
2017-07-27 04:51:09 +00:00
|
|
|
};
|
2017-05-15 19:16:18 +00:00
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
if let Some(mut db) = db {
|
2019-12-22 22:42:04 +00:00
|
|
|
let msg = format!(
|
|
|
|
"requested on the command line with `{} {}`",
|
|
|
|
match level {
|
|
|
|
Level::Allow => "-A",
|
|
|
|
Level::Warn => "-W",
|
|
|
|
Level::Deny => "-D",
|
|
|
|
Level::Forbid => "-F",
|
|
|
|
},
|
|
|
|
lint_name
|
|
|
|
);
|
2017-07-27 04:51:09 +00:00
|
|
|
db.note(&msg);
|
|
|
|
db.emit();
|
2017-05-15 19:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
/// Checks the name of a lint for its existence, and whether it was
|
|
|
|
/// renamed or removed. Generates a DiagnosticBuilder containing a
|
|
|
|
/// warning for renamed and removed lints. This is over both lint
|
|
|
|
/// names from attributes and those passed on the command line. Since
|
|
|
|
/// it emits non-fatal warnings and there are *two* lint passes that
|
|
|
|
/// inspect attributes, this is only run from the late pass to avoid
|
|
|
|
/// printing duplicate warnings.
|
2018-07-30 09:29:23 +00:00
|
|
|
pub fn check_lint_name(
|
|
|
|
&self,
|
|
|
|
lint_name: &str,
|
2019-09-03 06:02:32 +00:00
|
|
|
tool_name: Option<Symbol>,
|
2018-08-30 05:02:42 +00:00
|
|
|
) -> CheckLintNameResult<'_> {
|
2018-07-30 09:29:23 +00:00
|
|
|
let complete_name = if let Some(tool_name) = tool_name {
|
|
|
|
format!("{}::{}", tool_name, lint_name)
|
|
|
|
} else {
|
|
|
|
lint_name.to_string()
|
|
|
|
};
|
2018-08-27 21:25:31 +00:00
|
|
|
// If the lint was scoped with `tool::` check if the tool lint exists
|
2020-03-04 14:53:14 +00:00
|
|
|
if tool_name.is_some() {
|
2018-07-30 09:29:23 +00:00
|
|
|
match self.by_name.get(&complete_name) {
|
|
|
|
None => match self.lint_groups.get(&*complete_name) {
|
2018-08-27 21:25:31 +00:00
|
|
|
None => return CheckLintNameResult::Tool(Err((None, String::new()))),
|
2018-09-15 16:32:24 +00:00
|
|
|
Some(LintGroup { lint_ids, .. }) => {
|
|
|
|
return CheckLintNameResult::Tool(Ok(&lint_ids));
|
|
|
|
}
|
2018-07-30 09:29:23 +00:00
|
|
|
},
|
2018-08-27 21:25:31 +00:00
|
|
|
Some(&Id(ref id)) => return CheckLintNameResult::Tool(Ok(slice::from_ref(id))),
|
2018-07-30 09:29:23 +00:00
|
|
|
// If the lint was registered as removed or renamed by the lint tool, we don't need
|
|
|
|
// to treat tool_lints and rustc lints different and can use the code below.
|
|
|
|
_ => {}
|
2015-07-24 05:19:12 +00:00
|
|
|
}
|
2018-07-30 09:29:23 +00:00
|
|
|
}
|
|
|
|
match self.by_name.get(&complete_name) {
|
|
|
|
Some(&Renamed(ref new_name, _)) => CheckLintNameResult::Warning(
|
2019-12-22 22:42:04 +00:00
|
|
|
format!("lint `{}` has been renamed to `{}`", complete_name, new_name),
|
2018-07-30 09:29:23 +00:00
|
|
|
Some(new_name.to_owned()),
|
|
|
|
),
|
|
|
|
Some(&Removed(ref reason)) => CheckLintNameResult::Warning(
|
2018-08-27 21:25:31 +00:00
|
|
|
format!("lint `{}` has been removed: `{}`", complete_name, reason),
|
2018-07-30 09:29:23 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
None => match self.lint_groups.get(&*complete_name) {
|
2018-08-27 21:25:31 +00:00
|
|
|
// If neither the lint, nor the lint group exists check if there is a `clippy::`
|
|
|
|
// variant of this lint
|
|
|
|
None => self.check_tool_name_for_backwards_compat(&complete_name, "clippy"),
|
2018-09-15 16:32:24 +00:00
|
|
|
Some(LintGroup { lint_ids, depr, .. }) => {
|
2018-08-27 21:25:31 +00:00
|
|
|
// Check if the lint group name is deprecated
|
2018-09-15 16:32:24 +00:00
|
|
|
if let Some(LintAlias { name, silent }) = depr {
|
|
|
|
let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
|
|
|
|
return if *silent {
|
|
|
|
CheckLintNameResult::Ok(&lint_ids)
|
2018-09-15 14:26:45 +00:00
|
|
|
} else {
|
2020-03-05 13:29:58 +00:00
|
|
|
CheckLintNameResult::Tool(Err((Some(&lint_ids), (*name).to_string())))
|
2018-09-15 14:26:45 +00:00
|
|
|
};
|
2018-08-27 21:25:31 +00:00
|
|
|
}
|
2018-09-15 16:32:24 +00:00
|
|
|
CheckLintNameResult::Ok(&lint_ids)
|
2018-08-27 21:25:31 +00:00
|
|
|
}
|
2018-07-30 09:29:23 +00:00
|
|
|
},
|
2017-11-23 14:41:51 +00:00
|
|
|
Some(&Id(ref id)) => CheckLintNameResult::Ok(slice::from_ref(id)),
|
2015-07-24 05:19:12 +00:00
|
|
|
}
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
2018-08-27 21:25:31 +00:00
|
|
|
|
|
|
|
fn check_tool_name_for_backwards_compat(
|
|
|
|
&self,
|
|
|
|
lint_name: &str,
|
|
|
|
tool_name: &str,
|
2018-08-30 05:02:42 +00:00
|
|
|
) -> CheckLintNameResult<'_> {
|
2018-08-27 21:25:31 +00:00
|
|
|
let complete_name = format!("{}::{}", tool_name, lint_name);
|
|
|
|
match self.by_name.get(&complete_name) {
|
|
|
|
None => match self.lint_groups.get(&*complete_name) {
|
|
|
|
// Now we are sure, that this lint exists nowhere
|
2018-12-19 18:51:52 +00:00
|
|
|
None => {
|
2019-12-22 22:42:04 +00:00
|
|
|
let symbols =
|
|
|
|
self.by_name.keys().map(|name| Symbol::intern(&name)).collect::<Vec<_>>();
|
2018-12-19 18:51:52 +00:00
|
|
|
|
2020-07-08 10:03:37 +00:00
|
|
|
let suggestion = find_best_match_for_name(
|
|
|
|
symbols.iter(),
|
|
|
|
Symbol::intern(&lint_name.to_lowercase()),
|
|
|
|
None,
|
|
|
|
);
|
2018-12-19 18:51:52 +00:00
|
|
|
|
|
|
|
CheckLintNameResult::NoLint(suggestion)
|
|
|
|
}
|
2018-09-15 16:32:24 +00:00
|
|
|
Some(LintGroup { lint_ids, depr, .. }) => {
|
2018-09-15 14:26:45 +00:00
|
|
|
// Reaching this would be weird, but let's cover this case anyway
|
2018-09-15 16:32:24 +00:00
|
|
|
if let Some(LintAlias { name, silent }) = depr {
|
|
|
|
let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
|
|
|
|
return if *silent {
|
|
|
|
CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
|
2018-09-15 14:26:45 +00:00
|
|
|
} else {
|
2020-03-05 13:29:58 +00:00
|
|
|
CheckLintNameResult::Tool(Err((Some(&lint_ids), (*name).to_string())))
|
2018-09-15 14:26:45 +00:00
|
|
|
};
|
2018-08-27 21:25:31 +00:00
|
|
|
}
|
2018-09-15 16:32:24 +00:00
|
|
|
CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
|
2018-08-27 21:25:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(&Id(ref id)) => {
|
|
|
|
CheckLintNameResult::Tool(Err((Some(slice::from_ref(id)), complete_name)))
|
|
|
|
}
|
2018-12-19 18:51:52 +00:00
|
|
|
_ => CheckLintNameResult::NoLint(None),
|
2018-08-27 21:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 04:40:59 +00:00
|
|
|
/// Context for lint checking after type checking.
|
2020-06-25 20:41:36 +00:00
|
|
|
pub struct LateContext<'tcx> {
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Type context we're checking in.
|
2019-06-13 21:48:52 +00:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2020-06-25 23:56:23 +00:00
|
|
|
/// Current body, or `None` if outside a body.
|
|
|
|
pub enclosing_body: Option<hir::BodyId>,
|
|
|
|
|
2020-07-17 08:47:04 +00:00
|
|
|
/// Type-checking results for the current body. Access using the `typeck_results`
|
|
|
|
/// and `maybe_typeck_results` methods, which handle querying the typeck results on demand.
|
2020-06-25 23:56:23 +00:00
|
|
|
// FIXME(eddyb) move all the code accessing internal fields like this,
|
|
|
|
// to this module, to avoid exposing it to lint logic.
|
2020-07-17 08:47:04 +00:00
|
|
|
pub(super) cached_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
|
2020-06-25 23:56:23 +00:00
|
|
|
|
2017-06-07 12:21:55 +00:00
|
|
|
/// Parameter environment for the item we are in.
|
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
2015-11-19 11:16:35 +00:00
|
|
|
/// Items accessible from the crate being checked.
|
2020-06-25 20:41:36 +00:00
|
|
|
pub access_levels: &'tcx AccessLevels,
|
2014-06-17 23:55:34 +00:00
|
|
|
|
2017-05-15 19:16:18 +00:00
|
|
|
/// The store of registered lints and the lint levels.
|
2019-12-30 13:22:46 +00:00
|
|
|
pub lint_store: &'tcx LintStore,
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2019-12-30 13:22:46 +00:00
|
|
|
pub last_node_with_lint_attrs: hir::HirId,
|
2017-10-28 21:19:07 +00:00
|
|
|
|
|
|
|
/// Generic type parameters in scope for the item we are in.
|
2019-11-30 16:46:46 +00:00
|
|
|
pub generics: Option<&'tcx hir::Generics<'tcx>>,
|
2018-06-11 06:48:15 +00:00
|
|
|
|
|
|
|
/// We are only looking at one module
|
2019-12-30 13:11:49 +00:00
|
|
|
pub only_module: bool,
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 04:40:59 +00:00
|
|
|
/// Context for lint checking of the AST, after expansion, before lowering to
|
|
|
|
/// HIR.
|
|
|
|
pub struct EarlyContext<'a> {
|
|
|
|
/// Type context we're checking in.
|
|
|
|
pub sess: &'a Session,
|
|
|
|
|
|
|
|
/// The crate being checked.
|
|
|
|
pub krate: &'a ast::Crate,
|
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
pub builder: LintLevelsBuilder<'a>,
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2017-05-15 19:16:18 +00:00
|
|
|
/// The store of registered lints and the lint levels.
|
2019-12-30 13:11:49 +00:00
|
|
|
pub lint_store: &'a LintStore,
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
pub buffered: LintBuffer,
|
2019-01-18 06:40:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 14:40:17 +00:00
|
|
|
pub trait LintPassObject: Sized {}
|
2017-05-15 19:16:18 +00:00
|
|
|
|
2018-07-14 14:40:17 +00:00
|
|
|
impl LintPassObject for EarlyLintPassObject {}
|
2017-05-15 19:16:18 +00:00
|
|
|
|
2018-07-14 14:40:17 +00:00
|
|
|
impl LintPassObject for LateLintPassObject {}
|
2017-05-15 19:16:18 +00:00
|
|
|
|
2019-06-11 09:21:38 +00:00
|
|
|
pub trait LintContext: Sized {
|
2017-05-15 19:16:18 +00:00
|
|
|
type PassObject: LintPassObject;
|
|
|
|
|
2015-09-10 04:40:59 +00:00
|
|
|
fn sess(&self) -> &Session;
|
|
|
|
fn lints(&self) -> &LintStore;
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2020-02-05 15:28:23 +00:00
|
|
|
fn lookup_with_diagnostics(
|
2019-12-22 22:42:04 +00:00
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
2020-02-05 15:28:23 +00:00
|
|
|
span: Option<impl Into<MultiSpan>>,
|
2020-02-02 09:41:14 +00:00
|
|
|
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
|
2019-12-22 22:42:04 +00:00
|
|
|
diagnostic: BuiltinLintDiagnostics,
|
|
|
|
) {
|
2020-01-31 12:24:57 +00:00
|
|
|
self.lookup(lint, span, |lint| {
|
2020-02-02 09:41:14 +00:00
|
|
|
// We first generate a blank diagnostic.
|
|
|
|
let mut db = lint.build("");
|
|
|
|
|
|
|
|
// Now, set up surrounding context.
|
2020-01-31 12:24:57 +00:00
|
|
|
let sess = self.sess();
|
|
|
|
match diagnostic {
|
|
|
|
BuiltinLintDiagnostics::Normal => (),
|
|
|
|
BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
|
|
|
|
let (sugg, app) = match sess.source_map().span_to_snippet(span) {
|
|
|
|
Ok(s) if is_global => {
|
|
|
|
(format!("dyn ({})", s), Applicability::MachineApplicable)
|
|
|
|
}
|
|
|
|
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
|
|
|
|
Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
|
|
|
|
};
|
|
|
|
db.span_suggestion(span, "use `dyn`", sugg, app);
|
|
|
|
}
|
|
|
|
BuiltinLintDiagnostics::AbsPathWithModule(span) => {
|
|
|
|
let (sugg, app) = match sess.source_map().span_to_snippet(span) {
|
|
|
|
Ok(ref s) => {
|
|
|
|
// FIXME(Manishearth) ideally the emitting code
|
|
|
|
// can tell us whether or not this is global
|
2020-02-01 23:47:58 +00:00
|
|
|
let opt_colon =
|
|
|
|
if s.trim_start().starts_with("::") { "" } else { "::" };
|
2020-01-31 12:24:57 +00:00
|
|
|
|
|
|
|
(format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
|
|
|
|
}
|
|
|
|
Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders),
|
|
|
|
};
|
|
|
|
db.span_suggestion(span, "use `crate`", sugg, app);
|
|
|
|
}
|
|
|
|
BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(span) => {
|
|
|
|
db.span_label(
|
|
|
|
span,
|
|
|
|
"names from parent modules are not accessible without an explicit import",
|
|
|
|
);
|
|
|
|
}
|
2020-02-01 23:47:58 +00:00
|
|
|
BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(
|
|
|
|
span_def,
|
|
|
|
) => {
|
2020-01-31 12:24:57 +00:00
|
|
|
db.span_note(span_def, "the macro is defined here");
|
|
|
|
}
|
|
|
|
BuiltinLintDiagnostics::ElidedLifetimesInPaths(
|
2020-01-05 09:07:26 +00:00
|
|
|
n,
|
|
|
|
path_span,
|
|
|
|
incl_angl_brckt,
|
|
|
|
insertion_span,
|
|
|
|
anon_lts,
|
2020-01-31 12:24:57 +00:00
|
|
|
) => {
|
|
|
|
add_elided_lifetime_in_path_suggestion(
|
|
|
|
sess,
|
|
|
|
&mut db,
|
|
|
|
n,
|
|
|
|
path_span,
|
|
|
|
incl_angl_brckt,
|
|
|
|
insertion_span,
|
|
|
|
anon_lts,
|
2020-01-05 09:07:26 +00:00
|
|
|
);
|
|
|
|
}
|
2020-01-31 12:24:57 +00:00
|
|
|
BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
|
|
|
|
db.span_suggestion(span, ¬e, sugg, Applicability::MaybeIncorrect);
|
|
|
|
}
|
|
|
|
BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
|
|
|
|
if !replaces.is_empty() {
|
|
|
|
db.tool_only_multipart_suggestion(
|
|
|
|
&message,
|
|
|
|
replaces,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
|
|
|
|
for (span, is_imported) in spans {
|
|
|
|
let introduced = if is_imported { "imported" } else { "defined" };
|
|
|
|
db.span_label(
|
|
|
|
span,
|
|
|
|
format!("the item `{}` is already {} here", ident, introduced),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
|
2020-07-25 17:49:46 +00:00
|
|
|
stability::deprecation_suggestion(&mut db, "macro", suggestion, span)
|
2020-01-05 09:07:26 +00:00
|
|
|
}
|
2020-02-22 00:01:48 +00:00
|
|
|
BuiltinLintDiagnostics::UnusedDocComment(span) => {
|
2020-04-22 04:09:24 +00:00
|
|
|
db.span_label(span, "rustdoc does not generate documentation for macro invocations");
|
2020-02-22 00:01:48 +00:00
|
|
|
db.help("to document an item produced by a macro, \
|
|
|
|
the macro must produce the documentation as part of its expansion");
|
|
|
|
}
|
2020-01-05 09:07:26 +00:00
|
|
|
}
|
2020-02-02 09:41:14 +00:00
|
|
|
// Rewrap `db`, and pass control to the user.
|
|
|
|
decorate(LintDiagnosticBuilder::new(db));
|
2020-01-31 12:24:57 +00:00
|
|
|
});
|
2018-02-23 06:34:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 17:18:40 +00:00
|
|
|
// FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
|
|
|
|
// set the span in their `decorate` function (preferably using set_span).
|
2019-12-22 22:42:04 +00:00
|
|
|
fn lookup<S: Into<MultiSpan>>(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: Option<S>,
|
2020-01-31 12:24:57 +00:00
|
|
|
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
|
|
|
|
);
|
2015-12-20 21:00:43 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn struct_span_lint<S: Into<MultiSpan>>(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: S,
|
2020-02-01 23:47:58 +00:00
|
|
|
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
|
2020-01-31 12:24:57 +00:00
|
|
|
) {
|
|
|
|
self.lookup(lint, Some(span), decorate);
|
2015-12-20 21:00:43 +00:00
|
|
|
}
|
2015-09-10 04:40:59 +00:00
|
|
|
/// Emit a lint at the appropriate level, with no associated span.
|
2020-02-02 09:41:14 +00:00
|
|
|
fn lint(&self, lint: &'static Lint, decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>)) {
|
|
|
|
self.lookup(lint, None as Option<Span>, decorate);
|
2015-09-10 04:40:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> EarlyContext<'a> {
|
2019-12-30 13:11:49 +00:00
|
|
|
pub fn new(
|
2018-07-14 14:40:17 +00:00
|
|
|
sess: &'a Session,
|
2019-10-09 12:46:11 +00:00
|
|
|
lint_store: &'a LintStore,
|
2018-07-14 14:40:17 +00:00
|
|
|
krate: &'a ast::Crate,
|
|
|
|
buffered: LintBuffer,
|
2019-10-25 17:41:51 +00:00
|
|
|
warn_about_weird_lints: bool,
|
2018-07-14 14:40:17 +00:00
|
|
|
) -> EarlyContext<'a> {
|
2015-09-10 04:40:59 +00:00
|
|
|
EarlyContext {
|
2017-07-03 18:19:51 +00:00
|
|
|
sess,
|
|
|
|
krate,
|
2019-10-09 12:46:11 +00:00
|
|
|
lint_store,
|
2020-01-09 03:06:33 +00:00
|
|
|
builder: LintLevelsBuilder::new(sess, warn_about_weird_lints, lint_store),
|
2018-07-14 14:40:17 +00:00
|
|
|
buffered,
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-18 06:40:55 +00:00
|
|
|
}
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl LintContext for LateContext<'_> {
|
2017-05-15 19:16:18 +00:00
|
|
|
type PassObject = LateLintPassObject;
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Gets the overall compiler `Session` object.
|
2015-09-10 04:40:59 +00:00
|
|
|
fn sess(&self) -> &Session {
|
|
|
|
&self.tcx.sess
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lints(&self) -> &LintStore {
|
2019-01-31 02:04:39 +00:00
|
|
|
&*self.lint_store
|
2015-09-10 04:40:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn lookup<S: Into<MultiSpan>>(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: Option<S>,
|
2020-01-31 12:24:57 +00:00
|
|
|
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
|
|
|
|
) {
|
2019-02-06 13:16:11 +00:00
|
|
|
let hir_id = self.last_node_with_lint_attrs;
|
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
match span {
|
2020-01-31 12:24:57 +00:00
|
|
|
Some(s) => self.tcx.struct_span_lint_hir(lint, hir_id, s, decorate),
|
|
|
|
None => self.tcx.struct_lint_node(lint, hir_id, decorate),
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 04:40:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 09:21:38 +00:00
|
|
|
impl LintContext for EarlyContext<'_> {
|
2017-05-15 19:16:18 +00:00
|
|
|
type PassObject = EarlyLintPassObject;
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Gets the overall compiler `Session` object.
|
2015-09-10 04:40:59 +00:00
|
|
|
fn sess(&self) -> &Session {
|
|
|
|
&self.sess
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lints(&self) -> &LintStore {
|
2019-01-31 02:04:39 +00:00
|
|
|
&*self.lint_store
|
2015-09-10 04:40:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn lookup<S: Into<MultiSpan>>(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: Option<S>,
|
2020-01-31 12:24:57 +00:00
|
|
|
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
|
|
|
|
) {
|
|
|
|
self.builder.struct_lint(lint, span.map(|s| s.into()), decorate)
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
2019-01-18 06:40:55 +00:00
|
|
|
}
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateContext<'tcx> {
|
2020-07-17 08:47:04 +00:00
|
|
|
/// Gets the type-checking results for the current body,
|
2020-06-26 02:55:23 +00:00
|
|
|
/// or `None` if outside a body.
|
2020-07-17 08:47:04 +00:00
|
|
|
pub fn maybe_typeck_results(&self) -> Option<&'tcx ty::TypeckResults<'tcx>> {
|
|
|
|
self.cached_typeck_results.get().or_else(|| {
|
2020-06-26 02:55:23 +00:00
|
|
|
self.enclosing_body.map(|body| {
|
2020-07-17 08:47:04 +00:00
|
|
|
let typeck_results = self.tcx.typeck_body(body);
|
|
|
|
self.cached_typeck_results.set(Some(typeck_results));
|
|
|
|
typeck_results
|
2020-06-25 23:56:23 +00:00
|
|
|
})
|
2020-06-26 02:55:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-17 08:47:04 +00:00
|
|
|
/// Gets the type-checking results for the current body.
|
2020-06-26 02:55:23 +00:00
|
|
|
/// As this will ICE if called outside bodies, only call when working with
|
|
|
|
/// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
|
|
|
|
#[track_caller]
|
2020-07-17 08:47:04 +00:00
|
|
|
pub fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
|
|
|
|
self.maybe_typeck_results().expect("`LateContext::typeck_results` called outside of body")
|
2020-06-26 02:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the final resolution of a `QPath`, or `Res::Err` if unavailable.
|
2020-07-17 08:47:04 +00:00
|
|
|
/// Unlike `.typeck_results().qpath_res(qpath, id)`, this can be used even outside
|
2020-06-26 02:55:23 +00:00
|
|
|
/// bodies (e.g. for paths in `hir::Ty`), without any risk of ICE-ing.
|
|
|
|
pub fn qpath_res(&self, qpath: &hir::QPath<'_>, id: hir::HirId) -> Res {
|
|
|
|
match *qpath {
|
|
|
|
hir::QPath::Resolved(_, ref path) => path.res,
|
2020-08-04 13:34:24 +00:00
|
|
|
hir::QPath::TypeRelative(..) | hir::QPath::LangItem(..) => self
|
2020-07-17 08:47:04 +00:00
|
|
|
.maybe_typeck_results()
|
|
|
|
.and_then(|typeck_results| typeck_results.type_dependent_def(id))
|
2020-06-26 02:55:23 +00:00
|
|
|
.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
|
2020-06-25 23:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:36:11 +00:00
|
|
|
pub fn current_lint_root(&self) -> hir::HirId {
|
|
|
|
self.last_node_with_lint_attrs
|
|
|
|
}
|
2019-04-05 21:04:56 +00:00
|
|
|
|
|
|
|
/// Check if a `DefId`'s path matches the given absolute type path usage.
|
2019-04-07 17:47:54 +00:00
|
|
|
///
|
2019-11-02 15:12:33 +00:00
|
|
|
/// Anonymous scopes such as `extern` imports are matched with `kw::Invalid`;
|
|
|
|
/// inherent `impl` blocks are matched with the name of the type.
|
|
|
|
///
|
2019-04-07 17:47:54 +00:00
|
|
|
/// # Examples
|
2019-05-02 16:33:59 +00:00
|
|
|
///
|
|
|
|
/// ```rust,ignore (no context or def id available)
|
2019-05-14 13:58:22 +00:00
|
|
|
/// if cx.match_def_path(def_id, &[sym::core, sym::option, sym::Option]) {
|
2019-04-07 17:47:54 +00:00
|
|
|
/// // The given `def_id` is that of an `Option` type
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-05-14 13:58:22 +00:00
|
|
|
pub fn match_def_path(&self, def_id: DefId, path: &[Symbol]) -> bool {
|
2019-04-07 17:47:54 +00:00
|
|
|
let names = self.get_def_path(def_id);
|
|
|
|
|
2019-05-14 13:58:22 +00:00
|
|
|
names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| a == b)
|
2019-04-07 17:47:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 13:58:22 +00:00
|
|
|
/// Gets the absolute path of `def_id` as a vector of `Symbol`.
|
2019-04-07 17:47:54 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
2019-05-02 16:33:59 +00:00
|
|
|
///
|
|
|
|
/// ```rust,ignore (no context or def id available)
|
2019-04-07 17:47:54 +00:00
|
|
|
/// let def_path = cx.get_def_path(def_id);
|
2019-05-14 13:58:22 +00:00
|
|
|
/// if let &[sym::core, sym::option, sym::Option] = &def_path[..] {
|
2019-04-07 17:47:54 +00:00
|
|
|
/// // The given `def_id` is that of an `Option` type
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-05-14 13:58:22 +00:00
|
|
|
pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
|
2019-06-11 19:03:44 +00:00
|
|
|
pub struct AbsolutePathPrinter<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2019-04-05 21:04:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
|
2019-04-05 21:04:56 +00:00
|
|
|
type Error = !;
|
|
|
|
|
2019-05-14 13:58:22 +00:00
|
|
|
type Path = Vec<Symbol>;
|
2019-04-05 21:04:56 +00:00
|
|
|
type Region = ();
|
|
|
|
type Type = ();
|
|
|
|
type DynExistential = ();
|
2019-03-18 10:50:57 +00:00
|
|
|
type Const = ();
|
2019-04-05 21:04:56 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2019-04-05 21:04:56 +00:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_dyn_existential(
|
|
|
|
self,
|
|
|
|
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
|
2019-03-18 10:50:57 +00:00
|
|
|
) -> Result<Self::DynExistential, Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn print_const(self, _ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
|
2019-04-05 21:04:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
|
2019-05-14 13:58:22 +00:00
|
|
|
Ok(vec![self.tcx.original_crate_name(cnum)])
|
2019-04-05 21:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn path_qualified(
|
|
|
|
self,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2019-05-14 13:58:22 +00:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-04-05 21:04:56 +00:00
|
|
|
if trait_ref.is_none() {
|
2019-09-16 18:08:35 +00:00
|
|
|
if let ty::Adt(def, substs) = self_ty.kind {
|
2019-04-05 21:04:56 +00:00
|
|
|
return self.print_def_path(def.did, substs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This shouldn't ever be needed, but just in case:
|
2020-09-02 07:40:56 +00:00
|
|
|
with_no_trimmed_paths(|| {
|
|
|
|
Ok(vec![match trait_ref {
|
|
|
|
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
|
|
|
|
None => Symbol::intern(&format!("<{}>", self_ty)),
|
|
|
|
}])
|
|
|
|
})
|
2019-04-05 21:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn path_append_impl(
|
|
|
|
self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
|
|
|
_disambiguated_data: &DisambiguatedDefPathData,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2019-05-14 13:58:22 +00:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-04-05 21:04:56 +00:00
|
|
|
let mut path = print_prefix(self)?;
|
|
|
|
|
|
|
|
// This shouldn't ever be needed, but just in case:
|
|
|
|
path.push(match trait_ref {
|
2020-09-02 07:40:56 +00:00
|
|
|
Some(trait_ref) => with_no_trimmed_paths(|| {
|
|
|
|
Symbol::intern(&format!(
|
|
|
|
"<impl {} for {}>",
|
|
|
|
trait_ref.print_only_trait_path(),
|
|
|
|
self_ty
|
|
|
|
))
|
|
|
|
}),
|
|
|
|
None => {
|
|
|
|
with_no_trimmed_paths(|| Symbol::intern(&format!("<impl {}>", self_ty)))
|
|
|
|
}
|
2019-04-05 21:04:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_append(
|
|
|
|
self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
|
|
|
disambiguated_data: &DisambiguatedDefPathData,
|
2019-05-14 13:58:22 +00:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-04-05 21:04:56 +00:00
|
|
|
let mut path = print_prefix(self)?;
|
|
|
|
|
|
|
|
// Skip `::{{constructor}}` on tuple/unit structs.
|
2020-03-22 12:36:56 +00:00
|
|
|
if let DefPathData::Ctor = disambiguated_data.data {
|
|
|
|
return Ok(path);
|
2019-04-05 21:04:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 03:25:08 +00:00
|
|
|
path.push(disambiguated_data.data.as_symbol());
|
2019-04-05 21:04:56 +00:00
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_generic_args(
|
|
|
|
self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-09-25 15:39:44 +00:00
|
|
|
_args: &[GenericArg<'tcx>],
|
2019-05-14 13:58:22 +00:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-04-05 21:04:56 +00:00
|
|
|
print_prefix(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
|
2019-04-05 21:04:56 +00:00
|
|
|
}
|
2019-01-31 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LayoutOf for LateContext<'tcx> {
|
2019-01-31 00:36:11 +00:00
|
|
|
type Ty = Ty<'tcx>;
|
2020-03-04 14:50:21 +00:00
|
|
|
type TyAndLayout = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
|
2019-01-31 00:36:11 +00:00
|
|
|
|
2020-03-04 14:50:21 +00:00
|
|
|
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
|
2019-01-31 00:36:11 +00:00
|
|
|
self.tcx.layout_of(self.param_env.and(ty))
|
|
|
|
}
|
|
|
|
}
|