summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/trait_specialization.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/consts/trait_specialization.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/consts/trait_specialization.rs')
-rw-r--r--tests/ui/consts/trait_specialization.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/ui/consts/trait_specialization.rs b/tests/ui/consts/trait_specialization.rs
new file mode 100644
index 000000000..c581ef6b0
--- /dev/null
+++ b/tests/ui/consts/trait_specialization.rs
@@ -0,0 +1,65 @@
+// ignore-wasm32-bare which doesn't support `std::process:exit()`
+// compile-flags: -Zmir-opt-level=3
+// run-pass
+
+// Tests that specialization does not cause optimizations running on polymorphic MIR to resolve
+// to a `default` implementation.
+
+#![feature(specialization)] //~ WARN the feature `specialization` is incomplete
+
+trait Marker {}
+
+trait SpecializedTrait {
+ const CONST_BOOL: bool;
+ const CONST_STR: &'static str;
+ fn method() -> &'static str;
+}
+impl <T> SpecializedTrait for T {
+ default const CONST_BOOL: bool = false;
+ default const CONST_STR: &'static str = "in default impl";
+ #[inline(always)]
+ default fn method() -> &'static str {
+ "in default impl"
+ }
+}
+impl <T: Marker> SpecializedTrait for T {
+ const CONST_BOOL: bool = true;
+ const CONST_STR: &'static str = "in specialized impl";
+ fn method() -> &'static str {
+ "in specialized impl"
+ }
+}
+
+fn const_bool<T>() -> &'static str {
+ if <T as SpecializedTrait>::CONST_BOOL {
+ "in specialized impl"
+ } else {
+ "in default impl"
+ }
+}
+fn const_str<T>() -> &'static str {
+ <T as SpecializedTrait>::CONST_STR
+}
+fn run_method<T>() -> &'static str {
+ <T as SpecializedTrait>::method()
+}
+
+struct TypeA;
+impl Marker for TypeA {}
+struct TypeB;
+
+#[inline(never)]
+fn exit_if_not_eq(left: &str, right: &str) {
+ if left != right {
+ std::process::exit(1);
+ }
+}
+
+pub fn main() {
+ exit_if_not_eq("in specialized impl", const_bool::<TypeA>());
+ exit_if_not_eq("in default impl", const_bool::<TypeB>());
+ exit_if_not_eq("in specialized impl", const_str::<TypeA>());
+ exit_if_not_eq("in default impl", const_str::<TypeB>());
+ exit_if_not_eq("in specialized impl", run_method::<TypeA>());
+ exit_if_not_eq("in default impl", run_method::<TypeB>());
+}