|
| 1 | +use rustc_hir as hir; |
| 2 | +use rustc_hir::def_id::LocalDefId; |
| 3 | +use rustc_hir::intravisit::{ErasedMap, FnKind, NestedVisitorMap, Visitor}; |
| 4 | +use rustc_middle::ty::query::Providers; |
| 5 | +use rustc_middle::ty::TyCtxt; |
| 6 | +use rustc_span::symbol::sym; |
| 7 | +use rustc_span::Span; |
| 8 | + |
| 9 | +fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { |
| 10 | + tcx.hir().visit_item_likes_in_module( |
| 11 | + module_def_id, |
| 12 | + &mut CheckNakedFunctions { tcx }.as_deep_visitor(), |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +crate fn provide(providers: &mut Providers) { |
| 17 | + *providers = Providers { check_mod_naked_functions, ..*providers }; |
| 18 | +} |
| 19 | + |
| 20 | +struct CheckNakedFunctions<'tcx> { |
| 21 | + tcx: TyCtxt<'tcx>, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> { |
| 25 | + type Map = ErasedMap<'tcx>; |
| 26 | + |
| 27 | + fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> { |
| 28 | + NestedVisitorMap::None |
| 29 | + } |
| 30 | + |
| 31 | + fn visit_fn( |
| 32 | + &mut self, |
| 33 | + fk: FnKind<'v>, |
| 34 | + _fd: &'tcx hir::FnDecl<'tcx>, |
| 35 | + body_id: hir::BodyId, |
| 36 | + _span: Span, |
| 37 | + _hir_id: hir::HirId, |
| 38 | + ) { |
| 39 | + match fk { |
| 40 | + // Rejected during attribute check. Do not validate further. |
| 41 | + FnKind::Closure(..) => return, |
| 42 | + FnKind::ItemFn(..) | FnKind::Method(..) => {} |
| 43 | + } |
| 44 | + |
| 45 | + let naked = fk.attrs().iter().any(|attr| attr.has_name(sym::naked)); |
| 46 | + if naked { |
| 47 | + let body = self.tcx.hir().body(body_id); |
| 48 | + check_params(self.tcx, body); |
| 49 | + check_body(self.tcx, body); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// Checks that parameters don't use patterns. Mirrors the checks for function declarations. |
| 55 | +fn check_params(tcx: TyCtxt<'_>, body: &hir::Body<'_>) { |
| 56 | + for param in body.params { |
| 57 | + match param.pat.kind { |
| 58 | + hir::PatKind::Wild |
| 59 | + | hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, _, None) => {} |
| 60 | + _ => { |
| 61 | + tcx.sess |
| 62 | + .struct_span_err( |
| 63 | + param.pat.span, |
| 64 | + "patterns not allowed in naked function parameters", |
| 65 | + ) |
| 66 | + .emit(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// Checks that function parameters aren't referenced in the function body. |
| 73 | +fn check_body<'tcx>(tcx: TyCtxt<'tcx>, body: &'tcx hir::Body<'tcx>) { |
| 74 | + let mut params = hir::HirIdSet::default(); |
| 75 | + for param in body.params { |
| 76 | + param.pat.each_binding(|_binding_mode, hir_id, _span, _ident| { |
| 77 | + params.insert(hir_id); |
| 78 | + }); |
| 79 | + } |
| 80 | + CheckBody { tcx, params }.visit_body(body); |
| 81 | +} |
| 82 | + |
| 83 | +struct CheckBody<'tcx> { |
| 84 | + tcx: TyCtxt<'tcx>, |
| 85 | + params: hir::HirIdSet, |
| 86 | +} |
| 87 | + |
| 88 | +impl<'tcx> Visitor<'tcx> for CheckBody<'tcx> { |
| 89 | + type Map = ErasedMap<'tcx>; |
| 90 | + |
| 91 | + fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> { |
| 92 | + NestedVisitorMap::None |
| 93 | + } |
| 94 | + |
| 95 | + fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { |
| 96 | + if let hir::ExprKind::Path(hir::QPath::Resolved( |
| 97 | + _, |
| 98 | + hir::Path { res: hir::def::Res::Local(var_hir_id), .. }, |
| 99 | + )) = expr.kind |
| 100 | + { |
| 101 | + if self.params.contains(var_hir_id) { |
| 102 | + self.tcx |
| 103 | + .sess |
| 104 | + .struct_span_err( |
| 105 | + expr.span, |
| 106 | + "use of parameters not allowed inside naked functions", |
| 107 | + ) |
| 108 | + .emit(); |
| 109 | + } |
| 110 | + } |
| 111 | + hir::intravisit::walk_expr(self, expr); |
| 112 | + } |
| 113 | +} |
0 commit comments