6d8d79595e
The RFC for arbitrary self types v2 declares that we should reject
"generic" self types. This commit does so.
The definition of "generic" was unclear in the RFC, but has been
explored in
https://github.com/rust-lang/rust/issues/129147
and the conclusion is that "generic" means any `self` type which
is a type parameter defined on the method itself, or references
to such a type.
This approach was chosen because other definitions of "generic"
don't work. Specifically,
* we can't filter out generic type _arguments_, because that would
filter out Rc<Self> and all the other types of smart pointer
we want to support;
* we can't filter out all type params, because Self itself is a
type param, and because existing Rust code depends on other
type params declared on the type (as opposed to the method).
This PR decides to make a new error code for this case, instead of
reusing the existing E0307 error. This makes the code a
bit more complex, but it seems we have an opportunity to provide
specific diagnostics for this case so we should do so.
This PR filters out generic self types whether or not the
'arbitrary self types' feature is enabled. However, it's believed
that it can't have any effect on code which uses stable Rust, since
there are no stable traits which can be used to indicate a valid
generic receiver type, and thus it would have been impossible to
write code which could trigger this new error case.
It is however possible that this could break existing code which
uses either of the unstable `arbitrary_self_types` or
`receiver_trait` features. This breakage is intentional; as
we move arbitrary self types towards stabilization we don't want
to continue to support generic such types.
This PR adds lots of extra tests to arbitrary-self-from-method-substs.
Most of these are ways to trigger a "type mismatch" error which
9b82580c73/compiler/rustc_hir_typeck/src/method/confirm.rs (L519)
hopes can be minimized by filtering out generics in this way.
We remove a FIXME from confirm.rs suggesting that we make this change.
It's still possible to cause type mismatch errors, and a subsequent
PR may be able to improve diagnostics in this area, but it's harder
to cause these errors without contrived uses of the turbofish.
This is a part of the arbitrary self types v2 project,
https://github.com/rust-lang/rfcs/pull/3519
https://github.com/rust-lang/rust/issues/44874
r? @wesleywiser
111 lines
3.2 KiB
Rust
111 lines
3.2 KiB
Rust
//@ revisions: default feature
|
|
#![cfg_attr(feature, feature(arbitrary_self_types))]
|
|
|
|
use std::ops::Deref;
|
|
use std::marker::PhantomData;
|
|
|
|
struct Foo(u32);
|
|
impl Foo {
|
|
fn get<R: Deref<Target = Self>>(self: R) -> u32 {
|
|
//~^ ERROR: invalid generic `self` parameter type
|
|
self.0
|
|
}
|
|
fn get1<R: Deref<Target = Self>>(self: &R) -> u32 {
|
|
//~^ ERROR: invalid generic `self` parameter type
|
|
self.0
|
|
}
|
|
fn get2<R: Deref<Target = Self>>(self: &mut R) -> u32 {
|
|
//~^ ERROR: invalid generic `self` parameter type
|
|
self.0
|
|
}
|
|
fn get3<R: Deref<Target = Self>>(self: std::rc::Rc<R>) -> u32 {
|
|
//~^ ERROR: invalid generic `self` parameter type
|
|
self.0
|
|
}
|
|
fn get4<R: Deref<Target = Self>>(self: &std::rc::Rc<R>) -> u32 {
|
|
//~^ ERROR: invalid generic `self` parameter type
|
|
self.0
|
|
}
|
|
fn get5<R: Deref<Target = Self>>(self: std::rc::Rc<&R>) -> u32 {
|
|
//~^ ERROR: invalid generic `self` parameter type
|
|
self.0
|
|
}
|
|
fn get6<FR: FindReceiver>(self: FR::Receiver, other: FR) -> u32 {
|
|
//[default]~^ ERROR: `<FR as FindReceiver>::Receiver` cannot be used as the type of `self`
|
|
42
|
|
}
|
|
}
|
|
|
|
|
|
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 Bar<R>(std::marker::PhantomData<R>);
|
|
|
|
impl<R: std::ops::Deref<Target = Self>> Bar<R> {
|
|
fn get(self: R) {}
|
|
//[default]~^ ERROR: `R` cannot be used as the type of `self`
|
|
}
|
|
|
|
trait FindReceiver {
|
|
type Receiver: Deref<Target = Foo>;
|
|
}
|
|
|
|
struct Silly;
|
|
impl FindReceiver for Silly {
|
|
type Receiver = std::rc::Rc<Foo>;
|
|
}
|
|
|
|
fn main() {
|
|
let mut foo = Foo(1);
|
|
foo.get::<&Foo>();
|
|
//[feature]~^ ERROR mismatched types
|
|
foo.get::<std::rc::Rc<Foo>>();
|
|
//[feature]~^ ERROR mismatched types
|
|
|
|
let smart_ptr = SmartPtr(&foo);
|
|
let smart_ptr2 = SmartPtr2(&foo);
|
|
smart_ptr.get(); // this compiles
|
|
smart_ptr.get::<SmartPtr2<Foo>>();
|
|
//[feature]~^ ERROR mismatched types
|
|
smart_ptr.get::<&Foo>();
|
|
//[feature]~^ ERROR mismatched types
|
|
|
|
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.get6(Silly);
|
|
//~^ ERROR type mismatch
|
|
let mut foo = Foo(1);
|
|
let foo = &foo;
|
|
foo.get6(Silly);
|
|
//~^ ERROR type mismatch
|
|
|
|
let t = std::rc::Rc::new(Bar(std::marker::PhantomData));
|
|
t.get();
|
|
//~^ ERROR its trait bounds were not satisfied
|
|
let t = &t;
|
|
// This is a further attempt at triggering 'type mismatch' errors
|
|
// from arbitrary self types without resorting to the turbofish.
|
|
// Ideally, here, t is Thing<Rc<Target=Self>> while we're going to call
|
|
// it with a &t method receiver. However, this doesn't work since that
|
|
// type of t becomes recursive and trait bounds can't be satisfied.
|
|
t.get();
|
|
//~^ ERROR its trait bounds were not satisfied
|
|
}
|