summaryrefslogtreecommitdiffstats
path: root/tests/ui/specialization/soundness
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/specialization/soundness
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/specialization/soundness')
-rw-r--r--tests/ui/specialization/soundness/partial_eq_range_inclusive.rs35
-rw-r--r--tests/ui/specialization/soundness/partial_ord_slice.rs42
2 files changed, 77 insertions, 0 deletions
diff --git a/tests/ui/specialization/soundness/partial_eq_range_inclusive.rs b/tests/ui/specialization/soundness/partial_eq_range_inclusive.rs
new file mode 100644
index 000000000..923dec892
--- /dev/null
+++ b/tests/ui/specialization/soundness/partial_eq_range_inclusive.rs
@@ -0,0 +1,35 @@
+// run-pass
+
+use std::cell::RefCell;
+use std::cmp::Ordering;
+
+struct Evil<'a, 'b> {
+ values: RefCell<Vec<&'a str>>,
+ to_insert: &'b String,
+}
+
+impl<'a, 'b> PartialEq for Evil<'a, 'b> {
+ fn eq(&self, _other: &Self) -> bool {
+ true
+ }
+}
+
+impl<'a> PartialOrd for Evil<'a, 'a> {
+ fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
+ self.values.borrow_mut().push(self.to_insert);
+ None
+ }
+}
+
+fn main() {
+ let e;
+ let values;
+ {
+ let to_insert = String::from("Hello, world!");
+ e = Evil { values: RefCell::new(Vec::new()), to_insert: &to_insert };
+ let range = &e..=&e;
+ let _ = range == range;
+ values = e.values;
+ }
+ assert_eq!(*values.borrow(), Vec::<&str>::new());
+}
diff --git a/tests/ui/specialization/soundness/partial_ord_slice.rs b/tests/ui/specialization/soundness/partial_ord_slice.rs
new file mode 100644
index 000000000..b9e80a48d
--- /dev/null
+++ b/tests/ui/specialization/soundness/partial_ord_slice.rs
@@ -0,0 +1,42 @@
+// Check that we aren't using unsound specialization in slice comparisons.
+
+// run-pass
+
+use std::cell::Cell;
+use std::cmp::Ordering;
+
+struct Evil<'a, 'b>(Cell<(&'a [i32], &'b [i32])>);
+
+impl PartialEq for Evil<'_, '_> {
+ fn eq(&self, _other: &Self) -> bool {
+ true
+ }
+}
+
+impl Eq for Evil<'_, '_> {}
+
+impl PartialOrd for Evil<'_, '_> {
+ fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
+ Some(Ordering::Equal)
+ }
+}
+
+impl<'a> Ord for Evil<'a, 'a> {
+ fn cmp(&self, _other: &Self) -> Ordering {
+ let (a, b) = self.0.get();
+ self.0.set((b, a));
+ Ordering::Equal
+ }
+}
+
+fn main() {
+ let x = &[1, 2, 3, 4];
+ let u = {
+ let a = Box::new([7, 8, 9, 10]);
+ let y = [Evil(Cell::new((x, &*a)))];
+ let _ = &y[..] <= &y[..];
+ let [Evil(c)] = y;
+ c.get().0
+ };
+ assert_eq!(u, &[1, 2, 3, 4]);
+}