summaryrefslogtreecommitdiffstats
path: root/src/test/ui/type-inference
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/type-inference
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/type-inference')
-rw-r--r--src/test/ui/type-inference/issue-30225.rs38
-rw-r--r--src/test/ui/type-inference/issue-30225.stderr9
-rw-r--r--src/test/ui/type-inference/or_else-multiple-type-params.rs10
-rw-r--r--src/test/ui/type-inference/or_else-multiple-type-params.stderr14
-rw-r--r--src/test/ui/type-inference/sort_by_key.rs5
-rw-r--r--src/test/ui/type-inference/sort_by_key.stderr14
-rw-r--r--src/test/ui/type-inference/unbounded-associated-type.rs16
-rw-r--r--src/test/ui/type-inference/unbounded-associated-type.stderr14
-rw-r--r--src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.rs9
-rw-r--r--src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr14
-rw-r--r--src/test/ui/type-inference/unbounded-type-param-in-fn.rs7
-rw-r--r--src/test/ui/type-inference/unbounded-type-param-in-fn.stderr14
12 files changed, 164 insertions, 0 deletions
diff --git a/src/test/ui/type-inference/issue-30225.rs b/src/test/ui/type-inference/issue-30225.rs
new file mode 100644
index 000000000..42315332c
--- /dev/null
+++ b/src/test/ui/type-inference/issue-30225.rs
@@ -0,0 +1,38 @@
+// Regression test for #30225, which was an ICE that would trigger as
+// a result of a poor interaction between trait result caching and
+// type inference. Specifically, at that time, unification could cause
+// unrelated type variables to become instantiated, if subtyping
+// relationships existed. These relationships are now propagated
+// through obligations and hence everything works out fine.
+
+trait Foo<U,V> : Sized {
+ fn foo(self, u: Option<U>, v: Option<V>) {}
+}
+
+struct A;
+struct B;
+
+impl Foo<A, B> for () {} // impl A
+impl Foo<u32, u32> for u32 {} // impl B, creating ambiguity
+
+fn toxic() {
+ // cache the resolution <() as Foo<$0,$1>> = impl A
+ let u = None;
+ let v = None;
+ Foo::foo((), u, v);
+}
+
+fn bomb() {
+ let mut u = None; // type is Option<$0>
+ let mut v = None; // type is Option<$1>
+ let mut x = None; // type is Option<$2>
+
+ Foo::foo(x.unwrap(),u,v); // register <$2 as Foo<$0, $1>>
+ u = v; // mark $0 and $1 in a subtype relationship
+ //~^ ERROR mismatched types
+ x = Some(()); // set $2 = (), allowing impl selection
+ // to proceed for <() as Foo<$0, $1>> = impl A.
+ // kaboom, this *used* to trigge an ICE
+}
+
+fn main() {}
diff --git a/src/test/ui/type-inference/issue-30225.stderr b/src/test/ui/type-inference/issue-30225.stderr
new file mode 100644
index 000000000..ccd05fa6b
--- /dev/null
+++ b/src/test/ui/type-inference/issue-30225.stderr
@@ -0,0 +1,9 @@
+error[E0308]: mismatched types
+ --> $DIR/issue-30225.rs:31:9
+ |
+LL | u = v; // mark $0 and $1 in a subtype relationship
+ | ^ expected struct `A`, found struct `B`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/src/test/ui/type-inference/or_else-multiple-type-params.rs b/src/test/ui/type-inference/or_else-multiple-type-params.rs
new file mode 100644
index 000000000..b15de2a45
--- /dev/null
+++ b/src/test/ui/type-inference/or_else-multiple-type-params.rs
@@ -0,0 +1,10 @@
+use std::process::{Command, Stdio};
+
+fn main() {
+ let process = Command::new("wc")
+ .stdout(Stdio::piped())
+ .spawn()
+ .or_else(|err| { //~ ERROR type annotations needed
+ panic!("oh no: {:?}", err);
+ }).unwrap();
+}
diff --git a/src/test/ui/type-inference/or_else-multiple-type-params.stderr b/src/test/ui/type-inference/or_else-multiple-type-params.stderr
new file mode 100644
index 000000000..6ac63a91e
--- /dev/null
+++ b/src/test/ui/type-inference/or_else-multiple-type-params.stderr
@@ -0,0 +1,14 @@
+error[E0282]: type annotations needed for `Result<Child, F>`
+ --> $DIR/or_else-multiple-type-params.rs:7:18
+ |
+LL | .or_else(|err| {
+ | ^^^^^
+ |
+help: try giving this closure an explicit return type
+ |
+LL | .or_else(|err| -> Result<Child, F> {
+ | +++++++++++++++++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.
diff --git a/src/test/ui/type-inference/sort_by_key.rs b/src/test/ui/type-inference/sort_by_key.rs
new file mode 100644
index 000000000..afc4d90b8
--- /dev/null
+++ b/src/test/ui/type-inference/sort_by_key.rs
@@ -0,0 +1,5 @@
+fn main() {
+ let mut lst: [([i32; 10], bool); 10] = [([0; 10], false); 10];
+ lst.sort_by_key(|&(v, _)| v.iter().sum()); //~ ERROR type annotations needed
+ println!("{:?}", lst);
+}
diff --git a/src/test/ui/type-inference/sort_by_key.stderr b/src/test/ui/type-inference/sort_by_key.stderr
new file mode 100644
index 000000000..0a48d5756
--- /dev/null
+++ b/src/test/ui/type-inference/sort_by_key.stderr
@@ -0,0 +1,14 @@
+error[E0282]: type annotations needed
+ --> $DIR/sort_by_key.rs:3:40
+ |
+LL | lst.sort_by_key(|&(v, _)| v.iter().sum());
+ | ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
+ |
+help: consider specifying the generic argument
+ |
+LL | lst.sort_by_key(|&(v, _)| v.iter().sum::<S>());
+ | +++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.
diff --git a/src/test/ui/type-inference/unbounded-associated-type.rs b/src/test/ui/type-inference/unbounded-associated-type.rs
new file mode 100644
index 000000000..0167e9436
--- /dev/null
+++ b/src/test/ui/type-inference/unbounded-associated-type.rs
@@ -0,0 +1,16 @@
+trait T {
+ type A;
+ fn foo(&self) -> Self::A {
+ panic!()
+ }
+}
+
+struct S<X>(std::marker::PhantomData<X>);
+
+impl<X> T for S<X> {
+ type A = X;
+}
+
+fn main() {
+ S(std::marker::PhantomData).foo(); //~ ERROR type annotations needed
+}
diff --git a/src/test/ui/type-inference/unbounded-associated-type.stderr b/src/test/ui/type-inference/unbounded-associated-type.stderr
new file mode 100644
index 000000000..e0fecc72f
--- /dev/null
+++ b/src/test/ui/type-inference/unbounded-associated-type.stderr
@@ -0,0 +1,14 @@
+error[E0282]: type annotations needed
+ --> $DIR/unbounded-associated-type.rs:15:7
+ |
+LL | S(std::marker::PhantomData).foo();
+ | ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `PhantomData`
+ |
+help: consider specifying the generic argument
+ |
+LL | S(std::marker::PhantomData::<T>).foo();
+ | +++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.
diff --git a/src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.rs b/src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.rs
new file mode 100644
index 000000000..81d054b3a
--- /dev/null
+++ b/src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.rs
@@ -0,0 +1,9 @@
+#[allow(invalid_type_param_default)]
+
+fn foo<T, U = u64>() -> (T, U) {
+ panic!()
+}
+
+fn main() {
+ foo(); //~ ERROR type annotations needed
+}
diff --git a/src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr b/src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr
new file mode 100644
index 000000000..209abfe5c
--- /dev/null
+++ b/src/test/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr
@@ -0,0 +1,14 @@
+error[E0282]: type annotations needed
+ --> $DIR/unbounded-type-param-in-fn-with-assoc-type.rs:8:5
+ |
+LL | foo();
+ | ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
+ |
+help: consider specifying the generic arguments
+ |
+LL | foo::<T, U>();
+ | ++++++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.
diff --git a/src/test/ui/type-inference/unbounded-type-param-in-fn.rs b/src/test/ui/type-inference/unbounded-type-param-in-fn.rs
new file mode 100644
index 000000000..1f336ed59
--- /dev/null
+++ b/src/test/ui/type-inference/unbounded-type-param-in-fn.rs
@@ -0,0 +1,7 @@
+fn foo<T>() -> T {
+ panic!()
+}
+
+fn main() {
+ foo(); //~ ERROR type annotations needed
+}
diff --git a/src/test/ui/type-inference/unbounded-type-param-in-fn.stderr b/src/test/ui/type-inference/unbounded-type-param-in-fn.stderr
new file mode 100644
index 000000000..d92892eeb
--- /dev/null
+++ b/src/test/ui/type-inference/unbounded-type-param-in-fn.stderr
@@ -0,0 +1,14 @@
+error[E0282]: type annotations needed
+ --> $DIR/unbounded-type-param-in-fn.rs:6:5
+ |
+LL | foo();
+ | ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
+ |
+help: consider specifying the generic argument
+ |
+LL | foo::<T>();
+ | +++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0282`.