Skip to content

Commit 34027c7

Browse files
committed
Test associated type as default, test that elided params are not inferred
1 parent d84f3ed commit 34027c7

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
13+
// file at the top-level directory of this distribution and at
14+
// http://rust-lang.org/COPYRIGHT.
15+
//
16+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
17+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
18+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
19+
// option. This file may not be copied, modified, or distributed
20+
// except according to those terms.
21+
//
22+
23+
#![feature(default_type_parameter_fallback)]
24+
25+
use std::vec::IntoIter;
26+
use std::iter::Sum;
27+
use std::slice::Iter;
28+
use std::ops::Add;
29+
30+
trait Itarator: Iterator {
31+
type Iten;
32+
// Bug: Even though it's unambiguos, using Self::Iten dosen't work here.
33+
// probably can be fixed in fn associated_path_def_to_ty.
34+
fn foo<T:Default=<Self as Itarator>::Iten>(&self) -> T {
35+
T::default()
36+
}
37+
38+
fn suma<S=<<Self as Itarator>::Iten as Add>::Output>(self) -> S
39+
where Self: Sized,
40+
S: Sum<<Self as Iterator>::Item>,
41+
{
42+
Sum::sum(self)
43+
}
44+
}
45+
46+
impl Itarator for IntoIter<u32> {
47+
type Iten = <IntoIter<u32> as Iterator>::Item;
48+
}
49+
50+
impl<'a> Itarator for Iter<'a, u32> {
51+
type Iten = <Iter<'a, u32> as Iterator>::Item;
52+
}
53+
54+
fn main() {
55+
let x = vec![0u32];
56+
{
57+
let v = x.iter();
58+
// Bug: if we put a cast such as `as u64`, inference fails.
59+
//The usual guess is that we propagate the origin but not the default of the inference var.
60+
v.suma();
61+
}
62+
x.clone().into_iter().suma();
63+
x.into_iter().suma();
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
struct Bar<T>(T);
15+
16+
impl Bar<T> {
17+
fn new<U, Z:Default=String>() -> Bar<Z> {
18+
Bar(Z::default())
19+
}
20+
}
21+
22+
fn main() {
23+
let _:u32 = foo::<usize>();
24+
let _:Bar<u32> = Bar::new::<usize>();
25+
}
26+
27+
fn foo<U:Default, T:Default=String>() -> T {
28+
T::default()
29+
}
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0412]: cannot find type `T` in this scope
2+
--> $DIR/elided_is_not_inferred.rs:16:10
3+
|
4+
16 | impl Bar<T> {
5+
| ^ not found in this scope
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/elided_is_not_inferred.rs:23:17
9+
|
10+
23 | let _:u32 = foo::<usize>();
11+
| ^^^^^^^^^^^^^^ expected u32, found struct `std::string::String`
12+
|
13+
= note: expected type `u32`
14+
found type `std::string::String`
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/elided_is_not_inferred.rs:24:22
18+
|
19+
24 | let _:Bar<u32> = Bar::new::<usize>();
20+
| ^^^^^^^^^^^^^^^^^^^ expected u32, found struct `std::string::String`
21+
|
22+
= note: expected type `Bar<u32>`
23+
found type `Bar<std::string::String>`
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)