Skip to content

Commit

Permalink
Auto merge of #134258 - bjorn3:no_public_specialization, r=petrochenkov
Browse files Browse the repository at this point in the history
Remove support for specializing ToString outside the standard library

This is the only trait specializable outside of the standard library. Before stabilizing specialization we will probably want to remove support for this. It was originally made specializable to allow a more efficient ToString in libproc_macro back when this way the only way to get any data out of a TokenStream. We now support getting individual tokens, so proc macros no longer need to call it as often.
  • Loading branch information
bors committed Dec 15, 2024
2 parents 25d004d + bc410fc commit 4cddd0e
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 59 deletions.
3 changes: 0 additions & 3 deletions clippy_lints/src/to_string_trait_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::ty::implements_trait;
use rustc_hir::{Impl, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
Expand Down Expand Up @@ -54,8 +53,6 @@ impl<'tcx> LateLintPass<'tcx> for ToStringTraitImpl {
}) = it.kind
&& let Some(trait_did) = trait_ref.trait_def_id()
&& cx.tcx.is_diagnostic_item(sym::ToString, trait_did)
&& let Some(display_did) = cx.tcx.get_diagnostic_item(sym::Display)
&& !implements_trait(cx, cx.tcx.type_of(it.owner_id).instantiate_identity(), display_did, &[])
{
span_lint_and_help(
cx,
Expand Down
43 changes: 0 additions & 43 deletions tests/ui/to_string_trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,3 @@ impl Bar {
String::from("Bar")
}
}

mod issue12263 {
pub struct MyStringWrapper<'a>(&'a str);

impl std::fmt::Display for MyStringWrapper<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl ToString for MyStringWrapper<'_> {
fn to_string(&self) -> String {
self.0.to_string()
}
}

pub struct S<T>(T);
impl std::fmt::Display for S<String> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
// no specialization if the generics differ, so lint
impl ToString for S<i32> {
fn to_string(&self) -> String {
todo!()
}
}

pub struct S2<T>(T);
impl std::fmt::Display for S2<String> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}

// also specialization if the generics don't differ
impl ToString for S2<String> {
fn to_string(&self) -> String {
todo!()
}
}
}
14 changes: 1 addition & 13 deletions tests/ui/to_string_trait_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,5 @@ LL | | }
= note: `-D clippy::to-string-trait-impl` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::to_string_trait_impl)]`

error: direct implementation of `ToString`
--> tests/ui/to_string_trait_impl.rs:56:5
|
LL | / impl ToString for S<i32> {
LL | | fn to_string(&self) -> String {
LL | | todo!()
LL | | }
LL | | }
| |_____^
|
= help: prefer implementing `Display` instead

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

0 comments on commit 4cddd0e

Please sign in to comment.