Auto merge of #102236 - cjgillot:compute_lint_levels_by_def, r=oli-obk
Compute lint levels by definition Second attempt to https://github.com/rust-lang/rust/pull/101620. I think that I have removed the perf regression.
This commit is contained in:
commit
57f097ea25
@ -96,6 +96,23 @@ impl<K: Ord, V> SortedMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to the value in the entry, or insert a new one.
|
||||
#[inline]
|
||||
pub fn get_mut_or_insert_default(&mut self, key: K) -> &mut V
|
||||
where
|
||||
K: Eq,
|
||||
V: Default,
|
||||
{
|
||||
let index = match self.lookup_index_for(&key) {
|
||||
Ok(index) => index,
|
||||
Err(index) => {
|
||||
self.data.insert(index, (key, V::default()));
|
||||
index
|
||||
}
|
||||
};
|
||||
unsafe { &mut self.data.get_unchecked_mut(index).1 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn clear(&mut self) {
|
||||
self.data.clear();
|
||||
|
@ -364,9 +364,10 @@ impl Diagnostic {
|
||||
// The lint index inside the attribute is manually transferred here.
|
||||
let lint_index = expectation_id.get_lint_index();
|
||||
expectation_id.set_lint_index(None);
|
||||
let mut stable_id = *unstable_to_stable
|
||||
let mut stable_id = unstable_to_stable
|
||||
.get(&expectation_id)
|
||||
.expect("each unstable `LintExpectationId` must have a matching stable id");
|
||||
.expect("each unstable `LintExpectationId` must have a matching stable id")
|
||||
.normalize();
|
||||
|
||||
stable_id.set_lint_index(lint_index);
|
||||
*expectation_id = stable_id;
|
||||
|
@ -1211,7 +1211,7 @@ impl HandlerInner {
|
||||
|
||||
if let Some(expectation_id) = diagnostic.level.get_expectation_id() {
|
||||
self.suppressed_expected_diag = true;
|
||||
self.fulfilled_expectations.insert(expectation_id);
|
||||
self.fulfilled_expectations.insert(expectation_id.normalize());
|
||||
}
|
||||
|
||||
if matches!(diagnostic.level, Warning(_))
|
||||
|
@ -558,7 +558,7 @@ pub struct LateContext<'tcx> {
|
||||
|
||||
/// Context for lint checking of the AST, after expansion, before lowering to HIR.
|
||||
pub struct EarlyContext<'a> {
|
||||
pub builder: LintLevelsBuilder<'a>,
|
||||
pub builder: LintLevelsBuilder<'a, crate::levels::TopDown>,
|
||||
pub buffered: LintBuffer,
|
||||
}
|
||||
|
||||
|
@ -58,6 +58,7 @@ impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
|
||||
F: FnOnce(&mut Self),
|
||||
{
|
||||
let is_crate_node = id == ast::CRATE_NODE_ID;
|
||||
debug!(?id);
|
||||
let push = self.context.builder.push(attrs, is_crate_node, None);
|
||||
|
||||
self.check_id(id);
|
||||
|
@ -16,8 +16,10 @@ fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
|
||||
return;
|
||||
}
|
||||
|
||||
let lint_expectations = tcx.lint_expectations(());
|
||||
let fulfilled_expectations = tcx.sess.diagnostic().steal_fulfilled_expectation_ids();
|
||||
let lint_expectations = &tcx.lint_levels(()).lint_expectations;
|
||||
|
||||
tracing::debug!(?lint_expectations, ?fulfilled_expectations);
|
||||
|
||||
for (id, expectation) in lint_expectations {
|
||||
// This check will always be true, since `lint_expectations` only
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -92,7 +92,7 @@ pub enum LintExpectationId {
|
||||
/// stable and can be cached. The additional index ensures that nodes with
|
||||
/// several expectations can correctly match diagnostics to the individual
|
||||
/// expectation.
|
||||
Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16> },
|
||||
Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16>, attr_id: Option<AttrId> },
|
||||
}
|
||||
|
||||
impl LintExpectationId {
|
||||
@ -116,13 +116,31 @@ impl LintExpectationId {
|
||||
|
||||
*lint_index = new_lint_index
|
||||
}
|
||||
|
||||
/// Prepares the id for hashing. Removes references to the ast.
|
||||
/// Should only be called when the id is stable.
|
||||
pub fn normalize(self) -> Self {
|
||||
match self {
|
||||
Self::Stable { hir_id, attr_index, lint_index, .. } => {
|
||||
Self::Stable { hir_id, attr_index, lint_index, attr_id: None }
|
||||
}
|
||||
Self::Unstable { .. } => {
|
||||
unreachable!("`normalize` called when `ExpectationId` is unstable")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<HCX: rustc_hir::HashStableContext> HashStable<HCX> for LintExpectationId {
|
||||
#[inline]
|
||||
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
|
||||
match self {
|
||||
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
|
||||
LintExpectationId::Stable {
|
||||
hir_id,
|
||||
attr_index,
|
||||
lint_index: Some(lint_index),
|
||||
attr_id: _,
|
||||
} => {
|
||||
hir_id.hash_stable(hcx, hasher);
|
||||
attr_index.hash_stable(hcx, hasher);
|
||||
lint_index.hash_stable(hcx, hasher);
|
||||
@ -142,9 +160,12 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
|
||||
#[inline]
|
||||
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
|
||||
match self {
|
||||
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
|
||||
(*hir_id, *attr_index, *lint_index)
|
||||
}
|
||||
LintExpectationId::Stable {
|
||||
hir_id,
|
||||
attr_index,
|
||||
lint_index: Some(lint_index),
|
||||
attr_id: _,
|
||||
} => (*hir_id, *attr_index, *lint_index),
|
||||
_ => {
|
||||
unreachable!("HashStable should only be called for a filled `LintExpectationId`")
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ use crate::ty::TyCtxt;
|
||||
use rustc_data_structures::fingerprint::Fingerprint;
|
||||
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
|
||||
use rustc_hir::definitions::DefPathHash;
|
||||
use rustc_hir::{HirId, OwnerId};
|
||||
use rustc_hir::{HirId, ItemLocalId, OwnerId};
|
||||
use rustc_query_system::dep_graph::FingerprintStyle;
|
||||
use rustc_span::symbol::Symbol;
|
||||
use std::hash::Hash;
|
||||
@ -194,7 +194,7 @@ impl DepNodeExt for DepNode {
|
||||
let kind = dep_kind_from_label_string(label)?;
|
||||
|
||||
match tcx.fingerprint_style(kind) {
|
||||
FingerprintStyle::Opaque => Err(()),
|
||||
FingerprintStyle::Opaque | FingerprintStyle::HirId => Err(()),
|
||||
FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
|
||||
FingerprintStyle::DefPathHash => {
|
||||
Ok(DepNode::from_def_path_hash(tcx, def_path_hash, kind))
|
||||
@ -344,7 +344,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
|
||||
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
|
||||
#[inline(always)]
|
||||
fn fingerprint_style() -> FingerprintStyle {
|
||||
FingerprintStyle::Opaque
|
||||
FingerprintStyle::HirId
|
||||
}
|
||||
|
||||
// We actually would not need to specialize the implementation of this
|
||||
@ -353,10 +353,36 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
|
||||
#[inline(always)]
|
||||
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
|
||||
let HirId { owner, local_id } = *self;
|
||||
|
||||
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
|
||||
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
|
||||
Fingerprint::new(
|
||||
// `owner` is local, so is completely defined by the local hash
|
||||
def_path_hash.local_hash(),
|
||||
local_id.as_u32().into(),
|
||||
)
|
||||
}
|
||||
|
||||
def_path_hash.0.combine(local_id)
|
||||
#[inline(always)]
|
||||
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
|
||||
let HirId { owner, local_id } = *self;
|
||||
format!("{}.{}", tcx.def_path_str(owner.to_def_id()), local_id.as_u32())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
|
||||
if tcx.fingerprint_style(dep_node.kind) == FingerprintStyle::HirId {
|
||||
let (local_hash, local_id) = Fingerprint::from(dep_node.hash).as_value();
|
||||
let def_path_hash = DefPathHash::new(tcx.sess.local_stable_crate_id(), local_hash);
|
||||
let def_id = tcx
|
||||
.def_path_hash_to_def_id(def_path_hash, &mut || {
|
||||
panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash)
|
||||
})
|
||||
.expect_local();
|
||||
let local_id = local_id
|
||||
.try_into()
|
||||
.unwrap_or_else(|_| panic!("local id should be u32, found {:?}", local_id));
|
||||
Some(HirId { owner: OwnerId { def_id }, local_id: ItemLocalId::from_u32(local_id) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ pub struct ParentHirIterator<'hir> {
|
||||
}
|
||||
|
||||
impl<'hir> Iterator for ParentHirIterator<'hir> {
|
||||
type Item = (HirId, Node<'hir>);
|
||||
type Item = HirId;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_id == CRATE_HIR_ID {
|
||||
@ -77,10 +77,7 @@ impl<'hir> Iterator for ParentHirIterator<'hir> {
|
||||
}
|
||||
|
||||
self.current_id = parent_id;
|
||||
if let Some(node) = self.map.find(parent_id) {
|
||||
return Some((parent_id, node));
|
||||
}
|
||||
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
|
||||
return Some(parent_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -393,8 +390,8 @@ impl<'hir> Map<'hir> {
|
||||
}
|
||||
|
||||
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
|
||||
for (parent, _) in self.parent_iter(hir_id) {
|
||||
if let Some(body) = self.find(parent).map(associated_body).flatten() {
|
||||
for (_, node) in self.parent_iter(hir_id) {
|
||||
if let Some(body) = associated_body(node) {
|
||||
return self.body_owner_def_id(body);
|
||||
}
|
||||
}
|
||||
@ -635,10 +632,17 @@ impl<'hir> Map<'hir> {
|
||||
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
|
||||
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
|
||||
#[inline]
|
||||
pub fn parent_iter(self, current_id: HirId) -> ParentHirIterator<'hir> {
|
||||
pub fn parent_id_iter(self, current_id: HirId) -> impl Iterator<Item = HirId> + 'hir {
|
||||
ParentHirIterator { current_id, map: self }
|
||||
}
|
||||
|
||||
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
|
||||
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
|
||||
#[inline]
|
||||
pub fn parent_iter(self, current_id: HirId) -> impl Iterator<Item = (HirId, Node<'hir>)> {
|
||||
self.parent_id_iter(current_id).filter_map(move |id| Some((id, self.find(id)?)))
|
||||
}
|
||||
|
||||
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
|
||||
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
|
||||
#[inline]
|
||||
|
@ -1,20 +1,20 @@
|
||||
use std::cmp;
|
||||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||
use rustc_data_structures::sorted_map::SortedMap;
|
||||
use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, MultiSpan};
|
||||
use rustc_hir::HirId;
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_query_system::ich::StableHashingContext;
|
||||
use rustc_hir::{HirId, ItemLocalId};
|
||||
use rustc_session::lint::{
|
||||
builtin::{self, FORBIDDEN_LINT_GROUPS},
|
||||
FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId,
|
||||
FutureIncompatibilityReason, Level, Lint, LintId,
|
||||
};
|
||||
use rustc_session::Session;
|
||||
use rustc_span::hygiene::MacroKind;
|
||||
use rustc_span::source_map::{DesugaringKind, ExpnKind};
|
||||
use rustc_span::{symbol, Span, Symbol, DUMMY_SP};
|
||||
|
||||
use crate::ty::TyCtxt;
|
||||
|
||||
/// How a lint level was set.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, HashStable, Debug)]
|
||||
pub enum LintLevelSource {
|
||||
@ -23,7 +23,12 @@ pub enum LintLevelSource {
|
||||
Default,
|
||||
|
||||
/// Lint level was set by an attribute.
|
||||
Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
|
||||
Node {
|
||||
name: Symbol,
|
||||
span: Span,
|
||||
/// RFC 2383 reason
|
||||
reason: Option<Symbol>,
|
||||
},
|
||||
|
||||
/// Lint level was set by a command-line flag.
|
||||
/// The provided `Level` is the level specified on the command line.
|
||||
@ -35,7 +40,7 @@ impl LintLevelSource {
|
||||
pub fn name(&self) -> Symbol {
|
||||
match *self {
|
||||
LintLevelSource::Default => symbol::kw::Default,
|
||||
LintLevelSource::Node(name, _, _) => name,
|
||||
LintLevelSource::Node { name, .. } => name,
|
||||
LintLevelSource::CommandLine(name, _) => name,
|
||||
}
|
||||
}
|
||||
@ -43,7 +48,7 @@ impl LintLevelSource {
|
||||
pub fn span(&self) -> Span {
|
||||
match *self {
|
||||
LintLevelSource::Default => DUMMY_SP,
|
||||
LintLevelSource::Node(_, span, _) => span,
|
||||
LintLevelSource::Node { span, .. } => span,
|
||||
LintLevelSource::CommandLine(_, _) => DUMMY_SP,
|
||||
}
|
||||
}
|
||||
@ -52,148 +57,140 @@ impl LintLevelSource {
|
||||
/// A tuple of a lint level and its source.
|
||||
pub type LevelAndSource = (Level, LintLevelSource);
|
||||
|
||||
#[derive(Debug, HashStable)]
|
||||
pub struct LintLevelSets {
|
||||
pub list: IndexVec<LintStackIndex, LintSet>,
|
||||
pub lint_cap: Level,
|
||||
/// Return type for the `shallow_lint_levels_on` query.
|
||||
///
|
||||
/// This map represents the set of allowed lints and allowance levels given
|
||||
/// by the attributes for *a single HirId*.
|
||||
#[derive(Default, Debug, HashStable)]
|
||||
pub struct ShallowLintLevelMap {
|
||||
pub specs: SortedMap<ItemLocalId, FxHashMap<LintId, LevelAndSource>>,
|
||||
}
|
||||
|
||||
rustc_index::newtype_index! {
|
||||
#[derive(HashStable)]
|
||||
pub struct LintStackIndex {
|
||||
const COMMAND_LINE = 0,
|
||||
}
|
||||
}
|
||||
/// From an initial level and source, verify the effect of special annotations:
|
||||
/// `warnings` lint level and lint caps.
|
||||
///
|
||||
/// The return of this function is suitable for diagnostics.
|
||||
pub fn reveal_actual_level(
|
||||
level: Option<Level>,
|
||||
src: &mut LintLevelSource,
|
||||
sess: &Session,
|
||||
lint: LintId,
|
||||
probe_for_lint_level: impl FnOnce(LintId) -> (Option<Level>, LintLevelSource),
|
||||
) -> Level {
|
||||
// If `level` is none then we actually assume the default level for this lint.
|
||||
let mut level = level.unwrap_or_else(|| lint.lint.default_level(sess.edition()));
|
||||
|
||||
#[derive(Debug, HashStable)]
|
||||
pub struct LintSet {
|
||||
// -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
|
||||
// flag.
|
||||
pub specs: FxHashMap<LintId, LevelAndSource>,
|
||||
|
||||
pub parent: LintStackIndex,
|
||||
}
|
||||
|
||||
impl LintLevelSets {
|
||||
pub fn new() -> Self {
|
||||
LintLevelSets { list: IndexVec::new(), lint_cap: Level::Forbid }
|
||||
// If we're about to issue a warning, check at the last minute for any
|
||||
// directives against the warnings "lint". If, for example, there's an
|
||||
// `allow(warnings)` in scope then we want to respect that instead.
|
||||
//
|
||||
// We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
|
||||
// triggers in cases (like #80988) where you have `forbid(warnings)`,
|
||||
// and so if we turned that into an error, it'd defeat the purpose of the
|
||||
// future compatibility warning.
|
||||
if level == Level::Warn && lint != LintId::of(FORBIDDEN_LINT_GROUPS) {
|
||||
let (warnings_level, warnings_src) = probe_for_lint_level(LintId::of(builtin::WARNINGS));
|
||||
if let Some(configured_warning_level) = warnings_level {
|
||||
if configured_warning_level != Level::Warn {
|
||||
level = configured_warning_level;
|
||||
*src = warnings_src;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_lint_level(
|
||||
// Ensure that we never exceed the `--cap-lints` argument unless the source is a --force-warn
|
||||
level = if let LintLevelSource::CommandLine(_, Level::ForceWarn(_)) = src {
|
||||
level
|
||||
} else {
|
||||
cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
|
||||
};
|
||||
|
||||
if let Some(driver_level) = sess.driver_lint_caps.get(&lint) {
|
||||
// Ensure that we never exceed driver level.
|
||||
level = cmp::min(*driver_level, level);
|
||||
}
|
||||
|
||||
level
|
||||
}
|
||||
|
||||
impl ShallowLintLevelMap {
|
||||
/// Perform a deep probe in the HIR tree looking for the actual level for the lint.
|
||||
/// This lint level is not usable for diagnostics, it needs to be corrected by
|
||||
/// `reveal_actual_level` beforehand.
|
||||
#[instrument(level = "trace", skip(self, tcx), ret)]
|
||||
fn probe_for_lint_level(
|
||||
&self,
|
||||
lint: &'static Lint,
|
||||
idx: LintStackIndex,
|
||||
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
|
||||
sess: &Session,
|
||||
) -> LevelAndSource {
|
||||
let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
|
||||
tcx: TyCtxt<'_>,
|
||||
id: LintId,
|
||||
start: HirId,
|
||||
) -> (Option<Level>, LintLevelSource) {
|
||||
if let Some(map) = self.specs.get(&start.local_id)
|
||||
&& let Some(&(level, src)) = map.get(&id)
|
||||
{
|
||||
return (Some(level), src);
|
||||
}
|
||||
|
||||
// If `level` is none then we actually assume the default level for this
|
||||
// lint.
|
||||
let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
|
||||
let mut owner = start.owner;
|
||||
let mut specs = &self.specs;
|
||||
|
||||
// If we're about to issue a warning, check at the last minute for any
|
||||
// directives against the warnings "lint". If, for example, there's an
|
||||
// `allow(warnings)` in scope then we want to respect that instead.
|
||||
//
|
||||
// We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
|
||||
// triggers in cases (like #80988) where you have `forbid(warnings)`,
|
||||
// and so if we turned that into an error, it'd defeat the purpose of the
|
||||
// future compatibility warning.
|
||||
if level == Level::Warn && LintId::of(lint) != LintId::of(FORBIDDEN_LINT_GROUPS) {
|
||||
let (warnings_level, warnings_src) =
|
||||
self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
|
||||
if let Some(configured_warning_level) = warnings_level {
|
||||
if configured_warning_level != Level::Warn {
|
||||
level = configured_warning_level;
|
||||
src = warnings_src;
|
||||
}
|
||||
for parent in tcx.hir().parent_id_iter(start) {
|
||||
if parent.owner != owner {
|
||||
owner = parent.owner;
|
||||
specs = &tcx.shallow_lint_levels_on(owner).specs;
|
||||
}
|
||||
if let Some(map) = specs.get(&parent.local_id)
|
||||
&& let Some(&(level, src)) = map.get(&id)
|
||||
{
|
||||
return (Some(level), src);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure that we never exceed the `--cap-lints` argument
|
||||
// unless the source is a --force-warn
|
||||
level = if let LintLevelSource::CommandLine(_, Level::ForceWarn(_)) = src {
|
||||
level
|
||||
} else {
|
||||
cmp::min(level, self.lint_cap)
|
||||
};
|
||||
|
||||
if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
|
||||
// Ensure that we never exceed driver level.
|
||||
level = cmp::min(*driver_level, level);
|
||||
}
|
||||
(None, LintLevelSource::Default)
|
||||
}
|
||||
|
||||
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
|
||||
#[instrument(level = "trace", skip(self, tcx), ret)]
|
||||
pub fn lint_level_id_at_node(
|
||||
&self,
|
||||
tcx: TyCtxt<'_>,
|
||||
lint: LintId,
|
||||
cur: HirId,
|
||||
) -> (Level, LintLevelSource) {
|
||||
let (level, mut src) = self.probe_for_lint_level(tcx, lint, cur);
|
||||
let level = reveal_actual_level(level, &mut src, tcx.sess, lint, |lint| {
|
||||
self.probe_for_lint_level(tcx, lint, cur)
|
||||
});
|
||||
(level, src)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_lint_id_level(
|
||||
&self,
|
||||
id: LintId,
|
||||
mut idx: LintStackIndex,
|
||||
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
|
||||
) -> (Option<Level>, LintLevelSource) {
|
||||
if let Some(specs) = aux {
|
||||
if let Some(&(level, src)) = specs.get(&id) {
|
||||
return (Some(level), src);
|
||||
}
|
||||
}
|
||||
impl TyCtxt<'_> {
|
||||
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
|
||||
pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> (Level, LintLevelSource) {
|
||||
self.shallow_lint_levels_on(id.owner).lint_level_id_at_node(self, LintId::of(lint), id)
|
||||
}
|
||||
|
||||
/// Walks upwards from `id` to find a node which might change lint levels with attributes.
|
||||
/// It stops at `bound` and just returns it if reached.
|
||||
pub fn maybe_lint_level_root_bounded(self, mut id: HirId, bound: HirId) -> HirId {
|
||||
let hir = self.hir();
|
||||
loop {
|
||||
let LintSet { ref specs, parent } = self.list[idx];
|
||||
if let Some(&(level, src)) = specs.get(&id) {
|
||||
return (Some(level), src);
|
||||
if id == bound {
|
||||
return bound;
|
||||
}
|
||||
if idx == COMMAND_LINE {
|
||||
return (None, LintLevelSource::Default);
|
||||
|
||||
if hir.attrs(id).iter().any(|attr| Level::from_attr(attr).is_some()) {
|
||||
return id;
|
||||
}
|
||||
idx = parent;
|
||||
let next = hir.get_parent_node(id);
|
||||
if next == id {
|
||||
bug!("lint traversal reached the root of the crate");
|
||||
}
|
||||
id = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LintLevelMap {
|
||||
/// This is a collection of lint expectations as described in RFC 2383, that
|
||||
/// can be fulfilled during this compilation session. This means that at least
|
||||
/// one expected lint is currently registered in the lint store.
|
||||
///
|
||||
/// The [`LintExpectationId`] is stored as a part of the [`Expect`](Level::Expect)
|
||||
/// lint level.
|
||||
pub lint_expectations: Vec<(LintExpectationId, LintExpectation)>,
|
||||
pub sets: LintLevelSets,
|
||||
pub id_to_set: FxHashMap<HirId, LintStackIndex>,
|
||||
}
|
||||
|
||||
impl LintLevelMap {
|
||||
/// If the `id` was previously registered with `register_id` when building
|
||||
/// this `LintLevelMap` this returns the corresponding lint level and source
|
||||
/// of the lint level for the lint provided.
|
||||
///
|
||||
/// If the `id` was not previously registered, returns `None`. If `None` is
|
||||
/// returned then the parent of `id` should be acquired and this function
|
||||
/// should be called again.
|
||||
pub fn level_and_source(
|
||||
&self,
|
||||
lint: &'static Lint,
|
||||
id: HirId,
|
||||
session: &Session,
|
||||
) -> Option<LevelAndSource> {
|
||||
self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
|
||||
#[inline]
|
||||
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
||||
let LintLevelMap { ref sets, ref id_to_set, ref lint_expectations } = *self;
|
||||
|
||||
id_to_set.hash_stable(hcx, hasher);
|
||||
lint_expectations.hash_stable(hcx, hasher);
|
||||
|
||||
hcx.while_hashing_spans(true, |hcx| sets.hash_stable(hcx, hasher))
|
||||
}
|
||||
}
|
||||
|
||||
/// This struct represents a lint expectation and holds all required information
|
||||
/// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
|
||||
/// the `LateLintPass` has completed.
|
||||
@ -261,11 +258,11 @@ pub fn explain_lint_level_source(
|
||||
));
|
||||
}
|
||||
}
|
||||
LintLevelSource::Node(lint_attr_name, src, reason) => {
|
||||
LintLevelSource::Node { name: lint_attr_name, span, reason, .. } => {
|
||||
if let Some(rationale) = reason {
|
||||
err.note(rationale.as_str());
|
||||
}
|
||||
err.span_note_once(src, "the lint level is defined here");
|
||||
err.span_note_once(span, "the lint level is defined here");
|
||||
if lint_attr_name.as_str() != name {
|
||||
let level_str = level.as_str();
|
||||
err.note_once(&format!(
|
||||
|
@ -274,10 +274,15 @@ rustc_queries! {
|
||||
separate_provide_extern
|
||||
}
|
||||
|
||||
query lint_levels(_: ()) -> LintLevelMap {
|
||||
query shallow_lint_levels_on(key: hir::OwnerId) -> rustc_middle::lint::ShallowLintLevelMap {
|
||||
eval_always // fetches `resolutions`
|
||||
arena_cache
|
||||
eval_always
|
||||
desc { "computing the lint levels for items in this crate" }
|
||||
desc { |tcx| "looking up lint levels for `{}`", tcx.def_path_str(key.to_def_id()) }
|
||||
}
|
||||
|
||||
query lint_expectations(_: ()) -> Vec<(LintExpectationId, LintExpectation)> {
|
||||
arena_cache
|
||||
desc { "computing `#[expect]`ed lints in this crate" }
|
||||
}
|
||||
|
||||
query parent_module_from_def_id(key: LocalDefId) -> LocalDefId {
|
||||
|
@ -4,7 +4,7 @@ use crate::arena::Arena;
|
||||
use crate::dep_graph::{DepGraph, DepKindStruct};
|
||||
use crate::hir::place::Place as HirPlace;
|
||||
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
|
||||
use crate::lint::{struct_lint_level, LintLevelSource};
|
||||
use crate::lint::struct_lint_level;
|
||||
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
|
||||
use crate::middle::resolve_lifetime;
|
||||
use crate::middle::stability;
|
||||
@ -57,7 +57,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
|
||||
use rustc_session::config::{CrateType, OutputFilenames};
|
||||
use rustc_session::cstore::CrateStoreDyn;
|
||||
use rustc_session::errors::TargetDataLayoutErrorsWrapper;
|
||||
use rustc_session::lint::{Level, Lint};
|
||||
use rustc_session::lint::Lint;
|
||||
use rustc_session::Limit;
|
||||
use rustc_session::Session;
|
||||
use rustc_span::def_id::{DefPathHash, StableCrateId};
|
||||
@ -2812,44 +2812,6 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
iter.intern_with(|xs| self.intern_bound_variable_kinds(xs))
|
||||
}
|
||||
|
||||
/// Walks upwards from `id` to find a node which might change lint levels with attributes.
|
||||
/// It stops at `bound` and just returns it if reached.
|
||||
pub fn maybe_lint_level_root_bounded(self, mut id: HirId, bound: HirId) -> HirId {
|
||||
let hir = self.hir();
|
||||
loop {
|
||||
if id == bound {
|
||||
return bound;
|
||||
}
|
||||
|
||||
if hir.attrs(id).iter().any(|attr| Level::from_attr(attr).is_some()) {
|
||||
return id;
|
||||
}
|
||||
let next = hir.get_parent_node(id);
|
||||
if next == id {
|
||||
bug!("lint traversal reached the root of the crate");
|
||||
}
|
||||
id = next;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lint_level_at_node(
|
||||
self,
|
||||
lint: &'static Lint,
|
||||
mut id: hir::HirId,
|
||||
) -> (Level, LintLevelSource) {
|
||||
let sets = self.lint_levels(());
|
||||
loop {
|
||||
if let Some(pair) = sets.level_and_source(lint, id, self.sess) {
|
||||
return pair;
|
||||
}
|
||||
let next = self.hir().get_parent_node(id);
|
||||
if next == id {
|
||||
bug!("lint traversal reached the root of the crate");
|
||||
}
|
||||
id = next;
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a lint at `span` from a lint struct (some type that implements `DecorateLint`,
|
||||
/// typically generated by `#[derive(LintDiagnostic)]`).
|
||||
pub fn emit_spanned_lint(
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::dep_graph;
|
||||
use crate::infer::canonical::{self, Canonical};
|
||||
use crate::lint::LintLevelMap;
|
||||
use crate::lint::LintExpectation;
|
||||
use crate::metadata::ModChild;
|
||||
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
|
||||
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
|
||||
@ -51,6 +51,7 @@ use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
|
||||
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
|
||||
use rustc_session::cstore::{CrateDepKind, CrateSource};
|
||||
use rustc_session::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLib};
|
||||
use rustc_session::lint::LintExpectationId;
|
||||
use rustc_session::utils::NativeLibKind;
|
||||
use rustc_session::Limits;
|
||||
use rustc_span::symbol::Symbol;
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Defines the set of legal keys that can be used in queries.
|
||||
|
||||
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
|
||||
use rustc_hir::hir_id::OwnerId;
|
||||
use rustc_hir::hir_id::{HirId, OwnerId};
|
||||
use rustc_middle::infer::canonical::Canonical;
|
||||
use rustc_middle::mir;
|
||||
use rustc_middle::traits;
|
||||
@ -557,3 +557,19 @@ impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
|
||||
DUMMY_SP
|
||||
}
|
||||
}
|
||||
|
||||
impl Key for HirId {
|
||||
#[inline(always)]
|
||||
fn query_crate_is_local(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
||||
tcx.hir().span(*self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn key_as_def_id(&self) -> Option<DefId> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
@ -94,6 +94,8 @@ impl<T: DepContext> HasDepContext for T {
|
||||
pub enum FingerprintStyle {
|
||||
/// The fingerprint is actually a DefPathHash.
|
||||
DefPathHash,
|
||||
/// The fingerprint is actually a HirId.
|
||||
HirId,
|
||||
/// Query key was `()` or equivalent, so fingerprint is just zero.
|
||||
Unit,
|
||||
/// Some opaque hash.
|
||||
@ -104,7 +106,9 @@ impl FingerprintStyle {
|
||||
#[inline]
|
||||
pub fn reconstructible(self) -> bool {
|
||||
match self {
|
||||
FingerprintStyle::DefPathHash | FingerprintStyle::Unit => true,
|
||||
FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
|
||||
true
|
||||
}
|
||||
FingerprintStyle::Opaque => false,
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,3 @@
|
||||
warning: denote infinite loops with `loop { ... }`
|
||||
--> $DIR/force_warn_expected_lints_fulfilled.rs:10:5
|
||||
|
|
||||
LL | while true {
|
||||
| ^^^^^^^^^^ help: use `loop`
|
||||
|
|
||||
= note: requested on the command line with `--force-warn while-true`
|
||||
|
||||
warning: unused variable: `x`
|
||||
--> $DIR/force_warn_expected_lints_fulfilled.rs:20:9
|
||||
|
|
||||
@ -36,5 +28,13 @@ LL | let mut what_does_the_fox_say = "*ding* *deng* *dung*";
|
||||
|
|
||||
= note: requested on the command line with `--force-warn unused-mut`
|
||||
|
||||
warning: denote infinite loops with `loop { ... }`
|
||||
--> $DIR/force_warn_expected_lints_fulfilled.rs:10:5
|
||||
|
|
||||
LL | while true {
|
||||
| ^^^^^^^^^^ help: use `loop`
|
||||
|
|
||||
= note: requested on the command line with `--force-warn while-true`
|
||||
|
||||
warning: 5 warnings emitted
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user