Check uses of Self in impls in the compiler rather than during expansion
Closes #23909
This commit is contained in:
parent
5e30f05a05
commit
dc8a8e9beb
11 changed files with 336 additions and 229 deletions
32
src/test/compile-fail/self-impl.rs
Normal file
32
src/test/compile-fail/self-impl.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that unsupported uses of `Self` in impls don't crash
|
||||
|
||||
struct Bar;
|
||||
|
||||
trait Foo {
|
||||
type Baz;
|
||||
}
|
||||
|
||||
impl Foo for Bar {
|
||||
type Baz = bool;
|
||||
}
|
||||
|
||||
impl Bar {
|
||||
fn f() {
|
||||
let _: <Self>::Baz = true;
|
||||
//~^ERROR: ambiguous associated type; specify the type using the syntax `<Bar as Trait>::Baz`
|
||||
let _: Self::Baz = true;
|
||||
//~^ERROR: ambiguous associated type; specify the type using the syntax `<Bar as Trait>::Baz`
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -22,6 +22,17 @@ impl Foo {
|
|||
fn foo(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
|
||||
Foo
|
||||
}
|
||||
|
||||
fn baz() {
|
||||
// Test that Self cannot be shadowed.
|
||||
type Foo = i32;
|
||||
// There is no empty method on i32.
|
||||
Self::empty();
|
||||
|
||||
let _: Self = Foo;
|
||||
}
|
||||
|
||||
fn empty() {}
|
||||
}
|
||||
|
||||
// Test uses when implementing a trait and with a type parameter.
|
||||
|
|
@ -30,12 +41,18 @@ pub struct Baz<X> {
|
|||
}
|
||||
|
||||
trait Bar<X> {
|
||||
type Qux;
|
||||
|
||||
fn bar(x: Self, y: &Self, z: Box<Self>) -> Self;
|
||||
fn dummy(&self, x: X) { }
|
||||
}
|
||||
|
||||
impl Bar<isize> for Box<Baz<isize>> {
|
||||
type Qux = i32;
|
||||
|
||||
fn bar(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
|
||||
let _: Self::Qux = 42;
|
||||
let _: <Self as Bar<isize>>::Qux = 42;
|
||||
box Baz { f: 42 }
|
||||
}
|
||||
}
|
||||
|
|
@ -43,6 +60,6 @@ impl Bar<isize> for Box<Baz<isize>> {
|
|||
fn main() {
|
||||
let _: Foo = Foo::foo(Foo, &Foo, box Foo);
|
||||
let _: Box<Baz<isize>> = Bar::bar(box Baz { f: 42 },
|
||||
&box Baz { f: 42 },
|
||||
box box Baz { f: 42 });
|
||||
&box Baz { f: 42 },
|
||||
box box Baz { f: 42 });
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue