]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/lint.rs
Serialize incr comp structures to file via fixed-size buffer
[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_session::lint::{builtin, Level, Lint, LintId};
9 use rustc_session::{DiagnosticMessageId, Session};
10 use rustc_span::hygiene::MacroKind;
11 use rustc_span::source_map::{DesugaringKind, ExpnKind, MultiSpan};
12 use rustc_span::{symbol, Span, Symbol, DUMMY_SP};
13
14 /// How a lint level was set.
15 #[derive(Clone, Copy, PartialEq, Eq, HashStable)]
16 pub enum LintLevelSource {
17     /// Lint is at the default level as declared
18     /// in rustc or a plugin.
19     Default,
20
21     /// Lint level was set by an attribute.
22     Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
23
24     /// Lint level was set by a command-line flag.
25     /// The provided `Level` is the level specified on the command line.
26     /// (The actual level may be lower due to `--cap-lints`.)
27     CommandLine(Symbol, Level),
28 }
29
30 impl LintLevelSource {
31     pub fn name(&self) -> Symbol {
32         match *self {
33             LintLevelSource::Default => symbol::kw::Default,
34             LintLevelSource::Node(name, _, _) => name,
35             LintLevelSource::CommandLine(name, _) => name,
36         }
37     }
38
39     pub fn span(&self) -> Span {
40         match *self {
41             LintLevelSource::Default => DUMMY_SP,
42             LintLevelSource::Node(_, span, _) => span,
43             LintLevelSource::CommandLine(_, _) => DUMMY_SP,
44         }
45     }
46 }
47
48 /// A tuple of a lint level and its source.
49 pub type LevelSource = (Level, LintLevelSource);
50
51 pub struct LintLevelSets {
52     pub list: Vec<LintSet>,
53     pub lint_cap: Level,
54 }
55
56 pub enum LintSet {
57     CommandLine {
58         // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
59         // flag.
60         specs: FxHashMap<LintId, LevelSource>,
61     },
62
63     Node {
64         specs: FxHashMap<LintId, LevelSource>,
65         parent: u32,
66     },
67 }
68
69 impl LintLevelSets {
70     pub fn new() -> Self {
71         LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
72     }
73
74     pub fn get_lint_level(
75         &self,
76         lint: &'static Lint,
77         idx: u32,
78         aux: Option<&FxHashMap<LintId, LevelSource>>,
79         sess: &Session,
80     ) -> LevelSource {
81         let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
82
83         // If `level` is none then we actually assume the default level for this
84         // lint.
85         let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
86
87         // If we're about to issue a warning, check at the last minute for any
88         // directives against the warnings "lint". If, for example, there's an
89         // `allow(warnings)` in scope then we want to respect that instead.
90         if level == Level::Warn {
91             let (warnings_level, warnings_src) =
92                 self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
93             if let Some(configured_warning_level) = warnings_level {
94                 if configured_warning_level != Level::Warn {
95                     level = configured_warning_level;
96                     src = warnings_src;
97                 }
98             }
99         }
100
101         // Ensure that we never exceed the `--cap-lints` argument.
102         level = cmp::min(level, self.lint_cap);
103
104         if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
105             // Ensure that we never exceed driver level.
106             level = cmp::min(*driver_level, level);
107         }
108
109         (level, src)
110     }
111
112     pub fn get_lint_id_level(
113         &self,
114         id: LintId,
115         mut idx: u32,
116         aux: Option<&FxHashMap<LintId, LevelSource>>,
117     ) -> (Option<Level>, LintLevelSource) {
118         if let Some(specs) = aux {
119             if let Some(&(level, src)) = specs.get(&id) {
120                 return (Some(level), src);
121             }
122         }
123         loop {
124             match self.list[idx as usize] {
125                 LintSet::CommandLine { ref specs } => {
126                     if let Some(&(level, src)) = specs.get(&id) {
127                         return (Some(level), src);
128                     }
129                     return (None, LintLevelSource::Default);
130                 }
131                 LintSet::Node { ref specs, parent } => {
132                     if let Some(&(level, src)) = specs.get(&id) {
133                         return (Some(level), src);
134                     }
135                     idx = parent;
136                 }
137             }
138         }
139     }
140 }
141
142 pub struct LintLevelMap {
143     pub sets: LintLevelSets,
144     pub id_to_set: FxHashMap<HirId, u32>,
145 }
146
147 impl LintLevelMap {
148     /// If the `id` was previously registered with `register_id` when building
149     /// this `LintLevelMap` this returns the corresponding lint level and source
150     /// of the lint level for the lint provided.
151     ///
152     /// If the `id` was not previously registered, returns `None`. If `None` is
153     /// returned then the parent of `id` should be acquired and this function
154     /// should be called again.
155     pub fn level_and_source(
156         &self,
157         lint: &'static Lint,
158         id: HirId,
159         session: &Session,
160     ) -> Option<LevelSource> {
161         self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
162     }
163 }
164
165 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
166     #[inline]
167     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
168         let LintLevelMap { ref sets, ref id_to_set } = *self;
169
170         id_to_set.hash_stable(hcx, hasher);
171
172         let LintLevelSets { ref list, lint_cap } = *sets;
173
174         lint_cap.hash_stable(hcx, hasher);
175
176         hcx.while_hashing_spans(true, |hcx| {
177             list.len().hash_stable(hcx, hasher);
178
179             // We are working under the assumption here that the list of
180             // lint-sets is built in a deterministic order.
181             for lint_set in list {
182                 ::std::mem::discriminant(lint_set).hash_stable(hcx, hasher);
183
184                 match *lint_set {
185                     LintSet::CommandLine { ref specs } => {
186                         specs.hash_stable(hcx, hasher);
187                     }
188                     LintSet::Node { ref specs, parent } => {
189                         specs.hash_stable(hcx, hasher);
190                         parent.hash_stable(hcx, hasher);
191                     }
192                 }
193             }
194         })
195     }
196 }
197
198 pub struct LintDiagnosticBuilder<'a>(DiagnosticBuilder<'a>);
199
200 impl<'a> LintDiagnosticBuilder<'a> {
201     /// Return the inner DiagnosticBuilder, first setting the primary message to `msg`.
202     pub fn build(mut self, msg: &str) -> DiagnosticBuilder<'a> {
203         self.0.set_primary_message(msg);
204         self.0
205     }
206
207     /// Create a LintDiagnosticBuilder from some existing DiagnosticBuilder.
208     pub fn new(err: DiagnosticBuilder<'a>) -> LintDiagnosticBuilder<'a> {
209         LintDiagnosticBuilder(err)
210     }
211 }
212
213 pub fn struct_lint_level<'s, 'd>(
214     sess: &'s Session,
215     lint: &'static Lint,
216     level: Level,
217     src: LintLevelSource,
218     span: Option<MultiSpan>,
219     decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>) + 'd,
220 ) {
221     // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
222     // the "real" work.
223     fn struct_lint_level_impl(
224         sess: &'s Session,
225         lint: &'static Lint,
226         level: Level,
227         src: LintLevelSource,
228         span: Option<MultiSpan>,
229         decorate: Box<dyn for<'b> FnOnce(LintDiagnosticBuilder<'b>) + 'd>,
230     ) {
231         // Check for future incompatibility lints and issue a stronger warning.
232         let lint_id = LintId::of(lint);
233         let future_incompatible = lint.future_incompatible;
234
235         let has_future_breakage =
236             future_incompatible.map_or(false, |incompat| incompat.future_breakage.is_some());
237
238         let mut err = match (level, span) {
239             (Level::Allow, span) => {
240                 if has_future_breakage {
241                     if let Some(span) = span {
242                         sess.struct_span_allow(span, "")
243                     } else {
244                         sess.struct_allow("")
245                     }
246                 } else {
247                     return;
248                 }
249             }
250             (Level::Warn, Some(span)) => sess.struct_span_warn(span, ""),
251             (Level::Warn, None) => sess.struct_warn(""),
252             (Level::Deny | Level::Forbid, Some(span)) => sess.struct_span_err(span, ""),
253             (Level::Deny | Level::Forbid, None) => sess.struct_err(""),
254         };
255
256         // If this code originates in a foreign macro, aka something that this crate
257         // did not itself author, then it's likely that there's nothing this crate
258         // can do about it. We probably want to skip the lint entirely.
259         if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
260             // Any suggestions made here are likely to be incorrect, so anything we
261             // emit shouldn't be automatically fixed by rustfix.
262             err.allow_suggestions(false);
263
264             // If this is a future incompatible lint it'll become a hard error, so
265             // we have to emit *something*. Also, if this lint occurs in the
266             // expansion of a macro from an external crate, allow individual lints
267             // to opt-out from being reported.
268             if future_incompatible.is_none() && !lint.report_in_external_macro {
269                 err.cancel();
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.
272                 return;
273             }
274         }
275
276         let name = lint.name_lower();
277         match src {
278             LintLevelSource::Default => {
279                 sess.diag_note_once(
280                     &mut err,
281                     DiagnosticMessageId::from(lint),
282                     &format!("`#[{}({})]` on by default", level.as_str(), name),
283                 );
284             }
285             LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
286                 let flag = match orig_level {
287                     Level::Warn => "-W",
288                     Level::Deny => "-D",
289                     Level::Forbid => "-F",
290                     Level::Allow => "-A",
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             const STANDARD_MESSAGE: &str = "this was previously accepted by the compiler but is being phased out; \
342                  it will become a hard error";
343
344             let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
345                 "once this method is added to the standard library, \
346                  the ambiguity may cause an error or change in behavior!"
347                     .to_owned()
348             } else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) {
349                 "this borrowing pattern was not meant to be accepted, \
350                  and may become a hard error in the future"
351                     .to_owned()
352             } else if let Some(edition) = future_incompatible.edition {
353                 format!("{} in the {} edition!", STANDARD_MESSAGE, edition)
354             } else {
355                 format!("{} in a future release!", STANDARD_MESSAGE)
356             };
357             let citation = format!("for more information, see {}", future_incompatible.reference);
358             err.warn(&explanation);
359             err.note(&citation);
360         }
361
362         // Finally, run `decorate`. This function is also responsible for emitting the diagnostic.
363         decorate(LintDiagnosticBuilder::new(err));
364     }
365     struct_lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
366 }
367
368 /// Returns whether `span` originates in a foreign crate's external macro.
369 ///
370 /// This is used to test whether a lint should not even begin to figure out whether it should
371 /// be reported on the current node.
372 pub fn in_external_macro(sess: &Session, span: Span) -> bool {
373     let expn_data = span.ctxt().outer_expn_data();
374     match expn_data.kind {
375         ExpnKind::Inlined | ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => {
376             false
377         }
378         ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
379         ExpnKind::Macro(MacroKind::Bang, _) => {
380             // Dummy span for the `def_site` means it's an external macro.
381             expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site)
382         }
383         ExpnKind::Macro { .. } => true, // definitely a plugin
384     }
385 }