Skip to content
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

Switch libcore array implementations to const generics. #60466

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Add two tests for const generics array impls.
crlf0710 committed Jun 12, 2019
commit 0138ad4b3312e84e5aac13482327fe09b1ee150c
133 changes: 133 additions & 0 deletions src/test/ui/const-generics/const-generics-array-impls-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// run-pass

#![allow(dead_code)]
#![allow(unused_variables)]

use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::convert::TryFrom;

#[derive(Copy, Clone, Debug)]
struct MyBool(bool);

impl PartialEq<bool> for MyBool {
fn eq(&self, other: &bool) -> bool {
self.0 == *other
}
}

impl PartialEq<MyBool> for bool {
fn eq(&self, other: &MyBool) -> bool {
*self == other.0
}
}

macro_rules! boolean_check_with_len {
($LEN:expr) => {{
let a = [true; $LEN];
let mut b = [true; $LEN];
let mut c = [false; $LEN];
let mut d = [false; $LEN];

// PartialEq, Debug
assert_eq!(a, b);
if $LEN > 0 {
assert_ne!(a, c);
} else {
assert_eq!(a, c);
}

// AsRef
let a_slice: &[bool] = a.as_ref();

// AsMut
let b_slice: &mut [bool] = b.as_mut();

// Borrow
let c_slice: &[bool] = c.borrow();

// BorrowMut
let d_slice: &mut [bool] = d.borrow_mut();

// TryFrom
let e_ref: &[bool; $LEN] = TryFrom::try_from(a_slice).unwrap();

// TryFrom#2
let f: [bool; $LEN] = TryFrom::try_from(a_slice).unwrap();

// TryFrom#3
let g_mut: &mut [bool; $LEN] = TryFrom::try_from(b_slice).unwrap();

// PartialEq, Eq, Hash
let h: HashSet<[bool; $LEN]> = HashSet::new();

// PartialEq, Eq, PartialOrd, Ord
let i: BTreeSet<[bool; $LEN]> = BTreeSet::new();

// IntoIterator#1
for j in &a {
let _ = j;
}

// IntoIterator#2
for k in &mut c {
let _ = k;
}

let l = [MyBool(true); $LEN];
let l_slice: &[MyBool] = l.as_ref();

let mut m = [MyBool(false); $LEN];
let m_slice: &mut [MyBool] = m.as_mut();

// PartialEq
assert_eq!(a, l);
assert_eq!(a, *l_slice);
assert_eq!(a_slice, l);
assert_eq!(a, l_slice);
assert_eq!(c, m_slice);
assert_eq!(m_slice, c);

// The currently omitted impls
/*
assert_eq!(a, &l);
assert_eq!(&l, a);
assert_eq!(a, &mut l);
assert_eq!(&mut l, a);
*/

/* Default is not using const generics now */
/*
assert_eq!(c, Default::default())
*/
}};
}

fn check_boolean_array_less_than_32() {
boolean_check_with_len!(0);
boolean_check_with_len!(1);
boolean_check_with_len!(2);
boolean_check_with_len!(4);
boolean_check_with_len!(8);
boolean_check_with_len!(16);
boolean_check_with_len!(31);
boolean_check_with_len!(32);
}

fn check_boolean_array_more_than_32() {
boolean_check_with_len!(33);
boolean_check_with_len!(64);
boolean_check_with_len!(128);
boolean_check_with_len!(256);
boolean_check_with_len!(512);
boolean_check_with_len!(1024);
}

fn main() {
// regression check
check_boolean_array_less_than_32();
// newly added
check_boolean_array_more_than_32();
}
111 changes: 111 additions & 0 deletions src/test/ui/const-generics/const-generics-array-impls-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// run-pass

#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
#![allow(dead_code)]
#![allow(unused_variables)]

use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::convert::TryFrom;

#[derive(Copy, Clone, Debug)]
struct MyBool(bool);

impl PartialEq<bool> for MyBool {
fn eq(&self, other: &bool) -> bool {
self.0 == *other
}
}

impl PartialEq<MyBool> for bool {
fn eq(&self, other: &MyBool) -> bool {
*self == other.0
}
}

fn check_boolean_array_with_const_generics<const LEN: usize>() {
let a = [true; LEN];
let mut b = [true; LEN];
let mut c = [false; LEN];
let mut d = [false; LEN];

// PartialEq, Debug
assert_eq!(a, b);
if LEN > 0 {
assert_ne!(a, c);
} else {
assert_eq!(a, c);
}

// AsRef
let a_slice: &[bool] = a.as_ref();

// AsMut
let b_slice: &mut [bool] = b.as_mut();

// Borrow
let c_slice: &[bool] = c.borrow();

// BorrowMut
let d_slice: &mut [bool] = d.borrow_mut();

// TryFrom
let e_ref: &[bool; LEN] = TryFrom::try_from(a_slice).unwrap();

// TryFrom#2
let f: [bool; LEN] = TryFrom::try_from(a_slice).unwrap();

// TryFrom#3
let g_mut: &mut [bool; LEN] = TryFrom::try_from(b_slice).unwrap();

// PartialEq, Eq, Hash
let h: HashSet<[bool; LEN]> = HashSet::new();

// PartialEq, Eq, PartialOrd, Ord
let i: BTreeSet<[bool; LEN]> = BTreeSet::new();

// IntoIterator#1
for j in &a {
let _ = j;
}

// IntoIterator#2
for k in &mut c {
let _ = k;
}

let l = [MyBool(true); LEN];
let l_slice: &[MyBool] = l.as_ref();

let mut m = [MyBool(false); LEN];
let m_slice: &mut [MyBool] = m.as_mut();

// PartialEq
assert_eq!(a, l);
assert_eq!(a, *l_slice);
assert_eq!(a_slice, l);
assert_eq!(a, l_slice);
assert_eq!(c, m_slice);
assert_eq!(m_slice, c);

// The currently omitted impls
/*
assert_eq!(a, &l);
assert_eq!(&l, a);
assert_eq!(a, &mut l);
assert_eq!(&mut l, a);
*/

/* Default is not using const generics now */
/*
assert_eq!(c, Default::default())
*/
}

fn main() {
check_boolean_array_with_const_generics::<{ 30 }>();
check_boolean_array_with_const_generics::<{ 40 }>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/const-types.rs:3:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^