Skip to content

Use Self instead of $type #67915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/libcore/convert/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ macro_rules! impl_from {
#[doc = $doc]
impl From<$Small> for $Large {
#[inline]
fn from(small: $Small) -> $Large {
small as $Large
fn from(small: $Small) -> Self {
small as Self
}
}
};
Expand Down Expand Up @@ -177,7 +177,7 @@ macro_rules! try_from_unbounded {
/// is outside of the range of the target type.
#[inline]
fn try_from(value: $source) -> Result<Self, Self::Error> {
Ok(value as $target)
Ok(value as Self)
}
}
)*}
Expand All @@ -194,9 +194,9 @@ macro_rules! try_from_lower_bounded {
/// number type. This returns an error if the source value
/// is outside of the range of the target type.
#[inline]
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
fn try_from(u: $source) -> Result<Self, Self::Error> {
if u >= 0 {
Ok(u as $target)
Ok(u as Self)
} else {
Err(TryFromIntError(()))
}
Expand All @@ -216,11 +216,11 @@ macro_rules! try_from_upper_bounded {
/// number type. This returns an error if the source value
/// is outside of the range of the target type.
#[inline]
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
if u > (<$target>::max_value() as $source) {
fn try_from(u: $source) -> Result<Self, Self::Error> {
if u > (Self::max_value() as $source) {
Err(TryFromIntError(()))
} else {
Ok(u as $target)
Ok(u as Self)
}
}
}
Expand All @@ -238,13 +238,13 @@ macro_rules! try_from_both_bounded {
/// number type. This returns an error if the source value
/// is outside of the range of the target type.
#[inline]
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
let min = <$target>::min_value() as $source;
let max = <$target>::max_value() as $source;
fn try_from(u: $source) -> Result<Self, Self::Error> {
let min = Self::min_value() as $source;
let max = Self::max_value() as $source;
if u < min || u > max {
Err(TryFromIntError(()))
} else {
Ok(u as $target)
Ok(u as Self)
}
}
}
Expand Down Expand Up @@ -385,10 +385,10 @@ macro_rules! nzint_impl_from {
#[doc = $doc]
impl From<$Small> for $Large {
#[inline]
fn from(small: $Small) -> $Large {
fn from(small: $Small) -> Self {
// SAFETY: input type guarantees the value is non-zero
unsafe {
<$Large>::new_unchecked(small.get().into())
Self::new_unchecked(small.get().into())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ trait Int:

macro_rules! doit {
($($t:ident)*) => ($(impl Int for $t {
fn zero() -> $t { 0 }
fn from_u8(u: u8) -> $t { u as $t }
fn zero() -> Self { 0 }
fn from_u8(u: u8) -> Self { u as Self }
fn to_u8(&self) -> u8 { *self as u8 }
fn to_u16(&self) -> u16 { *self as u16 }
fn to_u32(&self) -> u32 { *self as u32 }
Expand Down
16 changes: 8 additions & 8 deletions src/libcore/iter/traits/accum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ macro_rules! integer_sum_product {
(@impls $zero:expr, $one:expr, #[$attr:meta], $($a:ty)*) => ($(
#[$attr]
impl Sum for $a {
fn sum<I: Iterator<Item=$a>>(iter: I) -> $a {
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
iter.fold($zero, Add::add)
}
}

#[$attr]
impl Product for $a {
fn product<I: Iterator<Item=$a>>(iter: I) -> $a {
fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
iter.fold($one, Mul::mul)
}
}

#[$attr]
impl<'a> Sum<&'a $a> for $a {
fn sum<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold($zero, Add::add)
}
}

#[$attr]
impl<'a> Product<&'a $a> for $a {
fn product<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
fn product<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold($one, Mul::mul)
}
}
Expand All @@ -84,28 +84,28 @@ macro_rules! float_sum_product {
($($a:ident)*) => ($(
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
impl Sum for $a {
fn sum<I: Iterator<Item=$a>>(iter: I) -> $a {
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
iter.fold(0.0, Add::add)
}
}

#[stable(feature = "iter_arith_traits", since = "1.12.0")]
impl Product for $a {
fn product<I: Iterator<Item=$a>>(iter: I) -> $a {
fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
iter.fold(1.0, Mul::mul)
}
}

#[stable(feature = "iter_arith_traits", since = "1.12.0")]
impl<'a> Sum<&'a $a> for $a {
fn sum<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold(0.0, Add::add)
}
}

#[stable(feature = "iter_arith_traits", since = "1.12.0")]
impl<'a> Product<&'a $a> for $a {
fn product<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
fn product<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold(1.0, Mul::mul)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,15 @@ macro_rules! impls {

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Clone for $t<T> {
fn clone(&self) -> $t<T> {
$t
fn clone(&self) -> Self {
Self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Default for $t<T> {
fn default() -> $t<T> {
$t
fn default() -> Self {
Self
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/num/bignum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ macro_rules! define_bignum {
}

impl crate::clone::Clone for $name {
fn clone(&self) -> $name {
$name { size: self.size, base: self.base }
fn clone(&self) -> Self {
Self { size: self.size, base: self.base }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-8460.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ trait Int {
}
macro_rules! doit {
($($t:ident)*) => ($(impl Int for $t {
fn zero() -> $t { 0 }
fn one() -> $t { 1 }
fn zero() -> Self { 0 }
fn one() -> Self { 1 }
})*)
}
doit! { i8 i16 i32 i64 isize }
Expand Down