Skip to content

Commit

Permalink
Add inherent try_from and try_into methods to integer types
Browse files Browse the repository at this point in the history
This allows these methods to be used without importing the corresponding traits.

This causes new `unused-imports` warnings for existing imports used only by method calls. This is a non-trivial amount of churn, maybe worth it?

The warnings can be fixed by removing imports (like this PR does in rustc and tests), or by adding `#[allow(unused_import)]` to them until the minimum supported Rust version is incremented to one that has this PR.

The new methods are insta-stable in order to avoid causing `unstable-name-collisions` warnings. These would be harder to deal with since they happen at calls sites rather than trait imports.
  • Loading branch information
SimonSapin committed Nov 29, 2019
1 parent 2539b5f commit da301ef
Show file tree
Hide file tree
Showing 27 changed files with 171 additions and 152 deletions.
2 changes: 0 additions & 2 deletions src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,6 @@ pub trait TryInto<T>: Sized {
/// As described, [`i32`] implements `TryFrom<`[`i64`]`>`:
///
/// ```
/// use std::convert::TryFrom;
///
/// let big_number = 1_000_000_000_000i64;
/// // Silently truncates `big_number`, requires detecting
/// // and handling the truncation after the fact.
Expand Down
1 change: 0 additions & 1 deletion src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::convert::TryFrom;
use crate::mem;
use crate::ops::{self, Add, Sub, Try};
use crate::usize;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/num/dec2flt/rawfp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! take the universally-correct slow path (Algorithm M) for very small and very large numbers.
//! That algorithm needs only next_float() which does handle subnormals and zeros.
use crate::cmp::Ordering::{Equal, Greater, Less};
use crate::convert::{TryFrom, TryInto};
use crate::convert::TryFrom;
use crate::fmt::{Debug, LowerExp};
use crate::num::dec2flt::num::{self, Big};
use crate::num::dec2flt::table;
Expand Down
46 changes: 45 additions & 1 deletion src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#![stable(feature = "rust1", since = "1.0.0")]

use crate::convert::TryFrom;
use crate::convert::{TryFrom, TryInto};
use crate::fmt;
use crate::intrinsics;
use crate::mem;
Expand Down Expand Up @@ -278,6 +278,28 @@ $EndFeature, "
}
}

doc_comment! {
"FIXME docs",
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
#[inline]
pub fn try_from<T>(value: T) -> Result<Self, <Self as TryFrom<T>>::Error>
where Self: TryFrom<T>
{
TryFrom::try_from(value)
}
}

doc_comment! {
"FIXME docs",
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
#[inline]
pub fn try_into<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
where Self: TryInto<T>
{
TryInto::try_into(self)
}
}

doc_comment! {
concat!("Converts a string slice in a given base to an integer.
Expand Down Expand Up @@ -2340,6 +2362,28 @@ stringify!($MaxV), ");", $EndFeature, "
pub const fn max_value() -> Self { !0 }
}

doc_comment! {
"FIXME docs",
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
#[inline]
pub fn try_from<T>(value: T) -> Result<Self, <Self as TryFrom<T>>::Error>
where Self: TryFrom<T>
{
TryFrom::try_from(value)
}
}

doc_comment! {
"FIXME docs",
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
#[inline]
pub fn try_into<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
where Self: TryInto<T>
{
TryInto::try_into(self)
}
}

doc_comment! {
concat!("Converts a string slice in a given base to an integer.
Expand Down
1 change: 0 additions & 1 deletion src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::cell::Cell;
use core::convert::TryFrom;
use core::iter::*;
use core::{i8, i16, isize};
use core::usize;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/tests/num/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::convert::{TryFrom, TryInto};
use core::convert::TryFrom;
use core::cmp::PartialEq;
use core::fmt::Debug;
use core::marker::Copy;
Expand Down
1 change: 0 additions & 1 deletion src/librustc/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::ty::layout::{self, HasDataLayout, Size};

use rustc_macros::HashStable;

use std::convert::TryFrom;
use std::fmt::{self, Display};

/// Used by `check_in_alloc` to indicate context of check
Expand Down
1 change: 0 additions & 1 deletion src/librustc_apfloat/ieee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO};
use crate::{Float, FloatConvert, ParseError, Round, Status, StatusAnd};

use core::cmp::{self, Ordering};
use core::convert::TryFrom;
use core::fmt::{self, Write};
use core::marker::PhantomData;
use core::mem;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_metadata/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::rmeta::*;

use rustc_index::vec::Idx;
use rustc_serialize::{Encodable, opaque::Encoder};
use std::convert::TryInto;
use std::marker::PhantomData;
use std::num::NonZeroUsize;
use log::debug;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ mod simplify;
mod test;
mod util;

use std::convert::TryFrom;

impl<'a, 'tcx> Builder<'a, 'tcx> {
/// Generates MIR for a `match` expression.
///
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/build/matches/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::hair::*;
use rustc::mir::*;
use smallvec::SmallVec;
use std::u32;
use std::convert::TryInto;

impl<'a, 'tcx> Builder<'a, 'tcx> {
pub fn field_match_pairs<'pat>(
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::error::Error;
use std::borrow::{Borrow, Cow};
use std::hash::Hash;
use std::collections::hash_map::Entry;
use std::convert::TryInto;

use rustc::hir::def::DefKind;
use rustc::hir::def_id::DefId;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ use arena::TypedArena;

use smallvec::{smallvec, SmallVec};
use std::cmp::{self, max, min, Ordering};
use std::convert::TryInto;
use std::fmt;
use std::iter::{FromIterator, IntoIterator};
use std::ops::RangeInclusive;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Functions concerning immediate values and operands, and reading from operands.
//! All high-level functions to read from memory work on operands as sources.

use std::convert::{TryInto, TryFrom};

use rustc::{mir, ty};
use rustc::ty::layout::{
self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, PrimitiveExt, VariantIdx,
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! into a place.
//! All high-level functions to write to memory work on places as destinations.

use std::convert::TryFrom;
use std::hash::Hash;

use rustc::mir;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_mir/util/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use rustc::ty::util::IntTypeExt;
use rustc_index::vec::Idx;
use crate::util::patch::MirPatch;

use std::convert::TryInto;

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum DropFlagState {
Present, // i.e., initialized
Expand Down
1 change: 0 additions & 1 deletion src/librustc_parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_lexer::Base;
use rustc_lexer::unescape;

use std::char;
use std::convert::TryInto;
use rustc_data_structures::sync::Lrc;
use log::debug;

Expand Down
2 changes: 0 additions & 2 deletions src/libstd/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use crate::io::prelude::*;
use crate::cmp;
use crate::io::{self, Initializer, SeekFrom, Error, ErrorKind, IoSlice, IoSliceMut};

use core::convert::TryInto;

/// A `Cursor` wraps an in-memory buffer and provides it with a
/// [`Seek`] implementation.
///
Expand Down
1 change: 0 additions & 1 deletion src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ impl File {

#[cfg(not(target_os = "android"))]
{
use crate::convert::TryInto;
let size: off64_t = size
.try_into()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
Expand Down
1 change: 0 additions & 1 deletion src/libstd/sys/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::time::Duration;
use core::hash::{Hash, Hasher};

pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
use crate::convert::TryInto;

const NSEC_PER_SEC: u64 = 1_000_000_000;

Expand Down
1 change: 0 additions & 1 deletion src/libstd/sys/windows/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::fmt;
use crate::mem;
use crate::sys::c;
use crate::time::Duration;
use crate::convert::TryInto;

use core::hash::{Hash, Hasher};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::convert::TryInto;

struct S;

fn main() {
let _: u32 = 5i32.try_into::<32>().unwrap(); //~ ERROR wrong number of const arguments
//~^ ERROR wrong number of type arguments
S.f::<0>(); //~ ERROR no method named `f`
S::<0>; //~ ERROR wrong number of const arguments
}
14 changes: 10 additions & 4 deletions src/test/ui/const-generics/invalid-const-arg-for-type-param.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
error[E0107]: wrong number of const arguments: expected 0, found 1
--> $DIR/invalid-const-arg-for-type-param.rs:6:34
--> $DIR/invalid-const-arg-for-type-param.rs:4:34
|
LL | let _: u32 = 5i32.try_into::<32>().unwrap();
| ^^ unexpected const argument

error[E0107]: wrong number of type arguments: expected 1, found 0
--> $DIR/invalid-const-arg-for-type-param.rs:4:23
|
LL | let _: u32 = 5i32.try_into::<32>().unwrap();
| ^^^^^^^^ expected 1 type argument

error[E0599]: no method named `f` found for type `S` in the current scope
--> $DIR/invalid-const-arg-for-type-param.rs:7:7
--> $DIR/invalid-const-arg-for-type-param.rs:6:7
|
LL | struct S;
| --------- method `f` not found for this
Expand All @@ -14,12 +20,12 @@ LL | S.f::<0>();
| ^ method not found in `S`

error[E0107]: wrong number of const arguments: expected 0, found 1
--> $DIR/invalid-const-arg-for-type-param.rs:8:9
--> $DIR/invalid-const-arg-for-type-param.rs:7:9
|
LL | S::<0>;
| ^ unexpected const argument

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0107, E0599.
For more information about an error, try `rustc --explain E0107`.
3 changes: 0 additions & 3 deletions src/test/ui/numeric/numeric-cast.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// run-rustfix

// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
use std::convert::TryInto;

fn foo<N>(_x: N) {}

fn main() {
Expand Down
3 changes: 0 additions & 3 deletions src/test/ui/numeric/numeric-cast.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// run-rustfix

// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
use std::convert::TryInto;

fn foo<N>(_x: N) {}

fn main() {
Expand Down
Loading

0 comments on commit da301ef

Please sign in to comment.