Skip to content

Commit a11f31e

Browse files
committed
tests
1 parent 4ff1174 commit a11f31e

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

library/core/src/net/socket_addr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::display_buffer::DisplayBuffer;
22
use crate::fmt::{self, Write};
3+
use crate::marker::Destruct;
34
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
45

56
/// An internet socket address, either IPv4 or IPv6.
@@ -612,7 +613,8 @@ impl const From<SocketAddrV6> for SocketAddr {
612613
}
613614

614615
#[stable(feature = "addr_from_into_ip", since = "1.17.0")]
615-
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
616+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
617+
impl<I: ~const Into<IpAddr> + ~const Destruct> const From<(I, u16)> for SocketAddr {
616618
/// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`].
617619
///
618620
/// This conversion creates a [`SocketAddr::V4`] for an [`IpAddr::V4`]

library/core/src/num/nonzero.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ where
296296
}
297297

298298
#[stable(feature = "from_nonzero", since = "1.31.0")]
299-
impl<T> From<NonZero<T>> for T
299+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
300+
impl<T> const From<NonZero<T>> for T
300301
where
301302
T: ZeroablePrimitive,
302303
{

library/core/src/result.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@
534534
#![stable(feature = "rust1", since = "1.0.0")]
535535

536536
use crate::iter::{self, FusedIterator, TrustedLen};
537+
use crate::marker::Destruct;
537538
use crate::ops::{self, ControlFlow, Deref, DerefMut};
538539
use crate::{convert, fmt, hint};
539540

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// tests/const_conversion_tests.rs
2+
#![feature(const_trait_impl,f16, f128)]
3+
#![feature(ascii_char, ascii_char_variants)]
4+
5+
use std::convert::TryFrom;
6+
use std::net::{Ipv4Addr, IpAddr, SocketAddr};
7+
use std::str::FromStr;
8+
use std::num::NonZero;
9+
use std::ascii::Char as AsciiChar;
10+
11+
const _: () = {
12+
// --- From<AsciiChar> / From<char> / TryFrom<u32> for char ---
13+
let ac = AsciiChar::LessThanSign;
14+
let _: u8 = u8::from(ac);
15+
let _: u16 = u16::from(ac);
16+
let _: u32 = u32::from(ac);
17+
let _: u64 = u64::from(ac);
18+
let _: u128 = u128::from(ac);
19+
let _: char = char::from(ac);
20+
21+
let c = '🚀';
22+
let _: u32 = u32::from(c);
23+
let _: u64 = u64::from(c);
24+
let _: u128 = u128::from(c);
25+
let _: char = char::from(65u8);
26+
let _: char = char::try_from(0x1F680u32).unwrap();
27+
28+
29+
let mut st = String::from("world");
30+
//~^ ERROR: the trait bound `String: const From<&str>` is not satisfied
31+
32+
// --- AsRef / AsMut on primitive refs, arrays, strings ---
33+
let x = &5u8;
34+
let _: &u8 = x.as_ref();
35+
//~^ ERROR: the method `as_ref` exists for reference `&u8`, but its trait bounds were not satisfied
36+
let mut y = 6u8;
37+
let ym = &mut y;
38+
let _: &mut u8 = ym.as_mut();
39+
//~^ ERROR: the method `as_mut` exists for mutable reference `&mut u8`, but its trait bounds were not satisfied
40+
41+
let arr = [1u8, 2, 3];
42+
let _: &[u8] = arr.as_ref();
43+
//~^ ERROR: the trait bound `[u8; 3]: const AsRef<[u8]>` is not satisfied [E0277]
44+
let mut arr2 = [4u8, 5, 6];
45+
let _: &mut [u8] = arr2.as_mut();
46+
//~^ ERROR: the trait bound `[u8; 3]: const AsMut<[u8]>` is not satisfied [E0277]
47+
48+
let s = "hello";
49+
let _: &str = s.as_ref();
50+
51+
// --- bool → integers, isize ↔ usize ---
52+
let b = true;
53+
let _: u8 = u8::from(b);
54+
let _: i8 = i8::from(b);
55+
let _: u16 = u16::from(b);
56+
let _: i16 = i16::from(b);
57+
let _: usize = usize::from(b);
58+
let _: isize = isize::from(b);
59+
60+
let _ = usize::try_from(42_isize);
61+
let _ = isize::try_from(42_usize);
62+
63+
// --- NonZero conversions ---
64+
let nz8 = unsafe { NonZero::new_unchecked(5)};
65+
let _ = NonZero::<u16>::from(nz8);
66+
let _ = u8::from(nz8);
67+
68+
// --- IpAddr / SocketAddr ---
69+
let v4 = Ipv4Addr::new(127, 0, 0, 1);
70+
let _: u32 = u32::from(v4);
71+
let _: SocketAddr = SocketAddr::from((v4, 8080));
72+
73+
// --- FromStr for ints ---
74+
let _: u8 = u8::from_str("123").unwrap();
75+
let _: i16 = i16::from_str("-456").unwrap();
76+
77+
();
78+
};
79+
80+
fn main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0277]: the trait bound `String: const From<&str>` is not satisfied
2+
--> $DIR/const-from.rs:29:18
3+
|
4+
LL | let mut st = String::from("world");
5+
| ^^^^^^
6+
7+
error[E0599]: the method `as_ref` exists for reference `&u8`, but its trait bounds were not satisfied
8+
--> $DIR/const-from.rs:34:25
9+
|
10+
LL | let _: &u8 = x.as_ref();
11+
| ^^^^^^ method cannot be called on `&u8` due to unsatisfied trait bounds
12+
|
13+
= note: the following trait bounds were not satisfied:
14+
`u8: AsRef<_>`
15+
which is required by `&u8: AsRef<_>`
16+
17+
error[E0599]: the method `as_mut` exists for mutable reference `&mut u8`, but its trait bounds were not satisfied
18+
--> $DIR/const-from.rs:38:26
19+
|
20+
LL | let _: &mut u8 = ym.as_mut();
21+
| ^^^^^^ method cannot be called on `&mut u8` due to unsatisfied trait bounds
22+
|
23+
= note: the following trait bounds were not satisfied:
24+
`u8: AsMut<_>`
25+
which is required by `&mut u8: AsMut<_>`
26+
27+
error[E0277]: the trait bound `[u8; 3]: const AsRef<[u8]>` is not satisfied
28+
--> $DIR/const-from.rs:42:24
29+
|
30+
LL | let _: &[u8] = arr.as_ref();
31+
| ^^^^^^
32+
33+
error[E0277]: the trait bound `[u8; 3]: const AsMut<[u8]>` is not satisfied
34+
--> $DIR/const-from.rs:45:29
35+
|
36+
LL | let _: &mut [u8] = arr2.as_mut();
37+
| ^^^^^^
38+
39+
error: aborting due to 5 previous errors
40+
41+
Some errors have detailed explanations: E0277, E0599.
42+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)