summaryrefslogtreecommitdiffstats
path: root/src/test/ui/lazy-type-alias-impl-trait
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/lazy-type-alias-impl-trait
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/lazy-type-alias-impl-trait')
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/branches.rs25
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/branches.stderr16
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/branches2.rs28
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/branches3.rs36
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/branches3.stderr47
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/freeze_cycle.rs46
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/infer_cross_function.rs27
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/lifetime_inference.rs7
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/nested.rs23
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/opaque_vs_opaque.rs10
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/recursion.rs23
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/recursion2.rs23
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/recursion3.rs21
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/recursion3.stderr19
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/recursion4.rs23
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/recursion4.stderr29
-rw-r--r--src/test/ui/lazy-type-alias-impl-trait/unsized_sized_opaque.rs16
17 files changed, 419 insertions, 0 deletions
diff --git a/src/test/ui/lazy-type-alias-impl-trait/branches.rs b/src/test/ui/lazy-type-alias-impl-trait/branches.rs
new file mode 100644
index 000000000..95239e2e3
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/branches.rs
@@ -0,0 +1,25 @@
+#![feature(type_alias_impl_trait)]
+
+type Foo = impl std::fmt::Debug;
+
+fn foo(b: bool) -> Foo {
+ if b {
+ vec![42_i32]
+ } else {
+ std::iter::empty().collect()
+ }
+}
+
+type Bar = impl std::fmt::Debug;
+
+fn bar(b: bool) -> Bar {
+ let x: Bar = if b {
+ vec![42_i32]
+ } else {
+ std::iter::empty().collect()
+ //~^ ERROR a value of type `Bar` cannot be built from an iterator over elements of type `_`
+ };
+ x
+}
+
+fn main() {}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/branches.stderr b/src/test/ui/lazy-type-alias-impl-trait/branches.stderr
new file mode 100644
index 000000000..6b87da0c0
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/branches.stderr
@@ -0,0 +1,16 @@
+error[E0277]: a value of type `Bar` cannot be built from an iterator over elements of type `_`
+ --> $DIR/branches.rs:19:28
+ |
+LL | std::iter::empty().collect()
+ | ^^^^^^^ value of type `Bar` cannot be built from `std::iter::Iterator<Item=_>`
+ |
+ = help: the trait `FromIterator<_>` is not implemented for `Bar`
+note: required by a bound in `collect`
+ --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
+ |
+LL | fn collect<B: FromIterator<Self::Item>>(self) -> B
+ | ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `collect`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/lazy-type-alias-impl-trait/branches2.rs b/src/test/ui/lazy-type-alias-impl-trait/branches2.rs
new file mode 100644
index 000000000..04218f564
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/branches2.rs
@@ -0,0 +1,28 @@
+#![feature(type_alias_impl_trait)]
+
+// check-pass
+
+type Foo = impl std::iter::FromIterator<i32> + PartialEq<Vec<i32>> + std::fmt::Debug;
+
+fn foo(b: bool) -> Foo {
+ if b {
+ vec![42_i32]
+ } else {
+ std::iter::empty().collect()
+ }
+}
+
+fn bar(b: bool) -> impl PartialEq<Vec<i32>> + std::fmt::Debug {
+ if b {
+ vec![42_i32]
+ } else {
+ std::iter::empty().collect()
+ }
+}
+
+fn main() {
+ assert_eq!(foo(true), vec![42]);
+ assert_eq!(foo(false), vec![]);
+ assert_eq!(bar(true), vec![42]);
+ assert_eq!(bar(false), vec![]);
+}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/branches3.rs b/src/test/ui/lazy-type-alias-impl-trait/branches3.rs
new file mode 100644
index 000000000..30c0af8a5
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/branches3.rs
@@ -0,0 +1,36 @@
+#![feature(type_alias_impl_trait)]
+
+type Foo = impl for<'a> FnOnce(&'a str) -> usize;
+type Bar = impl FnOnce(&'static str) -> usize;
+
+fn foo() -> Foo {
+ if true {
+ |s| s.len() //~ ERROR type annotations needed
+ } else {
+ panic!()
+ }
+}
+fn bar() -> Bar {
+ if true {
+ |s| s.len() //~ ERROR type annotations needed
+ } else {
+ panic!()
+ }
+}
+
+fn foo2() -> impl for<'a> FnOnce(&'a str) -> usize {
+ if true {
+ |s| s.len() //~ ERROR type annotations needed
+ } else {
+ panic!()
+ }
+}
+fn bar2() -> impl FnOnce(&'static str) -> usize {
+ if true {
+ |s| s.len() //~ ERROR type annotations needed
+ } else {
+ panic!()
+ }
+}
+
+fn main() {}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/branches3.stderr b/src/test/ui/lazy-type-alias-impl-trait/branches3.stderr
new file mode 100644
index 000000000..420104e52
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/branches3.stderr
@@ -0,0 +1,47 @@
+error[E0282]: type annotations needed
+ --> $DIR/branches3.rs:8:10
+ |
+LL | |s| s.len()
+ | ^ - type must be known at this point
+ |
+help: consider giving this closure parameter an explicit type
+ |
+LL | |s: _| s.len()
+ | +++
+
+error[E0282]: type annotations needed
+ --> $DIR/branches3.rs:15:10
+ |
+LL | |s| s.len()
+ | ^ - type must be known at this point
+ |
+help: consider giving this closure parameter an explicit type
+ |
+LL | |s: _| s.len()
+ | +++
+
+error[E0282]: type annotations needed
+ --> $DIR/branches3.rs:23:10
+ |
+LL | |s| s.len()
+ | ^ - type must be known at this point
+ |
+help: consider giving this closure parameter an explicit type
+ |
+LL | |s: _| s.len()
+ | +++
+
+error[E0282]: type annotations needed
+ --> $DIR/branches3.rs:30:10
+ |
+LL | |s| s.len()
+ | ^ - type must be known at this point
+ |
+help: consider giving this closure parameter an explicit type
+ |
+LL | |s: _| s.len()
+ | +++
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0282`.
diff --git a/src/test/ui/lazy-type-alias-impl-trait/freeze_cycle.rs b/src/test/ui/lazy-type-alias-impl-trait/freeze_cycle.rs
new file mode 100644
index 000000000..10f6bd740
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/freeze_cycle.rs
@@ -0,0 +1,46 @@
+// check-pass
+
+#![feature(gen_future, generator_trait, negative_impls)]
+
+use std::ops::{Generator, GeneratorState};
+use std::task::{Poll, Context};
+use std::future::{Future};
+use std::ptr::NonNull;
+use std::pin::Pin;
+
+fn main() {}
+
+#[derive(Debug, Copy, Clone)]
+pub struct ResumeTy(NonNull<Context<'static>>);
+
+unsafe impl Send for ResumeTy {}
+
+unsafe impl Sync for ResumeTy {}
+
+pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
+where
+ T: Generator<ResumeTy, Yield = ()>,
+{
+ struct GenFuture<T: Generator<ResumeTy, Yield = ()>>(T);
+
+ // We rely on the fact that async/await futures are immovable in order to create
+ // self-referential borrows in the underlying generator.
+ impl<T: Generator<ResumeTy, Yield = ()>> !Unpin for GenFuture<T> {}
+
+ impl<T: Generator<ResumeTy, Yield = ()>> Future for GenFuture<T> {
+ type Output = T::Return;
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ // SAFETY: Safe because we're !Unpin + !Drop, and this is just a field projection.
+ let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) };
+
+ // Resume the generator, turning the `&mut Context` into a `NonNull` raw pointer. The
+ // `.await` lowering will safely cast that back to a `&mut Context`.
+ match gen.resume(ResumeTy(NonNull::from(cx).cast::<Context<'static>>())) {
+ GeneratorState::Yielded(()) => Poll::Pending,
+ GeneratorState::Complete(x) => Poll::Ready(x),
+ }
+ }
+ }
+
+ GenFuture(gen)
+}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/infer_cross_function.rs b/src/test/ui/lazy-type-alias-impl-trait/infer_cross_function.rs
new file mode 100644
index 000000000..d07d732c7
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/infer_cross_function.rs
@@ -0,0 +1,27 @@
+// check-pass
+
+fn main() {}
+
+trait Reader {}
+
+struct Unit<R>(R);
+struct ResDwarf<R>(R);
+
+struct Context<R: Reader> {
+ dwarf: ResDwarf<R>,
+}
+
+struct Range;
+
+struct ResUnit<R>(R);
+
+impl<R: Reader + 'static> Context<R> {
+ fn find_dwarf_unit(&self, probe: u64) -> Option<&Unit<R>> {
+ let x = self.find_units(probe);
+ None
+ }
+
+ fn find_units(&self, probe: u64) -> impl Iterator<Item = &ResUnit<R>> {
+ std::iter::empty()
+ }
+}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/lifetime_inference.rs b/src/test/ui/lazy-type-alias-impl-trait/lifetime_inference.rs
new file mode 100644
index 000000000..f75a88aa8
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/lifetime_inference.rs
@@ -0,0 +1,7 @@
+// check-pass
+
+fn main() {}
+
+fn nth<I: Iterator>(iter: &mut I, step: usize) -> impl FnMut() -> Option<I::Item> + '_ {
+ move || iter.nth(step)
+}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/nested.rs b/src/test/ui/lazy-type-alias-impl-trait/nested.rs
new file mode 100644
index 000000000..f82911127
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/nested.rs
@@ -0,0 +1,23 @@
+// check-pass
+
+fn main() {}
+
+struct RawTableInner<A> {
+ alloc: A,
+}
+
+impl<A> RawTableInner<A> {
+ fn prepare_resize(
+ self,
+ ) -> ScopeGuard<Self, impl FnMut(&mut Self)> {
+ ScopeGuard { dropfn: move |self_| {}, value: self, }
+ }
+}
+
+pub struct ScopeGuard<T, F>
+where
+ F: FnMut(&mut T),
+{
+ dropfn: F,
+ value: T,
+}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/opaque_vs_opaque.rs b/src/test/ui/lazy-type-alias-impl-trait/opaque_vs_opaque.rs
new file mode 100644
index 000000000..8d03b5158
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/opaque_vs_opaque.rs
@@ -0,0 +1,10 @@
+// check-pass
+
+fn main() {}
+
+fn filter_fold<T, Acc, PRED: FnMut(&T) -> bool, FOLD: FnMut(Acc, T) -> Acc>(
+ mut predicate: PRED,
+ mut fold: FOLD,
+) -> impl FnMut(Acc, T) -> Acc {
+ move |acc, item| if predicate(&item) { fold(acc, item) } else { acc }
+}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/recursion.rs b/src/test/ui/lazy-type-alias-impl-trait/recursion.rs
new file mode 100644
index 000000000..cf7cd5d26
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/recursion.rs
@@ -0,0 +1,23 @@
+#![feature(type_alias_impl_trait)]
+
+// check-pass
+
+type Foo = impl std::fmt::Debug;
+
+fn foo(b: bool) -> Foo {
+ if b {
+ return 42
+ }
+ let x: u32 = foo(false);
+ 99
+}
+
+fn bar(b: bool) -> impl std::fmt::Debug {
+ if b {
+ return 42
+ }
+ let x: u32 = bar(false);
+ 99
+}
+
+fn main() {}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/recursion2.rs b/src/test/ui/lazy-type-alias-impl-trait/recursion2.rs
new file mode 100644
index 000000000..6b3d9ff4c
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/recursion2.rs
@@ -0,0 +1,23 @@
+#![feature(type_alias_impl_trait)]
+
+// check-pass
+
+type Foo = impl std::fmt::Debug;
+
+fn foo(b: bool) -> Foo {
+ if b {
+ return vec![];
+ }
+ let x: Vec<i32> = foo(false);
+ std::iter::empty().collect()
+}
+
+fn bar(b: bool) -> impl std::fmt::Debug {
+ if b {
+ return vec![]
+ }
+ let x: Vec<i32> = bar(false);
+ std::iter::empty().collect()
+}
+
+fn main() {}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/recursion3.rs b/src/test/ui/lazy-type-alias-impl-trait/recursion3.rs
new file mode 100644
index 000000000..7f1cedae0
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/recursion3.rs
@@ -0,0 +1,21 @@
+#![feature(type_alias_impl_trait)]
+
+type Foo = impl std::fmt::Debug;
+
+fn foo(b: bool) -> Foo {
+ if b {
+ return 42
+ }
+ let x: u32 = foo(false) + 42; //~ ERROR cannot add
+ 99
+}
+
+fn bar(b: bool) -> impl std::fmt::Debug {
+ if b {
+ return 42
+ }
+ let x: u32 = bar(false) + 42; //~ ERROR cannot add
+ 99
+}
+
+fn main() {}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/recursion3.stderr b/src/test/ui/lazy-type-alias-impl-trait/recursion3.stderr
new file mode 100644
index 000000000..e1d5cafed
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/recursion3.stderr
@@ -0,0 +1,19 @@
+error[E0369]: cannot add `{integer}` to `Foo`
+ --> $DIR/recursion3.rs:9:29
+ |
+LL | let x: u32 = foo(false) + 42;
+ | ---------- ^ -- {integer}
+ | |
+ | Foo
+
+error[E0369]: cannot add `{integer}` to `impl Debug`
+ --> $DIR/recursion3.rs:17:29
+ |
+LL | let x: u32 = bar(false) + 42;
+ | ---------- ^ -- {integer}
+ | |
+ | impl Debug
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0369`.
diff --git a/src/test/ui/lazy-type-alias-impl-trait/recursion4.rs b/src/test/ui/lazy-type-alias-impl-trait/recursion4.rs
new file mode 100644
index 000000000..57dd7fb06
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/recursion4.rs
@@ -0,0 +1,23 @@
+#![feature(type_alias_impl_trait)]
+
+type Foo = impl std::fmt::Debug;
+
+fn foo(b: bool) -> Foo {
+ if b {
+ return vec![];
+ }
+ let mut x = foo(false);
+ x = std::iter::empty().collect(); //~ ERROR `Foo` cannot be built from an iterator
+ vec![]
+}
+
+fn bar(b: bool) -> impl std::fmt::Debug {
+ if b {
+ return vec![];
+ }
+ let mut x = bar(false);
+ x = std::iter::empty().collect(); //~ ERROR `impl Debug` cannot be built from an iterator
+ vec![]
+}
+
+fn main() {}
diff --git a/src/test/ui/lazy-type-alias-impl-trait/recursion4.stderr b/src/test/ui/lazy-type-alias-impl-trait/recursion4.stderr
new file mode 100644
index 000000000..42a1f782d
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/recursion4.stderr
@@ -0,0 +1,29 @@
+error[E0277]: a value of type `Foo` cannot be built from an iterator over elements of type `_`
+ --> $DIR/recursion4.rs:10:28
+ |
+LL | x = std::iter::empty().collect();
+ | ^^^^^^^ value of type `Foo` cannot be built from `std::iter::Iterator<Item=_>`
+ |
+ = help: the trait `FromIterator<_>` is not implemented for `Foo`
+note: required by a bound in `collect`
+ --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
+ |
+LL | fn collect<B: FromIterator<Self::Item>>(self) -> B
+ | ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `collect`
+
+error[E0277]: a value of type `impl Debug` cannot be built from an iterator over elements of type `_`
+ --> $DIR/recursion4.rs:19:28
+ |
+LL | x = std::iter::empty().collect();
+ | ^^^^^^^ value of type `impl Debug` cannot be built from `std::iter::Iterator<Item=_>`
+ |
+ = help: the trait `FromIterator<_>` is not implemented for `impl Debug`
+note: required by a bound in `collect`
+ --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
+ |
+LL | fn collect<B: FromIterator<Self::Item>>(self) -> B
+ | ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `collect`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/lazy-type-alias-impl-trait/unsized_sized_opaque.rs b/src/test/ui/lazy-type-alias-impl-trait/unsized_sized_opaque.rs
new file mode 100644
index 000000000..007101498
--- /dev/null
+++ b/src/test/ui/lazy-type-alias-impl-trait/unsized_sized_opaque.rs
@@ -0,0 +1,16 @@
+// check-pass
+
+fn main() {}
+
+pub struct PairSlices<'a, 'b, T> {
+ pub(crate) a0: &'a mut [T],
+ pub(crate) a1: &'a mut [T],
+ pub(crate) b0: &'b [T],
+ pub(crate) b1: &'b [T],
+}
+
+impl<'a, 'b, T> PairSlices<'a, 'b, T> {
+ pub fn remainder(self) -> impl Iterator<Item = &'b [T]> {
+ IntoIterator::into_iter([self.b0, self.b1])
+ }
+}