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
21pub(super) mod 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_builtin_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_id(&mut self, id: rustc_ast::NodeId) {
78 self.check_id(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 ast_visit::walk_pat(self, p);
105 lint_callback!(self, check_pat_post, p);
106 }
107
108 fn visit_pat_field(&mut self, field: &'ast ast::PatField) {
109 self.with_lint_attrs(field.id, &field.attrs, |cx| {
110 ast_visit::walk_pat_field(cx, field);
111 });
112 }
113
114 fn visit_expr(&mut self, e: &'ast ast::Expr) {
115 self.with_lint_attrs(e.id, &e.attrs, |cx| {
116 lint_callback!(cx, check_expr, e);
117 ast_visit::walk_expr(cx, e);
118 lint_callback!(cx, check_expr_post, e);
119 })
120 }
121
122 fn visit_expr_field(&mut self, f: &'ast ast::ExprField) {
123 self.with_lint_attrs(f.id, &f.attrs, |cx| {
124 ast_visit::walk_expr_field(cx, f);
125 })
126 }
127
128 fn visit_stmt(&mut self, s: &'ast ast::Stmt) {
129 self.with_lint_attrs(s.id, s.attrs(), |cx| {
138 lint_callback!(cx, check_stmt, s);
139 cx.check_id(s.id);
140 });
141 ast_visit::walk_stmt(self, s);
146 }
147
148 fn visit_fn(&mut self, fk: ast_visit::FnKind<'ast>, span: Span, id: ast::NodeId) {
149 lint_callback!(self, check_fn, fk, span, id);
150 self.check_id(id);
151 ast_visit::walk_fn(self, fk);
152 }
153
154 fn visit_field_def(&mut self, s: &'ast ast::FieldDef) {
155 self.with_lint_attrs(s.id, &s.attrs, |cx| {
156 ast_visit::walk_field_def(cx, s);
157 })
158 }
159
160 fn visit_variant(&mut self, v: &'ast ast::Variant) {
161 self.with_lint_attrs(v.id, &v.attrs, |cx| {
162 lint_callback!(cx, check_variant, v);
163 ast_visit::walk_variant(cx, v);
164 })
165 }
166
167 fn visit_ty(&mut self, t: &'ast ast::Ty) {
168 lint_callback!(self, check_ty, t);
169 ast_visit::walk_ty(self, t);
170 }
171
172 fn visit_ident(&mut self, ident: &Ident) {
173 lint_callback!(self, check_ident, ident);
174 }
175
176 fn visit_local(&mut self, l: &'ast ast::Local) {
177 self.with_lint_attrs(l.id, &l.attrs, |cx| {
178 lint_callback!(cx, check_local, l);
179 ast_visit::walk_local(cx, l);
180 })
181 }
182
183 fn visit_block(&mut self, b: &'ast ast::Block) {
184 lint_callback!(self, check_block, b);
185 ast_visit::walk_block(self, b);
186 }
187
188 fn visit_arm(&mut self, a: &'ast ast::Arm) {
189 self.with_lint_attrs(a.id, &a.attrs, |cx| {
190 lint_callback!(cx, check_arm, a);
191 ast_visit::walk_arm(cx, a);
192 })
193 }
194
195 fn visit_generic_arg(&mut self, arg: &'ast ast::GenericArg) {
196 lint_callback!(self, check_generic_arg, arg);
197 ast_visit::walk_generic_arg(self, arg);
198 }
199
200 fn visit_generic_param(&mut self, param: &'ast ast::GenericParam) {
201 self.with_lint_attrs(param.id, ¶m.attrs, |cx| {
202 lint_callback!(cx, check_generic_param, param);
203 ast_visit::walk_generic_param(cx, param);
204 });
205 }
206
207 fn visit_generics(&mut self, g: &'ast ast::Generics) {
208 lint_callback!(self, check_generics, g);
209 ast_visit::walk_generics(self, g);
210 }
211
212 fn visit_where_predicate(&mut self, p: &'ast ast::WherePredicate) {
213 lint_callback!(self, enter_where_predicate, p);
214 ast_visit::walk_where_predicate(self, p);
215 lint_callback!(self, exit_where_predicate, p);
216 }
217
218 fn visit_poly_trait_ref(&mut self, t: &'ast ast::PolyTraitRef) {
219 lint_callback!(self, check_poly_trait_ref, t);
220 ast_visit::walk_poly_trait_ref(self, t);
221 }
222
223 fn visit_assoc_item(&mut self, item: &'ast ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
224 self.with_lint_attrs(item.id, &item.attrs, |cx| {
225 match ctxt {
226 ast_visit::AssocCtxt::Trait => {
227 lint_callback!(cx, check_trait_item, item);
228 }
229 ast_visit::AssocCtxt::Impl { .. } => {
230 lint_callback!(cx, check_impl_item, item);
231 }
232 }
233 ast_visit::walk_assoc_item(cx, item, ctxt);
234 match ctxt {
235 ast_visit::AssocCtxt::Trait => {
236 lint_callback!(cx, check_trait_item_post, item);
237 }
238 ast_visit::AssocCtxt::Impl { .. } => {
239 lint_callback!(cx, check_impl_item_post, item);
240 }
241 }
242 });
243 }
244
245 fn visit_attribute(&mut self, attr: &'ast ast::Attribute) {
246 lint_callback!(self, check_attribute, attr);
247 ast_visit::walk_attribute(self, attr);
248 }
249
250 fn visit_macro_def(&mut self, mac: &'ast ast::MacroDef) {
251 lint_callback!(self, check_mac_def, mac);
252 }
253
254 fn visit_mac_call(&mut self, mac: &'ast ast::MacCall) {
255 lint_callback!(self, check_mac, mac);
256 ast_visit::walk_mac(self, mac);
257 }
258}
259
260struct RuntimeCombinedEarlyLintPass<'a> {
265 passes: &'a mut [EarlyLintPassObject],
266}
267
268#[allow(rustc::lint_pass_impl_without_macro)]
269impl LintPass for RuntimeCombinedEarlyLintPass<'_> {
270 fn name(&self) -> &'static str {
271 panic!()
272 }
273 fn get_lints(&self) -> crate::LintVec {
274 panic!()
275 }
276}
277
278macro_rules! impl_early_lint_pass {
279 ([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => (
280 impl EarlyLintPass for RuntimeCombinedEarlyLintPass<'_> {
281 $(fn $f(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
282 for pass in self.passes.iter_mut() {
283 pass.$f(context, $($param),*);
284 }
285 })*
286 }
287 )
288}
289
290crate::early_lint_methods!(impl_early_lint_pass, []);
291
292pub trait EarlyCheckNode<'a>: Copy {
295 fn id(self) -> ast::NodeId;
296 fn attrs(self) -> &'a [ast::Attribute];
297 fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>);
298}
299
300impl<'a> EarlyCheckNode<'a> for (&'a ast::Crate, &'a [ast::Attribute]) {
301 fn id(self) -> ast::NodeId {
302 ast::CRATE_NODE_ID
303 }
304 fn attrs(self) -> &'a [ast::Attribute] {
305 self.1
306 }
307 fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) {
308 lint_callback!(cx, check_crate, self.0);
309 ast_visit::walk_crate(cx, self.0);
310 lint_callback!(cx, check_crate_post, self.0);
311 }
312}
313
314impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [P<ast::Item>]) {
315 fn id(self) -> ast::NodeId {
316 self.0
317 }
318 fn attrs(self) -> &'a [ast::Attribute] {
319 self.1
320 }
321 fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) {
322 walk_list!(cx, visit_attribute, self.1);
323 walk_list!(cx, visit_item, self.2);
324 }
325}
326
327pub fn check_ast_node<'a>(
328 sess: &Session,
329 tcx: Option<TyCtxt<'_>>,
330 features: &Features,
331 pre_expansion: bool,
332 lint_store: &LintStore,
333 registered_tools: &RegisteredTools,
334 lint_buffer: Option<LintBuffer>,
335 builtin_lints: impl EarlyLintPass + 'static,
336 check_node: impl EarlyCheckNode<'a>,
337) {
338 let context = EarlyContext::new(
339 sess,
340 features,
341 !pre_expansion,
342 lint_store,
343 registered_tools,
344 lint_buffer.unwrap_or_default(),
345 );
346
347 let passes =
351 if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
352 if passes.is_empty() {
353 check_ast_node_inner(sess, tcx, check_node, context, builtin_lints);
354 } else {
355 let mut passes: Vec<_> = passes.iter().map(|mk_pass| (mk_pass)()).collect();
356 passes.push(Box::new(builtin_lints));
357 let pass = RuntimeCombinedEarlyLintPass { passes: &mut passes[..] };
358 check_ast_node_inner(sess, tcx, check_node, context, pass);
359 }
360}
361
362fn check_ast_node_inner<'a, T: EarlyLintPass>(
363 sess: &Session,
364 tcx: Option<TyCtxt<'_>>,
365 check_node: impl EarlyCheckNode<'a>,
366 context: EarlyContext<'_>,
367 pass: T,
368) {
369 let mut cx = EarlyContextAndPass { context, tcx, pass };
370
371 cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx));
372
373 for (id, lints) in cx.context.buffered.map {
377 if !lints.is_empty() {
378 assert!(
379 sess.dcx().has_errors().is_some(),
380 "failed to process buffered lint here (dummy = {})",
381 id == ast::DUMMY_NODE_ID
382 );
383 break;
384 }
385 }
386}