|
| 1 | +use rustc_hir::{def::DefKind,Body,Item,ItemKind,Path,QPath,TyKind}; |
| 2 | +use rustc_span::{def_id::DefId, sym, symbol::kw,MacroKind}; |
| 3 | + |
| 4 | +use smallvec::{smallvec,SmallVec}; |
| 5 | + |
| 6 | +usecrate::{lints::NonLocalDefinitionsDiag,LateContext,LateLintPass,LintContext}; |
| 7 | + |
| 8 | +declare_lint!{ |
| 9 | +/// The `non_local_definitions` lint checks for `impl` blocks and `#[macro_export]` |
| 10 | +/// macro inside bodies (functions, enum discriminant, ...). |
| 11 | +/// |
| 12 | +/// ### Example |
| 13 | +/// |
| 14 | +/// ```rust |
| 15 | +/// trait MyTrait {} |
| 16 | +/// struct MyStruct; |
| 17 | +/// |
| 18 | +/// fn foo() { |
| 19 | +/// impl MyTrait for MyStruct {} |
| 20 | +/// } |
| 21 | +/// ``` |
| 22 | +/// |
| 23 | +/// {{produces}} |
| 24 | +/// |
| 25 | +/// ### Explanation |
| 26 | +/// |
| 27 | +/// Creating non-local definitions go against expectation and can create discrepancies |
| 28 | +/// in tooling. It should be avoided. It may become deny-by-default in edition 2024 |
| 29 | +/// and higher, see see the tracking issue <https://github.com/rust-lang/rust/issues/120363>. |
| 30 | +/// |
| 31 | +/// An `impl` definition is non-local if it is nested inside an item and neither |
| 32 | +/// the type nor the trait are at the same nesting level as the `impl` block. |
| 33 | +/// |
| 34 | +/// All nested bodies (functions, enum discriminant, array length, consts) (expect for |
| 35 | +/// `const _: Ty = { ... }` in top-level module, which is still undecided) are checked. |
| 36 | +pubNON_LOCAL_DEFINITIONS, |
| 37 | +Warn, |
| 38 | +"checks for non-local definitions", |
| 39 | + report_in_external_macro |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Default)] |
| 43 | +pubstructNonLocalDefinitions{ |
| 44 | +body_depth:u32, |
| 45 | +} |
| 46 | + |
| 47 | +impl_lint_pass!(NonLocalDefinitions => [NON_LOCAL_DEFINITIONS]); |
| 48 | + |
| 49 | +// FIXME(Urgau): Figure out how to handle modules nested in bodies. |
| 50 | +// It's currently not handled by the current logic because modules are not bodies. |
| 51 | +// They don't even follow the correct order (check_body -> check_mod -> check_body_post) |
| 52 | +// instead check_mod is called after every body has been handled. |
| 53 | + |
| 54 | +impl<'tcx>LateLintPass<'tcx>forNonLocalDefinitions{ |
| 55 | +fncheck_body(&mutself,_cx:&LateContext<'tcx>,_body:&'tcxBody<'tcx>){ |
| 56 | +self.body_depth += 1; |
| 57 | +} |
| 58 | + |
| 59 | +fncheck_body_post(&mutself,_cx:&LateContext<'tcx>,_body:&'tcxBody<'tcx>){ |
| 60 | +self.body_depth -= 1; |
| 61 | +} |
| 62 | + |
| 63 | +fncheck_item(&mutself,cx:&LateContext<'tcx>,item:&'tcxItem<'tcx>){ |
| 64 | +ifself.body_depth == 0{ |
| 65 | +return; |
| 66 | +} |
| 67 | + |
| 68 | +let parent = cx.tcx.parent(item.owner_id.def_id.into()); |
| 69 | +let parent_def_kind = cx.tcx.def_kind(parent); |
| 70 | +let parent_opt_item_name = cx.tcx.opt_item_name(parent); |
| 71 | + |
| 72 | +// Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. |
| 73 | +ifself.body_depth == 1 |
| 74 | + && parent_def_kind == DefKind::Const |
| 75 | + && parent_opt_item_name == Some(kw::Underscore) |
| 76 | +{ |
| 77 | +return; |
| 78 | +} |
| 79 | + |
| 80 | +match item.kind{ |
| 81 | +ItemKind::Impl(impl_) => { |
| 82 | +// The RFC states: |
| 83 | +// |
| 84 | +// > An item nested inside an expression-containing item (through any |
| 85 | +// > level of nesting) may not define an impl Trait for Type unless |
| 86 | +// > either the **Trait** or the **Type** is also nested inside the |
| 87 | +// > same expression-containing item. |
| 88 | +// |
| 89 | +// To achieve this we get try to get the paths of the _Trait_ and |
| 90 | +// _Type_, and we look inside thoses paths to try a find in one |
| 91 | +// of them a type whose parent is the same as the impl definition. |
| 92 | +// |
| 93 | +// If that's the case this means that this impl block declaration |
| 94 | +// is using local items and so we don't lint on it. |
| 95 | + |
| 96 | +// We also ignore anon-const in item by including the anon-const |
| 97 | +// parent as well; and since it's quite uncommon, we use smallvec |
| 98 | +// to avoid unnecessary heap allocations. |
| 99 | +let local_parents:SmallVec<[DefId;1]> = if parent_def_kind == DefKind::Const |
| 100 | + && parent_opt_item_name == Some(kw::Underscore) |
| 101 | +{ |
| 102 | +smallvec![parent, cx.tcx.parent(parent)] |
| 103 | +}else{ |
| 104 | +smallvec![parent] |
| 105 | +}; |
| 106 | + |
| 107 | +let self_ty_has_local_parent = match impl_.self_ty.kind{ |
| 108 | +TyKind::Path(QPath::Resolved(_, ty_path)) => { |
| 109 | +path_has_local_parent(ty_path, cx,&*local_parents) |
| 110 | +} |
| 111 | +TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => { |
| 112 | +path_has_local_parent( |
| 113 | + principle_poly_trait_ref.trait_ref.path, |
| 114 | + cx, |
| 115 | +&*local_parents, |
| 116 | +) |
| 117 | +} |
| 118 | +TyKind::TraitObject([], _, _) |
| 119 | + | TyKind::InferDelegation(_, _) |
| 120 | + | TyKind::Slice(_) |
| 121 | + | TyKind::Array(_, _) |
| 122 | + | TyKind::Ptr(_) |
| 123 | + | TyKind::Ref(_, _) |
| 124 | + | TyKind::BareFn(_) |
| 125 | + | TyKind::Never |
| 126 | + | TyKind::Tup(_) |
| 127 | + | TyKind::Path(_) |
| 128 | + | TyKind::AnonAdt(_) |
| 129 | + | TyKind::OpaqueDef(_, _, _) |
| 130 | + | TyKind::Typeof(_) |
| 131 | + | TyKind::Infer |
| 132 | + | TyKind::Err(_) => false, |
| 133 | +}; |
| 134 | + |
| 135 | +let of_trait_has_local_parent = impl_ |
| 136 | +.of_trait |
| 137 | +.map(|of_trait| path_has_local_parent(of_trait.path, cx,&*local_parents)) |
| 138 | +.unwrap_or(false); |
| 139 | + |
| 140 | +// If none of them have a local parent (LOGICAL NOR) this means that |
| 141 | +// this impl definition is a non-local definition and so we lint on it. |
| 142 | +if !(self_ty_has_local_parent || of_trait_has_local_parent){ |
| 143 | + cx.emit_span_lint( |
| 144 | +NON_LOCAL_DEFINITIONS, |
| 145 | + item.span, |
| 146 | +NonLocalDefinitionsDiag::Impl{ |
| 147 | +depth:self.body_depth, |
| 148 | +body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent), |
| 149 | +body_name: parent_opt_item_name |
| 150 | +.map(|s| s.to_ident_string()) |
| 151 | +.unwrap_or_else(|| "<unnameable>".to_string()), |
| 152 | +}, |
| 153 | +) |
| 154 | +} |
| 155 | +} |
| 156 | +ItemKind::Macro(_macro,MacroKind::Bang) |
| 157 | +if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) => |
| 158 | +{ |
| 159 | + cx.emit_span_lint( |
| 160 | +NON_LOCAL_DEFINITIONS, |
| 161 | + item.span, |
| 162 | +NonLocalDefinitionsDiag::MacroRules{ |
| 163 | +depth:self.body_depth, |
| 164 | +body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent), |
| 165 | +body_name: parent_opt_item_name |
| 166 | +.map(|s| s.to_ident_string()) |
| 167 | +.unwrap_or_else(|| "<unnameable>".to_string()), |
| 168 | +}, |
| 169 | +) |
| 170 | +} |
| 171 | + _ => {} |
| 172 | +} |
| 173 | +} |
| 174 | +} |
| 175 | + |
| 176 | +/// Given a path and a parent impl def id, this checks if the if parent resolution |
| 177 | +/// def id correspond to the def id of the parent impl definition. |
| 178 | +/// |
| 179 | +/// Given this path, we will look at the path (and ignore any generic args): |
| 180 | +/// |
| 181 | +/// ```text |
| 182 | +/// std::convert::PartialEq<Foo<Bar>> |
| 183 | +/// ^^^^^^^^^^^^^^^^^^^^^^^ |
| 184 | +/// ``` |
| 185 | +fnpath_has_local_parent(path:&Path<'_>,cx:&LateContext<'_>,local_parents:&[DefId]) -> bool{ |
| 186 | + path.res.opt_def_id().is_some_and(|did| local_parents.contains(&cx.tcx.parent(did))) |
| 187 | +} |
0 commit comments