Skip to content

Commit 3279f7e

Browse files
authored
Rollup merge of #108807 - MU001999:lint/suspicious_auto_trait_impls, r=lcnr
Emit the suspicious_auto_trait_impls for negative impls as well Fixes #108804
2 parents ab7dd09 + 717f93c commit 3279f7e

9 files changed

+128
-9
lines changed

Diff for: compiler/rustc_hir_analysis/src/coherence/orphan.rs

-4
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,6 @@ fn lint_auto_trait_impl<'tcx>(
478478
trait_ref: ty::TraitRef<'tcx>,
479479
impl_def_id: LocalDefId,
480480
) {
481-
if tcx.impl_polarity(impl_def_id) != ImplPolarity::Positive {
482-
return;
483-
}
484-
485481
assert_eq!(trait_ref.substs.len(), 1);
486482
let self_ty = trait_ref.self_ty();
487483
let (self_type_did, substs) = match self_ty.kind() {
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(negative_impls)]
2+
#![deny(suspicious_auto_trait_impls)]
3+
4+
use std::marker::PhantomData;
5+
6+
struct ContainsVec<T>(Vec<T>);
7+
impl !Send for ContainsVec<u32> {}
8+
//~^ ERROR
9+
//~| WARNING this will change its meaning
10+
11+
pub struct WithPhantomDataSend<T, U>(PhantomData<T>, U);
12+
impl<T> !Send for WithPhantomDataSend<*const T, u8> {}
13+
//~^ ERROR
14+
//~| WARNING this will change its meaning
15+
16+
pub struct WithLifetime<'a, T>(&'a (), T);
17+
impl<T> !Sync for WithLifetime<'static, Option<T>> {}
18+
//~^ ERROR
19+
//~| WARNING this will change its meaning
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: cross-crate traits with a default impl, like `Send`, should not be specialized
2+
--> $DIR/suspicious-negative-impls-lint.rs:7:1
3+
|
4+
LL | impl !Send for ContainsVec<u32> {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= warning: this will change its meaning in a future release!
8+
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
9+
= note: `u32` is not a generic parameter
10+
note: try using the same sequence of generic parameters as the struct definition
11+
--> $DIR/suspicious-negative-impls-lint.rs:6:1
12+
|
13+
LL | struct ContainsVec<T>(Vec<T>);
14+
| ^^^^^^^^^^^^^^^^^^^^^
15+
note: the lint level is defined here
16+
--> $DIR/suspicious-negative-impls-lint.rs:2:9
17+
|
18+
LL | #![deny(suspicious_auto_trait_impls)]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: cross-crate traits with a default impl, like `Send`, should not be specialized
22+
--> $DIR/suspicious-negative-impls-lint.rs:12:1
23+
|
24+
LL | impl<T> !Send for WithPhantomDataSend<*const T, u8> {}
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
= warning: this will change its meaning in a future release!
28+
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
29+
= note: `*const T` is not a generic parameter
30+
note: try using the same sequence of generic parameters as the struct definition
31+
--> $DIR/suspicious-negative-impls-lint.rs:11:1
32+
|
33+
LL | pub struct WithPhantomDataSend<T, U>(PhantomData<T>, U);
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
36+
error: cross-crate traits with a default impl, like `Sync`, should not be specialized
37+
--> $DIR/suspicious-negative-impls-lint.rs:17:1
38+
|
39+
LL | impl<T> !Sync for WithLifetime<'static, Option<T>> {}
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
|
42+
= warning: this will change its meaning in a future release!
43+
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
44+
= note: `Option<T>` is not a generic parameter
45+
note: try using the same sequence of generic parameters as the struct definition
46+
--> $DIR/suspicious-negative-impls-lint.rs:16:1
47+
|
48+
LL | pub struct WithLifetime<'a, T>(&'a (), T);
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
error: aborting due to 3 previous errors
52+

Diff for: tests/ui/coherence/coherence-conflicting-negative-trait-impl.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ impl<T: MyTrait> !Send for TestType<T> {} //~ ERROR found both positive and nega
1313
unsafe impl<T: 'static> Send for TestType<T> {} //~ ERROR conflicting implementations
1414

1515
impl !Send for TestType<i32> {}
16+
//~^ WARNING
17+
//~| WARNING this will change its meaning
1618

1719
fn main() {}

Diff for: tests/ui/coherence/coherence-conflicting-negative-trait-impl.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,23 @@ LL | unsafe impl<T: MyTrait + 'static> Send for TestType<T> {}
1616
LL | unsafe impl<T: 'static> Send for TestType<T> {}
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `TestType<_>`
1818

19-
error: aborting due to 2 previous errors
19+
warning: cross-crate traits with a default impl, like `Send`, should not be specialized
20+
--> $DIR/coherence-conflicting-negative-trait-impl.rs:15:1
21+
|
22+
LL | impl !Send for TestType<i32> {}
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= warning: this will change its meaning in a future release!
26+
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
27+
= note: `i32` is not a generic parameter
28+
note: try using the same sequence of generic parameters as the struct definition
29+
--> $DIR/coherence-conflicting-negative-trait-impl.rs:7:1
30+
|
31+
LL | struct TestType<T>(::std::marker::PhantomData<T>);
32+
| ^^^^^^^^^^^^^^^^^^
33+
= note: `#[warn(suspicious_auto_trait_impls)]` on by default
34+
35+
error: aborting due to 2 previous errors; 1 warning emitted
2036

2137
Some errors have detailed explanations: E0119, E0751.
2238
For more information about an error, try `rustc --explain E0119`.

Diff for: tests/ui/coherence/coherence-orphan.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ impl TheTrait<TheType> for isize { }
1414

1515
impl TheTrait<isize> for TheType { }
1616

17-
impl !Send for Vec<isize> { }
18-
//~^ ERROR E0117
17+
impl !Send for Vec<isize> { } //~ ERROR E0117
18+
//~^ WARNING
19+
//~| WARNING this will change its meaning
1920

2021
fn main() { }

Diff for: tests/ui/coherence/coherence-orphan.stderr

+14-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ LL | impl !Send for Vec<isize> { }
2121
|
2222
= note: define and implement a trait or new type instead
2323

24-
error: aborting due to 2 previous errors
24+
warning: cross-crate traits with a default impl, like `Send`, should not be specialized
25+
--> $DIR/coherence-orphan.rs:17:1
26+
|
27+
LL | impl !Send for Vec<isize> { }
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
29+
|
30+
= warning: this will change its meaning in a future release!
31+
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
32+
= note: `isize` is not a generic parameter
33+
note: try using the same sequence of generic parameters as the struct definition
34+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
35+
= note: `#[warn(suspicious_auto_trait_impls)]` on by default
36+
37+
error: aborting due to 2 previous errors; 1 warning emitted
2538

2639
For more information about this error, try `rustc --explain E0117`.

Diff for: tests/ui/issues/issue-106755.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ impl<T: MyTrait> !Send for TestType<T> {} //~ ERROR found both positive and nega
1515
unsafe impl<T: 'static> Send for TestType<T> {} //~ ERROR conflicting implementations
1616

1717
impl !Send for TestType<i32> {}
18+
//~^ WARNING
19+
//~| WARNING this will change its meaning
1820

1921
fn main() {}

Diff for: tests/ui/issues/issue-106755.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,23 @@ LL | unsafe impl<T: MyTrait + 'static> Send for TestType<T> {}
1616
LL | unsafe impl<T: 'static> Send for TestType<T> {}
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `TestType<_>`
1818

19-
error: aborting due to 2 previous errors
19+
warning: cross-crate traits with a default impl, like `Send`, should not be specialized
20+
--> $DIR/issue-106755.rs:17:1
21+
|
22+
LL | impl !Send for TestType<i32> {}
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= warning: this will change its meaning in a future release!
26+
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
27+
= note: `i32` is not a generic parameter
28+
note: try using the same sequence of generic parameters as the struct definition
29+
--> $DIR/issue-106755.rs:9:1
30+
|
31+
LL | struct TestType<T>(::std::marker::PhantomData<T>);
32+
| ^^^^^^^^^^^^^^^^^^
33+
= note: `#[warn(suspicious_auto_trait_impls)]` on by default
34+
35+
error: aborting due to 2 previous errors; 1 warning emitted
2036

2137
Some errors have detailed explanations: E0119, E0751.
2238
For more information about an error, try `rustc --explain E0119`.

0 commit comments

Comments
 (0)