From 218caa410aa38c29984be31a5229b9fa717560ee Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:13 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- .../auxiliary/overloaded_autoderef_xc.rs | 30 +++++++ tests/ui/overloaded/fixup-deref-mut.rs | 50 ++++++++++++ tests/ui/overloaded/issue-14958.rs | 31 +++++++ tests/ui/overloaded/overloaded-autoderef-count.rs | 74 +++++++++++++++++ .../ui/overloaded/overloaded-autoderef-indexing.rs | 20 +++++ tests/ui/overloaded/overloaded-autoderef-order.rs | 73 +++++++++++++++++ tests/ui/overloaded/overloaded-autoderef-vtable.rs | 39 +++++++++ tests/ui/overloaded/overloaded-autoderef-xcrate.rs | 9 ++ tests/ui/overloaded/overloaded-autoderef.rs | 43 ++++++++++ tests/ui/overloaded/overloaded-calls-nontuple.rs | 30 +++++++ .../ui/overloaded/overloaded-calls-nontuple.stderr | 34 ++++++++ .../overloaded/overloaded-calls-object-one-arg.rs | 13 +++ .../overloaded/overloaded-calls-object-two-args.rs | 13 +++ .../overloaded-calls-object-zero-args.rs | 13 +++ .../overloaded/overloaded-calls-param-vtables.rs | 32 ++++++++ tests/ui/overloaded/overloaded-calls-simple.rs | 78 ++++++++++++++++++ tests/ui/overloaded/overloaded-calls-zero-args.rs | 30 +++++++ tests/ui/overloaded/overloaded-deref-count.rs | 78 ++++++++++++++++++ tests/ui/overloaded/overloaded-deref.rs | 43 ++++++++++ tests/ui/overloaded/overloaded-index-assoc-list.rs | 48 +++++++++++ tests/ui/overloaded/overloaded-index-autoderef.rs | 75 +++++++++++++++++ tests/ui/overloaded/overloaded-index-in-field.rs | 46 +++++++++++ tests/ui/overloaded/overloaded-index.rs | 64 +++++++++++++++ .../overloaded_deref_with_ref_pattern.rs | 95 ++++++++++++++++++++++ ...overloaded_deref_with_ref_pattern_issue15609.rs | 37 +++++++++ 25 files changed, 1098 insertions(+) create mode 100644 tests/ui/overloaded/auxiliary/overloaded_autoderef_xc.rs create mode 100644 tests/ui/overloaded/fixup-deref-mut.rs create mode 100644 tests/ui/overloaded/issue-14958.rs create mode 100644 tests/ui/overloaded/overloaded-autoderef-count.rs create mode 100644 tests/ui/overloaded/overloaded-autoderef-indexing.rs create mode 100644 tests/ui/overloaded/overloaded-autoderef-order.rs create mode 100644 tests/ui/overloaded/overloaded-autoderef-vtable.rs create mode 100644 tests/ui/overloaded/overloaded-autoderef-xcrate.rs create mode 100644 tests/ui/overloaded/overloaded-autoderef.rs create mode 100644 tests/ui/overloaded/overloaded-calls-nontuple.rs create mode 100644 tests/ui/overloaded/overloaded-calls-nontuple.stderr create mode 100644 tests/ui/overloaded/overloaded-calls-object-one-arg.rs create mode 100644 tests/ui/overloaded/overloaded-calls-object-two-args.rs create mode 100644 tests/ui/overloaded/overloaded-calls-object-zero-args.rs create mode 100644 tests/ui/overloaded/overloaded-calls-param-vtables.rs create mode 100644 tests/ui/overloaded/overloaded-calls-simple.rs create mode 100644 tests/ui/overloaded/overloaded-calls-zero-args.rs create mode 100644 tests/ui/overloaded/overloaded-deref-count.rs create mode 100644 tests/ui/overloaded/overloaded-deref.rs create mode 100644 tests/ui/overloaded/overloaded-index-assoc-list.rs create mode 100644 tests/ui/overloaded/overloaded-index-autoderef.rs create mode 100644 tests/ui/overloaded/overloaded-index-in-field.rs create mode 100644 tests/ui/overloaded/overloaded-index.rs create mode 100644 tests/ui/overloaded/overloaded_deref_with_ref_pattern.rs create mode 100644 tests/ui/overloaded/overloaded_deref_with_ref_pattern_issue15609.rs (limited to 'tests/ui/overloaded') diff --git a/tests/ui/overloaded/auxiliary/overloaded_autoderef_xc.rs b/tests/ui/overloaded/auxiliary/overloaded_autoderef_xc.rs new file mode 100644 index 000000000..112455f91 --- /dev/null +++ b/tests/ui/overloaded/auxiliary/overloaded_autoderef_xc.rs @@ -0,0 +1,30 @@ +use std::ops::Deref; + +struct DerefWithHelper { + pub helper: H, + pub value: Option +} + +trait Helper { + fn helper_borrow(&self) -> &T; +} + +impl Helper for Option { + fn helper_borrow(&self) -> &T { + self.as_ref().unwrap() + } +} + +impl> Deref for DerefWithHelper { + type Target = T; + + fn deref(&self) -> &T { + self.helper.helper_borrow() + } +} + +// Test cross-crate autoderef + vtable. +pub fn check(x: T, y: T) -> bool { + let d: DerefWithHelper, T> = DerefWithHelper { helper: Some(x), value: None }; + d.eq(&y) +} diff --git a/tests/ui/overloaded/fixup-deref-mut.rs b/tests/ui/overloaded/fixup-deref-mut.rs new file mode 100644 index 000000000..6b2fd72b8 --- /dev/null +++ b/tests/ui/overloaded/fixup-deref-mut.rs @@ -0,0 +1,50 @@ +// run-pass + +#![allow(dead_code)] +// pretty-expanded FIXME #23616 + +use std::ops::{Deref, DerefMut}; + +// Generic unique/owned smaht pointer. +struct Own { + value: *mut T +} + +impl Deref for Own { + type Target = T; + + fn deref<'a>(&'a self) -> &'a T { + unsafe { &*self.value } + } +} + +impl DerefMut for Own { + fn deref_mut<'a>(&'a mut self) -> &'a mut T { + unsafe { &mut *self.value } + } +} + +struct Point { + x: isize, + y: isize +} + +impl Point { + fn get(&mut self) -> (isize, isize) { + (self.x, self.y) + } +} + +fn test0(mut x: Own) { + let _ = x.get(); +} + +fn test1(mut x: Own>>) { + let _ = x.get(); +} + +fn test2(mut x: Own>>) { + let _ = (**x).get(); +} + +fn main() {} diff --git a/tests/ui/overloaded/issue-14958.rs b/tests/ui/overloaded/issue-14958.rs new file mode 100644 index 000000000..a12564ca9 --- /dev/null +++ b/tests/ui/overloaded/issue-14958.rs @@ -0,0 +1,31 @@ +// run-pass +// pretty-expanded FIXME #23616 + +#![feature(fn_traits, unboxed_closures)] + +trait Foo { fn dummy(&self) { }} + +struct Bar; + +impl<'a> std::ops::Fn<(&'a (dyn Foo+'a),)> for Bar { + extern "rust-call" fn call(&self, _: (&'a dyn Foo,)) {} +} + +impl<'a> std::ops::FnMut<(&'a (dyn Foo+'a),)> for Bar { + extern "rust-call" fn call_mut(&mut self, a: (&'a dyn Foo,)) { self.call(a) } +} + +impl<'a> std::ops::FnOnce<(&'a (dyn Foo+'a),)> for Bar { + type Output = (); + extern "rust-call" fn call_once(self, a: (&'a dyn Foo,)) { self.call(a) } +} + +struct Baz; + +impl Foo for Baz {} + +fn main() { + let bar = Bar; + let baz = &Baz; + bar(baz); +} diff --git a/tests/ui/overloaded/overloaded-autoderef-count.rs b/tests/ui/overloaded/overloaded-autoderef-count.rs new file mode 100644 index 000000000..d58deda09 --- /dev/null +++ b/tests/ui/overloaded/overloaded-autoderef-count.rs @@ -0,0 +1,74 @@ +// run-pass +use std::cell::Cell; +use std::ops::{Deref, DerefMut}; + +#[derive(PartialEq)] +struct DerefCounter { + count_imm: Cell, + count_mut: usize, + value: T +} + +impl DerefCounter { + fn new(value: T) -> DerefCounter { + DerefCounter { + count_imm: Cell::new(0), + count_mut: 0, + value: value + } + } + + fn counts(&self) -> (usize, usize) { + (self.count_imm.get(), self.count_mut) + } +} + +impl Deref for DerefCounter { + type Target = T; + + fn deref(&self) -> &T { + self.count_imm.set(self.count_imm.get() + 1); + &self.value + } +} + +impl DerefMut for DerefCounter { + fn deref_mut(&mut self) -> &mut T { + self.count_mut += 1; + &mut self.value + } +} + +#[derive(PartialEq, Debug)] +struct Point { + x: isize, + y: isize +} + +impl Point { + fn get(&self) -> (isize, isize) { + (self.x, self.y) + } +} + +pub fn main() { + let mut p = DerefCounter::new(Point {x: 0, y: 0}); + + let _ = p.x; + assert_eq!(p.counts(), (1, 0)); + + let _ = &p.x; + assert_eq!(p.counts(), (2, 0)); + + let _ = &mut p.y; + assert_eq!(p.counts(), (2, 1)); + + p.x += 3; + assert_eq!(p.counts(), (2, 2)); + + p.get(); + assert_eq!(p.counts(), (3, 2)); + + // Check the final state. + assert_eq!(*p, Point {x: 3, y: 0}); +} diff --git a/tests/ui/overloaded/overloaded-autoderef-indexing.rs b/tests/ui/overloaded/overloaded-autoderef-indexing.rs new file mode 100644 index 000000000..1c8c7cca9 --- /dev/null +++ b/tests/ui/overloaded/overloaded-autoderef-indexing.rs @@ -0,0 +1,20 @@ +// run-pass + +use std::ops::Deref; + +struct DerefArray<'a, T:'a> { + inner: &'a [T] +} + +impl<'a, T> Deref for DerefArray<'a, T> { + type Target = &'a [T]; + + fn deref<'b>(&'b self) -> &'b &'a [T] { + &self.inner + } +} + +pub fn main() { + let a = &[1, 2, 3]; + assert_eq!(DerefArray {inner: a}[1], 2); +} diff --git a/tests/ui/overloaded/overloaded-autoderef-order.rs b/tests/ui/overloaded/overloaded-autoderef-order.rs new file mode 100644 index 000000000..f48bae55f --- /dev/null +++ b/tests/ui/overloaded/overloaded-autoderef-order.rs @@ -0,0 +1,73 @@ +// run-pass + +#![allow(dead_code)] + +use std::rc::Rc; +use std::ops::Deref; + +#[derive(Copy, Clone)] +struct DerefWrapper { + x: X, + y: Y +} + +impl DerefWrapper { + fn get_x(self) -> X { + self.x + } +} + +impl Deref for DerefWrapper { + type Target = Y; + + fn deref(&self) -> &Y { + &self.y + } +} + +mod priv_test { + use std::ops::Deref; + + #[derive(Copy, Clone)] + pub struct DerefWrapperHideX { + x: X, + pub y: Y + } + + impl DerefWrapperHideX { + pub fn new(x: X, y: Y) -> DerefWrapperHideX { + DerefWrapperHideX { + x: x, + y: y + } + } + } + + impl Deref for DerefWrapperHideX { + type Target = Y; + + fn deref(&self) -> &Y { + &self.y + } + } +} + +pub fn main() { + let nested = DerefWrapper {x: true, y: DerefWrapper {x: 0, y: 1}}; + + // Use the first field that you can find. + assert_eq!(nested.x, true); + assert_eq!((*nested).x, 0); + + // Same for methods, even though there are multiple + // candidates (at different nesting levels). + assert_eq!(nested.get_x(), true); + assert_eq!((*nested).get_x(), 0); + + // Also go through multiple levels of indirection. + assert_eq!(Rc::new(nested).x, true); + + let nested_priv = priv_test::DerefWrapperHideX::new(true, DerefWrapper {x: 0, y: 1}); + assert_eq!(nested_priv.x, 0); + assert_eq!((*nested_priv).x, 0); +} diff --git a/tests/ui/overloaded/overloaded-autoderef-vtable.rs b/tests/ui/overloaded/overloaded-autoderef-vtable.rs new file mode 100644 index 000000000..f8e6d1208 --- /dev/null +++ b/tests/ui/overloaded/overloaded-autoderef-vtable.rs @@ -0,0 +1,39 @@ +// run-pass +#![allow(dead_code)] + +use std::ops::Deref; + +struct DerefWithHelper { + helper: H, + value: T +} + +trait Helper { + fn helper_borrow(&self) -> &T; +} + +impl Helper for Option { + fn helper_borrow(&self) -> &T { + self.as_ref().unwrap() + } +} + +impl> Deref for DerefWithHelper { + type Target = T; + + fn deref(&self) -> &T { + self.helper.helper_borrow() + } +} + +struct Foo {x: isize} + +impl Foo { + fn foo(&self) -> isize {self.x} +} + +pub fn main() { + let x: DerefWithHelper, Foo> = + DerefWithHelper { helper: Some(Foo {x: 5}), value: Foo { x: 2 } }; + assert_eq!(x.foo(), 5); +} diff --git a/tests/ui/overloaded/overloaded-autoderef-xcrate.rs b/tests/ui/overloaded/overloaded-autoderef-xcrate.rs new file mode 100644 index 000000000..d065e825c --- /dev/null +++ b/tests/ui/overloaded/overloaded-autoderef-xcrate.rs @@ -0,0 +1,9 @@ +// run-pass +// aux-build:overloaded_autoderef_xc.rs + + +extern crate overloaded_autoderef_xc; + +fn main() { + assert!(overloaded_autoderef_xc::check(5, 5)); +} diff --git a/tests/ui/overloaded/overloaded-autoderef.rs b/tests/ui/overloaded/overloaded-autoderef.rs new file mode 100644 index 000000000..cae3ec906 --- /dev/null +++ b/tests/ui/overloaded/overloaded-autoderef.rs @@ -0,0 +1,43 @@ +// run-pass +#![allow(unused_variables)] +#![allow(stable_features)] + +use std::cell::RefCell; +use std::rc::Rc; + +#[derive(PartialEq, Debug)] +struct Point { + x: isize, + y: isize +} + +pub fn main() { + let box_5: Box<_> = Box::new(5_usize); + let point = Rc::new(Point {x: 2, y: 4}); + assert_eq!(point.x, 2); + assert_eq!(point.y, 4); + + let i = Rc::new(RefCell::new(2)); + let i_value = *i.borrow(); + *i.borrow_mut() = 5; + assert_eq!((i_value, *i.borrow()), (2, 5)); + + let s = Rc::new("foo".to_string()); + assert_eq!(&**s, "foo"); + + let mut_s = Rc::new(RefCell::new(String::from("foo"))); + mut_s.borrow_mut().push_str("bar"); + // HACK assert_eq! would panic here because it stores the LHS and RHS in two locals. + assert_eq!(&**mut_s.borrow(), "foobar"); + assert_eq!(&**mut_s.borrow_mut(), "foobar"); + + let p = Rc::new(RefCell::new(Point {x: 1, y: 2})); + p.borrow_mut().x = 3; + p.borrow_mut().y += 3; + assert_eq!(*p.borrow(), Point {x: 3, y: 5}); + + let v = Rc::new(RefCell::new([1, 2, 3])); + v.borrow_mut()[0] = 3; + v.borrow_mut()[1] += 3; + assert_eq!((v.borrow()[0], v.borrow()[1], v.borrow()[2]), (3, 5, 3)); +} diff --git a/tests/ui/overloaded/overloaded-calls-nontuple.rs b/tests/ui/overloaded/overloaded-calls-nontuple.rs new file mode 100644 index 000000000..32a3b93e0 --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-nontuple.rs @@ -0,0 +1,30 @@ +#![feature(fn_traits, unboxed_closures)] + +use std::ops::FnMut; + +struct S { + x: isize, + y: isize, +} + +impl FnMut for S { + //~^ ERROR type parameter to bare `FnMut` trait must be a tuple + extern "rust-call" fn call_mut(&mut self, z: isize) -> isize { + //~^ ERROR functions with the "rust-call" ABI must take a single non-self tuple argument + self.x + self.y + z + } +} + +impl FnOnce for S { + //~^ ERROR type parameter to bare `FnOnce` trait must be a tuple + type Output = isize; + extern "rust-call" fn call_once(mut self, z: isize) -> isize { + //~^ ERROR functions with the "rust-call" ABI must take a single non-self tuple argument + self.call_mut(z) + } +} + +fn main() { + let mut s = S { x: 1, y: 2 }; + drop(s(3)) +} diff --git a/tests/ui/overloaded/overloaded-calls-nontuple.stderr b/tests/ui/overloaded/overloaded-calls-nontuple.stderr new file mode 100644 index 000000000..2e1600782 --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-nontuple.stderr @@ -0,0 +1,34 @@ +error[E0059]: type parameter to bare `FnMut` trait must be a tuple + --> $DIR/overloaded-calls-nontuple.rs:10:6 + | +LL | impl FnMut for S { + | ^^^^^^^^^^^^ the trait `Tuple` is not implemented for `isize` + | +note: required by a bound in `FnMut` + --> $SRC_DIR/core/src/ops/function.rs:LL:COL + +error[E0059]: type parameter to bare `FnOnce` trait must be a tuple + --> $DIR/overloaded-calls-nontuple.rs:18:6 + | +LL | impl FnOnce for S { + | ^^^^^^^^^^^^^ the trait `Tuple` is not implemented for `isize` + | +note: required by a bound in `FnOnce` + --> $SRC_DIR/core/src/ops/function.rs:LL:COL + +error[E0277]: functions with the "rust-call" ABI must take a single non-self tuple argument + --> $DIR/overloaded-calls-nontuple.rs:12:5 + | +LL | extern "rust-call" fn call_mut(&mut self, z: isize) -> isize { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Tuple` is not implemented for `isize` + +error[E0277]: functions with the "rust-call" ABI must take a single non-self tuple argument + --> $DIR/overloaded-calls-nontuple.rs:21:5 + | +LL | extern "rust-call" fn call_once(mut self, z: isize) -> isize { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Tuple` is not implemented for `isize` + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0059, E0277. +For more information about an error, try `rustc --explain E0059`. diff --git a/tests/ui/overloaded/overloaded-calls-object-one-arg.rs b/tests/ui/overloaded/overloaded-calls-object-one-arg.rs new file mode 100644 index 000000000..1afab9a1f --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-object-one-arg.rs @@ -0,0 +1,13 @@ +// run-pass +// Tests calls to closure arguments where the closure takes 1 argument. +// This is a bit tricky due to rust-call ABI. + + +fn foo(f: &mut dyn FnMut(isize) -> isize) -> isize { + f(22) +} + +fn main() { + let z = foo(&mut |x| x *100); + assert_eq!(z, 2200); +} diff --git a/tests/ui/overloaded/overloaded-calls-object-two-args.rs b/tests/ui/overloaded/overloaded-calls-object-two-args.rs new file mode 100644 index 000000000..38087bc87 --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-object-two-args.rs @@ -0,0 +1,13 @@ +// run-pass +// Tests calls to closure arguments where the closure takes 2 arguments. +// This is a bit tricky due to rust-call ABI. + + +fn foo(f: &mut dyn FnMut(isize, isize) -> isize) -> isize { + f(1, 2) +} + +fn main() { + let z = foo(&mut |x, y| x * 10 + y); + assert_eq!(z, 12); +} diff --git a/tests/ui/overloaded/overloaded-calls-object-zero-args.rs b/tests/ui/overloaded/overloaded-calls-object-zero-args.rs new file mode 100644 index 000000000..9a7bfaa9b --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-object-zero-args.rs @@ -0,0 +1,13 @@ +// run-pass +// Tests calls to closure arguments where the closure takes 0 arguments. +// This is a bit tricky due to rust-call ABI. + + +fn foo(f: &mut dyn FnMut() -> isize) -> isize { + f() +} + +fn main() { + let z = foo(&mut || 22); + assert_eq!(z, 22); +} diff --git a/tests/ui/overloaded/overloaded-calls-param-vtables.rs b/tests/ui/overloaded/overloaded-calls-param-vtables.rs new file mode 100644 index 000000000..fde1ad20f --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-param-vtables.rs @@ -0,0 +1,32 @@ +// run-pass +// Tests that nested vtables work with overloaded calls. + +// pretty-expanded FIXME #23616 + +#![feature(unboxed_closures, fn_traits)] + +use std::marker::PhantomData; +use std::ops::Fn; +use std::ops::Add; + +struct G(PhantomData); + +impl<'a, A: Add> Fn<(A,)> for G { + extern "rust-call" fn call(&self, (arg,): (A,)) -> i32 { + arg.add(1) + } +} + +impl<'a, A: Add> FnMut<(A,)> for G { + extern "rust-call" fn call_mut(&mut self, args: (A,)) -> i32 { self.call(args) } +} + +impl<'a, A: Add> FnOnce<(A,)> for G { + type Output = i32; + extern "rust-call" fn call_once(self, args: (A,)) -> i32 { self.call(args) } +} + +fn main() { + // ICE trigger + (G(PhantomData))(1); +} diff --git a/tests/ui/overloaded/overloaded-calls-simple.rs b/tests/ui/overloaded/overloaded-calls-simple.rs new file mode 100644 index 000000000..413183607 --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-simple.rs @@ -0,0 +1,78 @@ +// run-pass + +#![feature(lang_items, unboxed_closures, fn_traits)] + +use std::ops::{Fn, FnMut, FnOnce}; + +struct S1 { + x: i32, + y: i32, +} + +impl FnMut<(i32,)> for S1 { + extern "rust-call" fn call_mut(&mut self, (z,): (i32,)) -> i32 { + self.x * self.y * z + } +} + +impl FnOnce<(i32,)> for S1 { + type Output = i32; + extern "rust-call" fn call_once(mut self, args: (i32,)) -> i32 { + self.call_mut(args) + } +} + +struct S2 { + x: i32, + y: i32, +} + +impl Fn<(i32,)> for S2 { + extern "rust-call" fn call(&self, (z,): (i32,)) -> i32 { + self.x * self.y * z + } +} + +impl FnMut<(i32,)> for S2 { + extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> i32 { self.call(args) } +} + +impl FnOnce<(i32,)> for S2 { + type Output = i32; + extern "rust-call" fn call_once(self, args: (i32,)) -> i32 { self.call(args) } +} + +struct S3 { + x: i32, + y: i32, +} + +impl FnOnce<(i32,i32)> for S3 { + type Output = i32; + extern "rust-call" fn call_once(self, (z,zz): (i32,i32)) -> i32 { + self.x * self.y * z * zz + } +} + +fn main() { + let mut s = S1 { + x: 3, + y: 3, + }; + let ans = s(3); + + assert_eq!(ans, 27); + let s = S2 { + x: 3, + y: 3, + }; + let ans = s.call((3,)); + assert_eq!(ans, 27); + + let s = S3 { + x: 3, + y: 3, + }; + let ans = s(3, 1); + assert_eq!(ans, 27); +} diff --git a/tests/ui/overloaded/overloaded-calls-zero-args.rs b/tests/ui/overloaded/overloaded-calls-zero-args.rs new file mode 100644 index 000000000..69ca88619 --- /dev/null +++ b/tests/ui/overloaded/overloaded-calls-zero-args.rs @@ -0,0 +1,30 @@ +// run-pass + +#![feature(unboxed_closures, fn_traits)] + +use std::ops::FnMut; + +struct S { + x: i32, + y: i32, +} + +impl FnMut<()> for S { + extern "rust-call" fn call_mut(&mut self, (): ()) -> i32 { + self.x * self.y + } +} + +impl FnOnce<()> for S { + type Output = i32; + extern "rust-call" fn call_once(mut self, args: ()) -> i32 { self.call_mut(args) } +} + +fn main() { + let mut s = S { + x: 3, + y: 3, + }; + let ans = s(); + assert_eq!(ans, 9); +} diff --git a/tests/ui/overloaded/overloaded-deref-count.rs b/tests/ui/overloaded/overloaded-deref-count.rs new file mode 100644 index 000000000..e2f1e10b5 --- /dev/null +++ b/tests/ui/overloaded/overloaded-deref-count.rs @@ -0,0 +1,78 @@ +// run-pass + +use std::cell::Cell; +use std::ops::{Deref, DerefMut}; +use std::vec::Vec; + +struct DerefCounter { + count_imm: Cell, + count_mut: usize, + value: T +} + +impl DerefCounter { + fn new(value: T) -> DerefCounter { + DerefCounter { + count_imm: Cell::new(0), + count_mut: 0, + value: value + } + } + + fn counts(&self) -> (usize, usize) { + (self.count_imm.get(), self.count_mut) + } +} + +impl Deref for DerefCounter { + type Target = T; + + fn deref(&self) -> &T { + self.count_imm.set(self.count_imm.get() + 1); + &self.value + } +} + +impl DerefMut for DerefCounter { + fn deref_mut(&mut self) -> &mut T { + self.count_mut += 1; + &mut self.value + } +} + +pub fn main() { + let mut n = DerefCounter::new(0); + let mut v = DerefCounter::new(Vec::new()); + + let _ = *n; // Immutable deref + copy a POD. + assert_eq!(n.counts(), (1, 0)); + + let _ = (&*n, &*v); // Immutable deref + borrow. + assert_eq!(n.counts(), (2, 0)); assert_eq!(v.counts(), (1, 0)); + + let _ = (&mut *n, &mut *v); // Mutable deref + mutable borrow. + assert_eq!(n.counts(), (2, 1)); assert_eq!(v.counts(), (1, 1)); + + let mut v2 = Vec::new(); + v2.push(1); + + *n = 5; *v = v2; // Mutable deref + assignment. + assert_eq!(n.counts(), (2, 2)); assert_eq!(v.counts(), (1, 2)); + + *n -= 3; // Mutable deref + assignment with binary operation. + assert_eq!(n.counts(), (2, 3)); + + // Immutable deref used for calling a method taking &self. (The + // typechecker is smarter now about doing this.) + (*n).to_string(); + assert_eq!(n.counts(), (3, 3)); + + // Mutable deref used for calling a method taking &mut self. + (*v).push(2); + assert_eq!(v.counts(), (1, 3)); + + // Check the final states. + assert_eq!(*n, 2); + let expected: &[_] = &[1, 2]; + assert_eq!((*v), expected); +} diff --git a/tests/ui/overloaded/overloaded-deref.rs b/tests/ui/overloaded/overloaded-deref.rs new file mode 100644 index 000000000..73d8232a3 --- /dev/null +++ b/tests/ui/overloaded/overloaded-deref.rs @@ -0,0 +1,43 @@ +// run-pass +use std::cell::RefCell; +use std::rc::Rc; +use std::string::String; + +#[derive(PartialEq, Debug)] +struct Point { + x: isize, + y: isize +} + +pub fn main() { + assert_eq!(*Rc::new(5), 5); + assert_eq!(***Rc::new(Box::new(Box::new(5))), 5); + assert_eq!(*Rc::new(Point {x: 2, y: 4}), Point {x: 2, y: 4}); + + let i = Rc::new(RefCell::new(2)); + let i_value = *(*i).borrow(); + *(*i).borrow_mut() = 5; + assert_eq!((i_value, *(*i).borrow()), (2, 5)); + + let s = Rc::new("foo".to_string()); + assert_eq!(*s, "foo".to_string()); + assert_eq!((*s), "foo"); + + let mut_s = Rc::new(RefCell::new(String::from("foo"))); + (*(*mut_s).borrow_mut()).push_str("bar"); + // assert_eq! would panic here because it stores the LHS and RHS in two locals. + assert_eq!((*(*mut_s).borrow()), "foobar"); + assert_eq!((*(*mut_s).borrow_mut()), "foobar"); + + let p = Rc::new(RefCell::new(Point {x: 1, y: 2})); + (*(*p).borrow_mut()).x = 3; + (*(*p).borrow_mut()).y += 3; + assert_eq!(*(*p).borrow(), Point {x: 3, y: 5}); + + let v = Rc::new(RefCell::new(vec![1, 2, 3])); + (*(*v).borrow_mut())[0] = 3; + (*(*v).borrow_mut())[1] += 3; + assert_eq!(((*(*v).borrow())[0], + (*(*v).borrow())[1], + (*(*v).borrow())[2]), (3, 5, 3)); +} diff --git a/tests/ui/overloaded/overloaded-index-assoc-list.rs b/tests/ui/overloaded/overloaded-index-assoc-list.rs new file mode 100644 index 000000000..eb027afea --- /dev/null +++ b/tests/ui/overloaded/overloaded-index-assoc-list.rs @@ -0,0 +1,48 @@ +// run-pass +// Test overloading of the `[]` operator. In particular test that it +// takes its argument *by reference*. + +use std::ops::Index; + +struct AssociationList { + pairs: Vec> } + +#[derive(Clone)] +struct AssociationPair { + key: K, + value: V +} + +impl AssociationList { + fn push(&mut self, key: K, value: V) { + self.pairs.push(AssociationPair {key: key, value: value}); + } +} + +impl<'a, K: PartialEq + std::fmt::Debug, V:Clone> Index<&'a K> for AssociationList { + type Output = V; + + fn index(&self, index: &K) -> &V { + for pair in &self.pairs { + if pair.key == *index { + return &pair.value + } + } + panic!("No value found for key: {:?}", index); + } +} + +pub fn main() { + let foo = "foo".to_string(); + let bar = "bar".to_string(); + + let mut list = AssociationList {pairs: Vec::new()}; + list.push(foo.clone(), 22); + list.push(bar.clone(), 44); + + assert_eq!(list[&foo], 22); + assert_eq!(list[&bar], 44); + + assert_eq!(list[&foo], 22); + assert_eq!(list[&bar], 44); +} diff --git a/tests/ui/overloaded/overloaded-index-autoderef.rs b/tests/ui/overloaded/overloaded-index-autoderef.rs new file mode 100644 index 000000000..41f9efa8c --- /dev/null +++ b/tests/ui/overloaded/overloaded-index-autoderef.rs @@ -0,0 +1,75 @@ +// run-pass +#![allow(stable_features)] + +// Test overloaded indexing combined with autoderef. + +use std::ops::{Index, IndexMut}; + +struct Foo { + x: isize, + y: isize, +} + +impl Index for Foo { + type Output = isize; + + fn index(&self, z: isize) -> &isize { + if z == 0 { + &self.x + } else { + &self.y + } + } +} + +impl IndexMut for Foo { + fn index_mut(&mut self, z: isize) -> &mut isize { + if z == 0 { + &mut self.x + } else { + &mut self.y + } + } +} + +trait Int { + fn get(self) -> isize; + fn get_from_ref(&self) -> isize; + fn inc(&mut self); +} + +impl Int for isize { + fn get(self) -> isize { self } + fn get_from_ref(&self) -> isize { *self } + fn inc(&mut self) { *self += 1; } +} + +fn main() { + let mut f: Box<_> = Box::new(Foo { + x: 1, + y: 2, + }); + + assert_eq!(f[1], 2); + + f[0] = 3; + + assert_eq!(f[0], 3); + + // Test explicit IndexMut where `f` must be autoderef: + { + let p = &mut f[1]; + *p = 4; + } + + // Test explicit Index where `f` must be autoderef: + { + let p = &f[1]; + assert_eq!(*p, 4); + } + + // Test calling methods with `&mut self`, `self, and `&self` receivers: + f[1].inc(); + assert_eq!(f[1].get(), 5); + assert_eq!(f[1].get_from_ref(), 5); +} diff --git a/tests/ui/overloaded/overloaded-index-in-field.rs b/tests/ui/overloaded/overloaded-index-in-field.rs new file mode 100644 index 000000000..8a1fa7deb --- /dev/null +++ b/tests/ui/overloaded/overloaded-index-in-field.rs @@ -0,0 +1,46 @@ +// run-pass +// Test using overloaded indexing when the "map" is stored in a +// field. This caused problems at some point. + +use std::ops::Index; + +struct Foo { + x: isize, + y: isize, +} + +struct Bar { + foo: Foo +} + +impl Index for Foo { + type Output = isize; + + fn index(&self, z: isize) -> &isize { + if z == 0 { + &self.x + } else { + &self.y + } + } +} + +trait Int { + fn get(self) -> isize; + fn get_from_ref(&self) -> isize; + fn inc(&mut self); +} + +impl Int for isize { + fn get(self) -> isize { self } + fn get_from_ref(&self) -> isize { *self } + fn inc(&mut self) { *self += 1; } +} + +fn main() { + let f = Bar { foo: Foo { + x: 1, + y: 2, + } }; + assert_eq!(f.foo[1].get(), 2); +} diff --git a/tests/ui/overloaded/overloaded-index.rs b/tests/ui/overloaded/overloaded-index.rs new file mode 100644 index 000000000..5ad6d2e70 --- /dev/null +++ b/tests/ui/overloaded/overloaded-index.rs @@ -0,0 +1,64 @@ +// run-pass +use std::ops::{Index, IndexMut}; + +struct Foo { + x: isize, + y: isize, +} + +impl Index for Foo { + type Output = isize; + + fn index(&self, z: isize) -> &isize { + if z == 0 { + &self.x + } else { + &self.y + } + } +} + +impl IndexMut for Foo { + fn index_mut(&mut self, z: isize) -> &mut isize { + if z == 0 { + &mut self.x + } else { + &mut self.y + } + } +} + +trait Int { + fn get(self) -> isize; + fn get_from_ref(&self) -> isize; + fn inc(&mut self); +} + +impl Int for isize { + fn get(self) -> isize { self } + fn get_from_ref(&self) -> isize { *self } + fn inc(&mut self) { *self += 1; } +} + +fn main() { + let mut f = Foo { + x: 1, + y: 2, + }; + assert_eq!(f[1], 2); + f[0] = 3; + assert_eq!(f[0], 3); + { + let p = &mut f[1]; + *p = 4; + } + { + let p = &f[1]; + assert_eq!(*p, 4); + } + + // Test calling methods with `&mut self`, `self, and `&self` receivers: + f[1].inc(); + assert_eq!(f[1].get(), 5); + assert_eq!(f[1].get_from_ref(), 5); +} diff --git a/tests/ui/overloaded/overloaded_deref_with_ref_pattern.rs b/tests/ui/overloaded/overloaded_deref_with_ref_pattern.rs new file mode 100644 index 000000000..c87ba6a02 --- /dev/null +++ b/tests/ui/overloaded/overloaded_deref_with_ref_pattern.rs @@ -0,0 +1,95 @@ +// run-pass +#![allow(unused_mut)] +#![allow(unused_variables)] +// Test that we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609). + +use std::ops::{Deref, DerefMut}; + +struct DerefOk(T); +struct DerefMutOk(T); + +impl Deref for DerefOk { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for DerefOk { + fn deref_mut(&mut self) -> &mut Self::Target { + panic!() + } +} + +impl Deref for DerefMutOk { + type Target = T; + fn deref(&self) -> &Self::Target { + panic!() + } +} + +impl DerefMut for DerefMutOk { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +fn main() { + // Check that mutable ref binding in match picks DerefMut + let mut b = DerefMutOk(0); + match *b { + ref mut n => n, + }; + + // Check that mutable ref binding in let picks DerefMut + let mut y = DerefMutOk(1); + let ref mut z = *y; + + // Check that immutable ref binding in match picks Deref + let mut b = DerefOk(2); + match *b { + ref n => n, + }; + + // Check that immutable ref binding in let picks Deref + let mut y = DerefOk(3); + let ref z = *y; + + // Check that mixed mutable/immutable ref binding in match picks DerefMut + let mut b = DerefMutOk((0, 9)); + match *b { + (ref mut n, ref m) => (n, m), + }; + + let mut b = DerefMutOk((0, 9)); + match *b { + (ref n, ref mut m) => (n, m), + }; + + // Check that mixed mutable/immutable ref binding in let picks DerefMut + let mut y = DerefMutOk((1, 8)); + let (ref mut z, ref a) = *y; + + let mut y = DerefMutOk((1, 8)); + let (ref z, ref mut a) = *y; + + // Check that multiple immutable ref bindings in match picks Deref + let mut b = DerefOk((2, 7)); + match *b { + (ref n, ref m) => (n, m), + }; + + // Check that multiple immutable ref bindings in let picks Deref + let mut y = DerefOk((3, 6)); + let (ref z, ref a) = *y; + + // Check that multiple mutable ref bindings in match picks DerefMut + let mut b = DerefMutOk((4, 5)); + match *b { + (ref mut n, ref mut m) => (n, m), + }; + + // Check that multiple mutable ref bindings in let picks DerefMut + let mut y = DerefMutOk((5, 4)); + let (ref mut z, ref mut a) = *y; +} diff --git a/tests/ui/overloaded/overloaded_deref_with_ref_pattern_issue15609.rs b/tests/ui/overloaded/overloaded_deref_with_ref_pattern_issue15609.rs new file mode 100644 index 000000000..61edd2ace --- /dev/null +++ b/tests/ui/overloaded/overloaded_deref_with_ref_pattern_issue15609.rs @@ -0,0 +1,37 @@ +// run-pass +#![allow(dead_code)] +#![allow(unused_variables)] +// Test that we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609). + +fn main() { + use std::cell::RefCell; + + struct S { + node: E, + } + + enum E { + Foo(u32), + Bar, + } + + // Check match + let x = RefCell::new(S { node: E::Foo(0) }); + + let mut b = x.borrow_mut(); + match b.node { + E::Foo(ref mut n) => *n += 1, + _ => (), + } + + // Check let + let x = RefCell::new(0); + let mut y = x.borrow_mut(); + let ref mut z = *y; + + fn foo(a: &mut RefCell>) { + if let Some(ref mut s) = *a.borrow_mut() { + s.push('a') + } + } +} -- cgit v1.2.3