1use rustc_ast::ptr::P;
8use rustc_ast::visit::{self as ast_visit, Visitor, walk_list};
9use rustc_ast::{self as ast, HasAttrs};
10use rustc_data_structures::stack::ensure_sufficient_stack;
11use rustc_feature::Features;
12use rustc_middle::ty::{RegisteredTools, TyCtxt};
13use rustc_session::Session;
14use rustc_session::lint::{BufferedEarlyLint, LintBuffer, LintPass};
15use rustc_span::{Ident, Span};
16use tracing::debug;
17
18use crate::context::{EarlyContext, LintContext, LintStore};
19use crate::passes::{EarlyLintPass, EarlyLintPassObject};
20
21mod diagnostics;
22
23macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
24 $cx.pass.$f(&$cx.context, $($args),*);
25}) }
26
27pub struct EarlyContextAndPass<'ecx, 'tcx, T: EarlyLintPass> {
30 context: EarlyContext<'ecx>,
31 tcx: Option<TyCtxt<'tcx>>,
32 pass: T,
33}
34
35impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> {
36 #[inline(always)]
38 #[allow(rustc::diagnostic_outside_of_impl)]
39 fn inlined_check_id(&mut self, id: ast::NodeId) {
40 for early_lint in self.context.buffered.take(id) {
41 let BufferedEarlyLint { span, node_id: _, lint_id, diagnostic } = early_lint;
42 self.context.opt_span_lint(lint_id.lint, span, |diag| {
43 diagnostics::decorate_lint(self.context.sess(), self.tcx, diagnostic, diag);
44 });
45 }
46 }
47
48 fn check_id(&mut self, id: ast::NodeId) {
50 self.inlined_check_id(id)
51 }
52
53 fn with_lint_attrs<F>(&mut self, id: ast::NodeId, attrs: &'_ [ast::Attribute], f: F)
57 where
58 F: FnOnce(&mut Self),
59 {
60 let is_crate_node = id == ast::CRATE_NODE_ID;
61 debug!(?id);
62 let push = self.context.builder.push(attrs, is_crate_node, None);
63
64 self.inlined_check_id(id);
65 debug!("early context: enter_attrs({:?})", attrs);
66 lint_callback!(self, check_attributes, attrs);
67 ensure_sufficient_stack(|| f(self));
68 debug!("early context: exit_attrs({:?})", attrs);
69 lint_callback!(self, check_attributes_post, attrs);
70 self.context.builder.pop(push);
71 }
72}
73
74impl<'ast, 'ecx, 'tcx, T: EarlyLintPass> ast_visit::Visitor<'ast>
75 for EarlyContextAndPass<'ecx, 'tcx, T>
76{
77 fn visit_coroutine_kind(&mut self, coroutine_kind: &'ast ast::CoroutineKind) -> Self::Result {
78 self.check_id(coroutine_kind.closure_id());
79 }
80
81 fn visit_param(&mut self, param: &'ast ast::Param) {
82 self.with_lint_attrs(param.id, ¶m.attrs, |cx| {
83 lint_callback!(cx, check_param, param);
84 ast_visit::walk_param(cx, param);
85 });
86 }
87
88 fn visit_item(&mut self, it: &'ast ast::Item) {
89 self.with_lint_attrs(it.id, &it.attrs, |cx| {
90 lint_callback!(cx, check_item, it);
91 ast_visit::walk_item(cx, it);
92 lint_callback!(cx, check_item_post, it);
93 })
94 }
95
96 fn visit_foreign_item(&mut self, it: &'ast ast::ForeignItem) {
97 self.with_lint_attrs(it.id, &it.attrs, |cx| {
98 ast_visit::walk_item(cx, it);
99 })
100 }
101
102 fn visit_pat(&mut self, p: &'ast ast::Pat) {
103 lint_callback!(self, check_pat, p);
104 self.check_id(p.id);
105 ast_visit::walk_pat(self, p);
106 lint_callback!(self, check_pat_post, p);
107 }
108
109 fn visit_pat_field(&mut self, field: &'ast ast::PatField) {
110 self.with_lint_attrs(field.id, &field.attrs, |cx| {
111 ast_visit::walk_pat_field(cx, field);
112 });
113 }
114
115 fn visit_anon_const(&mut self, c: &'ast ast::AnonConst) {
116 self.check_id(c.id);
117 ast_visit::walk_anon_const(self, c);
118 }
119
120 fn visit_expr(&mut self, e: &'ast ast::Expr) {
121 self.with_lint_attrs(e.id, &e.attrs, |cx| {
122 lint_callback!(cx, check_expr, e);
123 ast_visit::walk_expr(cx, e);
124 lint_callback!(cx, check_expr_post, e);
125 })
126 }
127
128 fn visit_expr_field(&mut self, f: &'ast ast::ExprField) {
129 self.with_lint_attrs(f.id, &f.attrs, |cx| {
130 ast_visit::walk_expr_field(cx, f);
131 })
132 }
133
134 fn visit_stmt(&mut self, s: &'ast ast::Stmt) {
135 self.with_lint_attrs(s.id, s.attrs(), |cx| {
144 lint_callback!(cx, check_stmt, s);
145 cx.check_id(s.id);
146 });
147 ast_visit::walk_stmt(self, s);
152 }
153
154 fn visit_fn(&mut self, fk: ast_visit::FnKind<'ast>, span: Span, id: ast::NodeId) {
155 lint_callback!(self, check_fn, fk, span, id);
156 self.check_id(id);
157 ast_visit::walk_fn(self, fk);
158 }
159
160 fn visit_variant_data(&mut self, s: &'ast ast::VariantData) {
161 if let Some(ctor_node_id) = s.ctor_node_id() {
162 self.check_id(ctor_node_id);
163 }
164 ast_visit::walk_struct_def(self, s);
165 }
166
167 fn visit_field_def(&mut self, s: &'ast ast::FieldDef) {
168 self.with_lint_attrs(s.id, &s.attrs, |cx| {
169 ast_visit::walk_field_def(cx, s);
170 })
171 }
172
173 fn visit_variant(&mut self, v: &'ast ast::Variant) {
174 self.with_lint_attrs(v.id, &v.attrs, |cx| {
175 lint_callback!(cx, check_variant, v);
176 ast_visit::walk_variant(cx, v);
177 })
178 }
179
180 fn visit_ty(&mut self, t: &'ast ast::Ty) {
181 lint_callback!(self, check_ty, t);
182 self.check_id(t.id);
183 ast_visit::walk_ty(self, t);
184 }
185
186 fn visit_ident(&mut self, ident: &Ident) {
187 lint_callback!(self, check_ident, ident);
188 }
189
190 fn visit_local(&mut self, l: &'ast ast::Local) {
191 self.with_lint_attrs(l.id, &l.attrs, |cx| {
192 lint_callback!(cx, check_local, l);
193 ast_visit::walk_local(cx, l);
194 })
195 }
196
197 fn visit_block(&mut self, b: &'ast ast::Block) {
198 lint_callback!(self, check_block, b);
199 self.check_id(b.id);
200 ast_visit::walk_block(self, b);
201 }
202
203 fn visit_arm(&mut self, a: &'ast ast::Arm) {
204 self.with_lint_attrs(a.id, &a.attrs, |cx| {
205 lint_callback!(cx, check_arm, a);
206 ast_visit::walk_arm(cx, a);
207 })
208 }
209
210 fn visit_generic_arg(&mut self, arg: &'ast ast::GenericArg) {
211 lint_callback!(self, check_generic_arg, arg);
212 ast_visit::walk_generic_arg(self, arg);
213 }
214
215 fn visit_generic_param(&mut self, param: &'ast ast::GenericParam) {
216 self.with_lint_attrs(param.id, ¶m.attrs, |cx| {
217 lint_callback!(cx, check_generic_param, param);
218 ast_visit::walk_generic_param(cx, param);
219 });
220 }
221
222 fn visit_generics(&mut self, g: &'ast ast::Generics) {
223 lint_callback!(self, check_generics, g);
224 ast_visit::walk_generics(self, g);
225 }
226
227 fn visit_where_predicate(&mut self, p: &'ast ast::WherePredicate) {
228 lint_callback!(self, enter_where_predicate, p);
229 ast_visit::walk_where_predicate(self, p);
230 lint_callback!(self, exit_where_predicate, p);
231 }
232
233 fn visit_poly_trait_ref(&mut self, t: &'ast ast::PolyTraitRef) {
234 lint_callback!(self, check_poly_trait_ref, t);
235 ast_visit::walk_poly_trait_ref(self, t);
236 }
237
238 fn visit_assoc_item(&mut self, item: &'ast ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
239 self.with_lint_attrs(item.id, &item.attrs, |cx| {
240 match ctxt {
241 ast_visit::AssocCtxt::Trait => {
242 lint_callback!(cx, check_trait_item, item);
243 }
244 ast_visit::AssocCtxt::Impl => {
245 lint_callback!(cx, check_impl_item, item);
246 }
247 }
248 ast_visit::walk_assoc_item(cx, item, ctxt);
249 match ctxt {
250 ast_visit::AssocCtxt::Trait => {
251 lint_callback!(cx, check_trait_item_post, item);
252 }
253 ast_visit::AssocCtxt::Impl => {
254 lint_callback!(cx, check_impl_item_post, item);
255 }
256 }
257 });
258 }
259
260 fn visit_lifetime(&mut self, lt: &'ast ast::Lifetime, _: ast_visit::LifetimeCtxt) {
261 self.check_id(lt.id);
262 ast_visit::walk_lifetime(self, lt);
263 }
264
265 fn visit_path(&mut self, p: &'ast ast::Path, id: ast::NodeId) {
266 self.check_id(id);
267 ast_visit::walk_path(self, p);
268 }
269
270 fn visit_path_segment(&mut self, s: &'ast ast::PathSegment) {
271 self.check_id(s.id);
272 ast_visit::walk_path_segment(self, s);
273 }
274
275 fn visit_attribute(&mut self, attr: &'ast ast::Attribute) {
276 lint_callback!(self, check_attribute, attr);
277 ast_visit::walk_attribute(self, attr);
278 }
279
280 fn visit_mac_def(&mut self, mac: &'ast ast::MacroDef, id: ast::NodeId) {
281 lint_callback!(self, check_mac_def, mac);
282 self.check_id(id);
283 }
284
285 fn visit_mac_call(&mut self, mac: &'ast ast::MacCall) {
286 lint_callback!(self, check_mac, mac);
287 ast_visit::walk_mac(self, mac);
288 }
289}
290
291struct RuntimeCombinedEarlyLintPass<'a> {
296 passes: &'a mut [EarlyLintPassObject],
297}
298
299#[allow(rustc::lint_pass_impl_without_macro)]
300impl LintPass for RuntimeCombinedEarlyLintPass<'_> {
301 fn name(&self) -> &'static str {
302 panic!()
303 }
304 fn get_lints(&self) -> crate::LintVec {
305 panic!()
306 }
307}
308
309macro_rules! impl_early_lint_pass {
310 ([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => (
311 impl EarlyLintPass for RuntimeCombinedEarlyLintPass<'_> {
312 $(fn $f(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
313 for pass in self.passes.iter_mut() {
314 pass.$f(context, $($param),*);
315 }
316 })*
317 }
318 )
319}
320
321crate::early_lint_methods!(impl_early_lint_pass, []);
322
323pub trait EarlyCheckNode<'a>: Copy {
326 fn id(self) -> ast::NodeId;
327 fn attrs(self) -> &'a [ast::Attribute];
328 fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>);
329}
330
331impl<'a> EarlyCheckNode<'a> for (&'a ast::Crate, &'a [ast::Attribute]) {
332 fn id(self) -> ast::NodeId {
333 ast::CRATE_NODE_ID
334 }
335 fn attrs(self) -> &'a [ast::Attribute] {
336 self.1
337 }
338 fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) {
339 lint_callback!(cx, check_crate, self.0);
340 ast_visit::walk_crate(cx, self.0);
341 lint_callback!(cx, check_crate_post, self.0);
342 }
343}
344
345impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [P<ast::Item>]) {
346 fn id(self) -> ast::NodeId {
347 self.0
348 }
349 fn attrs(self) -> &'a [ast::Attribute] {
350 self.1
351 }
352 fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) {
353 walk_list!(cx, visit_attribute, self.1);
354 walk_list!(cx, visit_item, self.2);
355 }
356}
357
358pub fn check_ast_node<'a>(
359 sess: &Session,
360 tcx: Option<TyCtxt<'_>>,
361 features: &Features,
362 pre_expansion: bool,
363 lint_store: &LintStore,
364 registered_tools: &RegisteredTools,
365 lint_buffer: Option<LintBuffer>,
366 builtin_lints: impl EarlyLintPass + 'static,
367 check_node: impl EarlyCheckNode<'a>,
368) {
369 let context = EarlyContext::new(
370 sess,
371 features,
372 !pre_expansion,
373 lint_store,
374 registered_tools,
375 lint_buffer.unwrap_or_default(),
376 );
377
378 let passes =
382 if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
383 if passes.is_empty() {
384 check_ast_node_inner(sess, tcx, check_node, context, builtin_lints);
385 } else {
386 let mut passes: Vec<_> = passes.iter().map(|mk_pass| (mk_pass)()).collect();
387 passes.push(Box::new(builtin_lints));
388 let pass = RuntimeCombinedEarlyLintPass { passes: &mut passes[..] };
389 check_ast_node_inner(sess, tcx, check_node, context, pass);
390 }
391}
392
393fn check_ast_node_inner<'a, T: EarlyLintPass>(
394 sess: &Session,
395 tcx: Option<TyCtxt<'_>>,
396 check_node: impl EarlyCheckNode<'a>,
397 context: EarlyContext<'_>,
398 pass: T,
399) {
400 let mut cx = EarlyContextAndPass { context, tcx, pass };
401
402 cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx));
403
404 for (id, lints) in cx.context.buffered.map {
408 if !lints.is_empty() {
409 assert!(
410 sess.dcx().has_errors().is_some(),
411 "failed to process buffered lint here (dummy = {})",
412 id == ast::DUMMY_NODE_ID
413 );
414 break;
415 }
416 }
417}