about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-03-25 20:34:49 -0400
committerGitHub <noreply@github.com>2025-03-25 20:34:49 -0400
commit5bd69d940e83db1971fbd1c5a999fdb308200cf7 (patch)
tree51506c918572e6943a2cf05bb4b89a23f0ba8f9d /tests
parentf6bfdff86240e3af8f740e05b0ec04a4090139c5 (diff)
parent0827f765864a17ac6354cc12ff353043cba62286 (diff)
downloadrust-5bd69d940e83db1971fbd1c5a999fdb308200cf7.tar.gz
rust-5bd69d940e83db1971fbd1c5a999fdb308200cf7.zip
Rollup merge of #138911 - compiler-errors:define-opaque, r=oli-obk
Allow defining opaques in statics and consts

r? oli-obk

Fixes https://github.com/rust-lang/rust/issues/138902
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/impl-trait/define-via-const.rs12
-rw-r--r--tests/ui/impl-trait/define-via-extern.rs16
-rw-r--r--tests/ui/impl-trait/define-via-extern.stderr14
-rw-r--r--tests/ui/type-alias-impl-trait/issue-53092-2.rs5
-rw-r--r--tests/ui/type-alias-impl-trait/issue-53092-2.stderr51
-rw-r--r--tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs10
-rw-r--r--tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr57
7 files changed, 82 insertions, 83 deletions
diff --git a/tests/ui/impl-trait/define-via-const.rs b/tests/ui/impl-trait/define-via-const.rs
new file mode 100644
index 00000000000..a4b9123654c
--- /dev/null
+++ b/tests/ui/impl-trait/define-via-const.rs
@@ -0,0 +1,12 @@
+//@ check-pass
+
+#![feature(type_alias_impl_trait)]
+
+type Closure = impl Fn(u32) -> u32;
+
+#[define_opaque(Closure)]
+const ADDER: Closure = |x| x + 1;
+
+fn main() {
+    let z = (ADDER)(1);
+}
diff --git a/tests/ui/impl-trait/define-via-extern.rs b/tests/ui/impl-trait/define-via-extern.rs
new file mode 100644
index 00000000000..599c31ff917
--- /dev/null
+++ b/tests/ui/impl-trait/define-via-extern.rs
@@ -0,0 +1,16 @@
+#![feature(type_alias_impl_trait)]
+
+type Hi = impl Sized;
+
+extern "C" {
+    #[define_opaque(Hi)] fn foo();
+    //~^ ERROR only functions, statics, and consts can define opaque types
+
+    #[define_opaque(Hi)] static HI: Hi;
+    //~^ ERROR only functions, statics, and consts can define opaque types
+}
+
+#[define_opaque(Hi)]
+fn main() {
+    let _: Hi = 0;
+}
diff --git a/tests/ui/impl-trait/define-via-extern.stderr b/tests/ui/impl-trait/define-via-extern.stderr
new file mode 100644
index 00000000000..4a0ca5edd47
--- /dev/null
+++ b/tests/ui/impl-trait/define-via-extern.stderr
@@ -0,0 +1,14 @@
+error: only functions, statics, and consts can define opaque types
+  --> $DIR/define-via-extern.rs:6:5
+   |
+LL |     #[define_opaque(Hi)] fn foo();
+   |     ^^^^^^^^^^^^^^^^^^^^
+
+error: only functions, statics, and consts can define opaque types
+  --> $DIR/define-via-extern.rs:9:5
+   |
+LL |     #[define_opaque(Hi)] static HI: Hi;
+   |     ^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
diff --git a/tests/ui/type-alias-impl-trait/issue-53092-2.rs b/tests/ui/type-alias-impl-trait/issue-53092-2.rs
index 5f44a9aa2df..1a530d27971 100644
--- a/tests/ui/type-alias-impl-trait/issue-53092-2.rs
+++ b/tests/ui/type-alias-impl-trait/issue-53092-2.rs
@@ -2,14 +2,15 @@
 #![allow(dead_code)]
 
 type Bug<T, U> = impl Fn(T) -> U + Copy;
+//~^ ERROR cycle detected when computing type of `Bug::{opaque#0}`
 
 #[define_opaque(Bug)]
-//~^ ERROR: only functions and methods can define opaque types
 const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
+//~^ ERROR item does not constrain `Bug::{opaque#0}`
 
 #[define_opaque(Bug)]
 fn make_bug<T, U: From<T>>() -> Bug<T, U> {
-    |x| x.into() //~ ERROR is not satisfied
+    |x| x.into()
 }
 
 fn main() {
diff --git a/tests/ui/type-alias-impl-trait/issue-53092-2.stderr b/tests/ui/type-alias-impl-trait/issue-53092-2.stderr
index 5739662ff80..3062e55dc49 100644
--- a/tests/ui/type-alias-impl-trait/issue-53092-2.stderr
+++ b/tests/ui/type-alias-impl-trait/issue-53092-2.stderr
@@ -1,25 +1,42 @@
-error: only functions and methods can define opaque types
-  --> $DIR/issue-53092-2.rs:6:1
+error[E0391]: cycle detected when computing type of `Bug::{opaque#0}`
+  --> $DIR/issue-53092-2.rs:4:18
    |
-LL | #[define_opaque(Bug)]
-   | ^^^^^^^^^^^^^^^^^^^^^
-
-error[E0277]: the trait bound `U: From<T>` is not satisfied
-  --> $DIR/issue-53092-2.rs:12:5
+LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
+   |                  ^^^^^^^^^^^^^^^^^^^^^^
+   |
+note: ...which requires computing type of opaque `Bug::{opaque#0}`...
+  --> $DIR/issue-53092-2.rs:4:18
+   |
+LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
+   |                  ^^^^^^^^^^^^^^^^^^^^^^
+note: ...which requires type-checking `CONST_BUG`...
+  --> $DIR/issue-53092-2.rs:8:1
    |
-LL |     |x| x.into()
-   |     ^^^^^^^^^^^^ the trait `From<T>` is not implemented for `U`
+LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = note: ...which requires computing layout of `Bug<u8, ()>`...
+   = note: ...which requires normalizing `Bug<u8, ()>`...
+   = note: ...which again requires computing type of `Bug::{opaque#0}`, completing the cycle
+note: cycle used when checking that `Bug::{opaque#0}` is well-formed
+  --> $DIR/issue-53092-2.rs:4:18
+   |
+LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
+   |                  ^^^^^^^^^^^^^^^^^^^^^^
+   = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
+
+error: item does not constrain `Bug::{opaque#0}`
+  --> $DIR/issue-53092-2.rs:8:7
    |
-note: required by a bound in `make_bug`
-  --> $DIR/issue-53092-2.rs:11:19
+LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
+   |       ^^^^^^^^^
    |
-LL | fn make_bug<T, U: From<T>>() -> Bug<T, U> {
-   |                   ^^^^^^^ required by this bound in `make_bug`
-help: consider restricting type parameter `U` with trait `From`
+   = note: consider removing `#[define_opaque]` or adding an empty `#[define_opaque()]`
+note: this opaque type is supposed to be constrained
+  --> $DIR/issue-53092-2.rs:4:18
    |
-LL | type Bug<T, U: std::convert::From<T>> = impl Fn(T) -> U + Copy;
-   |              +++++++++++++++++++++++
+LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
+   |                  ^^^^^^^^^^^^^^^^^^^^^^
 
 error: aborting due to 2 previous errors
 
-For more information about this error, try `rustc --explain E0277`.
+For more information about this error, try `rustc --explain E0391`.
diff --git a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs
index b5533eeecba..e21627e14b0 100644
--- a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs
+++ b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs
@@ -1,19 +1,15 @@
+//@ check-pass
+
 #![feature(type_alias_impl_trait)]
-// Ensures that `const` items can not constrain an opaque `impl Trait`.
 
 use std::fmt::Debug;
 
 pub type Foo = impl Debug;
-//~^ ERROR unconstrained opaque type
 
 #[define_opaque(Foo)]
-//~^ ERROR only functions and methods can define opaque types
 const _FOO: Foo = 5;
-//~^ ERROR mismatched types
 
 #[define_opaque(Foo)]
-//~^ ERROR only functions and methods can define opaque types
-static _BAR: Foo = 22_u32;
-//~^ ERROR mismatched types
+static _BAR: Foo = 22_i32;
 
 fn main() {}
diff --git a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr
deleted file mode 100644
index dc15da665f3..00000000000
--- a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr
+++ /dev/null
@@ -1,57 +0,0 @@
-error: only functions and methods can define opaque types
-  --> $DIR/type-alias-impl-trait-const.rs:9:1
-   |
-LL | #[define_opaque(Foo)]
-   | ^^^^^^^^^^^^^^^^^^^^^
-
-error: only functions and methods can define opaque types
-  --> $DIR/type-alias-impl-trait-const.rs:14:1
-   |
-LL | #[define_opaque(Foo)]
-   | ^^^^^^^^^^^^^^^^^^^^^
-
-error: unconstrained opaque type
-  --> $DIR/type-alias-impl-trait-const.rs:6:16
-   |
-LL | pub type Foo = impl Debug;
-   |                ^^^^^^^^^^
-   |
-   = note: `Foo` must be used in combination with a concrete type within the same crate
-
-error[E0308]: mismatched types
-  --> $DIR/type-alias-impl-trait-const.rs:11:19
-   |
-LL | pub type Foo = impl Debug;
-   |                ---------- the expected opaque type
-...
-LL | const _FOO: Foo = 5;
-   |                   ^ expected opaque type, found integer
-   |
-   = note: expected opaque type `Foo`
-                     found type `{integer}`
-note: this item must have a `#[define_opaque(Foo)]` attribute to be able to define hidden types
-  --> $DIR/type-alias-impl-trait-const.rs:11:7
-   |
-LL | const _FOO: Foo = 5;
-   |       ^^^^
-
-error[E0308]: mismatched types
-  --> $DIR/type-alias-impl-trait-const.rs:16:20
-   |
-LL | pub type Foo = impl Debug;
-   |                ---------- the expected opaque type
-...
-LL | static _BAR: Foo = 22_u32;
-   |                    ^^^^^^ expected opaque type, found `u32`
-   |
-   = note: expected opaque type `Foo`
-                     found type `u32`
-note: this item must have a `#[define_opaque(Foo)]` attribute to be able to define hidden types
-  --> $DIR/type-alias-impl-trait-const.rs:16:8
-   |
-LL | static _BAR: Foo = 22_u32;
-   |        ^^^^
-
-error: aborting due to 5 previous errors
-
-For more information about this error, try `rustc --explain E0308`.