Skip to content

Commit

Permalink
Remove suspicious auto trait lint
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Feb 14, 2024
1 parent 9584f78 commit b21d3ce
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 186 deletions.
59 changes: 29 additions & 30 deletions compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
//! crate or pertains to a type defined in this crate.

use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{DelayDm, ErrorGuaranteed};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_middle::ty::util::CheckRegions;
use rustc_middle::ty::GenericArgs;
use rustc_middle::ty::{
self, AliasKind, ImplPolarity, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
TypeVisitor,
};
use rustc_session::lint;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::Span;
use rustc_trait_selection::traits;
Expand Down Expand Up @@ -496,36 +495,36 @@ fn lint_auto_trait_impl<'tcx>(
return;
}

tcx.node_span_lint(
lint::builtin::SUSPICIOUS_AUTO_TRAIT_IMPLS,
tcx.local_def_id_to_hir_id(impl_def_id),
tcx.def_span(impl_def_id),
DelayDm(|| {
format!(
"cross-crate traits with a default impl, like `{}`, \
let msg = format!(
"cross-crate traits with a default impl, like `{}`, \
should not be specialized",
tcx.def_path_str(trait_ref.def_id),
)
}),
|lint| {
let item_span = tcx.def_span(self_type_did);
let self_descr = tcx.def_descr(self_type_did);
match arg {
ty::util::NotUniqueParam::DuplicateParam(arg) => {
lint.note(format!("`{arg}` is mentioned multiple times"));
}
ty::util::NotUniqueParam::NotParam(arg) => {
lint.note(format!("`{arg}` is not a generic parameter"));
}
}
lint.span_note(
item_span,
format!(
"try using the same sequence of generic parameters as the {self_descr} definition",
),
);
},
tcx.def_path_str(trait_ref.def_id),
);
let mut err: DiagnosticBuilder<'_, ()> =
DiagnosticBuilder::new(tcx.sess.dcx(), rustc_errors::Level::Error, msg);

let impl_span = tcx.def_span(impl_def_id);
err.span(impl_span);

match arg {
ty::util::NotUniqueParam::DuplicateParam(arg) => {
err.note(format!("`{arg}` is mentioned multiple times"));
}
ty::util::NotUniqueParam::NotParam(arg) => {
err.note(format!("`{arg}` is not a generic parameter"));
}
}

let self_span = tcx.def_span(self_type_did);
let self_descr = tcx.def_descr(self_type_did);
err.span_note(
self_span,
format!(
"try using the same sequence of generic parameters as the {self_descr} definition",
),
);

err.emit();
}

fn fast_reject_auto_impl<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, self_ty: Ty<'tcx>) -> bool {
Expand Down
35 changes: 0 additions & 35 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ declare_lint_pass! {
SOFT_UNSTABLE,
STABLE_FEATURES,
STATIC_MUT_REF,
SUSPICIOUS_AUTO_TRAIT_IMPLS,
TEST_UNSTABLE_LINT,
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
TRIVIAL_CASTS,
Expand Down Expand Up @@ -4032,40 +4031,6 @@ declare_lint! {
"duplicated attribute"
}

declare_lint! {
/// The `suspicious_auto_trait_impls` lint checks for potentially incorrect
/// implementations of auto traits.
///
/// ### Example
///
/// ```rust
/// struct Foo<T>(T);
///
/// unsafe impl<T> Send for Foo<*const T> {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// A type can implement auto traits, e.g. `Send`, `Sync` and `Unpin`,
/// in two different ways: either by writing an explicit impl or if
/// all fields of the type implement that auto trait.
///
/// The compiler disables the automatic implementation if an explicit one
/// exists for given type constructor. The exact rules governing this
/// were previously unsound, quite subtle, and have been recently modified.
/// This change caused the automatic implementation to be disabled in more
/// cases, potentially breaking some code.
pub SUSPICIOUS_AUTO_TRAIT_IMPLS,
Warn,
"the rules governing auto traits have recently changed resulting in potential breakage",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
reference: "issue #93367 <https://github.com/rust-lang/rust/issues/93367>",
};
}

declare_lint! {
/// The `deprecated_where_clause_location` lint detects when a where clause in front of the equals
/// in an associated type.
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/auto-traits/issue-117789.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![deny(suspicious_auto_trait_impls)]

auto trait Trait<P> {} //~ ERROR auto traits cannot have generic parameters
//~^ ERROR auto traits are experimental and possibly buggy
impl<P> Trait<P> for () {}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/auto-traits/issue-117789.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0567]: auto traits cannot have generic parameters
--> $DIR/issue-117789.rs:3:17
--> $DIR/issue-117789.rs:1:17
|
LL | auto trait Trait<P> {}
| -----^^^ help: remove the parameters
| |
| auto trait cannot have generic parameters

error[E0658]: auto traits are experimental and possibly buggy
--> $DIR/issue-117789.rs:3:1
--> $DIR/issue-117789.rs:1:1
|
LL | auto trait Trait<P> {}
| ^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/auto-traits/issue-83857-ub.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(suspicious_auto_trait_impls)]
// Tests that we don't incorrectly allow overlap between a builtin auto trait
// impl and a user written one. See #83857 for more details

Expand All @@ -8,6 +7,7 @@ struct Foo<T, U>(Always<T, U>);

trait False {}
unsafe impl<U: False> Send for Foo<u32, U> {}
//~^ ERROR cross-crate traits with a default impl, like `Send`, should not be specialized

trait WithAssoc {
type Output;
Expand Down
15 changes: 14 additions & 1 deletion tests/ui/auto-traits/issue-83857-ub.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
error: cross-crate traits with a default impl, like `Send`, should not be specialized
--> $DIR/issue-83857-ub.rs:9:1
|
LL | unsafe impl<U: False> Send for Foo<u32, U> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `u32` is not a generic parameter
note: try using the same sequence of generic parameters as the struct definition
--> $DIR/issue-83857-ub.rs:6:1
|
LL | struct Foo<T, U>(Always<T, U>);
| ^^^^^^^^^^^^^^^^

error[E0277]: `Foo<T, U>` cannot be sent between threads safely
--> $DIR/issue-83857-ub.rs:22:38
|
Expand All @@ -17,6 +30,6 @@ help: consider introducing a `where` clause, but there might be an alternative b
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) where Foo<T, U>: Send {
| +++++++++++++++++++++

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
17 changes: 5 additions & 12 deletions tests/ui/auto-traits/suspicious-impls-lint.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#![deny(suspicious_auto_trait_impls)]

use std::marker::PhantomData;

struct MayImplementSendOk<T>(T);
unsafe impl<T: Send> Send for MayImplementSendOk<T> {} // ok

struct MayImplementSendErr<T>(T);
unsafe impl<T: Send> Send for MayImplementSendErr<&T> {}
//~^ ERROR
//~| WARNING this will change its meaning
//~^ ERROR cross-crate traits with a default impl, like `Send`, should not be specialized

struct ContainsNonSendDirect<T>(*const T);
unsafe impl<T: Send> Send for ContainsNonSendDirect<&T> {} // ok
Expand All @@ -19,8 +16,7 @@ unsafe impl<T: Send> Send for ContainsIndirectNonSend<&T> {} // ok

struct ContainsVec<T>(Vec<T>);
unsafe impl Send for ContainsVec<i32> {}
//~^ ERROR
//~| WARNING this will change its meaning
//~^ ERROR cross-crate traits with a default impl, like `Send`, should not be specialized

struct TwoParams<T, U>(T, U);
unsafe impl<T: Send, U: Send> Send for TwoParams<T, U> {} // ok
Expand All @@ -30,21 +26,18 @@ unsafe impl<T: Send, U: Send> Send for TwoParamsFlipped<U, T> {} // ok

struct TwoParamsSame<T, U>(T, U);
unsafe impl<T: Send> Send for TwoParamsSame<T, T> {}
//~^ ERROR
//~| WARNING this will change its meaning
//~^ ERROR cross-crate traits with a default impl, like `Send`, should not be specialized

pub struct WithPhantomDataNonSend<T, U>(PhantomData<*const T>, U);
unsafe impl<T> Send for WithPhantomDataNonSend<T, i8> {} // ok

pub struct WithPhantomDataSend<T, U>(PhantomData<T>, U);
unsafe impl<T> Send for WithPhantomDataSend<*const T, i8> {}
//~^ ERROR
//~| WARNING this will change its meaning
//~^ ERROR cross-crate traits with a default impl, like `Send`, should not be specialized

pub struct WithLifetime<'a, T>(&'a (), T);
unsafe impl<T> Send for WithLifetime<'static, T> {} // ok
unsafe impl<T> Sync for WithLifetime<'static, Vec<T>> {}
//~^ ERROR
//~| WARNING this will change its meaning
//~^ ERROR cross-crate traits with a default impl, like `Sync`, should not be specialized

fn main() {}
35 changes: 10 additions & 25 deletions tests/ui/auto-traits/suspicious-impls-lint.stderr
Original file line number Diff line number Diff line change
@@ -1,79 +1,64 @@
error: cross-crate traits with a default impl, like `Send`, should not be specialized
--> $DIR/suspicious-impls-lint.rs:9:1
--> $DIR/suspicious-impls-lint.rs:7:1
|
LL | unsafe impl<T: Send> Send for MayImplementSendErr<&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: `&T` is not a generic parameter
note: try using the same sequence of generic parameters as the struct definition
--> $DIR/suspicious-impls-lint.rs:8:1
--> $DIR/suspicious-impls-lint.rs:6:1
|
LL | struct MayImplementSendErr<T>(T);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/suspicious-impls-lint.rs:1:9
|
LL | #![deny(suspicious_auto_trait_impls)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: cross-crate traits with a default impl, like `Send`, should not be specialized
--> $DIR/suspicious-impls-lint.rs:21:1
--> $DIR/suspicious-impls-lint.rs:18:1
|
LL | unsafe impl Send for ContainsVec<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/suspicious-impls-lint.rs:20:1
--> $DIR/suspicious-impls-lint.rs:17:1
|
LL | struct ContainsVec<T>(Vec<T>);
| ^^^^^^^^^^^^^^^^^^^^^

error: cross-crate traits with a default impl, like `Send`, should not be specialized
--> $DIR/suspicious-impls-lint.rs:32:1
--> $DIR/suspicious-impls-lint.rs:28:1
|
LL | unsafe impl<T: Send> Send for TwoParamsSame<T, 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: `T` is mentioned multiple times
note: try using the same sequence of generic parameters as the struct definition
--> $DIR/suspicious-impls-lint.rs:31:1
--> $DIR/suspicious-impls-lint.rs:27:1
|
LL | struct TwoParamsSame<T, U>(T, U);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: cross-crate traits with a default impl, like `Send`, should not be specialized
--> $DIR/suspicious-impls-lint.rs:40:1
--> $DIR/suspicious-impls-lint.rs:35:1
|
LL | unsafe impl<T> Send for WithPhantomDataSend<*const T, i8> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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-impls-lint.rs:39:1
--> $DIR/suspicious-impls-lint.rs:34: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-impls-lint.rs:46:1
--> $DIR/suspicious-impls-lint.rs:40:1
|
LL | unsafe impl<T> Sync for WithLifetime<'static, Vec<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: `Vec<T>` is not a generic parameter
note: try using the same sequence of generic parameters as the struct definition
--> $DIR/suspicious-impls-lint.rs:44:1
--> $DIR/suspicious-impls-lint.rs:38:1
|
LL | pub struct WithLifetime<'a, T>(&'a (), T);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 3 additions & 7 deletions tests/ui/auto-traits/suspicious-negative-impls-lint.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#![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
//~^ ERROR cross-crate traits with a default impl, like `Send`, should not be specialized

pub struct WithPhantomDataSend<T, U>(PhantomData<T>, U);
impl<T> !Send for WithPhantomDataSend<*const T, u8> {}
//~^ ERROR
//~| WARNING this will change its meaning
//~^ ERROR cross-crate traits with a default impl, like `Send`, should not be specialized

pub struct WithLifetime<'a, T>(&'a (), T);
impl<T> !Sync for WithLifetime<'static, Option<T>> {}
//~^ ERROR
//~| WARNING this will change its meaning
//~^ ERROR cross-crate traits with a default impl, like `Sync`, should not be specialized

fn main() {}
Loading

0 comments on commit b21d3ce

Please sign in to comment.