-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test associated type as default, test that elided params are not infe…
…rred
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/test/run-pass/default-ty-param-fallback/associated_type_as_default.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,64 @@ | ||
// 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. | ||
// compile-flags: --error-format=human | ||
|
||
// 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. | ||
// | ||
|
||
#![feature(default_type_parameter_fallback)] | ||
|
||
use std::vec::IntoIter; | ||
use std::iter::Sum; | ||
use std::slice::Iter; | ||
use std::ops::Add; | ||
|
||
trait Itarator: Iterator { | ||
type Iten; | ||
// Bug: Even though it's unambiguos, using Self::Iten dosen't work here. | ||
// probably can be fixed in fn associated_path_def_to_ty. | ||
fn foo<T:Default=<Self as Itarator>::Iten>(&self) -> T { | ||
T::default() | ||
} | ||
|
||
fn suma<S=<<Self as Itarator>::Iten as Add>::Output>(self) -> S | ||
where Self: Sized, | ||
S: Sum<<Self as Iterator>::Item>, | ||
{ | ||
Sum::sum(self) | ||
} | ||
} | ||
|
||
impl Itarator for IntoIter<u32> { | ||
type Iten = <IntoIter<u32> as Iterator>::Item; | ||
} | ||
|
||
impl<'a> Itarator for Iter<'a, u32> { | ||
type Iten = <Iter<'a, u32> as Iterator>::Item; | ||
} | ||
|
||
fn main() { | ||
let x = vec![0u32]; | ||
{ | ||
let v = x.iter(); | ||
// Bug: if we put a cast such as `as u64`, inference fails. | ||
//The usual guess is that we propagate the origin but not the default of the inference var. | ||
v.suma(); | ||
} | ||
x.clone().into_iter().suma(); | ||
x.into_iter().suma(); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/ui/default-ty-param-fallback/elided_is_not_inferred.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,30 @@ | ||
// 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. | ||
// compile-flags: --error-format=human | ||
|
||
#![feature(default_type_parameter_fallback)] | ||
|
||
struct Bar<T>(T); | ||
|
||
impl Bar<T> { | ||
fn new<U, Z:Default=String>() -> Bar<Z> { | ||
Bar(Z::default()) | ||
} | ||
} | ||
|
||
fn main() { | ||
let _:u32 = foo::<usize>(); | ||
let _:Bar<u32> = Bar::new::<usize>(); | ||
} | ||
|
||
fn foo<U:Default, T:Default=String>() -> T { | ||
T::default() | ||
} | ||
|
26 changes: 26 additions & 0 deletions
26
src/test/ui/default-ty-param-fallback/elided_is_not_inferred.stderr
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 @@ | ||
error[E0412]: cannot find type `T` in this scope | ||
--> $DIR/elided_is_not_inferred.rs:16:10 | ||
| | ||
16 | impl Bar<T> { | ||
| ^ not found in this scope | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/elided_is_not_inferred.rs:23:17 | ||
| | ||
23 | let _:u32 = foo::<usize>(); | ||
| ^^^^^^^^^^^^^^ expected u32, found struct `std::string::String` | ||
| | ||
= note: expected type `u32` | ||
found type `std::string::String` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/elided_is_not_inferred.rs:24:22 | ||
| | ||
24 | let _:Bar<u32> = Bar::new::<usize>(); | ||
| ^^^^^^^^^^^^^^^^^^^ expected u32, found struct `std::string::String` | ||
| | ||
= note: expected type `Bar<u32>` | ||
found type `Bar<std::string::String>` | ||
|
||
error: aborting due to 3 previous errors | ||
|