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