From 3c0de6394b934bd1228dff171d158992e5889bf3 Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Mon, 2 Sep 2024 12:52:39 +0000 Subject: [PATCH] Arbitrary self types: tests for 'mismatched types' Adding tests for cases where diagnostics aren't as good as they should be in the case of arbitrary self types. These are slightly duplicative of the existing arbitrary-self-from-method-substs.rs test, but test more permutations so it seems worth adding to the coverage as we explore improving the diagnostics here. Improved diagnostics were suggested in commit 05c5caa5002297d2e3ba31f2ead7556a732a2289 This is a part of the arbitrary self types v2 project, https://github.com/rust-lang/rfcs/pull/3519 and specifically the sub-issue exploring questions around generics, https://github.com/rust-lang/rust/issues/129147 --- ...-method-substs-no-turbofish.default.stderr | 29 ++++++++++++++ ...-method-substs-no-turbofish.feature.stderr | 17 +++++++++ ...ry-self-from-method-substs-no-turbofish.rs | 38 +++++++++++++++++++ .../self/arbitrary_self_types_mismatched.rs | 38 +++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.default.stderr create mode 100644 tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.feature.stderr create mode 100644 tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.rs create mode 100644 tests/ui/self/arbitrary_self_types_mismatched.rs diff --git a/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.default.stderr b/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.default.stderr new file mode 100644 index 0000000000000..9acb45ebffd47 --- /dev/null +++ b/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.default.stderr @@ -0,0 +1,29 @@ +error[E0658]: `::Receiver` cannot be used as the type of `self` without the `arbitrary_self_types` feature + --> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:13:36 + | +LL | fn get(self: FR::Receiver, other: FR) -> u32 { + | ^^^^^^^^^^^^ + | + = note: see issue #44874 for more information + = help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + = help: consider changing to `self`, `&self`, `&mut self`, `self: Box`, `self: Rc`, `self: Arc`, or `self: Pin

` (where P is one of the previous types except `Self`) + +error[E0271]: type mismatch resolving `::Receiver == Foo` + --> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:36:9 + | +LL | foo.get(Silly); + | ^^^ type mismatch resolving `::Receiver == Foo` + | +note: expected this to be `SmartPtr` + --> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21 + | +LL | type Receiver = SmartPtr; + | ^^^^^^^^^^^^^ + = note: expected struct `SmartPtr` + found struct `Foo` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0271, E0658. +For more information about an error, try `rustc --explain E0271`. diff --git a/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.feature.stderr b/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.feature.stderr new file mode 100644 index 0000000000000..1db83f0dceac0 --- /dev/null +++ b/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.feature.stderr @@ -0,0 +1,17 @@ +error[E0271]: type mismatch resolving `::Receiver == Foo` + --> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:36:9 + | +LL | foo.get(Silly); + | ^^^ type mismatch resolving `::Receiver == Foo` + | +note: expected this to be `SmartPtr` + --> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21 + | +LL | type Receiver = SmartPtr; + | ^^^^^^^^^^^^^ + = note: expected struct `SmartPtr` + found struct `Foo` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.rs b/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.rs new file mode 100644 index 0000000000000..27edc961abd7d --- /dev/null +++ b/tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.rs @@ -0,0 +1,38 @@ +//@ revisions: default feature +#![cfg_attr(feature, feature(arbitrary_self_types))] + +use std::ops::Deref; +use std::marker::PhantomData; + +trait FindReceiver { + type Receiver: Deref; +} + +struct Foo(u32); +impl Foo { + fn get(self: FR::Receiver, other: FR) -> u32 { + //[default]~^ ERROR: `::Receiver` cannot be used as the type of `self` + 42 + } +} + +struct SmartPtr(T); +impl Deref for SmartPtr { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +struct Silly; +impl FindReceiver for Silly { + type Receiver = SmartPtr; +} + +fn main() { + let mut foo = Foo(1); + // This test is slightly contrived in an attempt to generate a mismatched types + // error for the self type below, without using the turbofish. + foo.get(Silly); + //~^ ERROR type mismatch +} diff --git a/tests/ui/self/arbitrary_self_types_mismatched.rs b/tests/ui/self/arbitrary_self_types_mismatched.rs new file mode 100644 index 0000000000000..6c1819513e309 --- /dev/null +++ b/tests/ui/self/arbitrary_self_types_mismatched.rs @@ -0,0 +1,38 @@ +#![feature(arbitrary_self_types)] + +use std::ops::Deref; + +struct SmartPtr<'a, T: ?Sized>(&'a T); + +impl<'a, T: ?Sized> Deref for SmartPtr<'a, T> { + type Target = T; + fn deref(&self) -> &Self::Target { + unimplemented!() + } +} + +struct SmartPtr2<'a, T: ?Sized>(&'a T); + +impl<'a, T: ?Sized> Deref for SmartPtr2<'a, T> { + type Target = T; + fn deref(&self) -> &Self::Target { + unimplemented!() + } +} + + +struct Foo(u32); +impl Foo { + fn a>(self: R) { } +} + +fn main() { + let foo = Foo(1); + let smart_ptr = SmartPtr(&foo); + let smart_ptr2 = SmartPtr2(&foo); + smart_ptr.a(); // this compiles + smart_ptr.a::>(); + //~^ ERROR mismatched types + smart_ptr.a::<&Foo>(); + //~^ ERROR mismatched types +}