3 use crate::ich::StableHashingContext;
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6 use rustc_errors::{DiagnosticBuilder, DiagnosticId};
8 use rustc_index::vec::IndexVec;
9 use rustc_session::lint::{
10 builtin::{self, FORBIDDEN_LINT_GROUPS},
11 FutureIncompatibilityReason, Level, Lint, LintId,
13 use rustc_session::{DiagnosticMessageId, Session};
14 use rustc_span::hygiene::MacroKind;
15 use rustc_span::source_map::{DesugaringKind, ExpnKind, MultiSpan};
16 use rustc_span::{symbol, Span, Symbol, DUMMY_SP};
18 /// How a lint level was set.
19 #[derive(Clone, Copy, PartialEq, Eq, HashStable, Debug)]
20 pub enum LintLevelSource {
21 /// Lint is at the default level as declared
22 /// in rustc or a plugin.
25 /// Lint level was set by an attribute.
26 Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
28 /// Lint level was set by a command-line flag.
29 /// The provided `Level` is the level specified on the command line.
30 /// (The actual level may be lower due to `--cap-lints`.)
31 CommandLine(Symbol, Level),
34 impl LintLevelSource {
35 pub fn name(&self) -> Symbol {
37 LintLevelSource::Default => symbol::kw::Default,
38 LintLevelSource::Node(name, _, _) => name,
39 LintLevelSource::CommandLine(name, _) => name,
43 pub fn span(&self) -> Span {
45 LintLevelSource::Default => DUMMY_SP,
46 LintLevelSource::Node(_, span, _) => span,
47 LintLevelSource::CommandLine(_, _) => DUMMY_SP,
52 /// A tuple of a lint level and its source.
53 pub type LevelAndSource = (Level, LintLevelSource);
55 #[derive(Debug, HashStable)]
56 pub struct LintLevelSets {
57 pub list: IndexVec<LintStackIndex, LintSet>,
61 rustc_index::newtype_index! {
63 pub struct LintStackIndex {
64 const COMMAND_LINE = 0,
68 #[derive(Debug, HashStable)]
70 // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
72 pub specs: FxHashMap<LintId, LevelAndSource>,
74 pub parent: LintStackIndex,
78 pub fn new() -> Self {
79 LintLevelSets { list: IndexVec::new(), lint_cap: Level::Forbid }
82 pub fn get_lint_level(
86 aux: Option<&FxHashMap<LintId, LevelAndSource>>,
89 let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
91 // If `level` is none then we actually assume the default level for this
93 let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
95 // If we're about to issue a warning, check at the last minute for any
96 // directives against the warnings "lint". If, for example, there's an
97 // `allow(warnings)` in scope then we want to respect that instead.
99 // We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
100 // triggers in cases (like #80988) where you have `forbid(warnings)`,
101 // and so if we turned that into an error, it'd defeat the purpose of the
102 // future compatibility warning.
103 if level == Level::Warn && LintId::of(lint) != LintId::of(FORBIDDEN_LINT_GROUPS) {
104 let (warnings_level, warnings_src) =
105 self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
106 if let Some(configured_warning_level) = warnings_level {
107 if configured_warning_level != Level::Warn {
108 level = configured_warning_level;
114 // Ensure that we never exceed the `--cap-lints` argument
115 // unless the source is a --force-warn
116 level = if let LintLevelSource::CommandLine(_, Level::ForceWarn) = src {
119 cmp::min(level, self.lint_cap)
122 if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
123 // Ensure that we never exceed driver level.
124 level = cmp::min(*driver_level, level);
130 pub fn get_lint_id_level(
133 mut idx: LintStackIndex,
134 aux: Option<&FxHashMap<LintId, LevelAndSource>>,
135 ) -> (Option<Level>, LintLevelSource) {
136 if let Some(specs) = aux {
137 if let Some(&(level, src)) = specs.get(&id) {
138 return (Some(level), src);
142 let LintSet { ref specs, parent } = self.list[idx];
143 if let Some(&(level, src)) = specs.get(&id) {
144 return (Some(level), src);
146 if idx == COMMAND_LINE {
147 return (None, LintLevelSource::Default);
155 pub struct LintLevelMap {
156 pub sets: LintLevelSets,
157 pub id_to_set: FxHashMap<HirId, LintStackIndex>,
161 /// If the `id` was previously registered with `register_id` when building
162 /// this `LintLevelMap` this returns the corresponding lint level and source
163 /// of the lint level for the lint provided.
165 /// If the `id` was not previously registered, returns `None`. If `None` is
166 /// returned then the parent of `id` should be acquired and this function
167 /// should be called again.
168 pub fn level_and_source(
173 ) -> Option<LevelAndSource> {
174 self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
178 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
180 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
181 let LintLevelMap { ref sets, ref id_to_set } = *self;
183 id_to_set.hash_stable(hcx, hasher);
185 hcx.while_hashing_spans(true, |hcx| sets.hash_stable(hcx, hasher))
189 pub struct LintDiagnosticBuilder<'a>(DiagnosticBuilder<'a>);
191 impl<'a> LintDiagnosticBuilder<'a> {
192 /// Return the inner DiagnosticBuilder, first setting the primary message to `msg`.
193 pub fn build(mut self, msg: &str) -> DiagnosticBuilder<'a> {
194 self.0.set_primary_message(msg);
198 /// Create a LintDiagnosticBuilder from some existing DiagnosticBuilder.
199 pub fn new(err: DiagnosticBuilder<'a>) -> LintDiagnosticBuilder<'a> {
200 LintDiagnosticBuilder(err)
204 pub fn struct_lint_level<'s, 'd>(
208 src: LintLevelSource,
209 span: Option<MultiSpan>,
210 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>) + 'd,
212 // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
214 fn struct_lint_level_impl(
218 src: LintLevelSource,
219 span: Option<MultiSpan>,
220 decorate: Box<dyn for<'b> FnOnce(LintDiagnosticBuilder<'b>) + 'd>,
222 // Check for future incompatibility lints and issue a stronger warning.
223 let lint_id = LintId::of(lint);
224 let future_incompatible = lint.future_incompatible;
226 let has_future_breakage = future_incompatible.map_or(
227 // Default allow lints trigger too often for testing.
228 sess.opts.debugging_opts.future_incompat_test && lint.default_level != Level::Allow,
230 matches!(incompat.reason, FutureIncompatibilityReason::FutureReleaseErrorReportNow)
234 let mut err = match (level, span) {
235 (Level::Allow, span) => {
236 if has_future_breakage {
237 if let Some(span) = span {
238 sess.struct_span_allow(span, "")
240 sess.struct_allow("")
246 (Level::Warn, Some(span)) => sess.struct_span_warn(span, ""),
247 (Level::Warn, None) => sess.struct_warn(""),
248 (Level::ForceWarn, Some(span)) => sess.struct_span_force_warn(span, ""),
249 (Level::ForceWarn, None) => sess.struct_force_warn(""),
250 (Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
251 (Level::Deny | Level::Forbid, None) => sess.struct_err(""),
254 // If this code originates in a foreign macro, aka something that this crate
255 // did not itself author, then it's likely that there's nothing this crate
256 // can do about it. We probably want to skip the lint entirely.
257 if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
258 // Any suggestions made here are likely to be incorrect, so anything we
259 // emit shouldn't be automatically fixed by rustfix.
260 err.allow_suggestions(false);
262 // If this is a future incompatible that is not an edition fixing lint
263 // it'll become a hard error, so we have to emit *something*. Also,
264 // if this lint occurs in the expansion of a macro from an external crate,
265 // allow individual lints to opt-out from being reported.
266 let not_future_incompatible =
267 future_incompatible.map(|f| f.reason.edition().is_some()).unwrap_or(true);
268 if not_future_incompatible && !lint.report_in_external_macro {
270 // Don't continue further, since we don't want to have
271 // `diag_span_note_once` called for a diagnostic that isn't emitted.
276 let name = lint.name_lower();
278 LintLevelSource::Default => {
281 DiagnosticMessageId::from(lint),
282 &format!("`#[{}({})]` on by default", level.as_str(), name),
285 LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
286 let flag = match orig_level {
289 Level::Forbid => "-F",
290 Level::Allow => "-A",
291 Level::ForceWarn => "--force-warn",
293 let hyphen_case_lint_name = name.replace("_", "-");
294 if lint_flag_val.as_str() == name {
297 DiagnosticMessageId::from(lint),
299 "requested on the command line with `{} {}`",
300 flag, hyphen_case_lint_name
304 let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
307 DiagnosticMessageId::from(lint),
309 "`{} {}` implied by `{} {}`",
310 flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
315 LintLevelSource::Node(lint_attr_name, src, reason) => {
316 if let Some(rationale) = reason {
317 err.note(&rationale.as_str());
319 sess.diag_span_note_once(
321 DiagnosticMessageId::from(lint),
323 "the lint level is defined here",
325 if lint_attr_name.as_str() != name {
326 let level_str = level.as_str();
329 DiagnosticMessageId::from(lint),
331 "`#[{}({})]` implied by `#[{}({})]`",
332 level_str, name, level_str, lint_attr_name
339 let is_force_warn = matches!(level, Level::ForceWarn);
340 err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn });
342 if let Some(future_incompatible) = future_incompatible {
343 let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
344 "once this associated item is added to the standard library, the ambiguity may \
345 cause an error or change in behavior!"
347 } else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) {
348 "this borrowing pattern was not meant to be accepted, and may become a hard error \
351 } else if let FutureIncompatibilityReason::EditionError(edition) =
352 future_incompatible.reason
354 let current_edition = sess.edition();
356 "this is accepted in the current edition (Rust {}) but is a hard error in Rust {}!",
357 current_edition, edition
359 } else if let FutureIncompatibilityReason::EditionSemanticsChange(edition) =
360 future_incompatible.reason
362 format!("this changes meaning in Rust {}", edition)
364 "this was previously accepted by the compiler but is being phased out; \
365 it will become a hard error in a future release!"
368 if future_incompatible.explain_reason {
369 err.warn(&explanation);
371 if !future_incompatible.reference.is_empty() {
373 format!("for more information, see {}", future_incompatible.reference);
378 // Finally, run `decorate`. This function is also responsible for emitting the diagnostic.
379 decorate(LintDiagnosticBuilder::new(err));
381 struct_lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
384 /// Returns whether `span` originates in a foreign crate's external macro.
386 /// This is used to test whether a lint should not even begin to figure out whether it should
387 /// be reported on the current node.
388 pub fn in_external_macro(sess: &Session, span: Span) -> bool {
389 let expn_data = span.ctxt().outer_expn_data();
390 match expn_data.kind {
391 ExpnKind::Inlined | ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => {
394 ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
395 ExpnKind::Macro(MacroKind::Bang, _) => {
396 // Dummy span for the `def_site` means it's an external macro.
397 expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site)
399 ExpnKind::Macro { .. } => true, // definitely a plugin