diff options
Diffstat (limited to 'src/test/rustdoc/const-generics')
9 files changed, 243 insertions, 0 deletions
diff --git a/src/test/rustdoc/const-generics/add-impl.rs b/src/test/rustdoc/const-generics/add-impl.rs new file mode 100644 index 000000000..591139523 --- /dev/null +++ b/src/test/rustdoc/const-generics/add-impl.rs @@ -0,0 +1,17 @@ +#![crate_name = "foo"] + +use std::ops::Add; + +// @has foo/struct.Simd.html '//pre[@class="rust struct"]' 'pub struct Simd<T, const WIDTH: usize>' +pub struct Simd<T, const WIDTH: usize> { + inner: T, +} + +// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]//h3[@class="code-header in-band"]' 'impl Add<Simd<u8, 16>> for Simd<u8, 16>' +impl Add for Simd<u8, 16> { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { inner: 0 } + } +} diff --git a/src/test/rustdoc/const-generics/auxiliary/extern_crate.rs b/src/test/rustdoc/const-generics/auxiliary/extern_crate.rs new file mode 100644 index 000000000..55b632a48 --- /dev/null +++ b/src/test/rustdoc/const-generics/auxiliary/extern_crate.rs @@ -0,0 +1,16 @@ +// edition:2018 +pub fn extern_fn<const N: usize>() -> impl Iterator<Item = [u8; N]> { + [[0; N]; N].iter().copied() +} + +pub struct ExternTy<const N: usize> { + pub inner: [u8; N], +} + +pub type TyAlias<const N: usize> = ExternTy<N>; + +pub trait WTrait<const N: usize, const M: usize> { + fn hey<const P: usize>() -> usize { + N + M + P + } +} diff --git a/src/test/rustdoc/const-generics/const-generic-defaults.rs b/src/test/rustdoc/const-generics/const-generic-defaults.rs new file mode 100644 index 000000000..2693d9b59 --- /dev/null +++ b/src/test/rustdoc/const-generics/const-generic-defaults.rs @@ -0,0 +1,5 @@ +#![crate_name = "foo"] + +// @has foo/struct.Foo.html '//pre[@class="rust struct"]' \ +// 'pub struct Foo<const M: usize = 10, const N: usize = M, T = i32>(_);' +pub struct Foo<const M: usize = 10, const N: usize = M, T = i32>(T); diff --git a/src/test/rustdoc/const-generics/const-generic-slice.rs b/src/test/rustdoc/const-generics/const-generic-slice.rs new file mode 100644 index 000000000..4279de91f --- /dev/null +++ b/src/test/rustdoc/const-generics/const-generic-slice.rs @@ -0,0 +1,11 @@ +#![crate_name = "foo"] + +pub trait Array { + type Item; +} + +// @has foo/trait.Array.html +// @has - '//*[@class="impl has-srclink"]' 'impl<T, const N: usize> Array for [T; N]' +impl<T, const N: usize> Array for [T; N] { + type Item = T; +} diff --git a/src/test/rustdoc/const-generics/const-generics-docs.rs b/src/test/rustdoc/const-generics/const-generics-docs.rs new file mode 100644 index 000000000..352a8e646 --- /dev/null +++ b/src/test/rustdoc/const-generics/const-generics-docs.rs @@ -0,0 +1,128 @@ +// edition:2018 +// aux-build: extern_crate.rs +#![crate_name = "foo"] + +extern crate extern_crate; +// @has foo/fn.extern_fn.html '//pre[@class="rust fn"]' \ +// 'pub fn extern_fn<const N: usize>() -> impl Iterator<Item = [u8; N]>' +pub use extern_crate::extern_fn; +// @has foo/struct.ExternTy.html '//pre[@class="rust struct"]' \ +// 'pub struct ExternTy<const N: usize> {' +pub use extern_crate::ExternTy; +// @has foo/type.TyAlias.html '//pre[@class="rust typedef"]' \ +// 'type TyAlias<const N: usize> = ExternTy<N>;' +pub use extern_crate::TyAlias; +// @has foo/trait.WTrait.html '//pre[@class="rust trait"]' \ +// 'pub trait WTrait<const N: usize, const M: usize>' +// @has - '//*[@class="rust trait"]' 'fn hey<const P: usize>() -> usize' +pub use extern_crate::WTrait; + +// @has foo/trait.Trait.html '//pre[@class="rust trait"]' \ +// 'pub trait Trait<const N: usize>' +// @has - '//*[@id="impl-Trait%3C1%3E-for-u8"]//h3[@class="code-header in-band"]' 'impl Trait<1> for u8' +// @has - '//*[@id="impl-Trait%3C2%3E-for-u8"]//h3[@class="code-header in-band"]' 'impl Trait<2> for u8' +// @has - '//*[@id="impl-Trait%3C{1%20+%202}%3E-for-u8"]//h3[@class="code-header in-band"]' 'impl Trait<{1 + 2}> for u8' +// @has - '//*[@id="impl-Trait%3CN%3E-for-%5Bu8%3B%20N%5D"]//h3[@class="code-header in-band"]' \ +// 'impl<const N: usize> Trait<N> for [u8; N]' +pub trait Trait<const N: usize> {} +impl Trait<1> for u8 {} +impl Trait<2> for u8 {} +impl Trait<{1 + 2}> for u8 {} +impl<const N: usize> Trait<N> for [u8; N] {} + +// @has foo/struct.Foo.html '//pre[@class="rust struct"]' \ +// 'pub struct Foo<const N: usize> where u8: Trait<N>' +pub struct Foo<const N: usize> where u8: Trait<N>; +// @has foo/struct.Bar.html '//pre[@class="rust struct"]' 'pub struct Bar<T, const N: usize>(_)' +pub struct Bar<T, const N: usize>([T; N]); + +// @has foo/struct.Foo.html '//*[@id="impl-Foo%3CM%3E"]/h3[@class="code-header in-band"]' 'impl<const M: usize> Foo<M> where u8: Trait<M>' +impl<const M: usize> Foo<M> where u8: Trait<M> { + // @has - '//*[@id="associatedconstant.FOO_ASSOC"]' 'pub const FOO_ASSOC: usize' + pub const FOO_ASSOC: usize = M + 13; + + // @has - '//*[@id="method.hey"]' 'pub fn hey<const N: usize>(&self) -> Bar<u8, N>' + pub fn hey<const N: usize>(&self) -> Bar<u8, N> { + Bar([0; N]) + } +} + +// @has foo/struct.Bar.html '//*[@id="impl-Bar%3Cu8%2C%20M%3E"]/h3[@class="code-header in-band"]' 'impl<const M: usize> Bar<u8, M>' +impl<const M: usize> Bar<u8, M> { + // @has - '//*[@id="method.hey"]' \ + // 'pub fn hey<const N: usize>(&self) -> Foo<N> where u8: Trait<N>' + pub fn hey<const N: usize>(&self) -> Foo<N> where u8: Trait<N> { + Foo + } +} + +// @has foo/fn.test.html '//pre[@class="rust fn"]' \ +// 'pub fn test<const N: usize>() -> impl Trait<N> where u8: Trait<N>' +pub fn test<const N: usize>() -> impl Trait<N> where u8: Trait<N> { + 2u8 +} + +// @has foo/fn.a_sink.html '//pre[@class="rust fn"]' \ +// 'pub async fn a_sink<const N: usize>(v: [u8; N]) -> impl Trait<N>' +pub async fn a_sink<const N: usize>(v: [u8; N]) -> impl Trait<N> { + v +} + +// @has foo/fn.b_sink.html '//pre[@class="rust fn"]' \ +// 'pub async fn b_sink<const N: usize>(_: impl Trait<N>)' +pub async fn b_sink<const N: usize>(_: impl Trait<N>) {} + +// @has foo/fn.concrete.html '//pre[@class="rust fn"]' \ +// 'pub fn concrete() -> [u8; 22]' +pub fn concrete() -> [u8; 3 + std::mem::size_of::<u64>() << 1] { + Default::default() +} + +// @has foo/type.Faz.html '//pre[@class="rust typedef"]' \ +// 'type Faz<const N: usize> = [u8; N];' +pub type Faz<const N: usize> = [u8; N]; +// @has foo/type.Fiz.html '//pre[@class="rust typedef"]' \ +// 'type Fiz<const N: usize> = [[u8; N]; 48];' +pub type Fiz<const N: usize> = [[u8; N]; 3 << 4]; + +macro_rules! define_me { + ($t:tt<$q:tt>) => { + pub struct $t<const $q: usize>([u8; $q]); + } +} + +// @has foo/struct.Foz.html '//pre[@class="rust struct"]' \ +// 'pub struct Foz<const N: usize>(_);' +define_me!(Foz<N>); + +trait Q { + const ASSOC: usize; +} + +impl<const N: usize> Q for [u8; N] { + const ASSOC: usize = N; +} + +// @has foo/fn.q_user.html '//pre[@class="rust fn"]' \ +// 'pub fn q_user() -> [u8; 13]' +pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] { + [0; <[u8; 13] as Q>::ASSOC] +} + +// @has foo/union.Union.html '//pre[@class="rust union"]' \ +// 'pub union Union<const N: usize>' +pub union Union<const N: usize> { + // @has - //pre "pub arr: [u8; N]" + pub arr: [u8; N], + // @has - //pre "pub another_arr: [(); N]" + pub another_arr: [(); N], +} + +// @has foo/enum.Enum.html '//pre[@class="rust enum"]' \ +// 'pub enum Enum<const N: usize>' +pub enum Enum<const N: usize> { + // @has - //pre "Variant([u8; N])" + Variant([u8; N]), + // @has - //pre "EmptyVariant" + EmptyVariant, +} diff --git a/src/test/rustdoc/const-generics/const-impl.rs b/src/test/rustdoc/const-generics/const-impl.rs new file mode 100644 index 000000000..f1181d54a --- /dev/null +++ b/src/test/rustdoc/const-generics/const-impl.rs @@ -0,0 +1,37 @@ +#![allow(incomplete_features)] +#![feature(adt_const_params)] +#![crate_name = "foo"] + +#[derive(PartialEq, Eq)] +pub enum Order { + Sorted, + Unsorted, +} + +// @has foo/struct.VSet.html '//pre[@class="rust struct"]' 'pub struct VSet<T, const ORDER: Order>' +// @has foo/struct.VSet.html '//*[@id="impl-Send-for-VSet%3CT%2C%20ORDER%3E"]/h3[@class="code-header in-band"]' 'impl<T, const ORDER: Order> Send for VSet<T, ORDER>' +// @has foo/struct.VSet.html '//*[@id="impl-Sync-for-VSet%3CT%2C%20ORDER%3E"]/h3[@class="code-header in-band"]' 'impl<T, const ORDER: Order> Sync for VSet<T, ORDER>' +pub struct VSet<T, const ORDER: Order> { + inner: Vec<T>, +} + +// @has foo/struct.VSet.html '//*[@id="impl-VSet%3CT%2C%20{%20Order%3A%3ASorted%20}%3E"]/h3[@class="code-header in-band"]' 'impl<T> VSet<T, { Order::Sorted }>' +impl<T> VSet<T, { Order::Sorted }> { + pub fn new() -> Self { + Self { inner: Vec::new() } + } +} + +// @has foo/struct.VSet.html '//*[@id="impl-VSet%3CT%2C%20{%20Order%3A%3AUnsorted%20}%3E"]/h3[@class="code-header in-band"]' 'impl<T> VSet<T, { Order::Unsorted }>' +impl<T> VSet<T, { Order::Unsorted }> { + pub fn new() -> Self { + Self { inner: Vec::new() } + } +} + +pub struct Escape<const S: &'static str>; + +// @has foo/struct.Escape.html '//*[@id="impl-Escape%3Cr#%22%3Cscript%3Ealert(%22Escape%22)%3B%3C/script%3E%22#%3E"]/h3[@class="code-header in-band"]' 'impl Escape<r#"<script>alert("Escape");</script>"#>' +impl Escape<r#"<script>alert("Escape");</script>"#> { + pub fn f() {} +} diff --git a/src/test/rustdoc/const-generics/generic_const_exprs.rs b/src/test/rustdoc/const-generics/generic_const_exprs.rs new file mode 100644 index 000000000..215ee228e --- /dev/null +++ b/src/test/rustdoc/const-generics/generic_const_exprs.rs @@ -0,0 +1,7 @@ +#![crate_name = "foo"] +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] +// make sure that `ConstEvaluatable` predicates dont cause rustdoc to ICE #77647 +// @has foo/struct.Ice.html '//pre[@class="rust struct"]' \ +// 'pub struct Ice<const N: usize>;' +pub struct Ice<const N: usize> where [(); N + 1]:; diff --git a/src/test/rustdoc/const-generics/lazy_normalization_consts/const-equate-pred.rs b/src/test/rustdoc/const-generics/lazy_normalization_consts/const-equate-pred.rs new file mode 100644 index 000000000..4eac8e31e --- /dev/null +++ b/src/test/rustdoc/const-generics/lazy_normalization_consts/const-equate-pred.rs @@ -0,0 +1,18 @@ +#![crate_name = "foo"] +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] + +// Checking if `Send` is implemented for `Hasher` requires us to evaluate a `ConstEquate` predicate, +// which previously caused an ICE. + +pub struct Hasher<T> { + cv_stack: T, +} + +unsafe impl<T: Default> Send for Hasher<T> {} + +// @has foo/struct.Foo.html +// @has - '//h3[@class="code-header in-band"]' 'impl Send for Foo' +pub struct Foo { + hasher: Hasher<[u8; 3]>, +} diff --git a/src/test/rustdoc/const-generics/type-alias.rs b/src/test/rustdoc/const-generics/type-alias.rs new file mode 100644 index 000000000..ebda5b194 --- /dev/null +++ b/src/test/rustdoc/const-generics/type-alias.rs @@ -0,0 +1,4 @@ +#![crate_name = "foo"] + +// @has foo/type.CellIndex.html '//pre[@class="rust typedef"]' 'type CellIndex<const D: usize> = [i64; D];' +pub type CellIndex<const D: usize> = [i64; D]; |