diff options
| author | Deadbeef <ent3rm4n@gmail.com> | 2022-07-22 16:48:36 +0000 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2022-09-14 19:02:44 +0200 |
| commit | eb19a8a6208d992d6d4e661be8fa80c1eeab01f6 (patch) | |
| tree | 61095efad35d8de4b0096fb90dfa234c7d19f192 /compiler/rustc_middle/src | |
| parent | c97922dca563cb7f9385b18dbf7ca8c97f8e1597 (diff) | |
| download | rust-eb19a8a6208d992d6d4e661be8fa80c1eeab01f6.tar.gz rust-eb19a8a6208d992d6d4e661be8fa80c1eeab01f6.zip | |
Compute `lint_levels` by definition
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/lint.rs | 111 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/query/mod.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 23 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/query.rs | 4 |
4 files changed, 112 insertions, 36 deletions
diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index 2f45222de47..c659e677fa0 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -15,6 +15,8 @@ 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 +25,14 @@ 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>, + /// The lint tool. (e.g. rustdoc, clippy) + tool: Option<Symbol>, + }, /// Lint level was set by a command-line flag. /// The provided `Level` is the level specified on the command line. @@ -35,7 +44,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 +52,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, } } @@ -55,7 +64,6 @@ pub type LevelAndSource = (Level, LintLevelSource); #[derive(Debug, HashStable)] pub struct LintLevelSets { pub list: IndexVec<LintStackIndex, LintSet>, - pub lint_cap: Level, } rustc_index::newtype_index! { @@ -76,18 +84,16 @@ pub struct LintSet { impl LintLevelSets { pub fn new() -> Self { - LintLevelSets { list: IndexVec::new(), lint_cap: Level::Forbid } + LintLevelSets { list: IndexVec::new() } } - pub fn get_lint_level( - &self, - lint: &'static Lint, - idx: LintStackIndex, - aux: Option<&FxHashMap<LintId, LevelAndSource>>, + pub fn actual_level( + level: Option<Level>, + src: &mut LintLevelSource, sess: &Session, - ) -> LevelAndSource { - let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux); - + lint: &'static Lint, + get_lint_id_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.default_level(sess.edition())); @@ -101,12 +107,11 @@ impl LintLevelSets { // 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); + let (warnings_level, warnings_src) = get_lint_id_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; + *src = warnings_src; } } } @@ -116,7 +121,7 @@ impl LintLevelSets { level = if let LintLevelSource::CommandLine(_, Level::ForceWarn(_)) = src { level } else { - cmp::min(level, self.lint_cap) + cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid)) }; if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) { @@ -124,6 +129,22 @@ impl LintLevelSets { level = cmp::min(*driver_level, level); } + level + } + + pub fn get_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); + + let level = Self::actual_level(level, &mut src, sess, lint, |id| { + self.get_lint_id_level(id, idx, aux) + }); + (level, src) } @@ -193,6 +214,58 @@ impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap { hcx.while_hashing_spans(true, |hcx| sets.hash_stable(hcx, hasher)) } } +pub struct LintLevelQueryMap<'tcx> { + pub tcx: TyCtxt<'tcx>, + pub cur: HirId, + pub specs: FxHashMap<LintId, LevelAndSource>, +} + +impl<'tcx> LintLevelQueryMap<'tcx> { + pub fn lint_id_level(&self, id: LintId) -> (Option<Level>, LintLevelSource) { + Self::get_lint_id_level(id, self.cur, self.tcx, &self.specs) + } + + pub fn lint_level(&self, lint: &'static Lint) -> LevelAndSource { + Self::get_lint_level(LintId::of(lint), self.cur, self.tcx, &self.specs) + } + + pub fn get_lint_id_level( + id: LintId, + cur: HirId, + tcx: TyCtxt<'tcx>, + specs: &FxHashMap<LintId, LevelAndSource>, + ) -> (Option<Level>, LintLevelSource) { + if let Some(&(level, src)) = specs.get(&id) { + return (Some(level), src); + } + let mut cur = cur; + + loop { + let parent = tcx.hir().get_parent_node(cur); + if cur == parent { + return (None, LintLevelSource::Default); + } + let specs = tcx.lint_levels_on(parent); + if let Some(&(level, src)) = specs.get(&id) { + return (Some(level), src); + } + cur = parent + } + } + + pub fn get_lint_level( + id: LintId, + cur: HirId, + tcx: TyCtxt<'tcx>, + specs: &FxHashMap<LintId, LevelAndSource>, + ) -> (Level, LintLevelSource) { + let (level, mut src) = Self::get_lint_id_level(id, cur, tcx, specs); + let level = LintLevelSets::actual_level(level, &mut src, tcx.sess, id.lint, |id| { + Self::get_lint_id_level(id, cur, tcx, specs) + }); + (level, src) + } +} /// This struct represents a lint expectation and holds all required information /// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after @@ -261,11 +334,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!( diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 9cb19583121..09c336f9e4c 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -273,10 +273,14 @@ rustc_queries! { separate_provide_extern } - query lint_levels(_: ()) -> LintLevelMap { + query lint_levels_on(key: HirId) -> FxHashMap<LintId, LevelAndSource> { arena_cache - eval_always - desc { "computing the lint levels for items in this crate" } + desc { |tcx| "looking up lint levels for `{}`", key } + } + + 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 { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 94eddef944a..4cd9004fa18 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -53,7 +53,7 @@ use rustc_query_system::ich::StableHashingContext; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{CrateType, OutputFilenames}; use rustc_session::cstore::CrateStoreDyn; -use rustc_session::lint::{Level, Lint}; +use rustc_session::lint::{Level, Lint, LintId}; use rustc_session::Limit; use rustc_session::Session; use rustc_span::def_id::{DefPathHash, StableCrateId}; @@ -2832,19 +2832,16 @@ impl<'tcx> TyCtxt<'tcx> { pub fn lint_level_at_node( self, lint: &'static Lint, - mut id: hir::HirId, + 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; - } + let level_and_src = crate::lint::LintLevelQueryMap::get_lint_level( + LintId::of(lint), + id, + self, + self.lint_levels_on(id), + ); + debug!(?id, ?level_and_src); + level_and_src } /// Emit a lint at `span` from a lint struct (some type that implements `DecorateLint`, diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index a300a8df23d..1a1b020905b 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -1,6 +1,6 @@ use crate::dep_graph; use crate::infer::canonical::{self, Canonical}; -use crate::lint::LintLevelMap; +use crate::lint::{LevelAndSource, LintExpectation}; use crate::metadata::ModChild; use crate::middle::codegen_fn_attrs::CodegenFnAttrs; use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; @@ -44,12 +44,14 @@ use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId}; +use rustc_hir::hir_id::HirId; use rustc_hir::lang_items::{LangItem, LanguageItems}; use rustc_hir::{Crate, ItemLocalId, TraitCandidate}; 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, LintId}; use rustc_session::utils::NativeLibKind; use rustc_session::Limits; use rustc_span::symbol::Symbol; |
