From 4af560ecefe7c092f9584e20e43800b52bee4795 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Mon, 14 Sep 2020 23:35:46 +0200 Subject: [PATCH 01/24] Move zero-sized-vec-push test --- library/alloc/tests/vec.rs | 17 ++++++++++++++++ src/test/ui/zero-sized/zero-sized-vec-push.rs | 20 ------------------- 2 files changed, 17 insertions(+), 20 deletions(-) delete mode 100644 src/test/ui/zero-sized/zero-sized-vec-push.rs diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 771a293b8e5d4..34995d7e40f21 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1912,3 +1912,20 @@ fn test_vec_cycle_wrapped() { c3.refs.v[0].set(Some(&c1)); c3.refs.v[1].set(Some(&c2)); } + +#[test] +fn test_zero_sized_vec_push() { + const N: usize = 8; + + for len in 0..N { + let mut tester = Vec::with_capacity(len); + assert_eq!(tester.len(), 0); + assert!(tester.capacity() >= len); + for _ in 0..len { + tester.push(()); + } + assert_eq!(tester.len(), len); + assert_eq!(tester.iter().count(), len); + tester.clear(); + } +} diff --git a/src/test/ui/zero-sized/zero-sized-vec-push.rs b/src/test/ui/zero-sized/zero-sized-vec-push.rs deleted file mode 100644 index 9e9fbc972d5c1..0000000000000 --- a/src/test/ui/zero-sized/zero-sized-vec-push.rs +++ /dev/null @@ -1,20 +0,0 @@ -// run-pass -#![allow(unused_variables)] -use std::iter::Iterator; -use std::vec::Vec; - -fn main() { - const N: usize = 8; - - for len in 0..N { - let mut tester = Vec::with_capacity(len); - assert_eq!(tester.len(), 0); - assert!(tester.capacity() >= len); - for bit in 0..len { - tester.push(()); - } - assert_eq!(tester.len(), len); - assert_eq!(tester.iter().count(), len); - tester.clear(); - } -} From 85afbd8a15a7e37b35a0653693bd6acf3138f301 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 17 Oct 2020 18:47:58 +0200 Subject: [PATCH 02/24] Rebase conflicts --- library/alloc/tests/vec_deque.rs | 30 +++++++++++++++++ .../zero-sized/zero-sized-vec-deque-push.rs | 32 ------------------- 2 files changed, 30 insertions(+), 32 deletions(-) delete mode 100644 src/test/ui/zero-sized/zero-sized-vec-deque-push.rs diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs index 05cb3a2c03d79..705f0d62fbb7a 100644 --- a/library/alloc/tests/vec_deque.rs +++ b/library/alloc/tests/vec_deque.rs @@ -1698,3 +1698,33 @@ fn test_binary_search_by_key() { assert_eq!(deque.binary_search_by_key(&3, |&(v,)| v), Ok(2)); assert_eq!(deque.binary_search_by_key(&4, |&(v,)| v), Err(3)); } + +#[test] +fn test_zero_sized_push() { + const N: usize = 8; + + // Zero sized type + struct Zst; + + // Test that for all possible sequences of push_front / push_back, + // we end up with a deque of the correct size + + for len in 0..N { + let mut tester = VecDeque::with_capacity(len); + assert_eq!(tester.len(), 0); + assert!(tester.capacity() >= len); + for case in 0..(1 << len) { + assert_eq!(tester.len(), 0); + for bit in 0..len { + if case & (1 << bit) != 0 { + tester.push_front(Zst); + } else { + tester.push_back(Zst); + } + } + assert_eq!(tester.len(), len); + assert_eq!(tester.iter().count(), len); + tester.clear(); + } + } +} diff --git a/src/test/ui/zero-sized/zero-sized-vec-deque-push.rs b/src/test/ui/zero-sized/zero-sized-vec-deque-push.rs deleted file mode 100644 index c541208703b6b..0000000000000 --- a/src/test/ui/zero-sized/zero-sized-vec-deque-push.rs +++ /dev/null @@ -1,32 +0,0 @@ -// run-pass -use std::collections::VecDeque; -use std::iter::Iterator; - -fn main() { - const N: usize = 8; - - // Zero sized type - struct Zst; - - // Test that for all possible sequences of push_front / push_back, - // we end up with a deque of the correct size - - for len in 0..N { - let mut tester = VecDeque::with_capacity(len); - assert_eq!(tester.len(), 0); - assert!(tester.capacity() >= len); - for case in 0..(1 << len) { - assert_eq!(tester.len(), 0); - for bit in 0..len { - if case & (1 << bit) != 0 { - tester.push_front(Zst); - } else { - tester.push_back(Zst); - } - } - assert_eq!(tester.len(), len); - assert_eq!(tester.iter().count(), len); - tester.clear(); - } - } -} From 4fd06b9bb566aa444fb293bbe7ef1eef71bc1e08 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 3 Oct 2020 18:49:23 +0200 Subject: [PATCH 03/24] Move vec-macro-repeat test --- library/alloc/tests/vec.rs | 13 +++++++++++++ src/test/ui/array-slice-vec/vec-macro-repeat.rs | 15 --------------- 2 files changed, 13 insertions(+), 15 deletions(-) delete mode 100644 src/test/ui/array-slice-vec/vec-macro-repeat.rs diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 34995d7e40f21..ae7518bcad0e2 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1929,3 +1929,16 @@ fn test_zero_sized_vec_push() { tester.clear(); } } + +#[test] +fn test_vec_macro_repeat() { + assert_eq!(vec![1; 3], vec![1, 1, 1]); + assert_eq!(vec![1; 2], vec![1, 1]); + assert_eq!(vec![1; 1], vec![1]); + assert_eq!(vec![1; 0], vec![]); + + // from_elem syntax (see RFC 832) + let el = Box::new(1); + let n = 3; + assert_eq!(vec![el; n], vec![Box::new(1), Box::new(1), Box::new(1)]); +} diff --git a/src/test/ui/array-slice-vec/vec-macro-repeat.rs b/src/test/ui/array-slice-vec/vec-macro-repeat.rs deleted file mode 100644 index 7be8dadbe170e..0000000000000 --- a/src/test/ui/array-slice-vec/vec-macro-repeat.rs +++ /dev/null @@ -1,15 +0,0 @@ -// run-pass - - - -pub fn main() { - assert_eq!(vec![1; 3], vec![1, 1, 1]); - assert_eq!(vec![1; 2], vec![1, 1]); - assert_eq!(vec![1; 1], vec![1]); - assert_eq!(vec![1; 0], vec![]); - - // from_elem syntax (see RFC 832) - let el = Box::new(1); - let n = 3; - assert_eq!(vec![el; n], vec![Box::new(1), Box::new(1), Box::new(1)]); -} From 42271a537a7a833c7ed448297b48be3bbc98de8c Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 3 Oct 2020 19:03:38 +0200 Subject: [PATCH 04/24] Move vec swap test --- library/alloc/tests/vec.rs | 14 +++++++++++++- src/test/ui/swap-2.rs | 14 -------------- 2 files changed, 13 insertions(+), 15 deletions(-) delete mode 100644 src/test/ui/swap-2.rs diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index ae7518bcad0e2..47ebe56f9fd3b 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -3,7 +3,7 @@ use std::cell::Cell; use std::collections::TryReserveError::*; use std::fmt::Debug; use std::iter::InPlaceIterable; -use std::mem::size_of; +use std::mem::{size_of, swap}; use std::ops::Bound::*; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::rc::Rc; @@ -1942,3 +1942,15 @@ fn test_vec_macro_repeat() { let n = 3; assert_eq!(vec![el; n], vec![Box::new(1), Box::new(1), Box::new(1)]); } + +#[test] +fn test_vec_swap() { + let mut a: Vec = vec![0, 1, 2, 3, 4, 5, 6]; + a.swap(2, 4); + assert_eq!(a[2], 4); + assert_eq!(a[4], 2); + let mut n = 42; + swap(&mut n, &mut a[0]); + assert_eq!(a[0], 42); + assert_eq!(n, 0); +} diff --git a/src/test/ui/swap-2.rs b/src/test/ui/swap-2.rs deleted file mode 100644 index c8f298ec0e5cb..0000000000000 --- a/src/test/ui/swap-2.rs +++ /dev/null @@ -1,14 +0,0 @@ -// run-pass - -use std::mem::swap; - -pub fn main() { - let mut a: Vec = vec![0, 1, 2, 3, 4, 5, 6]; - a.swap(2, 4); - assert_eq!(a[2], 4); - assert_eq!(a[4], 2); - let mut n = 42; - swap(&mut n, &mut a[0]); - assert_eq!(a[0], 42); - assert_eq!(n, 0); -} From 1bdee96c5e6de445f09df34447a42553294f21ed Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 3 Oct 2020 19:19:50 +0200 Subject: [PATCH 05/24] Move subslice pattern tests to alloc/tests/slice.rs --- library/alloc/tests/slice.rs | 120 +++++++++++++++++ .../array-slice-vec/subslice-patterns-pass.rs | 126 ------------------ 2 files changed, 120 insertions(+), 126 deletions(-) delete mode 100644 src/test/ui/array-slice-vec/subslice-patterns-pass.rs diff --git a/library/alloc/tests/slice.rs b/library/alloc/tests/slice.rs index 1f561bebd908b..a4f0fb415fb3c 100644 --- a/library/alloc/tests/slice.rs +++ b/library/alloc/tests/slice.rs @@ -1,5 +1,6 @@ use std::cell::Cell; use std::cmp::Ordering::{self, Equal, Greater, Less}; +use std::convert::identity; use std::mem; use std::panic; use std::rc::Rc; @@ -1778,3 +1779,122 @@ fn repeat_generic_slice() { assert_eq!([1, 2, 3, 4].repeat(1), vec![1, 2, 3, 4]); assert_eq!([1, 2, 3, 4].repeat(3), vec![1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]); } + +#[test] +#[allow(unreachable_patterns)] +fn subslice_patterns() { + // This test comprehensively checks the passing static and dynamic semantics + // of subslice patterns `..`, `x @ ..`, `ref x @ ..`, and `ref mut @ ..` + // in slice patterns `[$($pat), $(,)?]` . + + #[derive(PartialEq, Debug, Clone)] + struct N(u8); + + macro_rules! n { + ($($e:expr),* $(,)?) => { + [$(N($e)),*] + } + } + + macro_rules! c { + ($inp:expr, $typ:ty, $out:expr $(,)?) => { + assert_eq!($out, identity::<$typ>($inp)); + }; + } + + macro_rules! m { + ($e:expr, $p:pat => $b:expr) => { + match $e { + $p => $b, + _ => panic!(), + } + }; + } + + // == Slices == + + // Matching slices using `ref` patterns: + let mut v = vec![N(0), N(1), N(2), N(3), N(4)]; + let mut vc = (0..=4).collect::>(); + + let [..] = v[..]; // Always matches. + m!(v[..], [N(0), ref sub @ .., N(4)] => c!(sub, &[N], n![1, 2, 3])); + m!(v[..], [N(0), ref sub @ ..] => c!(sub, &[N], n![1, 2, 3, 4])); + m!(v[..], [ref sub @ .., N(4)] => c!(sub, &[N], n![0, 1, 2, 3])); + m!(v[..], [ref sub @ .., _, _, _, _, _] => c!(sub, &[N], &n![] as &[N])); + m!(v[..], [_, _, _, _, _, ref sub @ ..] => c!(sub, &[N], &n![] as &[N])); + m!(vc[..], [x, .., y] => c!((x, y), (u8, u8), (0, 4))); + + // Matching slices using `ref mut` patterns: + let [..] = v[..]; // Always matches. + m!(v[..], [N(0), ref mut sub @ .., N(4)] => c!(sub, &mut [N], n![1, 2, 3])); + m!(v[..], [N(0), ref mut sub @ ..] => c!(sub, &mut [N], n![1, 2, 3, 4])); + m!(v[..], [ref mut sub @ .., N(4)] => c!(sub, &mut [N], n![0, 1, 2, 3])); + m!(v[..], [ref mut sub @ .., _, _, _, _, _] => c!(sub, &mut [N], &mut n![] as &mut [N])); + m!(v[..], [_, _, _, _, _, ref mut sub @ ..] => c!(sub, &mut [N], &mut n![] as &mut [N])); + m!(vc[..], [x, .., y] => c!((x, y), (u8, u8), (0, 4))); + + // Matching slices using default binding modes (&): + let [..] = &v[..]; // Always matches. + m!(&v[..], [N(0), sub @ .., N(4)] => c!(sub, &[N], n![1, 2, 3])); + m!(&v[..], [N(0), sub @ ..] => c!(sub, &[N], n![1, 2, 3, 4])); + m!(&v[..], [sub @ .., N(4)] => c!(sub, &[N], n![0, 1, 2, 3])); + m!(&v[..], [sub @ .., _, _, _, _, _] => c!(sub, &[N], &n![] as &[N])); + m!(&v[..], [_, _, _, _, _, sub @ ..] => c!(sub, &[N], &n![] as &[N])); + m!(&vc[..], [x, .., y] => c!((x, y), (&u8, &u8), (&0, &4))); + + // Matching slices using default binding modes (&mut): + let [..] = &mut v[..]; // Always matches. + m!(&mut v[..], [N(0), sub @ .., N(4)] => c!(sub, &mut [N], n![1, 2, 3])); + m!(&mut v[..], [N(0), sub @ ..] => c!(sub, &mut [N], n![1, 2, 3, 4])); + m!(&mut v[..], [sub @ .., N(4)] => c!(sub, &mut [N], n![0, 1, 2, 3])); + m!(&mut v[..], [sub @ .., _, _, _, _, _] => c!(sub, &mut [N], &mut n![] as &mut [N])); + m!(&mut v[..], [_, _, _, _, _, sub @ ..] => c!(sub, &mut [N], &mut n![] as &mut [N])); + m!(&mut vc[..], [x, .., y] => c!((x, y), (&mut u8, &mut u8), (&mut 0, &mut 4))); + + // == Arrays == + let mut v = n![0, 1, 2, 3, 4]; + let vc = [0, 1, 2, 3, 4]; + + // Matching arrays by value: + m!(v.clone(), [N(0), sub @ .., N(4)] => c!(sub, [N; 3], n![1, 2, 3])); + m!(v.clone(), [N(0), sub @ ..] => c!(sub, [N; 4], n![1, 2, 3, 4])); + m!(v.clone(), [sub @ .., N(4)] => c!(sub, [N; 4], n![0, 1, 2, 3])); + m!(v.clone(), [sub @ .., _, _, _, _, _] => c!(sub, [N; 0], n![] as [N; 0])); + m!(v.clone(), [_, _, _, _, _, sub @ ..] => c!(sub, [N; 0], n![] as [N; 0])); + m!(v.clone(), [x, .., y] => c!((x, y), (N, N), (N(0), N(4)))); + m!(v.clone(), [..] => ()); + + // Matching arrays by ref patterns: + m!(v, [N(0), ref sub @ .., N(4)] => c!(sub, &[N; 3], &n![1, 2, 3])); + m!(v, [N(0), ref sub @ ..] => c!(sub, &[N; 4], &n![1, 2, 3, 4])); + m!(v, [ref sub @ .., N(4)] => c!(sub, &[N; 4], &n![0, 1, 2, 3])); + m!(v, [ref sub @ .., _, _, _, _, _] => c!(sub, &[N; 0], &n![] as &[N; 0])); + m!(v, [_, _, _, _, _, ref sub @ ..] => c!(sub, &[N; 0], &n![] as &[N; 0])); + m!(vc, [x, .., y] => c!((x, y), (u8, u8), (0, 4))); + + // Matching arrays by ref mut patterns: + m!(v, [N(0), ref mut sub @ .., N(4)] => c!(sub, &mut [N; 3], &mut n![1, 2, 3])); + m!(v, [N(0), ref mut sub @ ..] => c!(sub, &mut [N; 4], &mut n![1, 2, 3, 4])); + m!(v, [ref mut sub @ .., N(4)] => c!(sub, &mut [N; 4], &mut n![0, 1, 2, 3])); + m!(v, [ref mut sub @ .., _, _, _, _, _] => c!(sub, &mut [N; 0], &mut n![] as &mut [N; 0])); + m!(v, [_, _, _, _, _, ref mut sub @ ..] => c!(sub, &mut [N; 0], &mut n![] as &mut [N; 0])); + + // Matching arrays by default binding modes (&): + m!(&v, [N(0), sub @ .., N(4)] => c!(sub, &[N; 3], &n![1, 2, 3])); + m!(&v, [N(0), sub @ ..] => c!(sub, &[N; 4], &n![1, 2, 3, 4])); + m!(&v, [sub @ .., N(4)] => c!(sub, &[N; 4], &n![0, 1, 2, 3])); + m!(&v, [sub @ .., _, _, _, _, _] => c!(sub, &[N; 0], &n![] as &[N; 0])); + m!(&v, [_, _, _, _, _, sub @ ..] => c!(sub, &[N; 0], &n![] as &[N; 0])); + m!(&v, [..] => ()); + m!(&v, [x, .., y] => c!((x, y), (&N, &N), (&N(0), &N(4)))); + + // Matching arrays by default binding modes (&mut): + m!(&mut v, [N(0), sub @ .., N(4)] => c!(sub, &mut [N; 3], &mut n![1, 2, 3])); + m!(&mut v, [N(0), sub @ ..] => c!(sub, &mut [N; 4], &mut n![1, 2, 3, 4])); + m!(&mut v, [sub @ .., N(4)] => c!(sub, &mut [N; 4], &mut n![0, 1, 2, 3])); + m!(&mut v, [sub @ .., _, _, _, _, _] => c!(sub, &mut [N; 0], &mut n![] as &[N; 0])); + m!(&mut v, [_, _, _, _, _, sub @ ..] => c!(sub, &mut [N; 0], &mut n![] as &[N; 0])); + m!(&mut v, [..] => ()); + m!(&mut v, [x, .., y] => c!((x, y), (&mut N, &mut N), (&mut N(0), &mut N(4)))); +} diff --git a/src/test/ui/array-slice-vec/subslice-patterns-pass.rs b/src/test/ui/array-slice-vec/subslice-patterns-pass.rs deleted file mode 100644 index e05790911f52d..0000000000000 --- a/src/test/ui/array-slice-vec/subslice-patterns-pass.rs +++ /dev/null @@ -1,126 +0,0 @@ -// This test comprehensively checks the passing static and dynamic semantics -// of subslice patterns `..`, `x @ ..`, `ref x @ ..`, and `ref mut @ ..` -// in slice patterns `[$($pat), $(,)?]` . - -// run-pass - -#![allow(unreachable_patterns)] - -use std::convert::identity; - -#[derive(PartialEq, Debug, Clone)] -struct N(u8); - -macro_rules! n { - ($($e:expr),* $(,)?) => { - [$(N($e)),*] - } -} - -macro_rules! c { - ($inp:expr, $typ:ty, $out:expr $(,)?) => { - assert_eq!($out, identity::<$typ>($inp)); - } -} - -macro_rules! m { - ($e:expr, $p:pat => $b:expr) => { - match $e { - $p => $b, - _ => panic!(), - } - } -} - -fn main() { - slices(); - arrays(); -} - -fn slices() { - // Matching slices using `ref` patterns: - let mut v = vec![N(0), N(1), N(2), N(3), N(4)]; - let mut vc = (0..=4).collect::>(); - - let [..] = v[..]; // Always matches. - m!(v[..], [N(0), ref sub @ .., N(4)] => c!(sub, &[N], n![1, 2, 3])); - m!(v[..], [N(0), ref sub @ ..] => c!(sub, &[N], n![1, 2, 3, 4])); - m!(v[..], [ref sub @ .., N(4)] => c!(sub, &[N], n![0, 1, 2, 3])); - m!(v[..], [ref sub @ .., _, _, _, _, _] => c!(sub, &[N], &n![] as &[N])); - m!(v[..], [_, _, _, _, _, ref sub @ ..] => c!(sub, &[N], &n![] as &[N])); - m!(vc[..], [x, .., y] => c!((x, y), (u8, u8), (0, 4))); - - // Matching slices using `ref mut` patterns: - let [..] = v[..]; // Always matches. - m!(v[..], [N(0), ref mut sub @ .., N(4)] => c!(sub, &mut [N], n![1, 2, 3])); - m!(v[..], [N(0), ref mut sub @ ..] => c!(sub, &mut [N], n![1, 2, 3, 4])); - m!(v[..], [ref mut sub @ .., N(4)] => c!(sub, &mut [N], n![0, 1, 2, 3])); - m!(v[..], [ref mut sub @ .., _, _, _, _, _] => c!(sub, &mut [N], &mut n![] as &mut [N])); - m!(v[..], [_, _, _, _, _, ref mut sub @ ..] => c!(sub, &mut [N], &mut n![] as &mut [N])); - m!(vc[..], [x, .., y] => c!((x, y), (u8, u8), (0, 4))); - - // Matching slices using default binding modes (&): - let [..] = &v[..]; // Always matches. - m!(&v[..], [N(0), sub @ .., N(4)] => c!(sub, &[N], n![1, 2, 3])); - m!(&v[..], [N(0), sub @ ..] => c!(sub, &[N], n![1, 2, 3, 4])); - m!(&v[..], [sub @ .., N(4)] => c!(sub, &[N], n![0, 1, 2, 3])); - m!(&v[..], [sub @ .., _, _, _, _, _] => c!(sub, &[N], &n![] as &[N])); - m!(&v[..], [_, _, _, _, _, sub @ ..] => c!(sub, &[N], &n![] as &[N])); - m!(&vc[..], [x, .., y] => c!((x, y), (&u8, &u8), (&0, &4))); - - // Matching slices using default binding modes (&mut): - let [..] = &mut v[..]; // Always matches. - m!(&mut v[..], [N(0), sub @ .., N(4)] => c!(sub, &mut [N], n![1, 2, 3])); - m!(&mut v[..], [N(0), sub @ ..] => c!(sub, &mut [N], n![1, 2, 3, 4])); - m!(&mut v[..], [sub @ .., N(4)] => c!(sub, &mut [N], n![0, 1, 2, 3])); - m!(&mut v[..], [sub @ .., _, _, _, _, _] => c!(sub, &mut [N], &mut n![] as &mut [N])); - m!(&mut v[..], [_, _, _, _, _, sub @ ..] => c!(sub, &mut [N], &mut n![] as &mut [N])); - m!(&mut vc[..], [x, .., y] => c!((x, y), (&mut u8, &mut u8), (&mut 0, &mut 4))); -} - -fn arrays() { - let mut v = n![0, 1, 2, 3, 4]; - let vc = [0, 1, 2, 3, 4]; - - // Matching arrays by value: - m!(v.clone(), [N(0), sub @ .., N(4)] => c!(sub, [N; 3], n![1, 2, 3])); - m!(v.clone(), [N(0), sub @ ..] => c!(sub, [N; 4], n![1, 2, 3, 4])); - m!(v.clone(), [sub @ .., N(4)] => c!(sub, [N; 4], n![0, 1, 2, 3])); - m!(v.clone(), [sub @ .., _, _, _, _, _] => c!(sub, [N; 0], n![] as [N; 0])); - m!(v.clone(), [_, _, _, _, _, sub @ ..] => c!(sub, [N; 0], n![] as [N; 0])); - m!(v.clone(), [x, .., y] => c!((x, y), (N, N), (N(0), N(4)))); - m!(v.clone(), [..] => ()); - - // Matching arrays by ref patterns: - m!(v, [N(0), ref sub @ .., N(4)] => c!(sub, &[N; 3], &n![1, 2, 3])); - m!(v, [N(0), ref sub @ ..] => c!(sub, &[N; 4], &n![1, 2, 3, 4])); - m!(v, [ref sub @ .., N(4)] => c!(sub, &[N; 4], &n![0, 1, 2, 3])); - m!(v, [ref sub @ .., _, _, _, _, _] => c!(sub, &[N; 0], &n![] as &[N; 0])); - m!(v, [_, _, _, _, _, ref sub @ ..] => c!(sub, &[N; 0], &n![] as &[N; 0])); - m!(vc, [x, .., y] => c!((x, y), (u8, u8), (0, 4))); - - // Matching arrays by ref mut patterns: - m!(v, [N(0), ref mut sub @ .., N(4)] => c!(sub, &mut [N; 3], &mut n![1, 2, 3])); - m!(v, [N(0), ref mut sub @ ..] => c!(sub, &mut [N; 4], &mut n![1, 2, 3, 4])); - m!(v, [ref mut sub @ .., N(4)] => c!(sub, &mut [N; 4], &mut n![0, 1, 2, 3])); - m!(v, [ref mut sub @ .., _, _, _, _, _] => c!(sub, &mut [N; 0], &mut n![] as &mut [N; 0])); - m!(v, [_, _, _, _, _, ref mut sub @ ..] => c!(sub, &mut [N; 0], &mut n![] as &mut [N; 0])); - - // Matching arrays by default binding modes (&): - m!(&v, [N(0), sub @ .., N(4)] => c!(sub, &[N; 3], &n![1, 2, 3])); - m!(&v, [N(0), sub @ ..] => c!(sub, &[N; 4], &n![1, 2, 3, 4])); - m!(&v, [sub @ .., N(4)] => c!(sub, &[N; 4], &n![0, 1, 2, 3])); - m!(&v, [sub @ .., _, _, _, _, _] => c!(sub, &[N; 0], &n![] as &[N; 0])); - m!(&v, [_, _, _, _, _, sub @ ..] => c!(sub, &[N; 0], &n![] as &[N; 0])); - m!(&v, [..] => ()); - m!(&v, [x, .., y] => c!((x, y), (&N, &N), (&N(0), &N(4)))); - - // Matching arrays by default binding modes (&mut): - m!(&mut v, [N(0), sub @ .., N(4)] => c!(sub, &mut [N; 3], &mut n![1, 2, 3])); - m!(&mut v, [N(0), sub @ ..] => c!(sub, &mut [N; 4], &mut n![1, 2, 3, 4])); - m!(&mut v, [sub @ .., N(4)] => c!(sub, &mut [N; 4], &mut n![0, 1, 2, 3])); - m!(&mut v, [sub @ .., _, _, _, _, _] => c!(sub, &mut [N; 0], &mut n![] as &[N; 0])); - m!(&mut v, [_, _, _, _, _, sub @ ..] => c!(sub, &mut [N; 0], &mut n![] as &[N; 0])); - m!(&mut v, [..] => ()); - m!(&mut v, [x, .., y] => c!((x, y), (&mut N, &mut N), (&mut N(0), &mut N(4)))); -} From 20e032e65007ff1376e8480c1fbdb0a5068028fa Mon Sep 17 00:00:00 2001 From: Gus Wynn Date: Tue, 15 Sep 2020 13:14:35 -0700 Subject: [PATCH 06/24] Make it more clear when complaining about async fn's return types --- .../nice_region_error/different_lifetimes.rs | 112 ++++++++++++------ .../error_reporting/nice_region_error/util.rs | 54 +++++++++ .../async-await/issues/issue-63388-1.stderr | 6 +- .../ret-impl-trait-one.stderr | 5 +- src/test/ui/issues/issue-76547.rs | 38 ++++++ src/test/ui/issues/issue-76547.stderr | 25 ++++ ...f_types_pin_lifetime_mismatch-async.stderr | 21 ++-- .../ui/self/elision/lt-ref-self-async.stderr | 42 ++++--- .../ui/self/elision/ref-mut-self-async.stderr | 42 ++++--- .../self/elision/ref-mut-struct-async.stderr | 35 +++--- .../ui/self/elision/ref-self-async.stderr | 49 ++++---- .../ui/self/elision/ref-struct-async.stderr | 35 +++--- 12 files changed, 331 insertions(+), 133 deletions(-) create mode 100644 src/test/ui/issues/issue-76547.rs create mode 100644 src/test/ui/issues/issue-76547.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/different_lifetimes.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/different_lifetimes.rs index 7ab18e54f7ea2..59786059fae67 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/different_lifetimes.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/different_lifetimes.rs @@ -102,43 +102,89 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { None => String::new(), }; - let (span_1, span_2, main_label, span_label) = match (sup_is_ret_type, sub_is_ret_type) { - (None, None) => { - let (main_label_1, span_label_1) = if ty_sup.hir_id == ty_sub.hir_id { + let (span_1, span_2, main_label, span_label, future_return_type) = + match (sup_is_ret_type, sub_is_ret_type) { + (None, None) => { + let (main_label_1, span_label_1) = if ty_sup.hir_id == ty_sub.hir_id { + ( + "this type is declared with multiple lifetimes...".to_owned(), + "...but data with one lifetime flows into the other here".to_owned(), + ) + } else { + ( + "these two types are declared with different lifetimes...".to_owned(), + format!("...but data{} flows{} here", span_label_var1, span_label_var2), + ) + }; + (ty_sup.span, ty_sub.span, main_label_1, span_label_1, None) + } + + (Some(ret_span), _) => { + let sup_future = self.future_return_type(scope_def_id_sup); + let (return_type, action) = if let Some(_) = sup_future { + ("returned future", "held across an await point") + } else { + ("return type", "returned") + }; + ( - "this type is declared with multiple lifetimes...".to_owned(), - "...but data with one lifetime flows into the other here".to_owned(), + ty_sub.span, + ret_span, + format!( + "this parameter and the {} are declared with different lifetimes...", + return_type + ), + format!("...but data{} is {} here", span_label_var1, action), + sup_future, ) - } else { + } + (_, Some(ret_span)) => { + let sub_future = self.future_return_type(scope_def_id_sub); + let (return_type, action) = if let Some(_) = sub_future { + ("returned future", "held across an await point") + } else { + ("return type", "returned") + }; + ( - "these two types are declared with different lifetimes...".to_owned(), - format!("...but data{} flows{} here", span_label_var1, span_label_var2), + ty_sup.span, + ret_span, + format!( + "this parameter and the {} are declared with different lifetimes...", + return_type + ), + format!("...but data{} is {} here", span_label_var1, action), + sub_future, ) - }; - (ty_sup.span, ty_sub.span, main_label_1, span_label_1) - } - - (Some(ret_span), _) => ( - ty_sub.span, - ret_span, - "this parameter and the return type are declared with different lifetimes..." - .to_owned(), - format!("...but data{} is returned here", span_label_var1), - ), - (_, Some(ret_span)) => ( - ty_sup.span, - ret_span, - "this parameter and the return type are declared with different lifetimes..." - .to_owned(), - format!("...but data{} is returned here", span_label_var1), - ), - }; - - struct_span_err!(self.tcx().sess, span, E0623, "lifetime mismatch") - .span_label(span_1, main_label) - .span_label(span_2, String::new()) - .span_label(span, span_label) - .emit(); + } + }; + + let mut e = struct_span_err!(self.tcx().sess, span, E0623, "lifetime mismatch"); + + e.span_label(span_1, main_label); + e.span_label(span_2, String::new()); + e.span_label(span, span_label); + + if let Some(t) = future_return_type { + let snip = self + .tcx() + .sess + .source_map() + .span_to_snippet(t.span) + .ok() + .and_then(|s| match (&t.kind, s.as_str()) { + (rustc_hir::TyKind::Tup(&[]), "") => Some("()".to_string()), + (_, "") => None, + _ => Some(s), + }) + .unwrap_or("{unnamed_type}".to_string()); + + e.span_label( + t.span, + &format!("this `async fn` implicitly returns an `impl Future`", snip), + ); + } + e.emit(); Some(ErrorReported) } } diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs index c055fed43f6d5..ca93b2777ab2a 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs @@ -85,6 +85,60 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { }) } + pub(super) fn future_return_type( + &self, + local_def_id: LocalDefId, + ) -> Option<&rustc_hir::Ty<'_>> { + if let Some(hir::IsAsync::Async) = self.asyncness(local_def_id) { + if let rustc_middle::ty::Opaque(def_id, _) = + self.tcx().type_of(local_def_id).fn_sig(self.tcx()).output().skip_binder().kind() + { + match self.tcx().hir().get_if_local(*def_id) { + Some(hir::Node::Item(hir::Item { + kind: + hir::ItemKind::OpaqueTy(hir::OpaqueTy { + bounds, + origin: hir::OpaqueTyOrigin::AsyncFn, + .. + }), + .. + })) => { + for b in bounds.iter() { + if let hir::GenericBound::LangItemTrait( + hir::LangItem::Future, + _span, + _hir_id, + generic_args, + ) = b + { + for type_binding in generic_args.bindings.iter() { + if type_binding.ident.name == rustc_span::sym::Output { + if let hir::TypeBindingKind::Equality { ty } = + type_binding.kind + { + return Some(ty); + } + } + } + } + } + } + _ => {} + } + } + } + None + } + + pub(super) fn asyncness(&self, local_def_id: LocalDefId) -> Option { + // similar to the asyncness fn in rustc_ty::ty + let hir_id = self.tcx().hir().local_def_id_to_hir_id(local_def_id); + let node = self.tcx().hir().get(hir_id); + let fn_like = rustc_middle::hir::map::blocks::FnLikeNode::from_node(node)?; + + Some(fn_like.asyncness()) + } + // Here, we check for the case where the anonymous region // is in the return type. // FIXME(#42703) - Need to handle certain cases here. diff --git a/src/test/ui/async-await/issues/issue-63388-1.stderr b/src/test/ui/async-await/issues/issue-63388-1.stderr index 8813183312ddf..ac29cca9d3f60 100644 --- a/src/test/ui/async-await/issues/issue-63388-1.stderr +++ b/src/test/ui/async-await/issues/issue-63388-1.stderr @@ -2,12 +2,14 @@ error[E0623]: lifetime mismatch --> $DIR/issue-63388-1.rs:14:9 | LL | &'a self, foo: &dyn Foo - | -------- this parameter and the return type are declared with different lifetimes... + | -------- this parameter and the returned future are declared with different lifetimes... LL | ) -> &dyn Foo | -------- + | | + | this `async fn` implicitly returns an `impl Future` LL | { LL | foo - | ^^^ ...but data from `foo` is returned here + | ^^^ ...but data from `foo` is held across an await point here error: aborting due to previous error diff --git a/src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-one.stderr b/src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-one.stderr index 7b8f290d6c2f8..5041b39a9e9d1 100644 --- a/src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-one.stderr +++ b/src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-one.stderr @@ -4,8 +4,9 @@ error[E0623]: lifetime mismatch LL | async fn async_ret_impl_trait1<'a, 'b>(a: &'a u8, b: &'b u8) -> impl Trait<'a> { | ------ ^^^^^^^^^^^^^^ | | | - | | ...but data from `b` is returned here - | this parameter and the return type are declared with different lifetimes... + | | ...but data from `b` is held across an await point here + | | this `async fn` implicitly returns an `impl Future>` + | this parameter and the returned future are declared with different lifetimes... error: aborting due to previous error diff --git a/src/test/ui/issues/issue-76547.rs b/src/test/ui/issues/issue-76547.rs new file mode 100644 index 0000000000000..feec086764a34 --- /dev/null +++ b/src/test/ui/issues/issue-76547.rs @@ -0,0 +1,38 @@ +// Test for for diagnostic improvement issue #76547 +// edition:2018 + +use std::{ + future::Future, + task::{Context, Poll} +}; +use std::pin::Pin; + +pub struct ListFut<'a>(&'a mut [&'a mut [u8]]); +impl<'a> Future for ListFut<'a> { + type Output = (); + + fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll { + unimplemented!() + } +} + +async fn fut(bufs: &mut [&mut [u8]]) { + ListFut(bufs).await + //~^ ERROR lifetime mismatch +} + +pub struct ListFut2<'a>(&'a mut [&'a mut [u8]]); +impl<'a> Future for ListFut2<'a> { + type Output = i32; + + fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll { + unimplemented!() + } +} + +async fn fut2(bufs: &mut [&mut [u8]]) -> i32 { + ListFut2(bufs).await + //~^ ERROR lifetime mismatch +} + +fn main() {} diff --git a/src/test/ui/issues/issue-76547.stderr b/src/test/ui/issues/issue-76547.stderr new file mode 100644 index 0000000000000..9bfb0f28028cb --- /dev/null +++ b/src/test/ui/issues/issue-76547.stderr @@ -0,0 +1,25 @@ +error[E0623]: lifetime mismatch + --> $DIR/issue-76547.rs:20:13 + | +LL | async fn fut(bufs: &mut [&mut [u8]]) { + | --------- - + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... +LL | ListFut(bufs).await + | ^^^^ ...but data from `bufs` is held across an await point here + +error[E0623]: lifetime mismatch + --> $DIR/issue-76547.rs:34:14 + | +LL | async fn fut2(bufs: &mut [&mut [u8]]) -> i32 { + | --------- --- + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... +LL | ListFut2(bufs).await + | ^^^^ ...but data from `bufs` is held across an await point here + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0623`. diff --git a/src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch-async.stderr b/src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch-async.stderr index 37297032632e1..e6846fb40494f 100644 --- a/src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch-async.stderr +++ b/src/test/ui/self/arbitrary_self_types_pin_lifetime_mismatch-async.stderr @@ -2,25 +2,28 @@ error[E0623]: lifetime mismatch --> $DIR/arbitrary_self_types_pin_lifetime_mismatch-async.rs:8:52 | LL | async fn a(self: Pin<&Foo>, f: &Foo) -> &Foo { f } - | ---- ---- ^ ...but data from `f` is returned here - | | - | this parameter and the return type are declared with different lifetimes... + | ---- ---- ^ ...but data from `f` is held across an await point here + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... error[E0623]: lifetime mismatch --> $DIR/arbitrary_self_types_pin_lifetime_mismatch-async.rs:11:82 | LL | async fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self, f) } - | ----- ----------------- ^ ...but data from `f` is returned here - | | - | this parameter and the return type are declared with different lifetimes... + | ----- ----------------- ^ ...but data from `f` is held across an await point here + | | | + | | this `async fn` implicitly returns an `impl Future, &Foo)>` + | this parameter and the returned future are declared with different lifetimes... error[E0623]: lifetime mismatch --> $DIR/arbitrary_self_types_pin_lifetime_mismatch-async.rs:17:64 | LL | async fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg } - | ----- --- ^^^ ...but data from `arg` is returned here - | | - | this parameter and the return type are declared with different lifetimes... + | ----- --- ^^^ ...but data from `arg` is held across an await point here + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... error: aborting due to 3 previous errors diff --git a/src/test/ui/self/elision/lt-ref-self-async.stderr b/src/test/ui/self/elision/lt-ref-self-async.stderr index badd973c37f08..3221d27085096 100644 --- a/src/test/ui/self/elision/lt-ref-self-async.stderr +++ b/src/test/ui/self/elision/lt-ref-self-async.stderr @@ -3,60 +3,66 @@ error[E0623]: lifetime mismatch | LL | async fn ref_self(&self, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/lt-ref-self-async.rs:19:9 | LL | async fn ref_Self(self: &Self, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/lt-ref-self-async.rs:23:9 | LL | async fn box_ref_Self(self: Box<&Self>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/lt-ref-self-async.rs:27:9 | LL | async fn pin_ref_Self(self: Pin<&Self>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/lt-ref-self-async.rs:31:9 | LL | async fn box_box_ref_Self(self: Box>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/lt-ref-self-async.rs:35:9 | LL | async fn box_pin_Self(self: Box>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error: aborting due to 6 previous errors diff --git a/src/test/ui/self/elision/ref-mut-self-async.stderr b/src/test/ui/self/elision/ref-mut-self-async.stderr index 73d942a83f89a..b6ca986923d2e 100644 --- a/src/test/ui/self/elision/ref-mut-self-async.stderr +++ b/src/test/ui/self/elision/ref-mut-self-async.stderr @@ -3,60 +3,66 @@ error[E0623]: lifetime mismatch | LL | async fn ref_self(&mut self, f: &u32) -> &u32 { | --------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-self-async.rs:19:9 | LL | async fn ref_Self(self: &mut Self, f: &u32) -> &u32 { | --------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-self-async.rs:23:9 | LL | async fn box_ref_Self(self: Box<&mut Self>, f: &u32) -> &u32 { | --------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-self-async.rs:27:9 | LL | async fn pin_ref_Self(self: Pin<&mut Self>, f: &u32) -> &u32 { | --------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-self-async.rs:31:9 | LL | async fn box_box_ref_Self(self: Box>, f: &u32) -> &u32 { | --------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-self-async.rs:35:9 | LL | async fn box_pin_ref_Self(self: Box>, f: &u32) -> &u32 { | --------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error: aborting due to 6 previous errors diff --git a/src/test/ui/self/elision/ref-mut-struct-async.stderr b/src/test/ui/self/elision/ref-mut-struct-async.stderr index 7d613c574486c..eda15d76390b6 100644 --- a/src/test/ui/self/elision/ref-mut-struct-async.stderr +++ b/src/test/ui/self/elision/ref-mut-struct-async.stderr @@ -3,50 +3,55 @@ error[E0623]: lifetime mismatch | LL | async fn ref_Struct(self: &mut Struct, f: &u32) -> &u32 { | ----------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-struct-async.rs:17:9 | LL | async fn box_ref_Struct(self: Box<&mut Struct>, f: &u32) -> &u32 { | ----------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-struct-async.rs:21:9 | LL | async fn pin_ref_Struct(self: Pin<&mut Struct>, f: &u32) -> &u32 { | ----------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-struct-async.rs:25:9 | LL | async fn box_box_ref_Struct(self: Box>, f: &u32) -> &u32 { | ----------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-mut-struct-async.rs:29:9 | LL | async fn box_pin_ref_Struct(self: Box>, f: &u32) -> &u32 { | ----------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error: aborting due to 5 previous errors diff --git a/src/test/ui/self/elision/ref-self-async.stderr b/src/test/ui/self/elision/ref-self-async.stderr index bda958241b67b..b42caa88c6fef 100644 --- a/src/test/ui/self/elision/ref-self-async.stderr +++ b/src/test/ui/self/elision/ref-self-async.stderr @@ -3,70 +3,77 @@ error[E0623]: lifetime mismatch | LL | async fn ref_self(&self, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-self-async.rs:29:9 | LL | async fn ref_Self(self: &Self, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-self-async.rs:33:9 | LL | async fn box_ref_Self(self: Box<&Self>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-self-async.rs:37:9 | LL | async fn pin_ref_Self(self: Pin<&Self>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-self-async.rs:41:9 | LL | async fn box_box_ref_Self(self: Box>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-self-async.rs:45:9 | LL | async fn box_pin_ref_Self(self: Box>, f: &u32) -> &u32 { | ----- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-self-async.rs:49:9 | LL | async fn wrap_ref_Self_Self(self: Wrap<&Self, Self>, f: &u8) -> &u8 { | ----- --- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error: aborting due to 7 previous errors diff --git a/src/test/ui/self/elision/ref-struct-async.stderr b/src/test/ui/self/elision/ref-struct-async.stderr index fc85450c4a7cd..599becd308062 100644 --- a/src/test/ui/self/elision/ref-struct-async.stderr +++ b/src/test/ui/self/elision/ref-struct-async.stderr @@ -3,50 +3,55 @@ error[E0623]: lifetime mismatch | LL | async fn ref_Struct(self: &Struct, f: &u32) -> &u32 { | ------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-struct-async.rs:17:9 | LL | async fn box_ref_Struct(self: Box<&Struct>, f: &u32) -> &u32 { | ------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-struct-async.rs:21:9 | LL | async fn pin_ref_Struct(self: Pin<&Struct>, f: &u32) -> &u32 { | ------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-struct-async.rs:25:9 | LL | async fn box_box_ref_Struct(self: Box>, f: &u32) -> &u32 { | ------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error[E0623]: lifetime mismatch --> $DIR/ref-struct-async.rs:29:9 | LL | async fn box_pin_Struct(self: Box>, f: &u32) -> &u32 { | ------- ---- - | | - | this parameter and the return type are declared with different lifetimes... + | | | + | | this `async fn` implicitly returns an `impl Future` + | this parameter and the returned future are declared with different lifetimes... LL | f - | ^ ...but data from `f` is returned here + | ^ ...but data from `f` is held across an await point here error: aborting due to 5 previous errors From 251f6da276c3bee6a0737b8b42d5d13295c3a4d8 Mon Sep 17 00:00:00 2001 From: Florian Warzecha Date: Sun, 25 Oct 2020 20:45:05 +0100 Subject: [PATCH 07/24] (rustdoc) fix test for trait impl display --- src/test/rustdoc/impl-everywhere.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/rustdoc/impl-everywhere.rs b/src/test/rustdoc/impl-everywhere.rs index 9d86dd3c29e98..742813262589b 100644 --- a/src/test/rustdoc/impl-everywhere.rs +++ b/src/test/rustdoc/impl-everywhere.rs @@ -8,23 +8,23 @@ pub struct Bar; impl Foo for Bar {} impl Foo2 for Bar {} -// @!has foo/fn.foo.html '//section[@id="main"]//pre' "x: &\'x impl Foo" -// @!has foo/fn.foo.html '//section[@id="main"]//pre' "-> &\'x impl Foo {" +// @has foo/fn.foo.html '//section[@id="main"]//pre' "x: &'x impl Foo" +// @has foo/fn.foo.html '//section[@id="main"]//pre' "-> &'x impl Foo" pub fn foo<'x>(x: &'x impl Foo) -> &'x impl Foo { x } -// @!has foo/fn.foo2.html '//section[@id="main"]//pre' "x: &\'x impl Foo" -// @!has foo/fn.foo2.html '//section[@id="main"]//pre' '-> impl Foo2 {' +// @has foo/fn.foo2.html '//section[@id="main"]//pre' "x: &'x impl Foo" +// @has foo/fn.foo2.html '//section[@id="main"]//pre' '-> impl Foo2' pub fn foo2<'x>(_x: &'x impl Foo) -> impl Foo2 { Bar } -// @!has foo/fn.foo_foo.html '//section[@id="main"]//pre' '-> impl Foo + Foo2 {' +// @has foo/fn.foo_foo.html '//section[@id="main"]//pre' '-> impl Foo + Foo2' pub fn foo_foo() -> impl Foo + Foo2 { Bar } -// @!has foo/fn.foo2.html '//section[@id="main"]//pre' "x: &'x (impl Foo + Foo2)" +// @has foo/fn.foo_foo_foo.html '//section[@id="main"]//pre' "x: &'x impl Foo + Foo2" pub fn foo_foo_foo<'x>(_x: &'x (impl Foo + Foo2)) { } From e4d109613eb0c61ab528c9ff632c5807a259b505 Mon Sep 17 00:00:00 2001 From: est31 Date: Wed, 28 Oct 2020 01:55:17 +0100 Subject: [PATCH 08/24] =?UTF-8?q?Change=20as=5Fstr=20=E2=86=92=20to=5Fstri?= =?UTF-8?q?ng=20in=20proc=5Fmacro::Ident::span()=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is no `as_str` function on Ident any more. Also change it to an intra doc link while we're at it. --- library/proc_macro/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 139b3591206e7..ad8f6e86766e0 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -881,7 +881,7 @@ impl Ident { } /// Returns the span of this `Ident`, encompassing the entire string returned - /// by `as_str`. + /// by [`to_string`](Self::to_string). #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn span(&self) -> Span { Span(self.0.span()) From 300362e0e4e30439bd2f55f3dc94596ab197607e Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 30 Oct 2020 13:03:01 -0700 Subject: [PATCH 09/24] Close the theme picker when Escape is pressed --- src/librustdoc/html/static/main.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 28bd1ba5247d6..69873524bd729 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -4,6 +4,7 @@ // Local js definitions: /* global addClass, getCurrentValue, hasClass */ /* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */ +/* global hideThemeButtonState */ if (!String.prototype.startsWith) { String.prototype.startsWith = function(searchString, position) { @@ -137,10 +138,6 @@ function defocusSearchBar() { sidebar.appendChild(div); } } - var themePickers = document.getElementsByClassName("theme-picker"); - if (themePickers && themePickers.length > 0) { - themePickers[0].style.display = "none"; - } } function hideSidebar() { @@ -155,10 +152,6 @@ function defocusSearchBar() { filler.remove(); } document.getElementsByTagName("body")[0].style.marginTop = ""; - var themePickers = document.getElementsByClassName("theme-picker"); - if (themePickers && themePickers.length > 0) { - themePickers[0].style.display = null; - } } function showSearchResults(search) { @@ -376,6 +369,7 @@ function defocusSearchBar() { document.title = titleBeforeSearch; } defocusSearchBar(); + hideThemeButtonState(); } function handleShortcut(ev) { From ac3a434ed947058635fbe1f09b1da8bd6f1ea242 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 30 Oct 2020 13:34:30 -0700 Subject: [PATCH 10/24] Allow the theme picker to work with arrow keys This is mostly motivated by docs.rs. It's really weird when arrow keys work in the top dropdown menu, but don't work in other dropdown menus on the same page. --- src/librustdoc/html/render/mod.rs | 48 +++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 0621eafd91347..8d07f479b6eae 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -798,17 +798,61 @@ function handleThemeButtonsBlur(e) {{ var active = document.activeElement; var related = e.relatedTarget; - if (active.id !== "themePicker" && + if (active.id !== "theme-picker" && (!active.parentNode || active.parentNode.id !== "theme-choices") && (!related || - (related.id !== "themePicker" && + (related.id !== "theme-picker" && (!related.parentNode || related.parentNode.id !== "theme-choices")))) {{ hideThemeButtonState(); }} }} +function handleThemeKeyPress(e) {{ + if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {{ return; }} + if (!themePicker.parentNode.contains(e.target)) {{ return; }} + var active = document.activeElement; + switch (e.key) {{ + case "ArrowUp": + e.preventDefault(); + if (active.previousElementSibling && e.target.id !== "theme-picker") {{ + active.previousElementSibling.focus(); + }} else {{ + showThemeButtonState(); + themes.lastElementChild.focus(); + }} + break; + case "ArrowDown": + e.preventDefault(); + if (active.nextElementSibling && e.target.id !== "theme-picker") {{ + active.nextElementSibling.focus(); + }} else {{ + showThemeButtonState(); + themes.firstElementChild.focus(); + }} + break; + case "Enter": + case "Return": + case "Space": + if (e.target.id === "theme-picker" && themes.style.display === "none") {{ + e.preventDefault(); + showThemeButtonState(); + themes.firstElementChild.focus(); + }} + break; + case "Home": + e.preventDefault(); + themes.firstElementChild.focus(); + break; + case "End": + e.preventDefault(); + themes.lastElementChild.focus(); + break; + }} +}}; + themePicker.onclick = switchThemeButtonState; themePicker.onblur = handleThemeButtonsBlur; +document.addEventListener("keydown", handleThemeKeyPress); {}.forEach(function(item) {{ var but = document.createElement("button"); but.textContent = item; From 6918a17aa4ed5d06c1fce292763ff26605c52b97 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 30 Oct 2020 13:35:41 -0700 Subject: [PATCH 11/24] Add aria roles for theme picker menu --- src/librustdoc/html/layout.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index b089bcb0862a5..db73af7ec1689 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -79,12 +79,12 @@ pub fn render( {sidebar}\ \
\ - \ -
\ +
\
\ \