Skip to content

Commit 5362bbe

Browse files
committed
Tests for default fallback: ui tests, run fail test, more run pass tests.
The tests under bad_messages in ui are the ones that have bad messages that need fixing.
1 parent 3857826 commit 5362bbe

28 files changed

+417
-13
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
// error-pattern:assertion failed: `(left == right)`
12+
13+
#![feature(default_type_parameter_fallback)]
14+
15+
fn foo<T=u64>(t: T) -> usize { std::mem::size_of_val(&t) }
16+
17+
fn main() { assert_eq!(foo(22), std::mem::size_of::<u64>()) }

src/test/run-pass/default-ty-param-fallback/enum.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(default_type_parameter_fallback)]
1212

1313
use std::path::Path;
14+
use std::mem::size_of;
1415

1516
enum Opt<T=String> {
1617
Som(T),
@@ -27,5 +28,11 @@ fn main() {
2728
}
2829

2930
// Defaults on fns take precedence.
30-
fn func1<P: AsRef<Path> = String>(p: Option<P>) { }
31-
fn func2<P: AsRef<Path> = String>(p: Opt<P>) { }
31+
fn func1<P: AsRef<Path> = &'static str>(p: Option<P>) {
32+
// Testing that we got &str rather than String.
33+
assert_eq!(size_of::<P>(), size_of::<&str>())
34+
}
35+
36+
fn func2<P: AsRef<Path> = &'static str>(p: Opt<P>) {
37+
assert_eq!(size_of::<P>(), size_of::<&str>())
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
trait PartialQe<Rhs = Self> {
14+
fn qe(&self, _: &Rhs) {}
15+
}
16+
17+
impl<A, B = A> PartialQe<Option<B>> for Option<A> {}
18+
19+
fn main() {
20+
PartialQe::qe(&Some("str"), &None);
21+
Some('a').qe(&None);
22+
}

src/test/run-pass/default-ty-param-fallback/trait_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn takes_foo<F:Foo>(_: F) {}
2929
fn main() {
3030
let x = Vac::new(); // x: Vac<$0>
3131
// adds oblig Vac<$0> : Foo,
32-
// and applies the default of `impl<T> Vac<T>` and
32+
// and applies the default of `impl<T> Vac<T>` and
3333
// `impl<T:Bar> Foo for Vac<T>`, which must agree.
3434
//
3535
// The default of F in takes_foo<F> makes no difference here,

src/test/compile-fail/default_ty_param_fallback_conflict.rs renamed to src/test/ui/default-ty-param-fallback/bad_messages/fallback_conflict.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,13 @@ use std::fmt::Debug;
1414

1515
// Example from the RFC
1616
fn foo<F:Default=usize>() -> F { F::default() }
17-
//~^ NOTE: a default was defined here...
1817

1918
fn bar<B:Debug=isize>(b: B) { println!("{:?}", b); }
20-
//~^ NOTE: a second default was defined here...
2119

2220
fn main() {
2321
// Here, F is instantiated with $0=uint
2422
let x = foo();
25-
//~^ ERROR: mismatched types
26-
//~| NOTE: conflicting type parameter defaults `usize` and `isize`
27-
//~| NOTE: ...that was applied to an unconstrained type variable here
2823

2924
// Here, B is instantiated with $1=uint, and constraint $0 <: $1 is added.
3025
bar(x);
31-
//~^ NOTE: ...that also applies to the same type variable here
3226
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/fallback_conflict.rs:25:9
3+
|
4+
25 | bar(x);
5+
| ^ expected isize, found usize
6+
7+
error: aborting due to previous error
8+

src/test/compile-fail/default_ty_param_fallback_conflict_cross_crate.rs renamed to src/test/ui/default-ty-param-fallback/bad_messages/fallback_conflict_cross_crate.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ extern crate default_param_test;
1717
use default_param_test::{Foo, bleh};
1818

1919
fn meh<X, B=bool>(x: Foo<X, B>) {}
20-
//~^ NOTE: a default was defined here...
2120

2221
fn main() {
2322
let foo = bleh();
24-
//~^ NOTE: ...that also applies to the same type variable here
2523

2624
meh(foo);
27-
//~^ ERROR: mismatched types:
28-
//~| NOTE: conflicting type parameter defaults `bool` and `char`
2925
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/fallback_conflict_cross_crate.rs:24:9
3+
|
4+
24 | meh(foo);
5+
| ^^^ expected bool, found char
6+
7+
error: aborting due to previous error
8+
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+
//
11+
12+
#![feature(default_type_parameter_fallback)]
13+
14+
use std::marker::PhantomData;
15+
16+
trait Id {
17+
type This;
18+
}
19+
20+
impl<A> Id for A {
21+
type This = A;
22+
}
23+
24+
struct Foo<X: Default = usize, Y = <X as Id>::This> {
25+
data: PhantomData<(X, Y)>
26+
}
27+
28+
impl<X: Default, Y> Foo<X, Y> {
29+
fn new() -> Foo<X, Y> {
30+
Foo { data: PhantomData }
31+
}
32+
}
33+
34+
fn main() {
35+
let foo = Foo::new();
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0283]: type annotations required: cannot resolve `_: std::default::Default`
2+
--> $DIR/default_dependent_associated_type.rs:35:15
3+
|
4+
35 | let foo = Foo::new();
5+
| ^^^^^^^^
6+
|
7+
= note: required by `<Foo<X, Y>>::new`
8+
9+
error: aborting due to previous error
10+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
12+
#![feature(default_type_parameter_fallback)]
13+
use std::marker::PhantomData;
14+
15+
struct Foo<T,U=T> { t: T, data: PhantomData<U> }
16+
17+
fn main() {
18+
let foo = Foo { t: 'a', data: PhantomData };
19+
}
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/dependent_defaults.rs:18:15
3+
|
4+
18 | let foo = Foo { t: 'a', data: PhantomData };
5+
| --- ^^^ cannot infer type for `U`
6+
| |
7+
| consider giving `foo` a type
8+
9+
error: aborting due to previous error
10+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
enum Opt<T=String> {
12+
Som(T),
13+
Non,
14+
}
15+
16+
fn main() {
17+
Opt::Non;
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/feature_gate.rs:17:5
3+
|
4+
17 | Opt::Non;
5+
| ^^^^^^^^ cannot infer type for `T`
6+
7+
error: aborting due to previous error
8+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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::path::Path;
14+
15+
enum Opt<T=String> {
16+
Som(T),
17+
Non,
18+
}
19+
20+
fn main() {
21+
// Defaults on the type definiton work, as long no other params are interfering.
22+
let _ = Opt::Non;
23+
let _: Opt<_> = Opt::Non;
24+
25+
func1(None);
26+
func2(Opt::Non);
27+
}
28+
29+
// Defaults on fns take precedence.
30+
fn func1<P: AsRef<Path> = String>(p: Option<P>) { }
31+
fn func2<P: AsRef<Path> = String>(p: Opt<P>) { }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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::path::Path;
14+
15+
fn func<P: AsRef<Path> = String>(p: Option<P>) {
16+
match p {
17+
None => { println!("None"); }
18+
Some(path) => { println!("{:?}", path.as_ref()); }
19+
}
20+
}
21+
22+
fn main() {
23+
// Dont fallback to future-proof against default on `noner`.
24+
func(noner());
25+
}
26+
27+
fn noner<T>() -> Option<T> { None }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/future_proof.rs:24:5
3+
|
4+
24 | func(noner());
5+
| ^^^^ cannot infer type for `P`
6+
7+
error: aborting due to previous error
8+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
struct Foo;
14+
15+
impl Foo {
16+
fn method<A:Default=String>(&self) -> A {
17+
A::default()
18+
}
19+
}
20+
21+
fn main() {
22+
let f = Foo.method();
23+
println!("{}", f);
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/method_call_test.rs:22:9
3+
|
4+
22 | let f = Foo.method();
5+
| ^
6+
| |
7+
| cannot infer type for `_`
8+
| consider giving `f` a type
9+
10+
error: aborting due to previous error
11+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
12+
#![feature(default_type_parameter_fallback)]
13+
14+
use std::marker::PhantomData;
15+
16+
struct DeterministicHasher;
17+
struct RandomHasher;
18+
19+
20+
struct MyHashMap<K, V, H=DeterministicHasher> {
21+
data: PhantomData<(K, V, H)>
22+
}
23+
24+
impl<K, V, H> MyHashMap<K, V, H> {
25+
fn new() -> MyHashMap<K, V, H> {
26+
MyHashMap { data: PhantomData }
27+
}
28+
}
29+
30+
mod mystd {
31+
use super::{MyHashMap, RandomHasher};
32+
pub type HashMap<K, V, H=RandomHasher> = MyHashMap<K, V, H>;
33+
}
34+
35+
fn try_me<H>(hash_map: mystd::HashMap<i32, i32, H>) {}
36+
37+
fn main() {
38+
let hash_map = mystd::HashMap::new();
39+
try_me(hash_map);
40+
}
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/struct_and_type_alias.rs:38:20
3+
|
4+
38 | let hash_map = mystd::HashMap::new();
5+
| -------- ^^^^^^^^^^^^^^^^^^^ cannot infer type for `H`
6+
| |
7+
| consider giving `hash_map` a type
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)