Skip to content
/ rust Public
forked from rust-lang/rust

Commit 32b2e63

Browse files
authored
Rollup merge of rust-lang#121813 - Urgau:misc-non_local_defs-lint, r=cjgillot
Misc improvements to non local defs lint implementation This PR is a collection of small improvements I found when I [needlessly tried](https://www.github.com/rust-lang/rust/pull/120393#issuecomment-1971787475) to fix a "perf-regression" in the lint implementation. I recommend looking at each commit individually.
2 parents 3c6b895 + 98dbe9a commit 32b2e63

File tree

5 files changed

+78
-30
lines changed

5 files changed

+78
-30
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4133,7 +4133,6 @@ dependencies = [
41334133
"rustc_target",
41344134
"rustc_trait_selection",
41354135
"rustc_type_ir",
4136-
"smallvec",
41374136
"tracing",
41384137
"unicode-security",
41394138
]

compiler/rustc_lint/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ rustc_span = { path = "../rustc_span" }
2323
rustc_target = { path = "../rustc_target" }
2424
rustc_trait_selection = { path = "../rustc_trait_selection" }
2525
rustc_type_ir = { path = "../rustc_type_ir" }
26-
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
2726
tracing = "0.1"
2827
unicode-security = "0.1.0"
2928
# tidy-alphabetical-end

compiler/rustc_lint/src/non_local_def.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use rustc_hir::{def::DefKind, Body, Item, ItemKind, Node, Path, QPath, TyKind};
22
use rustc_span::def_id::{DefId, LOCAL_CRATE};
33
use rustc_span::{sym, symbol::kw, ExpnKind, MacroKind};
44

5-
use smallvec::{smallvec, SmallVec};
6-
75
use crate::lints::{NonLocalDefinitionsCargoUpdateNote, NonLocalDefinitionsDiag};
86
use crate::{LateContext, LateLintPass, LintContext};
97

@@ -85,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
8583
if let Some(def_id) = oexpn.macro_def_id
8684
&& let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind
8785
&& def_id.krate != LOCAL_CRATE
88-
&& std::env::var_os("CARGO").is_some()
86+
&& rustc_session::utils::was_invoked_from_cargo()
8987
{
9088
Some(NonLocalDefinitionsCargoUpdateNote {
9189
macro_kind: macro_kind.descr(),
@@ -114,25 +112,25 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
114112
// is using local items and so we don't lint on it.
115113

116114
// We also ignore anon-const in item by including the anon-const
117-
// parent as well; and since it's quite uncommon, we use smallvec
118-
// to avoid unnecessary heap allocations.
119-
let local_parents: SmallVec<[DefId; 1]> = if parent_def_kind == DefKind::Const
115+
// parent as well.
116+
let parent_parent = if parent_def_kind == DefKind::Const
120117
&& parent_opt_item_name == Some(kw::Underscore)
121118
{
122-
smallvec![parent, cx.tcx.parent(parent)]
119+
Some(cx.tcx.parent(parent))
123120
} else {
124-
smallvec![parent]
121+
None
125122
};
126123

127124
let self_ty_has_local_parent = match impl_.self_ty.kind {
128125
TyKind::Path(QPath::Resolved(_, ty_path)) => {
129-
path_has_local_parent(ty_path, cx, &*local_parents)
126+
path_has_local_parent(ty_path, cx, parent, parent_parent)
130127
}
131128
TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => {
132129
path_has_local_parent(
133130
principle_poly_trait_ref.trait_ref.path,
134131
cx,
135-
&*local_parents,
132+
parent,
133+
parent_parent,
136134
)
137135
}
138136
TyKind::TraitObject([], _, _)
@@ -154,7 +152,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
154152

155153
let of_trait_has_local_parent = impl_
156154
.of_trait
157-
.map(|of_trait| path_has_local_parent(of_trait.path, cx, &*local_parents))
155+
.map(|of_trait| path_has_local_parent(of_trait.path, cx, parent, parent_parent))
158156
.unwrap_or(false);
159157

160158
// If none of them have a local parent (LOGICAL NOR) this means that
@@ -218,6 +216,16 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
218216
/// std::convert::PartialEq<Foo<Bar>>
219217
/// ^^^^^^^^^^^^^^^^^^^^^^^
220218
/// ```
221-
fn path_has_local_parent(path: &Path<'_>, cx: &LateContext<'_>, local_parents: &[DefId]) -> bool {
222-
path.res.opt_def_id().is_some_and(|did| local_parents.contains(&cx.tcx.parent(did)))
219+
fn path_has_local_parent(
220+
path: &Path<'_>,
221+
cx: &LateContext<'_>,
222+
impl_parent: DefId,
223+
impl_parent_parent: Option<DefId>,
224+
) -> bool {
225+
path.res.opt_def_id().is_some_and(|did| {
226+
did.is_local() && {
227+
let res_parent = cx.tcx.parent(did);
228+
res_parent == impl_parent || Some(res_parent) == impl_parent_parent
229+
}
230+
})
223231
}

tests/ui/lint/non_local_definitions.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ check-pass
22
//@ edition:2021
33
//@ aux-build:non_local_macro.rs
4-
//@ rustc-env:CARGO=/usr/bin/cargo
4+
//@ rustc-env:CARGO_CRATE_NAME=non_local_def
55

66
#![feature(inline_const)]
77
#![warn(non_local_definitions)]
@@ -245,6 +245,26 @@ fn bad() {
245245
//~^ WARN non-local `impl` definition
246246
}
247247

248+
trait Uto9 {}
249+
trait Uto10 {}
250+
const _: u32 = {
251+
let _a = || {
252+
impl Uto9 for Test {}
253+
//~^ WARN non-local `impl` definition
254+
255+
1
256+
};
257+
258+
type A = [u32; {
259+
impl Uto10 for Test {}
260+
//~^ WARN non-local `impl` definition
261+
262+
1
263+
}];
264+
265+
1
266+
};
267+
248268
struct UwU<T>(T);
249269

250270
fn fun() {

tests/ui/lint/non_local_definitions.stderr

+36-14
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,29 @@ LL | impl<T> Uto8 for T {}
442442
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
443443

444444
warning: non-local `impl` definition, they should be avoided as they go against expectation
445-
--> $DIR/non_local_definitions.rs:253:5
445+
--> $DIR/non_local_definitions.rs:252:9
446+
|
447+
LL | impl Uto9 for Test {}
448+
| ^^^^^^^^^^^^^^^^^^^^^
449+
|
450+
= help: move this `impl` block outside the of the current closure `<unnameable>` and up 2 bodies
451+
= note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
452+
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
453+
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
454+
455+
warning: non-local `impl` definition, they should be avoided as they go against expectation
456+
--> $DIR/non_local_definitions.rs:259:9
457+
|
458+
LL | impl Uto10 for Test {}
459+
| ^^^^^^^^^^^^^^^^^^^^^^
460+
|
461+
= help: move this `impl` block outside the of the current constant expression `<unnameable>` and up 2 bodies
462+
= note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
463+
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
464+
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
465+
466+
warning: non-local `impl` definition, they should be avoided as they go against expectation
467+
--> $DIR/non_local_definitions.rs:273:5
446468
|
447469
LL | / impl Default for UwU<OwO> {
448470
LL | |
@@ -458,7 +480,7 @@ LL | | }
458480
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
459481

460482
warning: non-local `impl` definition, they should be avoided as they go against expectation
461-
--> $DIR/non_local_definitions.rs:264:5
483+
--> $DIR/non_local_definitions.rs:284:5
462484
|
463485
LL | / impl From<Cat> for () {
464486
LL | |
@@ -474,7 +496,7 @@ LL | | }
474496
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
475497

476498
warning: non-local `impl` definition, they should be avoided as they go against expectation
477-
--> $DIR/non_local_definitions.rs:273:5
499+
--> $DIR/non_local_definitions.rs:293:5
478500
|
479501
LL | / impl AsRef<Cat> for () {
480502
LL | |
@@ -488,7 +510,7 @@ LL | | }
488510
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
489511

490512
warning: non-local `impl` definition, they should be avoided as they go against expectation
491-
--> $DIR/non_local_definitions.rs:284:5
513+
--> $DIR/non_local_definitions.rs:304:5
492514
|
493515
LL | / impl PartialEq<B> for G {
494516
LL | |
@@ -504,7 +526,7 @@ LL | | }
504526
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
505527

506528
warning: non-local `impl` definition, they should be avoided as they go against expectation
507-
--> $DIR/non_local_definitions.rs:301:5
529+
--> $DIR/non_local_definitions.rs:321:5
508530
|
509531
LL | / impl PartialEq<Dog> for &Dog {
510532
LL | |
@@ -520,7 +542,7 @@ LL | | }
520542
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
521543

522544
warning: non-local `impl` definition, they should be avoided as they go against expectation
523-
--> $DIR/non_local_definitions.rs:308:5
545+
--> $DIR/non_local_definitions.rs:328:5
524546
|
525547
LL | / impl PartialEq<()> for Dog {
526548
LL | |
@@ -536,7 +558,7 @@ LL | | }
536558
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
537559

538560
warning: non-local `impl` definition, they should be avoided as they go against expectation
539-
--> $DIR/non_local_definitions.rs:315:5
561+
--> $DIR/non_local_definitions.rs:335:5
540562
|
541563
LL | / impl PartialEq<()> for &Dog {
542564
LL | |
@@ -552,7 +574,7 @@ LL | | }
552574
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
553575

554576
warning: non-local `impl` definition, they should be avoided as they go against expectation
555-
--> $DIR/non_local_definitions.rs:322:5
577+
--> $DIR/non_local_definitions.rs:342:5
556578
|
557579
LL | / impl PartialEq<Dog> for () {
558580
LL | |
@@ -568,7 +590,7 @@ LL | | }
568590
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
569591

570592
warning: non-local `impl` definition, they should be avoided as they go against expectation
571-
--> $DIR/non_local_definitions.rs:344:5
593+
--> $DIR/non_local_definitions.rs:364:5
572594
|
573595
LL | / impl From<Wrap<Wrap<Lion>>> for () {
574596
LL | |
@@ -584,7 +606,7 @@ LL | | }
584606
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
585607

586608
warning: non-local `impl` definition, they should be avoided as they go against expectation
587-
--> $DIR/non_local_definitions.rs:351:5
609+
--> $DIR/non_local_definitions.rs:371:5
588610
|
589611
LL | / impl From<()> for Wrap<Lion> {
590612
LL | |
@@ -600,7 +622,7 @@ LL | | }
600622
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
601623

602624
warning: non-local `impl` definition, they should be avoided as they go against expectation
603-
--> $DIR/non_local_definitions.rs:364:13
625+
--> $DIR/non_local_definitions.rs:384:13
604626
|
605627
LL | impl MacroTrait for OutsideStruct {}
606628
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -615,7 +637,7 @@ LL | m!();
615637
= note: this warning originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
616638

617639
warning: non-local `impl` definition, they should be avoided as they go against expectation
618-
--> $DIR/non_local_definitions.rs:374:1
640+
--> $DIR/non_local_definitions.rs:394:1
619641
|
620642
LL | non_local_macro::non_local_impl!(CargoUpdate);
621643
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -628,7 +650,7 @@ LL | non_local_macro::non_local_impl!(CargoUpdate);
628650
= note: this warning originates in the macro `non_local_macro::non_local_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
629651

630652
warning: non-local `macro_rules!` definition, they should be avoided as they go against expectation
631-
--> $DIR/non_local_definitions.rs:377:1
653+
--> $DIR/non_local_definitions.rs:397:1
632654
|
633655
LL | non_local_macro::non_local_macro_rules!(my_macro);
634656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -640,5 +662,5 @@ LL | non_local_macro::non_local_macro_rules!(my_macro);
640662
= note: the macro `non_local_macro::non_local_macro_rules` may come from an old version of the `non_local_macro` crate, try updating your dependency with `cargo update -p non_local_macro`
641663
= note: this warning originates in the macro `non_local_macro::non_local_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
642664

643-
warning: 50 warnings emitted
665+
warning: 52 warnings emitted
644666

0 commit comments

Comments
 (0)