Skip to content

Commit

Permalink
Rollup merge of #108807 - MU001999:lint/suspicious_auto_trait_impls, …
Browse files Browse the repository at this point in the history
…r=lcnr

Emit the suspicious_auto_trait_impls for negative impls as well

Fixes #108804
  • Loading branch information
matthiaskrgr authored Mar 6, 2023
2 parents ab7dd09 + 717f93c commit 3279f7e
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 9 deletions.
4 changes: 0 additions & 4 deletions compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,6 @@ fn lint_auto_trait_impl<'tcx>(
trait_ref: ty::TraitRef<'tcx>,
impl_def_id: LocalDefId,
) {
if tcx.impl_polarity(impl_def_id) != ImplPolarity::Positive {
return;
}

assert_eq!(trait_ref.substs.len(), 1);
let self_ty = trait_ref.self_ty();
let (self_type_did, substs) = match self_ty.kind() {
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/auto-traits/suspicious-negative-impls-lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(negative_impls)]
#![deny(suspicious_auto_trait_impls)]

use std::marker::PhantomData;

struct ContainsVec<T>(Vec<T>);
impl !Send for ContainsVec<u32> {}
//~^ ERROR
//~| WARNING this will change its meaning

pub struct WithPhantomDataSend<T, U>(PhantomData<T>, U);
impl<T> !Send for WithPhantomDataSend<*const T, u8> {}
//~^ ERROR
//~| WARNING this will change its meaning

pub struct WithLifetime<'a, T>(&'a (), T);
impl<T> !Sync for WithLifetime<'static, Option<T>> {}
//~^ ERROR
//~| WARNING this will change its meaning

fn main() {}
52 changes: 52 additions & 0 deletions tests/ui/auto-traits/suspicious-negative-impls-lint.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
error: cross-crate traits with a default impl, like `Send`, should not be specialized
--> $DIR/suspicious-negative-impls-lint.rs:7:1
|
LL | impl !Send for ContainsVec<u32> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
= note: `u32` is not a generic parameter
note: try using the same sequence of generic parameters as the struct definition
--> $DIR/suspicious-negative-impls-lint.rs:6:1
|
LL | struct ContainsVec<T>(Vec<T>);
| ^^^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/suspicious-negative-impls-lint.rs:2:9
|
LL | #![deny(suspicious_auto_trait_impls)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: cross-crate traits with a default impl, like `Send`, should not be specialized
--> $DIR/suspicious-negative-impls-lint.rs:12:1
|
LL | impl<T> !Send for WithPhantomDataSend<*const T, u8> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
= note: `*const T` is not a generic parameter
note: try using the same sequence of generic parameters as the struct definition
--> $DIR/suspicious-negative-impls-lint.rs:11:1
|
LL | pub struct WithPhantomDataSend<T, U>(PhantomData<T>, U);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: cross-crate traits with a default impl, like `Sync`, should not be specialized
--> $DIR/suspicious-negative-impls-lint.rs:17:1
|
LL | impl<T> !Sync for WithLifetime<'static, Option<T>> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #93367 <https://github.com/rust-lang/rust/issues/93367>
= note: `Option<T>` is not a generic parameter
note: try using the same sequence of generic parameters as the struct definition
--> $DIR/suspicious-negative-impls-lint.rs:16:1
|
LL | pub struct WithLifetime<'a, T>(&'a (), T);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

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

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

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ LL | unsafe impl<T: MyTrait + 'static> Send for TestType<T> {}
LL | unsafe impl<T: 'static> Send for TestType<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `TestType<_>`

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

error: aborting due to 2 previous errors; 1 warning emitted

Some errors have detailed explanations: E0119, E0751.
For more information about an error, try `rustc --explain E0119`.
5 changes: 3 additions & 2 deletions tests/ui/coherence/coherence-orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ impl TheTrait<TheType> for isize { }

impl TheTrait<isize> for TheType { }

impl !Send for Vec<isize> { }
//~^ ERROR E0117
impl !Send for Vec<isize> { } //~ ERROR E0117
//~^ WARNING
//~| WARNING this will change its meaning

fn main() { }
15 changes: 14 additions & 1 deletion tests/ui/coherence/coherence-orphan.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ LL | impl !Send for Vec<isize> { }
|
= note: define and implement a trait or new type instead

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

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0117`.
2 changes: 2 additions & 0 deletions tests/ui/issues/issue-106755.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ impl<T: MyTrait> !Send for TestType<T> {} //~ ERROR found both positive and nega
unsafe impl<T: 'static> Send for TestType<T> {} //~ ERROR conflicting implementations

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

fn main() {}
18 changes: 17 additions & 1 deletion tests/ui/issues/issue-106755.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ LL | unsafe impl<T: MyTrait + 'static> Send for TestType<T> {}
LL | unsafe impl<T: 'static> Send for TestType<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `TestType<_>`

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

error: aborting due to 2 previous errors; 1 warning emitted

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

0 comments on commit 3279f7e

Please sign in to comment.