forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rollup merge of rust-lang#22452: nikomatsakis/issue-22040-18956-Self
The big change here is that we update the object-safety rules to prohibit references to `Self` in the supertrait listing. See rust-lang#22040 for the motivation. The other change is to handle the interaction of defaults that reference `Self` and object types (where `Self` is erased). We force users to give an explicit type in that scenario. r? @aturon
- Loading branch information
Showing
9 changed files
with
251 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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. | ||
|
||
// Regression test for #22040. | ||
|
||
use std::fmt::Debug; | ||
|
||
trait Expr: Debug + PartialEq { | ||
fn print_element_count(&self); | ||
} | ||
|
||
//#[derive(PartialEq)] | ||
#[derive(Debug)] | ||
struct SExpr<'x> { | ||
elements: Vec<Box<Expr+ 'x>>, | ||
} | ||
|
||
impl<'x> PartialEq for SExpr<'x> { | ||
fn eq(&self, other:&SExpr<'x>) -> bool { | ||
println!("L1: {} L2: {}", self.elements.len(), other.elements.len()); | ||
let result = self.elements.len() == other.elements.len(); | ||
|
||
println!("Got compare {}", result); | ||
return result; | ||
} | ||
} | ||
|
||
impl <'x> SExpr<'x> { | ||
fn new() -> SExpr<'x> { return SExpr{elements: Vec::new(),}; } | ||
} | ||
|
||
impl <'x> Expr for SExpr<'x> { | ||
fn print_element_count(&self) { | ||
println!("element count: {}", self.elements.len()); | ||
} | ||
} | ||
|
||
fn main() { | ||
let a: Box<Expr> = Box::new(SExpr::new()); //~ ERROR trait `Expr` is not object-safe | ||
let b: Box<Expr> = Box::new(SExpr::new()); //~ ERROR trait `Expr` is not object-safe | ||
|
||
assert_eq!(a , b); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/compile-fail/object-safety-supertrait-mentions-Self.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2014 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. | ||
|
||
// Check that we correctly prevent users from making trait objects | ||
// form traits that make use of `Self` in an argument or return position. | ||
|
||
trait Bar<T> { | ||
fn bar(&self, x: &T); | ||
} | ||
|
||
trait Baz : Bar<Self> { | ||
} | ||
|
||
fn make_bar<T:Bar<u32>>(t: &T) -> &Bar<u32> { | ||
t | ||
} | ||
|
||
fn make_baz<T:Baz>(t: &T) -> &Baz { | ||
t | ||
//~^ ERROR `Baz` is not object-safe | ||
//~| NOTE the trait cannot use `Self` as a type parameter in the supertrait listing | ||
} | ||
|
||
fn main() { | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/compile-fail/type-parameter-defaults-referencing-Self-ppaux.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 a default that references `Self` which is then used in an | ||
// object type. Issue #18956. In this case, the value is supplied by | ||
// the user, but pretty-printing the type during the error message | ||
// caused an ICE. | ||
|
||
trait MyAdd<Rhs=Self> { fn add(&self, other: &Rhs) -> Self; } | ||
|
||
impl MyAdd for i32 { | ||
fn add(&self, other: &i32) -> i32 { *self + *other } | ||
} | ||
|
||
fn main() { | ||
let x = 5; | ||
let y = x as MyAdd<i32>; | ||
//~^ ERROR as `MyAdd<i32>` | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/compile-fail/type-parameter-defaults-referencing-Self.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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 a default that references `Self` which is then used in an object type. | ||
// Issue #18956. | ||
|
||
#![feature(default_type_params)] | ||
|
||
trait Foo<T=Self> { | ||
fn method(&self); | ||
} | ||
|
||
fn foo(x: &Foo) { } | ||
//~^ ERROR the type parameter `T` must be explicitly specified | ||
|
||
fn main() { } |