Skip to content

Commit da4dcad

Browse files
committed
Fallback: Tests to clarify defaults for Self in trait impls.
1 parent 91da0cc commit da4dcad

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(default_type_parameter_fallback)]
12+
13+
use std::fmt::Debug;
14+
15+
trait B {
16+
fn b(&self) -> Self;
17+
}
18+
19+
// Strangely, having a default for `Self`
20+
// will fail for fundamental types such as `T` or `Box<T>`.
21+
// Replace `Option` for `Box` in this test to reproduce.
22+
impl<T=String> B for Option<T>
23+
where T: Default
24+
{
25+
fn b(&self) -> Option<T> {
26+
Some(T::default())
27+
}
28+
}
29+
30+
fn main() {
31+
let x = None;
32+
foo(x.b());
33+
}
34+
35+
fn foo<T=String>(a: Option<T>)
36+
where T: Debug + PartialEq<&'static str> {
37+
assert_eq!(a.unwrap(), "");
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
// compile-flags: --error-format=human
11+
12+
#![feature(default_type_parameter_fallback)]
13+
14+
use std::fmt::Debug;
15+
16+
trait B {
17+
fn b(&self) -> Self;
18+
}
19+
20+
impl<T=String> B for Option<T>
21+
where T: Default
22+
{
23+
fn b(&self) -> Option<T> {
24+
Some(T::default())
25+
}
26+
}
27+
28+
fn main() {
29+
let x = None;
30+
foo(x.b());
31+
}
32+
33+
fn foo<T=i32>(a: Option<T>)
34+
where T: Debug + PartialEq<&'static str> {
35+
assert_eq!(a.unwrap(), "");
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/trait_method_call_default_self.rs:29:13
3+
|
4+
29 | let x = None;
5+
| - ^^^^ cannot infer type for `T`
6+
| |
7+
| consider giving `x` a type
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)