]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/lint.rs
Rollup merge of #93692 - mfrw:mfrw/document-keyword-in, r=dtolnay
[rust.git] / compiler / rustc_middle / src / lint.rs
1 use std::cmp;
2
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5 use rustc_errors::{
6     Diagnostic, DiagnosticBuilder, DiagnosticId, EmissionGuarantee, ErrorGuaranteed,
7 };
8 use rustc_hir::HirId;
9 use rustc_index::vec::IndexVec;
10 use rustc_query_system::ich::StableHashingContext;
11 use rustc_session::lint::{
12     builtin::{self, FORBIDDEN_LINT_GROUPS},
13     FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId,
14 };
15 use rustc_session::{DiagnosticMessageId, Session};
16 use rustc_span::hygiene::MacroKind;
17 use rustc_span::source_map::{DesugaringKind, ExpnKind, MultiSpan};
18 use rustc_span::{symbol, Span, Symbol, DUMMY_SP};
19
20 /// How a lint level was set.
21 #[derive(Clone, Copy, PartialEq, Eq, HashStable, Debug)]
22 pub enum LintLevelSource {
23     /// Lint is at the default level as declared
24     /// in rustc or a plugin.
25     Default,
26
27     /// Lint level was set by an attribute.
28     Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
29
30     /// Lint level was set by a command-line flag.
31     /// The provided `Level` is the level specified on the command line.
32     /// (The actual level may be lower due to `--cap-lints`.)
33     CommandLine(Symbol, Level),
34 }
35
36 impl LintLevelSource {
37     pub fn name(&self) -> Symbol {
38         match *self {
39             LintLevelSource::Default => symbol::kw::Default,
40             LintLevelSource::Node(name, _, _) => name,
41             LintLevelSource::CommandLine(name, _) => name,
42         }
43     }
44
45     pub fn span(&self) -> Span {
46         match *self {
47             LintLevelSource::Default => DUMMY_SP,
48             LintLevelSource::Node(_, span, _) => span,
49             LintLevelSource::CommandLine(_, _) => DUMMY_SP,
50         }
51     }
52 }
53
54 /// A tuple of a lint level and its source.
55 pub type LevelAndSource = (Level, LintLevelSource);
56
57 #[derive(Debug, HashStable)]
58 pub struct LintLevelSets {
59     pub list: IndexVec<LintStackIndex, LintSet>,
60     pub lint_cap: Level,
61 }
62
63 rustc_index::newtype_index! {
64     #[derive(HashStable)]
65     pub struct LintStackIndex {
66         const COMMAND_LINE = 0,
67     }
68 }
69
70 #[derive(Debug, HashStable)]
71 pub struct LintSet {
72     // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
73     // flag.
74     pub specs: FxHashMap<LintId, LevelAndSource>,
75
76     pub parent: LintStackIndex,
77 }
78
79 impl LintLevelSets {
80     pub fn new() -> Self {
81         LintLevelSets { list: IndexVec::new(), lint_cap: Level::Forbid }
82     }
83
84     pub fn get_lint_level(
85         &self,
86         lint: &'static Lint,
87         idx: LintStackIndex,
88         aux: Option<&FxHashMap<LintId, LevelAndSource>>,
89         sess: &Session,
90     ) -> LevelAndSource {
91         let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
92
93         // If `level` is none then we actually assume the default level for this
94         // lint.
95         let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
96
97         // If we're about to issue a warning, check at the last minute for any
98         // directives against the warnings "lint". If, for example, there's an
99         // `allow(warnings)` in scope then we want to respect that instead.
100         //
101         // We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
102         // triggers in cases (like #80988) where you have `forbid(warnings)`,
103         // and so if we turned that into an error, it'd defeat the purpose of the
104         // future compatibility warning.
105         if level == Level::Warn && LintId::of(lint) != LintId::of(FORBIDDEN_LINT_GROUPS) {
106             let (warnings_level, warnings_src) =
107                 self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
108             if let Some(configured_warning_level) = warnings_level {
109                 if configured_warning_level != Level::Warn {
110                     level = configured_warning_level;
111                     src = warnings_src;
112                 }
113             }
114         }
115
116         // Ensure that we never exceed the `--cap-lints` argument
117         // unless the source is a --force-warn
118         level = if let LintLevelSource::CommandLine(_, Level::ForceWarn) = src {
119             level
120         } else {
121             cmp::min(level, self.lint_cap)
122         };
123
124         if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
125             // Ensure that we never exceed driver level.
126             level = cmp::min(*driver_level, level);
127         }
128
129         (level, src)
130     }
131
132     pub fn get_lint_id_level(
133         &self,
134         id: LintId,
135         mut idx: LintStackIndex,
136         aux: Option<&FxHashMap<LintId, LevelAndSource>>,
137     ) -> (Option<Level>, LintLevelSource) {
138         if let Some(specs) = aux {
139             if let Some(&(level, src)) = specs.get(&id) {
140                 return (Some(level), src);
141             }
142         }
143         loop {
144             let LintSet { ref specs, parent } = self.list[idx];
145             if let Some(&(level, src)) = specs.get(&id) {
146                 return (Some(level), src);
147             }
148             if idx == COMMAND_LINE {
149                 return (None, LintLevelSource::Default);
150             }
151             idx = parent;
152         }
153     }
154 }
155
156 #[derive(Debug)]
157 pub struct LintLevelMap {
158     /// This is a collection of lint expectations as described in RFC 2383, that
159     /// can be fulfilled during this compilation session. This means that at least
160     /// one expected lint is currently registered in the lint store.
161     ///
162     /// The [`LintExpectationId`] is stored as a part of the [`Expect`](Level::Expect)
163     /// lint level.
164     pub lint_expectations: Vec<(LintExpectationId, LintExpectation)>,
165     pub sets: LintLevelSets,
166     pub id_to_set: FxHashMap<HirId, LintStackIndex>,
167 }
168
169 impl LintLevelMap {
170     /// If the `id` was previously registered with `register_id` when building
171     /// this `LintLevelMap` this returns the corresponding lint level and source
172     /// of the lint level for the lint provided.
173     ///
174     /// If the `id` was not previously registered, returns `None`. If `None` is
175     /// returned then the parent of `id` should be acquired and this function
176     /// should be called again.
177     pub fn level_and_source(
178         &self,
179         lint: &'static Lint,
180         id: HirId,
181         session: &Session,
182     ) -> Option<LevelAndSource> {
183         self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
184     }
185 }
186
187 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
188     #[inline]
189     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
190         let LintLevelMap { ref sets, ref id_to_set, ref lint_expectations } = *self;
191
192         id_to_set.hash_stable(hcx, hasher);
193         lint_expectations.hash_stable(hcx, hasher);
194
195         hcx.while_hashing_spans(true, |hcx| sets.hash_stable(hcx, hasher))
196     }
197 }
198
199 /// This struct represents a lint expectation and holds all required information
200 /// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
201 /// the `LateLintPass` has completed.
202 #[derive(Clone, Debug, HashStable)]
203 pub struct LintExpectation {
204     /// The reason for this expectation that can optionally be added as part of
205     /// the attribute. It will be displayed as part of the lint message.
206     pub reason: Option<Symbol>,
207     /// The [`Span`] of the attribute that this expectation originated from.
208     pub emission_span: Span,
209     /// Lint messages for the `unfulfilled_lint_expectations` lint will be
210     /// adjusted to include an additional note. Therefore, we have to track if
211     /// the expectation is for the lint.
212     pub is_unfulfilled_lint_expectations: bool,
213 }
214
215 impl LintExpectation {
216     pub fn new(
217         reason: Option<Symbol>,
218         emission_span: Span,
219         is_unfulfilled_lint_expectations: bool,
220     ) -> Self {
221         Self { reason, emission_span, is_unfulfilled_lint_expectations }
222     }
223 }
224
225 pub struct LintDiagnosticBuilder<'a, G: EmissionGuarantee>(DiagnosticBuilder<'a, G>);
226
227 impl<'a, G: EmissionGuarantee> LintDiagnosticBuilder<'a, G> {
228     /// Return the inner `DiagnosticBuilder`, first setting the primary message to `msg`.
229     pub fn build(mut self, msg: &str) -> DiagnosticBuilder<'a, G> {
230         self.0.set_primary_message(msg);
231         self.0.set_is_lint();
232         self.0
233     }
234
235     /// Create a `LintDiagnosticBuilder` from some existing `DiagnosticBuilder`.
236     pub fn new(err: DiagnosticBuilder<'a, G>) -> LintDiagnosticBuilder<'a, G> {
237         LintDiagnosticBuilder(err)
238     }
239 }
240
241 impl<'a> LintDiagnosticBuilder<'a, ErrorGuaranteed> {
242     pub fn forget_guarantee(self) -> LintDiagnosticBuilder<'a, ()> {
243         LintDiagnosticBuilder(self.0.forget_guarantee())
244     }
245 }
246
247 pub fn explain_lint_level_source(
248     sess: &Session,
249     lint: &'static Lint,
250     level: Level,
251     src: LintLevelSource,
252     err: &mut Diagnostic,
253 ) {
254     let name = lint.name_lower();
255     match src {
256         LintLevelSource::Default => {
257             sess.diag_note_once(
258                 err,
259                 DiagnosticMessageId::from(lint),
260                 &format!("`#[{}({})]` on by default", level.as_str(), name),
261             );
262         }
263         LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
264             let flag = match orig_level {
265                 Level::Warn => "-W",
266                 Level::Deny => "-D",
267                 Level::Forbid => "-F",
268                 Level::Allow => "-A",
269                 Level::ForceWarn => "--force-warn",
270                 Level::Expect(_) => {
271                     unreachable!("the expect level does not have a commandline flag")
272                 }
273             };
274             let hyphen_case_lint_name = name.replace('_', "-");
275             if lint_flag_val.as_str() == name {
276                 sess.diag_note_once(
277                     err,
278                     DiagnosticMessageId::from(lint),
279                     &format!(
280                         "requested on the command line with `{} {}`",
281                         flag, hyphen_case_lint_name
282                     ),
283                 );
284             } else {
285                 let hyphen_case_flag_val = lint_flag_val.as_str().replace('_', "-");
286                 sess.diag_note_once(
287                     err,
288                     DiagnosticMessageId::from(lint),
289                     &format!(
290                         "`{} {}` implied by `{} {}`",
291                         flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
292                     ),
293                 );
294             }
295         }
296         LintLevelSource::Node(lint_attr_name, src, reason) => {
297             if let Some(rationale) = reason {
298                 err.note(rationale.as_str());
299             }
300             sess.diag_span_note_once(
301                 err,
302                 DiagnosticMessageId::from(lint),
303                 src,
304                 "the lint level is defined here",
305             );
306             if lint_attr_name.as_str() != name {
307                 let level_str = level.as_str();
308                 sess.diag_note_once(
309                     err,
310                     DiagnosticMessageId::from(lint),
311                     &format!(
312                         "`#[{}({})]` implied by `#[{}({})]`",
313                         level_str, name, level_str, lint_attr_name
314                     ),
315                 );
316             }
317         }
318     }
319 }
320
321 pub fn struct_lint_level<'s, 'd>(
322     sess: &'s Session,
323     lint: &'static Lint,
324     level: Level,
325     src: LintLevelSource,
326     span: Option<MultiSpan>,
327     decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a, ()>) + 'd,
328 ) {
329     // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
330     // the "real" work.
331     fn struct_lint_level_impl<'s, 'd>(
332         sess: &'s Session,
333         lint: &'static Lint,
334         level: Level,
335         src: LintLevelSource,
336         span: Option<MultiSpan>,
337         decorate: Box<dyn for<'b> FnOnce(LintDiagnosticBuilder<'b, ()>) + 'd>,
338     ) {
339         // Check for future incompatibility lints and issue a stronger warning.
340         let future_incompatible = lint.future_incompatible;
341
342         let has_future_breakage = future_incompatible.map_or(
343             // Default allow lints trigger too often for testing.
344             sess.opts.debugging_opts.future_incompat_test && lint.default_level != Level::Allow,
345             |incompat| {
346                 matches!(incompat.reason, FutureIncompatibilityReason::FutureReleaseErrorReportNow)
347             },
348         );
349
350         let mut err = match (level, span) {
351             (Level::Allow, span) => {
352                 if has_future_breakage {
353                     if let Some(span) = span {
354                         sess.struct_span_allow(span, "")
355                     } else {
356                         sess.struct_allow("")
357                     }
358                 } else {
359                     return;
360                 }
361             }
362             (Level::Expect(expect_id), _) => {
363                 // This case is special as we actually allow the lint itself in this context, but
364                 // we can't return early like in the case for `Level::Allow` because we still
365                 // need the lint diagnostic to be emitted to `rustc_error::HanderInner`.
366                 //
367                 // We can also not mark the lint expectation as fulfilled here right away, as it
368                 // can still be cancelled in the decorate function. All of this means that we simply
369                 // create a `DiagnosticBuilder` and continue as we would for warnings.
370                 sess.struct_expect("", expect_id)
371             }
372             (Level::Warn | Level::ForceWarn, Some(span)) => sess.struct_span_warn(span, ""),
373             (Level::Warn | Level::ForceWarn, None) => sess.struct_warn(""),
374             (Level::Deny | Level::Forbid, Some(span)) => {
375                 let mut builder = sess.diagnostic().struct_err_lint("");
376                 builder.set_span(span);
377                 builder
378             }
379             (Level::Deny | Level::Forbid, None) => sess.diagnostic().struct_err_lint(""),
380         };
381
382         // If this code originates in a foreign macro, aka something that this crate
383         // did not itself author, then it's likely that there's nothing this crate
384         // can do about it. We probably want to skip the lint entirely.
385         if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
386             // Any suggestions made here are likely to be incorrect, so anything we
387             // emit shouldn't be automatically fixed by rustfix.
388             err.disable_suggestions();
389
390             // If this is a future incompatible that is not an edition fixing lint
391             // it'll become a hard error, so we have to emit *something*. Also,
392             // if this lint occurs in the expansion of a macro from an external crate,
393             // allow individual lints to opt-out from being reported.
394             let not_future_incompatible =
395                 future_incompatible.map(|f| f.reason.edition().is_some()).unwrap_or(true);
396             if not_future_incompatible && !lint.report_in_external_macro {
397                 err.cancel();
398                 // Don't continue further, since we don't want to have
399                 // `diag_span_note_once` called for a diagnostic that isn't emitted.
400                 return;
401             }
402         }
403
404         // Lint diagnostics that are covered by the expect level will not be emitted outside
405         // the compiler. It is therefore not necessary to add any information for the user.
406         // This will therefore directly call the decorate function which will in turn emit
407         // the `Diagnostic`.
408         if let Level::Expect(_) = level {
409             let name = lint.name_lower();
410             err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn: false });
411             decorate(LintDiagnosticBuilder::new(err));
412             return;
413         }
414
415         explain_lint_level_source(sess, lint, level, src, &mut err);
416
417         let name = lint.name_lower();
418         let is_force_warn = matches!(level, Level::ForceWarn);
419         err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn });
420
421         if let Some(future_incompatible) = future_incompatible {
422             let explanation = match future_incompatible.reason {
423                 FutureIncompatibilityReason::FutureReleaseError
424                 | FutureIncompatibilityReason::FutureReleaseErrorReportNow => {
425                     "this was previously accepted by the compiler but is being phased out; \
426                          it will become a hard error in a future release!"
427                         .to_owned()
428                 }
429                 FutureIncompatibilityReason::FutureReleaseSemanticsChange => {
430                     "this will change its meaning in a future release!".to_owned()
431                 }
432                 FutureIncompatibilityReason::EditionError(edition) => {
433                     let current_edition = sess.edition();
434                     format!(
435                         "this is accepted in the current edition (Rust {}) but is a hard error in Rust {}!",
436                         current_edition, edition
437                     )
438                 }
439                 FutureIncompatibilityReason::EditionSemanticsChange(edition) => {
440                     format!("this changes meaning in Rust {}", edition)
441                 }
442                 FutureIncompatibilityReason::Custom(reason) => reason.to_owned(),
443             };
444
445             if future_incompatible.explain_reason {
446                 err.warn(&explanation);
447             }
448             if !future_incompatible.reference.is_empty() {
449                 let citation =
450                     format!("for more information, see {}", future_incompatible.reference);
451                 err.note(&citation);
452             }
453         }
454
455         // Finally, run `decorate`. This function is also responsible for emitting the diagnostic.
456         decorate(LintDiagnosticBuilder::new(err));
457     }
458     struct_lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
459 }
460
461 /// Returns whether `span` originates in a foreign crate's external macro.
462 ///
463 /// This is used to test whether a lint should not even begin to figure out whether it should
464 /// be reported on the current node.
465 pub fn in_external_macro(sess: &Session, span: Span) -> bool {
466     let expn_data = span.ctxt().outer_expn_data();
467     match expn_data.kind {
468         ExpnKind::Inlined
469         | ExpnKind::Root
470         | ExpnKind::Desugaring(DesugaringKind::ForLoop | DesugaringKind::WhileLoop) => false,
471         ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
472         ExpnKind::Macro(MacroKind::Bang, _) => {
473             // Dummy span for the `def_site` means it's an external macro.
474             expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site)
475         }
476         ExpnKind::Macro { .. } => true, // definitely a plugin
477     }
478 }