Skip to content

Commit da301ef

Browse files
committed
Add inherent try_from and try_into methods to integer types
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.
1 parent 2539b5f commit da301ef

File tree

27 files changed

+171
-152
lines changed

27 files changed

+171
-152
lines changed

src/libcore/convert.rs

-2
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,6 @@ pub trait TryInto<T>: Sized {
451451
/// As described, [`i32`] implements `TryFrom<`[`i64`]`>`:
452452
///
453453
/// ```
454-
/// use std::convert::TryFrom;
455-
///
456454
/// let big_number = 1_000_000_000_000i64;
457455
/// // Silently truncates `big_number`, requires detecting
458456
/// // and handling the truncation after the fact.

src/libcore/iter/range.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::convert::TryFrom;
21
use crate::mem;
32
use crate::ops::{self, Add, Sub, Try};
43
use crate::usize;

src/libcore/num/dec2flt/rawfp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! take the universally-correct slow path (Algorithm M) for very small and very large numbers.
1919
//! That algorithm needs only next_float() which does handle subnormals and zeros.
2020
use crate::cmp::Ordering::{Equal, Greater, Less};
21-
use crate::convert::{TryFrom, TryInto};
21+
use crate::convert::TryFrom;
2222
use crate::fmt::{Debug, LowerExp};
2323
use crate::num::dec2flt::num::{self, Big};
2424
use crate::num::dec2flt::table;

src/libcore/num/mod.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
#![stable(feature = "rust1", since = "1.0.0")]
66

7-
use crate::convert::TryFrom;
7+
use crate::convert::{TryFrom, TryInto};
88
use crate::fmt;
99
use crate::intrinsics;
1010
use crate::mem;
@@ -278,6 +278,28 @@ $EndFeature, "
278278
}
279279
}
280280

281+
doc_comment! {
282+
"FIXME docs",
283+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
284+
#[inline]
285+
pub fn try_from<T>(value: T) -> Result<Self, <Self as TryFrom<T>>::Error>
286+
where Self: TryFrom<T>
287+
{
288+
TryFrom::try_from(value)
289+
}
290+
}
291+
292+
doc_comment! {
293+
"FIXME docs",
294+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
295+
#[inline]
296+
pub fn try_into<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
297+
where Self: TryInto<T>
298+
{
299+
TryInto::try_into(self)
300+
}
301+
}
302+
281303
doc_comment! {
282304
concat!("Converts a string slice in a given base to an integer.
283305
@@ -2340,6 +2362,28 @@ stringify!($MaxV), ");", $EndFeature, "
23402362
pub const fn max_value() -> Self { !0 }
23412363
}
23422364

2365+
doc_comment! {
2366+
"FIXME docs",
2367+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
2368+
#[inline]
2369+
pub fn try_from<T>(value: T) -> Result<Self, <Self as TryFrom<T>>::Error>
2370+
where Self: TryFrom<T>
2371+
{
2372+
TryFrom::try_from(value)
2373+
}
2374+
}
2375+
2376+
doc_comment! {
2377+
"FIXME docs",
2378+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
2379+
#[inline]
2380+
pub fn try_into<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
2381+
where Self: TryInto<T>
2382+
{
2383+
TryInto::try_into(self)
2384+
}
2385+
}
2386+
23432387
doc_comment! {
23442388
concat!("Converts a string slice in a given base to an integer.
23452389

src/libcore/tests/iter.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::cell::Cell;
2-
use core::convert::TryFrom;
32
use core::iter::*;
43
use core::{i8, i16, isize};
54
use core::usize;

src/libcore/tests/num/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::convert::{TryFrom, TryInto};
1+
use core::convert::TryFrom;
22
use core::cmp::PartialEq;
33
use core::fmt::Debug;
44
use core::marker::Copy;

src/librustc/mir/interpret/pointer.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::ty::layout::{self, HasDataLayout, Size};
55

66
use rustc_macros::HashStable;
77

8-
use std::convert::TryFrom;
98
use std::fmt::{self, Display};
109

1110
/// Used by `check_in_alloc` to indicate context of check

src/librustc_apfloat/ieee.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO};
22
use crate::{Float, FloatConvert, ParseError, Round, Status, StatusAnd};
33

44
use core::cmp::{self, Ordering};
5-
use core::convert::TryFrom;
65
use core::fmt::{self, Write};
76
use core::marker::PhantomData;
87
use core::mem;

src/librustc_metadata/rmeta/table.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::rmeta::*;
22

33
use rustc_index::vec::Idx;
44
use rustc_serialize::{Encodable, opaque::Encoder};
5-
use std::convert::TryInto;
65
use std::marker::PhantomData;
76
use std::num::NonZeroUsize;
87
use log::debug;

src/librustc_mir/build/matches/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ mod simplify;
2626
mod test;
2727
mod util;
2828

29-
use std::convert::TryFrom;
30-
3129
impl<'a, 'tcx> Builder<'a, 'tcx> {
3230
/// Generates MIR for a `match` expression.
3331
///

src/librustc_mir/build/matches/util.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::hair::*;
44
use rustc::mir::*;
55
use smallvec::SmallVec;
66
use std::u32;
7-
use std::convert::TryInto;
87

98
impl<'a, 'tcx> Builder<'a, 'tcx> {
109
pub fn field_match_pairs<'pat>(

src/librustc_mir/const_eval.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::error::Error;
55
use std::borrow::{Borrow, Cow};
66
use std::hash::Hash;
77
use std::collections::hash_map::Entry;
8-
use std::convert::TryInto;
98

109
use rustc::hir::def::DefKind;
1110
use rustc::hir::def_id::DefId;

src/librustc_mir/hair/pattern/_match.rs

-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ use arena::TypedArena;
253253

254254
use smallvec::{smallvec, SmallVec};
255255
use std::cmp::{self, max, min, Ordering};
256-
use std::convert::TryInto;
257256
use std::fmt;
258257
use std::iter::{FromIterator, IntoIterator};
259258
use std::ops::RangeInclusive;

src/librustc_mir/interpret/operand.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Functions concerning immediate values and operands, and reading from operands.
22
//! All high-level functions to read from memory work on operands as sources.
33
4-
use std::convert::{TryInto, TryFrom};
5-
64
use rustc::{mir, ty};
75
use rustc::ty::layout::{
86
self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, PrimitiveExt, VariantIdx,

src/librustc_mir/interpret/place.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! into a place.
33
//! All high-level functions to write to memory work on places as destinations.
44
5-
use std::convert::TryFrom;
65
use std::hash::Hash;
76

87
use rustc::mir;

src/librustc_mir/util/elaborate_drops.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use rustc::ty::util::IntTypeExt;
1010
use rustc_index::vec::Idx;
1111
use crate::util::patch::MirPatch;
1212

13-
use std::convert::TryInto;
14-
1513
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
1614
pub enum DropFlagState {
1715
Present, // i.e., initialized

src/librustc_parse/lexer/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_lexer::Base;
99
use rustc_lexer::unescape;
1010

1111
use std::char;
12-
use std::convert::TryInto;
1312
use rustc_data_structures::sync::Lrc;
1413
use log::debug;
1514

src/libstd/io/cursor.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::io::prelude::*;
33
use crate::cmp;
44
use crate::io::{self, Initializer, SeekFrom, Error, ErrorKind, IoSlice, IoSliceMut};
55

6-
use core::convert::TryInto;
7-
86
/// A `Cursor` wraps an in-memory buffer and provides it with a
97
/// [`Seek`] implementation.
108
///

src/libstd/sys/unix/fs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,6 @@ impl File {
752752

753753
#[cfg(not(target_os = "android"))]
754754
{
755-
use crate::convert::TryInto;
756755
let size: off64_t = size
757756
.try_into()
758757
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

src/libstd/sys/unix/time.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::time::Duration;
44
use core::hash::{Hash, Hasher};
55

66
pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
7-
use crate::convert::TryInto;
87

98
const NSEC_PER_SEC: u64 = 1_000_000_000;
109

src/libstd/sys/windows/time.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::fmt;
33
use crate::mem;
44
use crate::sys::c;
55
use crate::time::Duration;
6-
use crate::convert::TryInto;
76

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

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::convert::TryInto;
2-
31
struct S;
42

53
fn main() {
64
let _: u32 = 5i32.try_into::<32>().unwrap(); //~ ERROR wrong number of const arguments
5+
//~^ ERROR wrong number of type arguments
76
S.f::<0>(); //~ ERROR no method named `f`
87
S::<0>; //~ ERROR wrong number of const arguments
98
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
error[E0107]: wrong number of const arguments: expected 0, found 1
2-
--> $DIR/invalid-const-arg-for-type-param.rs:6:34
2+
--> $DIR/invalid-const-arg-for-type-param.rs:4:34
33
|
44
LL | let _: u32 = 5i32.try_into::<32>().unwrap();
55
| ^^ unexpected const argument
66

7+
error[E0107]: wrong number of type arguments: expected 1, found 0
8+
--> $DIR/invalid-const-arg-for-type-param.rs:4:23
9+
|
10+
LL | let _: u32 = 5i32.try_into::<32>().unwrap();
11+
| ^^^^^^^^ expected 1 type argument
12+
713
error[E0599]: no method named `f` found for type `S` in the current scope
8-
--> $DIR/invalid-const-arg-for-type-param.rs:7:7
14+
--> $DIR/invalid-const-arg-for-type-param.rs:6:7
915
|
1016
LL | struct S;
1117
| --------- method `f` not found for this
@@ -14,12 +20,12 @@ LL | S.f::<0>();
1420
| ^ method not found in `S`
1521

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

22-
error: aborting due to 3 previous errors
28+
error: aborting due to 4 previous errors
2329

2430
Some errors have detailed explanations: E0107, E0599.
2531
For more information about an error, try `rustc --explain E0107`.

src/test/ui/numeric/numeric-cast.fixed

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// run-rustfix
22

3-
// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
4-
use std::convert::TryInto;
5-
63
fn foo<N>(_x: N) {}
74

85
fn main() {

src/test/ui/numeric/numeric-cast.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// run-rustfix
22

3-
// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
4-
use std::convert::TryInto;
5-
63
fn foo<N>(_x: N) {}
74

85
fn main() {

0 commit comments

Comments
 (0)