Skip to content

Commit 769d54e

Browse files
committed
deduplicate warnings (attempt 2)
1 parent 0a00c74 commit 769d54e

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

compiler/rustc_passes/src/stability.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use rustc_data_structures::fx::FxIndexMap;
88
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
99
use rustc_feature::{EnabledLangFeature, EnabledLibFeature};
1010
use rustc_hir::attrs::{AttributeKind, DeprecatedSince};
11-
use rustc_hir::def::{DefKind, Res};
11+
use rustc_hir::def::{CtorOf, 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,28 @@ 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(CtorOf::Struct, _) = 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+
let UsePath { segments, res: _, span } = *path;
757+
self.visit_path(&Path { segments, res: value_ns_res, span }, hir_id);
758+
} else {
759+
// if there's no duplicate, just walk as normal
760+
intravisit::walk_use(self, path, hir_id)
761+
}
762+
}
763+
742764
fn visit_path(&mut self, path: &hir::Path<'tcx>, id: hir::HirId) {
743765
if let Some(def_id) = path.res.opt_def_id() {
744766
let method_span = path.segments.last().map(|s| s.ident.span);

tests/ui/deprecation/unit_and_tuple_struct.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ pub mod a {
1010

1111

1212
use a::Foo;
13-
//~^ ERROR use of deprecated struct `a::Foo`
14-
//~| ERROR use of deprecated unit struct `a::Foo`
13+
//~^ ERROR use of deprecated unit struct `a::Foo`
1514
use a::Bar;
16-
//~^ ERROR use of deprecated struct `a::Bar`
17-
//~| ERROR use of deprecated tuple struct `a::Bar`
15+
//~^ ERROR use of deprecated tuple struct `a::Bar`
1816
use a::Baz;
1917
//~^ ERROR use of deprecated struct `a::Baz`
2018

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: use of deprecated struct `a::Foo`
1+
error: use of deprecated unit struct `a::Foo`
22
--> $DIR/unit_and_tuple_struct.rs:12:8
33
|
44
LL | use a::Foo;
@@ -10,47 +10,35 @@ 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:12:8
15-
|
16-
LL | use a::Foo;
17-
| ^^^
18-
19-
error: use of deprecated struct `a::Bar`
20-
--> $DIR/unit_and_tuple_struct.rs:15: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:15:8
14+
--> $DIR/unit_and_tuple_struct.rs:14:8
2715
|
2816
LL | use a::Bar;
2917
| ^^^
3018

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

3725
error: use of deprecated unit struct `a::Foo`
38-
--> $DIR/unit_and_tuple_struct.rs:22:6
26+
--> $DIR/unit_and_tuple_struct.rs:20:6
3927
|
4028
LL | a::Foo;
4129
| ^^^
4230

4331
error: use of deprecated tuple struct `a::Bar`
44-
--> $DIR/unit_and_tuple_struct.rs:24:6
32+
--> $DIR/unit_and_tuple_struct.rs:22:6
4533
|
4634
LL | a::Bar();
4735
| ^^^
4836

4937
error: use of deprecated struct `a::Baz`
50-
--> $DIR/unit_and_tuple_struct.rs:26:6
38+
--> $DIR/unit_and_tuple_struct.rs:24:6
5139
|
5240
LL | a::Baz {};
5341
| ^^^
5442

55-
error: aborting due to 8 previous errors
43+
error: aborting due to 6 previous errors
5644

0 commit comments

Comments
 (0)