Skip to content

Commit 1b00911

Browse files
committed
deduplicate warnings (attempt 2)
1 parent c6a8c46 commit 1b00911

File tree

3 files changed

+49
-49
lines changed

3 files changed

+49
-49
lines changed

compiler/rustc_passes/src/stability.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
1313
use rustc_hir::intravisit::{self, Visitor, VisitorExt};
1414
use rustc_hir::{
15-
self as hir, AmbigArg, ConstStability, DefaultBodyStability, FieldDef, Item, ItemKind,
16-
Stability, StabilityLevel, StableSince, TraitRef, Ty, TyKind, UnstableReason,
15+
self as hir, AmbigArg, ConstStability, DefaultBodyStability, FieldDef, HirId, Item, ItemKind,
16+
Path, Stability, StabilityLevel, StableSince, TraitRef, Ty, TyKind, UnstableReason, UsePath,
1717
VERSION_PLACEHOLDER, Variant, find_attr,
1818
};
1919
use rustc_middle::hir::nested_filter;
@@ -739,6 +739,35 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
739739
intravisit::walk_poly_trait_ref(self, t);
740740
}
741741

742+
fn visit_use(&mut self, path: &'tcx UsePath<'tcx>, hir_id: HirId) {
743+
let res = path.res;
744+
745+
// A use item can import something from two namespaces at the same time.
746+
// For deprecation/stability we don't want to warn twice.
747+
// This specifically happens with constructors for unit/tuple structs.
748+
if let Some(ty_ns_res) = res.type_ns
749+
&& let Some(value_ns_res) = res.value_ns
750+
&& let Some(type_ns_did) = ty_ns_res.opt_def_id()
751+
&& let Some(value_ns_did) = value_ns_res.opt_def_id()
752+
&& let DefKind::Ctor(.., _) = self.tcx.def_kind(value_ns_did)
753+
&& self.tcx.parent(value_ns_did) == type_ns_did
754+
{
755+
// Only visit the value namespace path when we've detected a duplicate,
756+
// not the type namespace path.
757+
let UsePath { segments, res: _, span } = *path;
758+
self.visit_path(&Path { segments, res: value_ns_res, span }, hir_id);
759+
760+
// Though, visit the macro namespace if it exists,
761+
// regardless of the checks above relating to constructors.
762+
if let Some(res) = res.macro_ns {
763+
self.visit_path(&Path { segments, res, span }, hir_id);
764+
}
765+
} else {
766+
// if there's no duplicate, just walk as normal
767+
intravisit::walk_use(self, path, hir_id)
768+
}
769+
}
770+
742771
fn visit_path(&mut self, path: &hir::Path<'tcx>, id: hir::HirId) {
743772
if let Some(def_id) = path.res.opt_def_id() {
744773
let method_span = path.segments.last().map(|s| s.ident.span);

tests/ui/deprecation/unit_and_tuple_struct.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![deny(deprecated)]
2-
#![allow(unused_imports)]
32

43
#[deprecated]
54
pub mod a {
@@ -16,20 +15,16 @@ pub mod a {
1615

1716

1817
use a::Foo;
19-
//~^ ERROR use of deprecated struct `a::Foo`
20-
//~| ERROR use of deprecated unit struct `a::Foo`
18+
//~^ ERROR use of deprecated unit struct `a::Foo`
2119
use a::Bar;
22-
//~^ ERROR use of deprecated struct `a::Bar`
23-
//~| ERROR use of deprecated tuple struct `a::Bar`
20+
//~^ ERROR use of deprecated tuple struct `a::Bar`
2421
use a::Baz;
2522
//~^ ERROR use of deprecated struct `a::Baz`
2623

2724
use a::Enum::VFoo;
28-
//~^ ERROR use of deprecated variant `a::Enum::VFoo`
29-
//~| ERROR use of deprecated unit variant `a::Enum::VFoo`
25+
//~^ ERROR use of deprecated unit variant `a::Enum::VFoo`
3026
use a::Enum::VBar;
31-
//~^ ERROR use of deprecated variant `a::Enum::VBar`
32-
//~| ERROR use of deprecated tuple variant `a::Enum::VBar`
27+
//~^ ERROR use of deprecated tuple variant `a::Enum::VBar`
3328
use a::Enum::VBaz;
3429
//~^ ERROR use of deprecated variant `a::Enum::VBaz`
3530

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error: use of deprecated struct `a::Foo`
2-
--> $DIR/unit_and_tuple_struct.rs:18:8
1+
error: use of deprecated unit struct `a::Foo`
2+
--> $DIR/unit_and_tuple_struct.rs:17:8
33
|
44
LL | use a::Foo;
55
| ^^^
@@ -10,95 +10,71 @@ note: the lint level is defined here
1010
LL | #![deny(deprecated)]
1111
| ^^^^^^^^^^
1212

13-
error: use of deprecated unit struct `a::Foo`
14-
--> $DIR/unit_and_tuple_struct.rs:18:8
15-
|
16-
LL | use a::Foo;
17-
| ^^^
18-
19-
error: use of deprecated struct `a::Bar`
20-
--> $DIR/unit_and_tuple_struct.rs:21:8
21-
|
22-
LL | use a::Bar;
23-
| ^^^
24-
2513
error: use of deprecated tuple struct `a::Bar`
26-
--> $DIR/unit_and_tuple_struct.rs:21:8
14+
--> $DIR/unit_and_tuple_struct.rs:19:8
2715
|
2816
LL | use a::Bar;
2917
| ^^^
3018

3119
error: use of deprecated struct `a::Baz`
32-
--> $DIR/unit_and_tuple_struct.rs:24:8
20+
--> $DIR/unit_and_tuple_struct.rs:21:8
3321
|
3422
LL | use a::Baz;
3523
| ^^^
3624

37-
error: use of deprecated variant `a::Enum::VFoo`
38-
--> $DIR/unit_and_tuple_struct.rs:27:14
39-
|
40-
LL | use a::Enum::VFoo;
41-
| ^^^^
42-
4325
error: use of deprecated unit variant `a::Enum::VFoo`
44-
--> $DIR/unit_and_tuple_struct.rs:27:14
26+
--> $DIR/unit_and_tuple_struct.rs:24:14
4527
|
4628
LL | use a::Enum::VFoo;
4729
| ^^^^
4830

49-
error: use of deprecated variant `a::Enum::VBar`
50-
--> $DIR/unit_and_tuple_struct.rs:30:14
51-
|
52-
LL | use a::Enum::VBar;
53-
| ^^^^
54-
5531
error: use of deprecated tuple variant `a::Enum::VBar`
56-
--> $DIR/unit_and_tuple_struct.rs:30:14
32+
--> $DIR/unit_and_tuple_struct.rs:26:14
5733
|
5834
LL | use a::Enum::VBar;
5935
| ^^^^
6036

6137
error: use of deprecated variant `a::Enum::VBaz`
62-
--> $DIR/unit_and_tuple_struct.rs:33:14
38+
--> $DIR/unit_and_tuple_struct.rs:28:14
6339
|
6440
LL | use a::Enum::VBaz;
6541
| ^^^^
6642

6743
error: use of deprecated unit struct `a::Foo`
68-
--> $DIR/unit_and_tuple_struct.rs:37:6
44+
--> $DIR/unit_and_tuple_struct.rs:32:6
6945
|
7046
LL | a::Foo;
7147
| ^^^
7248

7349
error: use of deprecated tuple struct `a::Bar`
74-
--> $DIR/unit_and_tuple_struct.rs:39:6
50+
--> $DIR/unit_and_tuple_struct.rs:34:6
7551
|
7652
LL | a::Bar();
7753
| ^^^
7854

7955
error: use of deprecated struct `a::Baz`
80-
--> $DIR/unit_and_tuple_struct.rs:41:6
56+
--> $DIR/unit_and_tuple_struct.rs:36:6
8157
|
8258
LL | a::Baz {};
8359
| ^^^
8460

8561
error: use of deprecated unit variant `a::Enum::VFoo`
86-
--> $DIR/unit_and_tuple_struct.rs:44:12
62+
--> $DIR/unit_and_tuple_struct.rs:39:12
8763
|
8864
LL | a::Enum::VFoo;
8965
| ^^^^
9066

9167
error: use of deprecated tuple variant `a::Enum::VBar`
92-
--> $DIR/unit_and_tuple_struct.rs:46:12
68+
--> $DIR/unit_and_tuple_struct.rs:41:12
9369
|
9470
LL | a::Enum::VBar();
9571
| ^^^^
9672

9773
error: use of deprecated variant `a::Enum::VBaz`
98-
--> $DIR/unit_and_tuple_struct.rs:48:12
74+
--> $DIR/unit_and_tuple_struct.rs:43:12
9975
|
10076
LL | a::Enum::VBaz{};
10177
| ^^^^
10278

103-
error: aborting due to 16 previous errors
79+
error: aborting due to 12 previous errors
10480

0 commit comments

Comments
 (0)