]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/early.rs
Auto merge of #67788 - cjgillot:delint-day, r=Zoxc
[rust.git] / src / librustc_lint / early.rs
1 //! Implementation of lint checking.
2 //!
3 //! The lint checking is mostly consolidated into one pass which runs
4 //! after all other analyses. Throughout compilation, lint warnings
5 //! can be added via the `add_lint` method on the Session structure. This
6 //! requires a span and an ID of the node that the lint is being added to. The
7 //! lint isn't actually emitted at that time because it is unknown what the
8 //! actual lint level at that location is.
9 //!
10 //! To actually emit lint warnings/errors, a separate pass is used.
11 //! A context keeps track of the current state of all lint levels.
12 //! Upon entering a node of the ast which can modify the lint settings, the
13 //! previous lint state is pushed onto a stack and the ast is then recursed
14 //! upon. As the ast is traversed, this keeps track of the current lint level
15 //! for all lint attributes.
16
17 use rustc::lint::{EarlyContext, LintStore};
18 use rustc::lint::{EarlyLintPass, EarlyLintPassObject};
19 use rustc::lint::{LintBuffer, LintContext, LintPass};
20 use rustc::session::Session;
21 use rustc::util::common::time;
22
23 use rustc_span::Span;
24 use std::slice;
25 use syntax::ast;
26 use syntax::visit as ast_visit;
27
28 use log::debug;
29
30 macro_rules! run_early_pass { ($cx:expr, $f:ident, $($args:expr),*) => ({
31     $cx.pass.$f(&$cx.context, $($args),*);
32 }) }
33
34 struct EarlyContextAndPass<'a, T: EarlyLintPass> {
35     context: EarlyContext<'a>,
36     pass: T,
37 }
38
39 impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
40     fn check_id(&mut self, id: ast::NodeId) {
41         for early_lint in self.context.buffered.take(id) {
42             self.context.lookup_and_emit_with_diagnostics(
43                 early_lint.lint_id.lint,
44                 Some(early_lint.span.clone()),
45                 &early_lint.msg,
46                 early_lint.diagnostic,
47             );
48         }
49     }
50
51     /// Merge the lints specified by any lint attributes into the
52     /// current lint context, call the provided function, then reset the
53     /// lints in effect to their previous state.
54     fn with_lint_attrs<F>(&mut self, id: ast::NodeId, attrs: &'a [ast::Attribute], f: F)
55     where
56         F: FnOnce(&mut Self),
57     {
58         let push = self.context.builder.push(attrs, &self.context.lint_store);
59         self.check_id(id);
60         self.enter_attrs(attrs);
61         f(self);
62         self.exit_attrs(attrs);
63         self.context.builder.pop(push);
64     }
65
66     fn enter_attrs(&mut self, attrs: &'a [ast::Attribute]) {
67         debug!("early context: enter_attrs({:?})", attrs);
68         run_early_pass!(self, enter_lint_attrs, attrs);
69     }
70
71     fn exit_attrs(&mut self, attrs: &'a [ast::Attribute]) {
72         debug!("early context: exit_attrs({:?})", attrs);
73         run_early_pass!(self, exit_lint_attrs, attrs);
74     }
75 }
76
77 impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> {
78     fn visit_param(&mut self, param: &'a ast::Param) {
79         self.with_lint_attrs(param.id, &param.attrs, |cx| {
80             run_early_pass!(cx, check_param, param);
81             ast_visit::walk_param(cx, param);
82         });
83     }
84
85     fn visit_item(&mut self, it: &'a ast::Item) {
86         self.with_lint_attrs(it.id, &it.attrs, |cx| {
87             run_early_pass!(cx, check_item, it);
88             ast_visit::walk_item(cx, it);
89             run_early_pass!(cx, check_item_post, it);
90         })
91     }
92
93     fn visit_foreign_item(&mut self, it: &'a ast::ForeignItem) {
94         self.with_lint_attrs(it.id, &it.attrs, |cx| {
95             run_early_pass!(cx, check_foreign_item, it);
96             ast_visit::walk_foreign_item(cx, it);
97             run_early_pass!(cx, check_foreign_item_post, it);
98         })
99     }
100
101     fn visit_pat(&mut self, p: &'a ast::Pat) {
102         run_early_pass!(self, check_pat, p);
103         self.check_id(p.id);
104         ast_visit::walk_pat(self, p);
105         run_early_pass!(self, check_pat_post, p);
106     }
107
108     fn visit_expr(&mut self, e: &'a ast::Expr) {
109         self.with_lint_attrs(e.id, &e.attrs, |cx| {
110             run_early_pass!(cx, check_expr, e);
111             ast_visit::walk_expr(cx, e);
112         })
113     }
114
115     fn visit_stmt(&mut self, s: &'a ast::Stmt) {
116         run_early_pass!(self, check_stmt, s);
117         self.check_id(s.id);
118         ast_visit::walk_stmt(self, s);
119     }
120
121     fn visit_fn(
122         &mut self,
123         fk: ast_visit::FnKind<'a>,
124         decl: &'a ast::FnDecl,
125         span: Span,
126         id: ast::NodeId,
127     ) {
128         run_early_pass!(self, check_fn, fk, decl, span, id);
129         self.check_id(id);
130         ast_visit::walk_fn(self, fk, decl, span);
131         run_early_pass!(self, check_fn_post, fk, decl, span, id);
132     }
133
134     fn visit_variant_data(&mut self, s: &'a ast::VariantData) {
135         run_early_pass!(self, check_struct_def, s);
136         if let Some(ctor_hir_id) = s.ctor_id() {
137             self.check_id(ctor_hir_id);
138         }
139         ast_visit::walk_struct_def(self, s);
140         run_early_pass!(self, check_struct_def_post, s);
141     }
142
143     fn visit_struct_field(&mut self, s: &'a ast::StructField) {
144         self.with_lint_attrs(s.id, &s.attrs, |cx| {
145             run_early_pass!(cx, check_struct_field, s);
146             ast_visit::walk_struct_field(cx, s);
147         })
148     }
149
150     fn visit_variant(&mut self, v: &'a ast::Variant) {
151         self.with_lint_attrs(v.id, &v.attrs, |cx| {
152             run_early_pass!(cx, check_variant, v);
153             ast_visit::walk_variant(cx, v);
154             run_early_pass!(cx, check_variant_post, v);
155         })
156     }
157
158     fn visit_ty(&mut self, t: &'a ast::Ty) {
159         run_early_pass!(self, check_ty, t);
160         self.check_id(t.id);
161         ast_visit::walk_ty(self, t);
162     }
163
164     fn visit_ident(&mut self, ident: ast::Ident) {
165         run_early_pass!(self, check_ident, ident);
166     }
167
168     fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, _a: &[ast::Attribute], n: ast::NodeId) {
169         run_early_pass!(self, check_mod, m, s, n);
170         self.check_id(n);
171         ast_visit::walk_mod(self, m);
172         run_early_pass!(self, check_mod_post, m, s, n);
173     }
174
175     fn visit_local(&mut self, l: &'a ast::Local) {
176         self.with_lint_attrs(l.id, &l.attrs, |cx| {
177             run_early_pass!(cx, check_local, l);
178             ast_visit::walk_local(cx, l);
179         })
180     }
181
182     fn visit_block(&mut self, b: &'a ast::Block) {
183         run_early_pass!(self, check_block, b);
184         self.check_id(b.id);
185         ast_visit::walk_block(self, b);
186         run_early_pass!(self, check_block_post, b);
187     }
188
189     fn visit_arm(&mut self, a: &'a ast::Arm) {
190         run_early_pass!(self, check_arm, a);
191         ast_visit::walk_arm(self, a);
192     }
193
194     fn visit_expr_post(&mut self, e: &'a ast::Expr) {
195         run_early_pass!(self, check_expr_post, e);
196     }
197
198     fn visit_generic_param(&mut self, param: &'a ast::GenericParam) {
199         run_early_pass!(self, check_generic_param, param);
200         ast_visit::walk_generic_param(self, param);
201     }
202
203     fn visit_generics(&mut self, g: &'a ast::Generics) {
204         run_early_pass!(self, check_generics, g);
205         ast_visit::walk_generics(self, g);
206     }
207
208     fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) {
209         run_early_pass!(self, check_where_predicate, p);
210         ast_visit::walk_where_predicate(self, p);
211     }
212
213     fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef, m: &'a ast::TraitBoundModifier) {
214         run_early_pass!(self, check_poly_trait_ref, t, m);
215         ast_visit::walk_poly_trait_ref(self, t, m);
216     }
217
218     fn visit_trait_item(&mut self, trait_item: &'a ast::AssocItem) {
219         self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| {
220             run_early_pass!(cx, check_trait_item, trait_item);
221             ast_visit::walk_trait_item(cx, trait_item);
222             run_early_pass!(cx, check_trait_item_post, trait_item);
223         });
224     }
225
226     fn visit_impl_item(&mut self, impl_item: &'a ast::AssocItem) {
227         self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| {
228             run_early_pass!(cx, check_impl_item, impl_item);
229             ast_visit::walk_impl_item(cx, impl_item);
230             run_early_pass!(cx, check_impl_item_post, impl_item);
231         });
232     }
233
234     fn visit_lifetime(&mut self, lt: &'a ast::Lifetime) {
235         run_early_pass!(self, check_lifetime, lt);
236         self.check_id(lt.id);
237     }
238
239     fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) {
240         run_early_pass!(self, check_path, p, id);
241         self.check_id(id);
242         ast_visit::walk_path(self, p);
243     }
244
245     fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
246         run_early_pass!(self, check_attribute, attr);
247     }
248
249     fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) {
250         run_early_pass!(self, check_mac_def, mac, id);
251         self.check_id(id);
252     }
253
254     fn visit_mac(&mut self, mac: &'a ast::Mac) {
255         // FIXME(#54110): So, this setup isn't really right. I think
256         // that (a) the libsyntax visitor ought to be doing this as
257         // part of `walk_mac`, and (b) we should be calling
258         // `visit_path`, *but* that would require a `NodeId`, and I
259         // want to get #53686 fixed quickly. -nmatsakis
260         ast_visit::walk_path(self, &mac.path);
261
262         run_early_pass!(self, check_mac, mac);
263     }
264 }
265
266 struct EarlyLintPassObjects<'a> {
267     lints: &'a mut [EarlyLintPassObject],
268 }
269
270 #[allow(rustc::lint_pass_impl_without_macro)]
271 impl LintPass for EarlyLintPassObjects<'_> {
272     fn name(&self) -> &'static str {
273         panic!()
274     }
275 }
276
277 macro_rules! expand_early_lint_pass_impl_methods {
278     ([$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
279         $(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
280             for obj in self.lints.iter_mut() {
281                 obj.$name(context, $($param),*);
282             }
283         })*
284     )
285 }
286
287 macro_rules! early_lint_pass_impl {
288     ([], [$($methods:tt)*]) => (
289         impl EarlyLintPass for EarlyLintPassObjects<'_> {
290             expand_early_lint_pass_impl_methods!([$($methods)*]);
291         }
292     )
293 }
294
295 early_lint_methods!(early_lint_pass_impl, []);
296
297 fn early_lint_crate<T: EarlyLintPass>(
298     sess: &Session,
299     lint_store: &LintStore,
300     krate: &ast::Crate,
301     pass: T,
302     buffered: LintBuffer,
303     warn_about_weird_lints: bool,
304 ) -> LintBuffer {
305     let mut cx = EarlyContextAndPass {
306         context: EarlyContext::new(sess, lint_store, krate, buffered, warn_about_weird_lints),
307         pass,
308     };
309
310     // Visit the whole crate.
311     cx.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |cx| {
312         // since the root module isn't visited as an item (because it isn't an
313         // item), warn for it here.
314         run_early_pass!(cx, check_crate, krate);
315
316         ast_visit::walk_crate(cx, krate);
317
318         run_early_pass!(cx, check_crate_post, krate);
319     });
320     cx.context.buffered
321 }
322
323 pub fn check_ast_crate<T: EarlyLintPass>(
324     sess: &Session,
325     lint_store: &LintStore,
326     krate: &ast::Crate,
327     pre_expansion: bool,
328     lint_buffer: Option<LintBuffer>,
329     builtin_lints: T,
330 ) {
331     let mut passes: Vec<_> = if pre_expansion {
332         lint_store.pre_expansion_passes.iter().map(|p| (p)()).collect()
333     } else {
334         lint_store.early_passes.iter().map(|p| (p)()).collect()
335     };
336     let mut buffered = lint_buffer.unwrap_or_default();
337
338     if !sess.opts.debugging_opts.no_interleave_lints {
339         buffered =
340             early_lint_crate(sess, lint_store, krate, builtin_lints, buffered, pre_expansion);
341
342         if !passes.is_empty() {
343             buffered = early_lint_crate(
344                 sess,
345                 lint_store,
346                 krate,
347                 EarlyLintPassObjects { lints: &mut passes[..] },
348                 buffered,
349                 pre_expansion,
350             );
351         }
352     } else {
353         for pass in &mut passes {
354             buffered = time(sess, &format!("running lint: {}", pass.name()), || {
355                 early_lint_crate(
356                     sess,
357                     lint_store,
358                     krate,
359                     EarlyLintPassObjects { lints: slice::from_mut(pass) },
360                     buffered,
361                     pre_expansion,
362                 )
363             });
364         }
365     }
366
367     // All of the buffered lints should have been emitted at this point.
368     // If not, that means that we somehow buffered a lint for a node id
369     // that was not lint-checked (perhaps it doesn't exist?). This is a bug.
370     //
371     // Rustdoc runs everybody-loops before the early lints and removes
372     // function bodies, so it's totally possible for linted
373     // node ids to not exist (e.g., macros defined within functions for the
374     // unused_macro lint) anymore. So we only run this check
375     // when we're not in rustdoc mode. (see issue #47639)
376     if !sess.opts.actually_rustdoc {
377         for (_id, lints) in buffered.map {
378             for early_lint in lints {
379                 sess.delay_span_bug(early_lint.span, "failed to process buffered lint here");
380             }
381         }
382     }
383 }