about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorBryanskiy <ivakin.kir@gmail.com>2024-09-30 21:07:36 +0300
committerBryanskiy <ivakin.kir@gmail.com>2025-05-04 22:03:15 +0300
commit14535312b522c0524dd94633cc6a49992b12cecd (patch)
treef397262aa2e597623ac7dc9a721da0360398808d /tests
parent62c5f58f57670ce65e7fec34f8c4ba00c27da2d9 (diff)
downloadrust-14535312b522c0524dd94633cc6a49992b12cecd.tar.gz
rust-14535312b522c0524dd94633cc6a49992b12cecd.zip
Initial support for dynamically linked crates
Diffstat (limited to 'tests')
-rw-r--r--tests/run-make/export/compile-interface-error/app.rs3
-rw-r--r--tests/run-make/export/compile-interface-error/liblibr.rs5
-rw-r--r--tests/run-make/export/compile-interface-error/rmake.rs9
-rw-r--r--tests/run-make/export/disambiguator/app.rs7
-rw-r--r--tests/run-make/export/disambiguator/libr.rs27
-rw-r--r--tests/run-make/export/disambiguator/rmake.rs12
-rw-r--r--tests/run-make/export/extern-opt/app.rs6
-rw-r--r--tests/run-make/export/extern-opt/libinterface.rs4
-rw-r--r--tests/run-make/export/extern-opt/libr.rs5
-rw-r--r--tests/run-make/export/extern-opt/rmake.rs23
-rw-r--r--tests/run-make/export/simple/app.rs8
-rw-r--r--tests/run-make/export/simple/libr.rs22
-rw-r--r--tests/run-make/export/simple/rmake.rs12
-rw-r--r--tests/ui/attributes/export/crate-type-2.rs2
-rw-r--r--tests/ui/attributes/export/crate-type-2.stderr9
-rw-r--r--tests/ui/attributes/export/crate-type.rs2
-rw-r--r--tests/ui/attributes/export/crate-type.stderr9
-rw-r--r--tests/ui/attributes/export/exportable.rs139
-rw-r--r--tests/ui/attributes/export/exportable.stderr130
-rw-r--r--tests/ui/attributes/export/lang-item.rs8
-rw-r--r--tests/ui/attributes/export/lang-item.stderr8
-rw-r--r--tests/ui/feature-gates/feature-gate-export_stable.rs5
-rw-r--r--tests/ui/feature-gates/feature-gate-export_stable.stderr13
23 files changed, 468 insertions, 0 deletions
diff --git a/tests/run-make/export/compile-interface-error/app.rs b/tests/run-make/export/compile-interface-error/app.rs
new file mode 100644
index 00000000000..f619745a711
--- /dev/null
+++ b/tests/run-make/export/compile-interface-error/app.rs
@@ -0,0 +1,3 @@
+extern crate libr;
+
+fn main() {}
diff --git a/tests/run-make/export/compile-interface-error/liblibr.rs b/tests/run-make/export/compile-interface-error/liblibr.rs
new file mode 100644
index 00000000000..906d8d7be12
--- /dev/null
+++ b/tests/run-make/export/compile-interface-error/liblibr.rs
@@ -0,0 +1,5 @@
+#![feature(export_stable)]
+
+// interface file is broken(priv fn):
+#[export_stable]
+extern "C" fn foo();
diff --git a/tests/run-make/export/compile-interface-error/rmake.rs b/tests/run-make/export/compile-interface-error/rmake.rs
new file mode 100644
index 00000000000..89474e9d4fb
--- /dev/null
+++ b/tests/run-make/export/compile-interface-error/rmake.rs
@@ -0,0 +1,9 @@
+use run_make_support::rustc;
+
+fn main() {
+    // Do not produce the interface, use the broken one.
+    rustc()
+        .input("app.rs")
+        .run_fail()
+        .assert_stderr_contains("couldn't compile interface");
+}
diff --git a/tests/run-make/export/disambiguator/app.rs b/tests/run-make/export/disambiguator/app.rs
new file mode 100644
index 00000000000..27e0e2280e5
--- /dev/null
+++ b/tests/run-make/export/disambiguator/app.rs
@@ -0,0 +1,7 @@
+extern crate libr;
+
+use libr::*;
+
+fn main() {
+    assert_eq!(S::<S2>::foo(), 2);
+}
diff --git a/tests/run-make/export/disambiguator/libr.rs b/tests/run-make/export/disambiguator/libr.rs
new file mode 100644
index 00000000000..b294d5c9e8e
--- /dev/null
+++ b/tests/run-make/export/disambiguator/libr.rs
@@ -0,0 +1,27 @@
+// `S::<S2>::foo` and `S::<S1>::foo` have same `DefPath` modulo disambiguator.
+// `libr.rs` interface may not contain `S::<S1>::foo` as private items aren't
+// exportable. We should make sure that original `S::<S2>::foo` and the one
+// produced during interface generation have same mangled names.
+
+#![feature(export_stable)]
+#![crate_type = "sdylib"]
+
+#[export_stable]
+#[repr(C)]
+pub struct S<T>(pub T);
+
+struct S1;
+pub struct S2;
+
+impl S<S1> {
+    extern "C" fn foo() -> i32 {
+        1
+    }
+}
+
+#[export_stable]
+impl S<S2> {
+    pub extern "C" fn foo() -> i32 {
+        2
+    }
+}
diff --git a/tests/run-make/export/disambiguator/rmake.rs b/tests/run-make/export/disambiguator/rmake.rs
new file mode 100644
index 00000000000..743db1933fb
--- /dev/null
+++ b/tests/run-make/export/disambiguator/rmake.rs
@@ -0,0 +1,12 @@
+use run_make_support::rustc;
+
+fn main() {
+    rustc()
+        .env("RUSTC_FORCE_RUSTC_VERSION", "1")
+        .input("libr.rs")
+        .run();
+    rustc()
+        .env("RUSTC_FORCE_RUSTC_VERSION", "2")
+        .input("app.rs")
+        .run();
+}
diff --git a/tests/run-make/export/extern-opt/app.rs b/tests/run-make/export/extern-opt/app.rs
new file mode 100644
index 00000000000..765c9925d5f
--- /dev/null
+++ b/tests/run-make/export/extern-opt/app.rs
@@ -0,0 +1,6 @@
+extern crate libr;
+use libr::*;
+
+fn main() {
+    assert_eq!(foo(1), 1);
+}
diff --git a/tests/run-make/export/extern-opt/libinterface.rs b/tests/run-make/export/extern-opt/libinterface.rs
new file mode 100644
index 00000000000..313cfbe7d59
--- /dev/null
+++ b/tests/run-make/export/extern-opt/libinterface.rs
@@ -0,0 +1,4 @@
+#![feature(export_stable)]
+
+#[export_stable]
+pub extern "C" fn foo(x: i32) -> i32;
diff --git a/tests/run-make/export/extern-opt/libr.rs b/tests/run-make/export/extern-opt/libr.rs
new file mode 100644
index 00000000000..026ebb4233d
--- /dev/null
+++ b/tests/run-make/export/extern-opt/libr.rs
@@ -0,0 +1,5 @@
+#![feature(export_stable)]
+#![crate_type = "sdylib"]
+
+#[export_stable]
+pub extern "C" fn foo(x: i32) -> i32 { x }
diff --git a/tests/run-make/export/extern-opt/rmake.rs b/tests/run-make/export/extern-opt/rmake.rs
new file mode 100644
index 00000000000..821e2eb2149
--- /dev/null
+++ b/tests/run-make/export/extern-opt/rmake.rs
@@ -0,0 +1,23 @@
+use run_make_support::{rustc, dynamic_lib_name};
+
+fn main() {
+    rustc()
+        .env("RUSTC_FORCE_RUSTC_VERSION", "1")
+        .input("libr.rs")
+        .run();
+
+    rustc()
+        .env("RUSTC_FORCE_RUSTC_VERSION", "2")
+        .input("app.rs")
+        .extern_("libr", "libinterface.rs")
+        .extern_("libr", dynamic_lib_name("libr"))
+        .run();
+
+    rustc()
+        .env("RUSTC_FORCE_RUSTC_VERSION", "2")
+        .input("app.rs")
+        .extern_("libr", "interface.rs") // wrong interface format
+        .extern_("libr", dynamic_lib_name("libr"))
+        .run_fail()
+        .assert_stderr_contains("extern location for libr does not exist");
+}
diff --git a/tests/run-make/export/simple/app.rs b/tests/run-make/export/simple/app.rs
new file mode 100644
index 00000000000..ba34bdd7b56
--- /dev/null
+++ b/tests/run-make/export/simple/app.rs
@@ -0,0 +1,8 @@
+extern crate libr;
+use libr::*;
+
+fn main() {
+    let s = m::S { x: 42 };
+    assert_eq!(m::foo1(s), 42);
+    assert_eq!(m::S::foo2(1), 1);
+}
diff --git a/tests/run-make/export/simple/libr.rs b/tests/run-make/export/simple/libr.rs
new file mode 100644
index 00000000000..e10b76a6e52
--- /dev/null
+++ b/tests/run-make/export/simple/libr.rs
@@ -0,0 +1,22 @@
+#![feature(export_stable)]
+#![crate_type = "sdylib"]
+
+#[export_stable]
+pub mod m {
+    #[repr(C)]
+    pub struct S {
+        pub x: i32,
+    }
+
+    pub extern "C" fn foo1(x: S) -> i32 {
+        x.x
+    }
+
+    pub type Integer = i32;
+
+    impl S {
+        pub extern "C" fn foo2(x: Integer) -> Integer {
+            x
+        }
+    }
+}
diff --git a/tests/run-make/export/simple/rmake.rs b/tests/run-make/export/simple/rmake.rs
new file mode 100644
index 00000000000..743db1933fb
--- /dev/null
+++ b/tests/run-make/export/simple/rmake.rs
@@ -0,0 +1,12 @@
+use run_make_support::rustc;
+
+fn main() {
+    rustc()
+        .env("RUSTC_FORCE_RUSTC_VERSION", "1")
+        .input("libr.rs")
+        .run();
+    rustc()
+        .env("RUSTC_FORCE_RUSTC_VERSION", "2")
+        .input("app.rs")
+        .run();
+}
diff --git a/tests/ui/attributes/export/crate-type-2.rs b/tests/ui/attributes/export/crate-type-2.rs
new file mode 100644
index 00000000000..f0379f6d797
--- /dev/null
+++ b/tests/ui/attributes/export/crate-type-2.rs
@@ -0,0 +1,2 @@
+//@ compile-flags: --crate-type=sdylib
+//~^ ERROR  `sdylib` crate type is unstable
diff --git a/tests/ui/attributes/export/crate-type-2.stderr b/tests/ui/attributes/export/crate-type-2.stderr
new file mode 100644
index 00000000000..7ce6a500113
--- /dev/null
+++ b/tests/ui/attributes/export/crate-type-2.stderr
@@ -0,0 +1,9 @@
+error[E0658]: `sdylib` crate type is unstable
+   |
+   = note: see issue #139939 <https://github.com/rust-lang/rust/issues/139939> for more information
+   = help: add `#![feature(export_stable)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/attributes/export/crate-type.rs b/tests/ui/attributes/export/crate-type.rs
new file mode 100644
index 00000000000..bd092bbb1a1
--- /dev/null
+++ b/tests/ui/attributes/export/crate-type.rs
@@ -0,0 +1,2 @@
+#![crate_type = "sdylib"]
+//~^ ERROR  `sdylib` crate type is unstable
diff --git a/tests/ui/attributes/export/crate-type.stderr b/tests/ui/attributes/export/crate-type.stderr
new file mode 100644
index 00000000000..7ce6a500113
--- /dev/null
+++ b/tests/ui/attributes/export/crate-type.stderr
@@ -0,0 +1,9 @@
+error[E0658]: `sdylib` crate type is unstable
+   |
+   = note: see issue #139939 <https://github.com/rust-lang/rust/issues/139939> for more information
+   = help: add `#![feature(export_stable)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/attributes/export/exportable.rs b/tests/ui/attributes/export/exportable.rs
new file mode 100644
index 00000000000..f592fce88cd
--- /dev/null
+++ b/tests/ui/attributes/export/exportable.rs
@@ -0,0 +1,139 @@
+//@ compile-flags: -Zunstable-options -Csymbol-mangling-version=v0
+
+#![crate_type = "sdylib"]
+#![allow(incomplete_features, improper_ctypes_definitions)]
+#![feature(export_stable)]
+#![feature(inherent_associated_types)]
+
+mod m {
+    #[export_stable]
+    pub struct S;
+    //~^ ERROR private items are not exportable
+
+    pub fn foo() -> i32 { 0 }
+    //~^ ERROR only functions with "C" ABI are exportable
+}
+
+#[export_stable]
+pub use m::foo;
+
+#[export_stable]
+pub mod m1 {
+    #[repr(C)]
+    pub struct S1; // OK, public type with stable repr
+
+    struct S2;
+
+    pub struct S3;
+    //~^ ERROR types with unstable layout are not exportable
+}
+
+pub mod fn_sig {
+    #[export_stable]
+    pub fn foo1() {}
+    //~^ ERROR only functions with "C" ABI are exportable
+
+    #[export_stable]
+    #[repr(C)]
+    pub struct S;
+
+    #[export_stable]
+    pub extern "C" fn foo2(x: S) -> i32 { 0 }
+
+    #[export_stable]
+    pub extern "C" fn foo3(x: Box<S>) -> i32 { 0 }
+    //~^ ERROR function with `#[export_stable]` attribute uses type `Box<fn_sig::S>`, which is not exportable
+}
+
+pub mod impl_item {
+    pub struct S;
+
+    impl S {
+        #[export_stable]
+        pub extern "C" fn foo1(&self) -> i32 { 0 }
+        //~^ ERROR method with `#[export_stable]` attribute uses type `&impl_item::S`, which is not exportable
+
+        #[export_stable]
+        pub extern "C" fn foo2(self) -> i32 { 0 }
+        //~^ ERROR method with `#[export_stable]` attribute uses type `impl_item::S`, which is not exportable
+    }
+
+    pub struct S2<T>(T);
+
+    impl<T> S2<T> {
+        #[export_stable]
+        pub extern "C" fn foo1(&self) {}
+        //~^ ERROR generic functions are not exportable
+    }
+}
+
+pub mod tys {
+    pub trait Trait {
+        type Type;
+    }
+    pub struct S;
+
+    impl Trait for S {
+        type Type = (u32,);
+    }
+
+    #[export_stable]
+    pub extern "C" fn foo1(x: <S as Trait>::Type) -> u32 { x.0 }
+    //~^ ERROR function with `#[export_stable]` attribute uses type `(u32,)`, which is not exportable
+
+    #[export_stable]
+    pub type Type = [i32; 4];
+
+    #[export_stable]
+    pub extern "C" fn foo2(_x: Type) {}
+    //~^ ERROR function with `#[export_stable]` attribute uses type `[i32; 4]`, which is not exportable
+
+    impl S {
+        #[export_stable]
+        pub type Type = extern "C" fn();
+    }
+
+    #[export_stable]
+    pub extern "C" fn foo3(_x: S::Type) {}
+    //~^ ERROR function with `#[export_stable]` attribute uses type `extern "C" fn()`, which is not exportable
+
+    #[export_stable]
+    pub extern "C" fn foo4() -> impl Copy {
+    //~^ ERROR function with `#[export_stable]` attribute uses type `impl Copy`, which is not exportable
+        0
+    }
+}
+
+pub mod privacy {
+    #[export_stable]
+    #[repr(C)]
+    pub struct S1 {
+        pub x: i32
+    }
+
+    #[export_stable]
+    #[repr(C)]
+    pub struct S2 {
+    //~^ ERROR ADT types with private fields are not exportable
+        x: i32
+    }
+
+    #[export_stable]
+    #[repr(i32)]
+    enum E {
+    //~^ ERROR private items are not exportable
+        Variant1 { x: i32 }
+    }
+}
+
+pub mod use_site {
+    #[export_stable]
+    pub trait Trait {}
+    //~^ ERROR trait's are not exportable
+
+    #[export_stable]
+    pub const C: i32 = 0;
+    //~^ ERROR constant's are not exportable
+}
+
+fn main() {}
diff --git a/tests/ui/attributes/export/exportable.stderr b/tests/ui/attributes/export/exportable.stderr
new file mode 100644
index 00000000000..0f6469d35c3
--- /dev/null
+++ b/tests/ui/attributes/export/exportable.stderr
@@ -0,0 +1,130 @@
+error: private items are not exportable
+  --> $DIR/exportable.rs:10:5
+   |
+LL |     pub struct S;
+   |     ^^^^^^^^^^^^
+   |
+note: is only usable at visibility `pub(crate)`
+  --> $DIR/exportable.rs:10:5
+   |
+LL |     pub struct S;
+   |     ^^^^^^^^^^^^
+
+error: private items are not exportable
+  --> $DIR/exportable.rs:123:5
+   |
+LL |     enum E {
+   |     ^^^^^^
+   |
+note: is only usable at visibility `pub(self)`
+  --> $DIR/exportable.rs:123:5
+   |
+LL |     enum E {
+   |     ^^^^^^
+
+error: trait's are not exportable
+  --> $DIR/exportable.rs:131:5
+   |
+LL |     pub trait Trait {}
+   |     ^^^^^^^^^^^^^^^
+
+error: constant's are not exportable
+  --> $DIR/exportable.rs:135:5
+   |
+LL |     pub const C: i32 = 0;
+   |     ^^^^^^^^^^^^^^^^
+
+error: only functions with "C" ABI are exportable
+  --> $DIR/exportable.rs:13:5
+   |
+LL |     pub fn foo() -> i32 { 0 }
+   |     ^^^^^^^^^^^^^^^^^^^
+
+error: types with unstable layout are not exportable
+  --> $DIR/exportable.rs:27:5
+   |
+LL |     pub struct S3;
+   |     ^^^^^^^^^^^^^
+
+error: only functions with "C" ABI are exportable
+  --> $DIR/exportable.rs:33:5
+   |
+LL |     pub fn foo1() {}
+   |     ^^^^^^^^^^^^^
+
+error: function with `#[export_stable]` attribute uses type `Box<fn_sig::S>`, which is not exportable
+  --> $DIR/exportable.rs:44:5
+   |
+LL |     pub extern "C" fn foo3(x: Box<S>) -> i32 { 0 }
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^------^^^^^^^^
+   |                               |
+   |                               not exportable
+
+error: method with `#[export_stable]` attribute uses type `&impl_item::S`, which is not exportable
+  --> $DIR/exportable.rs:53:9
+   |
+LL |         pub extern "C" fn foo1(&self) -> i32 { 0 }
+   |         ^^^^^^^^^^^^^^^^^^^^^^^-----^^^^^^^^
+   |                                |
+   |                                not exportable
+
+error: method with `#[export_stable]` attribute uses type `impl_item::S`, which is not exportable
+  --> $DIR/exportable.rs:57:9
+   |
+LL |         pub extern "C" fn foo2(self) -> i32 { 0 }
+   |         ^^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^^
+   |                                |
+   |                                not exportable
+
+error: generic functions are not exportable
+  --> $DIR/exportable.rs:65:9
+   |
+LL |         pub extern "C" fn foo1(&self) {}
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: function with `#[export_stable]` attribute uses type `(u32,)`, which is not exportable
+  --> $DIR/exportable.rs:81:5
+   |
+LL |     pub extern "C" fn foo1(x: <S as Trait>::Type) -> u32 { x.0 }
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^------------------^^^^^^^^
+   |                               |
+   |                               not exportable
+
+error: function with `#[export_stable]` attribute uses type `[i32; 4]`, which is not exportable
+  --> $DIR/exportable.rs:88:5
+   |
+LL |     pub extern "C" fn foo2(_x: Type) {}
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^----^
+   |                                |
+   |                                not exportable
+
+error: function with `#[export_stable]` attribute uses type `extern "C" fn()`, which is not exportable
+  --> $DIR/exportable.rs:97:5
+   |
+LL |     pub extern "C" fn foo3(_x: S::Type) {}
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^-------^
+   |                                |
+   |                                not exportable
+
+error: function with `#[export_stable]` attribute uses type `impl Copy`, which is not exportable
+  --> $DIR/exportable.rs:101:5
+   |
+LL |     pub extern "C" fn foo4() -> impl Copy {
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------
+   |                                 |
+   |                                 not exportable
+
+error: ADT types with private fields are not exportable
+  --> $DIR/exportable.rs:116:5
+   |
+LL |     pub struct S2 {
+   |     ^^^^^^^^^^^^^
+   |
+note: `x` is private
+  --> $DIR/exportable.rs:118:9
+   |
+LL |         x: i32
+   |         ^^^^^^
+
+error: aborting due to 16 previous errors
+
diff --git a/tests/ui/attributes/export/lang-item.rs b/tests/ui/attributes/export/lang-item.rs
new file mode 100644
index 00000000000..b923b41a957
--- /dev/null
+++ b/tests/ui/attributes/export/lang-item.rs
@@ -0,0 +1,8 @@
+#![feature(no_core, lang_items, export_stable)]
+#![allow(incomplete_features)]
+#![crate_type = "sdylib"]
+#![no_core]
+
+#[lang = "sized"]
+//~^ ERROR lang items are not allowed in stable dylibs
+trait Sized {}
diff --git a/tests/ui/attributes/export/lang-item.stderr b/tests/ui/attributes/export/lang-item.stderr
new file mode 100644
index 00000000000..8c0741bdb6f
--- /dev/null
+++ b/tests/ui/attributes/export/lang-item.stderr
@@ -0,0 +1,8 @@
+error: lang items are not allowed in stable dylibs
+  --> $DIR/lang-item.rs:6:1
+   |
+LL | #[lang = "sized"]
+   | ^^^^^^^^^^^^^^^^^
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/feature-gates/feature-gate-export_stable.rs b/tests/ui/feature-gates/feature-gate-export_stable.rs
new file mode 100644
index 00000000000..5d05fee059b
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-export_stable.rs
@@ -0,0 +1,5 @@
+#![crate_type="lib"]
+
+#[export_stable]
+//~^ ERROR the `#[export_stable]` attribute is an experimental feature
+pub mod a {}
diff --git a/tests/ui/feature-gates/feature-gate-export_stable.stderr b/tests/ui/feature-gates/feature-gate-export_stable.stderr
new file mode 100644
index 00000000000..6beb52a77e5
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-export_stable.stderr
@@ -0,0 +1,13 @@
+error[E0658]: the `#[export_stable]` attribute is an experimental feature
+  --> $DIR/feature-gate-export_stable.rs:3:1
+   |
+LL | #[export_stable]
+   | ^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #139939 <https://github.com/rust-lang/rust/issues/139939> for more information
+   = help: add `#![feature(export_stable)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0658`.