Skip to content

Commit

Permalink
Test associated type as default, test that elided params are not infe…
Browse files Browse the repository at this point in the history
…rred
  • Loading branch information
leoyvens committed Nov 29, 2017
1 parent d84f3ed commit 34027c7
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
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 src/test/ui/default-ty-param-fallback/elided_is_not_inferred.rs
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()
}

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

0 comments on commit 34027c7

Please sign in to comment.