rustc_lint/
non_local_def.rs

1use rustc_errors::MultiSpan;
2use rustc_hir::def::{DefKind, Res};
3use rustc_hir::intravisit::{self, Visitor, VisitorExt};
4use rustc_hir::{Body, HirId, Item, ItemKind, Node, Path, TyKind};
5use rustc_middle::ty::TyCtxt;
6use rustc_session::{declare_lint, impl_lint_pass};
7use rustc_span::def_id::{DefId, LOCAL_CRATE};
8use rustc_span::{ExpnKind, MacroKind, Span, kw, sym};
9
10use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag};
11use crate::{LateContext, LateLintPass, LintContext, fluent_generated as fluent};
12
13declare_lint! {
14    /// The `non_local_definitions` lint checks for `impl` blocks and `#[macro_export]`
15    /// macro inside bodies (functions, enum discriminant, ...).
16    ///
17    /// ### Example
18    ///
19    /// ```rust
20    /// #![warn(non_local_definitions)]
21    /// trait MyTrait {}
22    /// struct MyStruct;
23    ///
24    /// fn foo() {
25    ///     impl MyTrait for MyStruct {}
26    /// }
27    /// ```
28    ///
29    /// {{produces}}
30    ///
31    /// ### Explanation
32    ///
33    /// Creating non-local definitions go against expectation and can create discrepancies
34    /// in tooling. It should be avoided. It may become deny-by-default in edition 2024
35    /// and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>.
36    ///
37    /// An `impl` definition is non-local if it is nested inside an item and neither
38    /// the type nor the trait are at the same nesting level as the `impl` block.
39    ///
40    /// All nested bodies (functions, enum discriminant, array length, consts) (expect for
41    /// `const _: Ty = { ... }` in top-level module, which is still undecided) are checked.
42    pub NON_LOCAL_DEFINITIONS,
43    Warn,
44    "checks for non-local definitions",
45    report_in_external_macro
46}
47
48#[derive(Default)]
49pub(crate) struct NonLocalDefinitions {
50    body_depth: u32,
51}
52
53impl_lint_pass!(NonLocalDefinitions => [NON_LOCAL_DEFINITIONS]);
54
55// FIXME(Urgau): Figure out how to handle modules nested in bodies.
56// It's currently not handled by the current logic because modules are not bodies.
57// They don't even follow the correct order (check_body -> check_mod -> check_body_post)
58// instead check_mod is called after every body has been handled.
59
60impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
61    fn check_body(&mut self, _cx: &LateContext<'tcx>, _body: &Body<'tcx>) {
62        self.body_depth += 1;
63    }
64
65    fn check_body_post(&mut self, _cx: &LateContext<'tcx>, _body: &Body<'tcx>) {
66        self.body_depth -= 1;
67    }
68
69    fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
70        if self.body_depth == 0 {
71            return;
72        }
73
74        let def_id = item.owner_id.def_id.into();
75        let parent = cx.tcx.parent(def_id);
76        let parent_def_kind = cx.tcx.def_kind(parent);
77        let parent_opt_item_name = cx.tcx.opt_item_name(parent);
78
79        // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module.
80        if self.body_depth == 1
81            && parent_def_kind == DefKind::Const
82            && parent_opt_item_name == Some(kw::Underscore)
83        {
84            return;
85        }
86
87        let cargo_update = || {
88            let oexpn = item.span.ctxt().outer_expn_data();
89            if let Some(def_id) = oexpn.macro_def_id
90                && let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind
91                && def_id.krate != LOCAL_CRATE
92                && rustc_session::utils::was_invoked_from_cargo()
93            {
94                Some(NonLocalDefinitionsCargoUpdateNote {
95                    macro_kind: macro_kind.descr(),
96                    macro_name,
97                    crate_name: cx.tcx.crate_name(def_id.krate),
98                })
99            } else {
100                None
101            }
102        };
103
104        // determining if we are in a doctest context can't currently be determined
105        // by the code itself (there are no specific attributes), but fortunately rustdoc
106        // sets a perma-unstable env var for libtest so we just reuse that for now
107        let is_at_toplevel_doctest = || {
108            self.body_depth == 2
109                && cx.tcx.env_var_os("UNSTABLE_RUSTDOC_TEST_PATH".as_ref()).is_some()
110        };
111
112        match item.kind {
113            ItemKind::Impl(impl_) => {
114                // The RFC states:
115                //
116                // > An item nested inside an expression-containing item (through any
117                // > level of nesting) may not define an impl Trait for Type unless
118                // > either the **Trait** or the **Type** is also nested inside the
119                // > same expression-containing item.
120                //
121                // To achieve this we get try to get the paths of the _Trait_ and
122                // _Type_, and we look inside those paths to try a find in one
123                // of them a type whose parent is the same as the impl definition.
124                //
125                // If that's the case this means that this impl block declaration
126                // is using local items and so we don't lint on it.
127
128                // 1. We collect all the `hir::Path` from the `Self` type and `Trait` ref
129                // of the `impl` definition
130                let mut collector = PathCollector { paths: Vec::new() };
131                collector.visit_ty_unambig(&impl_.self_ty);
132                if let Some(of_trait) = &impl_.of_trait {
133                    collector.visit_trait_ref(of_trait);
134                }
135
136                // 1.5. Remove any path that doesn't resolve to a `DefId` or if it resolve to a
137                // type-param (e.g. `T`).
138                collector.paths.retain(
139                    |p| matches!(p.res, Res::Def(def_kind, _) if def_kind != DefKind::TyParam),
140                );
141
142                // 1.9. We retrieve the parent def id of the impl item, ...
143                //
144                // ... modulo const-anons items, for enhanced compatibility with the ecosystem
145                // as that pattern is common with `serde`, `bevy`, ...
146                //
147                // For this example we want the `DefId` parent of the outermost const-anon items.
148                // ```
149                // const _: () = { // the parent of this const-anon
150                //     const _: () = {
151                //         impl Foo {}
152                //     };
153                // };
154                // ```
155                //
156                // It isn't possible to mix a impl in a module with const-anon, but an item can
157                // be put inside a module and referenced by a impl so we also have to treat the
158                // item parent as transparent to module and for consistency we have to do the same
159                // for impl, otherwise the item-def and impl-def won't have the same parent.
160                let outermost_impl_parent = peel_parent_while(cx.tcx, parent, |tcx, did| {
161                    tcx.def_kind(did) == DefKind::Mod
162                        || (tcx.def_kind(did) == DefKind::Const
163                            && tcx.opt_item_name(did) == Some(kw::Underscore))
164                });
165
166                // 2. We check if any of the paths reference a the `impl`-parent.
167                //
168                // If that the case we bail out, as was asked by T-lang, even though this isn't
169                // correct from a type-system point of view, as inference exists and one-impl-rule
170                // make its so that we could still leak the impl.
171                if collector
172                    .paths
173                    .iter()
174                    .any(|path| path_has_local_parent(path, cx, parent, outermost_impl_parent))
175                {
176                    return;
177                }
178
179                // Get the span of the parent const item ident (if it's a not a const anon).
180                //
181                // Used to suggest changing the const item to a const anon.
182                let span_for_const_anon_suggestion = if parent_def_kind == DefKind::Const
183                    && parent_opt_item_name != Some(kw::Underscore)
184                    && let Some(parent) = parent.as_local()
185                    && let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent)
186                    && let ItemKind::Const(ident, ty, _, _) = item.kind
187                    && let TyKind::Tup(&[]) = ty.kind
188                {
189                    Some(ident.span)
190                } else {
191                    None
192                };
193
194                let const_anon = matches!(parent_def_kind, DefKind::Const | DefKind::Static { .. })
195                    .then_some(span_for_const_anon_suggestion);
196
197                let impl_span = item.span.shrink_to_lo().to(impl_.self_ty.span);
198                let mut ms = MultiSpan::from_span(impl_span);
199
200                for path in &collector.paths {
201                    // FIXME: While a translatable diagnostic message can have an argument
202                    // we (currently) have no way to set different args per diag msg with
203                    // `MultiSpan::push_span_label`.
204                    #[allow(rustc::untranslatable_diagnostic)]
205                    ms.push_span_label(
206                        path_span_without_args(path),
207                        format!("`{}` is not local", path_name_to_string(path)),
208                    );
209                }
210
211                let doctest = is_at_toplevel_doctest();
212
213                if !doctest {
214                    ms.push_span_label(
215                        cx.tcx.def_span(parent),
216                        fluent::lint_non_local_definitions_impl_move_help,
217                    );
218                }
219
220                let macro_to_change =
221                    if let ExpnKind::Macro(kind, name) = item.span.ctxt().outer_expn_data().kind {
222                        Some((name.to_string(), kind.descr()))
223                    } else {
224                        None
225                    };
226
227                cx.emit_span_lint(
228                    NON_LOCAL_DEFINITIONS,
229                    ms,
230                    NonLocalDefinitionsDiag::Impl {
231                        depth: self.body_depth,
232                        body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent),
233                        body_name: parent_opt_item_name
234                            .map(|s| s.to_ident_string())
235                            .unwrap_or_else(|| "<unnameable>".to_string()),
236                        cargo_update: cargo_update(),
237                        const_anon,
238                        doctest,
239                        macro_to_change,
240                    },
241                )
242            }
243            ItemKind::Macro(_, _macro, MacroKind::Bang)
244                if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) =>
245            {
246                cx.emit_span_lint(
247                    NON_LOCAL_DEFINITIONS,
248                    item.span,
249                    NonLocalDefinitionsDiag::MacroRules {
250                        depth: self.body_depth,
251                        body_kind_descr: cx.tcx.def_kind_descr(parent_def_kind, parent),
252                        body_name: parent_opt_item_name
253                            .map(|s| s.to_ident_string())
254                            .unwrap_or_else(|| "<unnameable>".to_string()),
255                        cargo_update: cargo_update(),
256                        doctest: is_at_toplevel_doctest(),
257                    },
258                )
259            }
260            _ => {}
261        }
262    }
263}
264
265/// Simple hir::Path collector
266struct PathCollector<'tcx> {
267    paths: Vec<Path<'tcx>>,
268}
269
270impl<'tcx> Visitor<'tcx> for PathCollector<'tcx> {
271    fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) {
272        self.paths.push(path.clone()); // need to clone, bc of the restricted lifetime
273        intravisit::walk_path(self, path)
274    }
275}
276
277/// Given a path, this checks if the if the parent resolution def id corresponds to
278/// the def id of the parent impl definition (the direct one and the outermost one).
279///
280/// Given this path, we will look at the path (and ignore any generic args):
281///
282/// ```text
283///    std::convert::PartialEq<Foo<Bar>>
284///    ^^^^^^^^^^^^^^^^^^^^^^^
285/// ```
286#[inline]
287fn path_has_local_parent(
288    path: &Path<'_>,
289    cx: &LateContext<'_>,
290    impl_parent: DefId,
291    outermost_impl_parent: Option<DefId>,
292) -> bool {
293    path.res
294        .opt_def_id()
295        .is_some_and(|did| did_has_local_parent(did, cx.tcx, impl_parent, outermost_impl_parent))
296}
297
298/// Given a def id this checks if the parent def id (modulo modules) correspond to
299/// the def id of the parent impl definition (the direct one and the outermost one).
300#[inline]
301fn did_has_local_parent(
302    did: DefId,
303    tcx: TyCtxt<'_>,
304    impl_parent: DefId,
305    outermost_impl_parent: Option<DefId>,
306) -> bool {
307    if !did.is_local() {
308        return false;
309    }
310
311    let Some(parent_did) = tcx.opt_parent(did) else {
312        return false;
313    };
314
315    peel_parent_while(tcx, parent_did, |tcx, did| {
316        tcx.def_kind(did) == DefKind::Mod
317            || (tcx.def_kind(did) == DefKind::Const
318                && tcx.opt_item_name(did) == Some(kw::Underscore))
319    })
320    .map(|parent_did| parent_did == impl_parent || Some(parent_did) == outermost_impl_parent)
321    .unwrap_or(false)
322}
323
324/// Given a `DefId` checks if it satisfies `f` if it does check with it's parent and continue
325/// until it doesn't satisfies `f` and return the last `DefId` checked.
326///
327/// In other word this method return the first `DefId` that doesn't satisfies `f`.
328#[inline]
329fn peel_parent_while(
330    tcx: TyCtxt<'_>,
331    mut did: DefId,
332    mut f: impl FnMut(TyCtxt<'_>, DefId) -> bool,
333) -> Option<DefId> {
334    while !did.is_crate_root() && f(tcx, did) {
335        did = tcx.opt_parent(did).filter(|parent_did| parent_did.is_local())?;
336    }
337
338    Some(did)
339}
340
341/// Return for a given `Path` the span until the last args
342fn path_span_without_args(path: &Path<'_>) -> Span {
343    if let Some(args) = &path.segments.last().unwrap().args {
344        path.span.until(args.span_ext)
345    } else {
346        path.span
347    }
348}
349
350/// Return a "error message-able" ident for the last segment of the `Path`
351fn path_name_to_string(path: &Path<'_>) -> String {
352    path.segments.last().unwrap().ident.to_string()
353}