]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/lint.rs
Use a newtype_index instead of a u32.
[rust.git] / compiler / rustc_middle / src / lint.rs
1 use std::cmp;
2
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};
7 use rustc_hir::HirId;
8 use rustc_index::vec::IndexVec;
9 use rustc_session::lint::{
10     builtin::{self, FORBIDDEN_LINT_GROUPS},
11     FutureIncompatibilityReason, Level, Lint, LintId,
12 };
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};
17
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.
23     Default,
24
25     /// Lint level was set by an attribute.
26     Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
27
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),
32 }
33
34 impl LintLevelSource {
35     pub fn name(&self) -> Symbol {
36         match *self {
37             LintLevelSource::Default => symbol::kw::Default,
38             LintLevelSource::Node(name, _, _) => name,
39             LintLevelSource::CommandLine(name, _) => name,
40         }
41     }
42
43     pub fn span(&self) -> Span {
44         match *self {
45             LintLevelSource::Default => DUMMY_SP,
46             LintLevelSource::Node(_, span, _) => span,
47             LintLevelSource::CommandLine(_, _) => DUMMY_SP,
48         }
49     }
50 }
51
52 /// A tuple of a lint level and its source.
53 pub type LevelAndSource = (Level, LintLevelSource);
54
55 #[derive(Debug, HashStable)]
56 pub struct LintLevelSets {
57     pub list: IndexVec<LintStackIndex, LintSet>,
58     pub lint_cap: Level,
59 }
60
61 rustc_index::newtype_index! {
62     #[derive(HashStable)]
63     pub struct LintStackIndex {
64         const COMMAND_LINE = 0,
65     }
66 }
67
68 #[derive(Debug, HashStable)]
69 pub enum LintSet {
70     CommandLine {
71         // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
72         // flag.
73         specs: FxHashMap<LintId, LevelAndSource>,
74     },
75
76     Node {
77         specs: FxHashMap<LintId, LevelAndSource>,
78         parent: LintStackIndex,
79     },
80 }
81
82 impl LintLevelSets {
83     pub fn new() -> Self {
84         LintLevelSets { list: IndexVec::new(), lint_cap: Level::Forbid }
85     }
86
87     pub fn get_lint_level(
88         &self,
89         lint: &'static Lint,
90         idx: LintStackIndex,
91         aux: Option<&FxHashMap<LintId, LevelAndSource>>,
92         sess: &Session,
93     ) -> LevelAndSource {
94         let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
95
96         // If `level` is none then we actually assume the default level for this
97         // lint.
98         let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
99
100         // If we're about to issue a warning, check at the last minute for any
101         // directives against the warnings "lint". If, for example, there's an
102         // `allow(warnings)` in scope then we want to respect that instead.
103         //
104         // We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
105         // triggers in cases (like #80988) where you have `forbid(warnings)`,
106         // and so if we turned that into an error, it'd defeat the purpose of the
107         // future compatibility warning.
108         if level == Level::Warn && LintId::of(lint) != LintId::of(FORBIDDEN_LINT_GROUPS) {
109             let (warnings_level, warnings_src) =
110                 self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
111             if let Some(configured_warning_level) = warnings_level {
112                 if configured_warning_level != Level::Warn {
113                     level = configured_warning_level;
114                     src = warnings_src;
115                 }
116             }
117         }
118
119         // Ensure that we never exceed the `--cap-lints` argument.
120         level = cmp::min(level, self.lint_cap);
121
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);
125         }
126
127         (level, src)
128     }
129
130     pub fn get_lint_id_level(
131         &self,
132         id: LintId,
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);
139             }
140         }
141         loop {
142             match self.list[idx] {
143                 LintSet::CommandLine { ref specs } => {
144                     if let Some(&(level, src)) = specs.get(&id) {
145                         return (Some(level), src);
146                     }
147                     return (None, LintLevelSource::Default);
148                 }
149                 LintSet::Node { ref specs, parent } => {
150                     if let Some(&(level, src)) = specs.get(&id) {
151                         return (Some(level), src);
152                     }
153                     idx = parent;
154                 }
155             }
156         }
157     }
158 }
159
160 #[derive(Debug)]
161 pub struct LintLevelMap {
162     pub sets: LintLevelSets,
163     pub id_to_set: FxHashMap<HirId, LintStackIndex>,
164 }
165
166 impl LintLevelMap {
167     /// If the `id` was previously registered with `register_id` when building
168     /// this `LintLevelMap` this returns the corresponding lint level and source
169     /// of the lint level for the lint provided.
170     ///
171     /// If the `id` was not previously registered, returns `None`. If `None` is
172     /// returned then the parent of `id` should be acquired and this function
173     /// should be called again.
174     pub fn level_and_source(
175         &self,
176         lint: &'static Lint,
177         id: HirId,
178         session: &Session,
179     ) -> Option<LevelAndSource> {
180         self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
181     }
182 }
183
184 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
185     #[inline]
186     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
187         let LintLevelMap { ref sets, ref id_to_set } = *self;
188
189         id_to_set.hash_stable(hcx, hasher);
190
191         hcx.while_hashing_spans(true, |hcx| sets.hash_stable(hcx, hasher))
192     }
193 }
194
195 pub struct LintDiagnosticBuilder<'a>(DiagnosticBuilder<'a>);
196
197 impl<'a> LintDiagnosticBuilder<'a> {
198     /// Return the inner DiagnosticBuilder, first setting the primary message to `msg`.
199     pub fn build(mut self, msg: &str) -> DiagnosticBuilder<'a> {
200         self.0.set_primary_message(msg);
201         self.0
202     }
203
204     /// Create a LintDiagnosticBuilder from some existing DiagnosticBuilder.
205     pub fn new(err: DiagnosticBuilder<'a>) -> LintDiagnosticBuilder<'a> {
206         LintDiagnosticBuilder(err)
207     }
208 }
209
210 pub fn struct_lint_level<'s, 'd>(
211     sess: &'s Session,
212     lint: &'static Lint,
213     level: Level,
214     src: LintLevelSource,
215     span: Option<MultiSpan>,
216     decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>) + 'd,
217 ) {
218     // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
219     // the "real" work.
220     fn struct_lint_level_impl(
221         sess: &'s Session,
222         lint: &'static Lint,
223         level: Level,
224         src: LintLevelSource,
225         span: Option<MultiSpan>,
226         decorate: Box<dyn for<'b> FnOnce(LintDiagnosticBuilder<'b>) + 'd>,
227     ) {
228         // Check for future incompatibility lints and issue a stronger warning.
229         let lint_id = LintId::of(lint);
230         let future_incompatible = lint.future_incompatible;
231
232         let has_future_breakage =
233             future_incompatible.map_or(false, |incompat| incompat.future_breakage.is_some());
234
235         let mut err = match (level, span) {
236             (Level::Allow, span) => {
237                 if has_future_breakage {
238                     if let Some(span) = span {
239                         sess.struct_span_allow(span, "")
240                     } else {
241                         sess.struct_allow("")
242                     }
243                 } else {
244                     return;
245                 }
246             }
247             (Level::Warn | Level::ForceWarn, Some(span)) => sess.struct_span_warn(span, ""),
248             (Level::Warn | Level::ForceWarn, None) => sess.struct_warn(""),
249             (Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
250             (Level::Deny | Level::Forbid, None) => sess.struct_err(""),
251         };
252
253         // If this code originates in a foreign macro, aka something that this crate
254         // did not itself author, then it's likely that there's nothing this crate
255         // can do about it. We probably want to skip the lint entirely.
256         if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
257             // Any suggestions made here are likely to be incorrect, so anything we
258             // emit shouldn't be automatically fixed by rustfix.
259             err.allow_suggestions(false);
260
261             // If this is a future incompatible that is not an edition fixing lint
262             // it'll become a hard error, so we have to emit *something*. Also,
263             // if this lint occurs in the expansion of a macro from an external crate,
264             // allow individual lints to opt-out from being reported.
265             let not_future_incompatible =
266                 future_incompatible.map(|f| f.reason.edition().is_some()).unwrap_or(true);
267             if not_future_incompatible && !lint.report_in_external_macro {
268                 err.cancel();
269                 // Don't continue further, since we don't want to have
270                 // `diag_span_note_once` called for a diagnostic that isn't emitted.
271                 return;
272             }
273         }
274
275         let name = lint.name_lower();
276         match src {
277             LintLevelSource::Default => {
278                 sess.diag_note_once(
279                     &mut err,
280                     DiagnosticMessageId::from(lint),
281                     &format!("`#[{}({})]` on by default", level.as_str(), name),
282                 );
283             }
284             LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
285                 let flag = match orig_level {
286                     Level::Warn => "-W",
287                     Level::Deny => "-D",
288                     Level::Forbid => "-F",
289                     Level::Allow => "-A",
290                     Level::ForceWarn => "--force-warns",
291                 };
292                 let hyphen_case_lint_name = name.replace("_", "-");
293                 if lint_flag_val.as_str() == name {
294                     sess.diag_note_once(
295                         &mut err,
296                         DiagnosticMessageId::from(lint),
297                         &format!(
298                             "requested on the command line with `{} {}`",
299                             flag, hyphen_case_lint_name
300                         ),
301                     );
302                 } else {
303                     let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
304                     sess.diag_note_once(
305                         &mut err,
306                         DiagnosticMessageId::from(lint),
307                         &format!(
308                             "`{} {}` implied by `{} {}`",
309                             flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
310                         ),
311                     );
312                 }
313             }
314             LintLevelSource::Node(lint_attr_name, src, reason) => {
315                 if let Some(rationale) = reason {
316                     err.note(&rationale.as_str());
317                 }
318                 sess.diag_span_note_once(
319                     &mut err,
320                     DiagnosticMessageId::from(lint),
321                     src,
322                     "the lint level is defined here",
323                 );
324                 if lint_attr_name.as_str() != name {
325                     let level_str = level.as_str();
326                     sess.diag_note_once(
327                         &mut err,
328                         DiagnosticMessageId::from(lint),
329                         &format!(
330                             "`#[{}({})]` implied by `#[{}({})]`",
331                             level_str, name, level_str, lint_attr_name
332                         ),
333                     );
334                 }
335             }
336         }
337
338         err.code(DiagnosticId::Lint { name, has_future_breakage });
339
340         if let Some(future_incompatible) = future_incompatible {
341             let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
342                 "once this associated item is added to the standard library, the ambiguity may \
343                  cause an error or change in behavior!"
344                     .to_owned()
345             } else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) {
346                 "this borrowing pattern was not meant to be accepted, and may become a hard error \
347                  in the future"
348                     .to_owned()
349             } else if let FutureIncompatibilityReason::EditionError(edition) =
350                 future_incompatible.reason
351             {
352                 let current_edition = sess.edition();
353                 format!(
354                     "this is accepted in the current edition (Rust {}) but is a hard error in Rust {}!",
355                     current_edition, edition
356                 )
357             } else if let FutureIncompatibilityReason::EditionSemanticsChange(edition) =
358                 future_incompatible.reason
359             {
360                 format!("this changes meaning in Rust {}", edition)
361             } else {
362                 "this was previously accepted by the compiler but is being phased out; \
363                  it will become a hard error in a future release!"
364                     .to_owned()
365             };
366             if future_incompatible.explain_reason {
367                 err.warn(&explanation);
368             }
369             if !future_incompatible.reference.is_empty() {
370                 let citation =
371                     format!("for more information, see {}", future_incompatible.reference);
372                 err.note(&citation);
373             }
374         }
375
376         // Finally, run `decorate`. This function is also responsible for emitting the diagnostic.
377         decorate(LintDiagnosticBuilder::new(err));
378     }
379     struct_lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
380 }
381
382 /// Returns whether `span` originates in a foreign crate's external macro.
383 ///
384 /// This is used to test whether a lint should not even begin to figure out whether it should
385 /// be reported on the current node.
386 pub fn in_external_macro(sess: &Session, span: Span) -> bool {
387     let expn_data = span.ctxt().outer_expn_data();
388     match expn_data.kind {
389         ExpnKind::Inlined | ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => {
390             false
391         }
392         ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
393         ExpnKind::Macro { kind: MacroKind::Bang, name: _, proc_macro: _ } => {
394             // Dummy span for the `def_site` means it's an external macro.
395             expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site)
396         }
397         ExpnKind::Macro { .. } => true, // definitely a plugin
398     }
399 }