Skip to content

Commit 06dff80

Browse files
committed
Add/Modify tests for argument position impl Trait
1 parent bdff946 commit 06dff80

File tree

6 files changed

+160
-22
lines changed

6 files changed

+160
-22
lines changed

src/test/compile-fail/impl-trait/disallowed.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010

1111
#![feature(conservative_impl_trait)]
1212

13-
fn arguments(_: impl Fn(),
14-
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
15-
_: Vec<impl Clone>) {}
16-
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
17-
1813
type Factory<R> = impl Fn() -> R;
1914
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
2015

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2017 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(universal_impl_trait)]
12+
use std::fmt::Debug;
13+
14+
trait Foo {
15+
fn foo(&self, &impl Debug);
16+
}
17+
18+
impl Foo for () {
19+
fn foo<U: Debug>(&self, _: &U) { }
20+
//~^ Error method `foo` has incompatible signature for trait
21+
}
22+
23+
trait Bar {
24+
fn bar<U: Debug>(&self, &U);
25+
}
26+
27+
impl Bar for () {
28+
fn bar(&self, _: &impl Debug) { }
29+
//~^ Error method `bar` has incompatible signature for trait
30+
}
31+
32+
fn main() {}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2017 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+
//! A simple test for testing many permutations of allowedness of
12+
//! impl Trait
13+
#![feature(conservative_impl_trait, universal_impl_trait, dyn_trait)]
14+
use std::fmt::Debug;
15+
16+
// Allowed
17+
fn simple_universal(_: impl Debug) { panic!() }
18+
19+
// Allowed
20+
fn simple_existential() -> impl Debug { panic!() }
21+
22+
// Allowed
23+
fn collection_universal(_: Vec<impl Debug>) { panic!() }
24+
25+
// Allowed
26+
fn collection_existential() -> Vec<impl Debug> { panic!() }
27+
28+
// Disallowed
29+
fn fn_type_universal(_: fn(impl Debug)) { panic!() }
30+
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
31+
32+
// Disallowed
33+
fn fn_type_existential() -> fn(impl Debug) { panic!() }
34+
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
35+
36+
// Allowed
37+
fn dyn_universal(_: &dyn Iterator<Item = impl Debug>) { panic!() }
38+
39+
// Disallowed
40+
fn dyn_fn_trait(_: &dyn Fn(impl Debug)) { panic!() }
41+
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
42+
43+
// Allowed
44+
fn nested_universal(_: impl Iterator<Item = impl Iterator>) { panic!() }
45+
46+
// Allowed
47+
fn nested_existential() -> impl IntoIterator<Item = impl IntoIterator> {
48+
vec![vec![0; 10], vec![12; 7], vec![8; 3]]
49+
}
50+
51+
// Disallowed
52+
fn universal_fn_trait(_: impl Fn(impl Debug)) { panic!() }
53+
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
54+
55+
// Disallowed
56+
struct ImplMember { x: impl Debug }
57+
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
58+
59+
// Disallowed
60+
trait Universal {
61+
// FIXME, should error?
62+
fn universal(impl Debug);
63+
}
64+
65+
// Disallowed
66+
trait Existential {
67+
fn existential() -> impl Debug;
68+
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
69+
}
70+
71+
fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 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(universal_impl_trait)]
12+
use std::fmt::Display;
13+
14+
fn check_display_eq(iter: impl IntoIterator<Item = impl Display>) {
15+
let mut collected = String::new();
16+
for it in iter {
17+
let disp = format!("{} ", it);
18+
collected.push_str(&disp);
19+
}
20+
assert_eq!("0 3 27 823 4891 1 0", collected.trim());
21+
}
22+
23+
fn main() {
24+
let i32_list = [0i32, 3, 27, 823, 4891, 1, 0];
25+
let i32_list_vec = vec![0i32, 3, 27, 823, 4891, 1, 0];
26+
let u32_list = [0u32, 3, 27, 823, 4891, 1, 0];
27+
let u32_list_vec = vec![0u32, 3, 27, 823, 4891, 1, 0];
28+
let u16_list = [0u16, 3, 27, 823, 4891, 1, 0];
29+
let str_list = ["0", "3", "27", "823", "4891", "1", "0"];
30+
let str_list_vec = vec!["0", "3", "27", "823", "4891", "1", "0"];
31+
32+
check_display_eq(&i32_list);
33+
check_display_eq(i32_list_vec);
34+
check_display_eq(&u32_list);
35+
check_display_eq(u32_list_vec);
36+
check_display_eq(&u16_list);
37+
check_display_eq(&str_list);
38+
check_display_eq(str_list_vec);
39+
}

src/test/run-pass/impl-trait/example-calendar.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2016-2017 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,7 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(conservative_impl_trait, fn_traits, step_trait, unboxed_closures)]
11+
#![feature(conservative_impl_trait,
12+
universal_impl_trait,
13+
fn_traits,
14+
step_trait,
15+
unboxed_closures
16+
)]
1217

1318
//! Derived from: <https://raw.githubusercontent.com/quickfur/dcal/master/dcal.d>.
1419
//!
@@ -457,9 +462,9 @@ fn test_group_by() {
457462
///
458463
/// Groups an iterator of dates by month.
459464
///
460-
fn by_month<It>(it: It)
461-
-> impl Iterator<Item=(u32, impl Iterator<Item=NaiveDate> + Clone)> + Clone
462-
where It: Iterator<Item=NaiveDate> + Clone {
465+
fn by_month(it: impl Iterator<Item=NaiveDate> + Clone)
466+
-> impl Iterator<Item=(u32, impl Iterator<Item=NaiveDate> + Clone)> + Clone
467+
{
463468
it.group_by(|d| d.month())
464469
}
465470

@@ -474,9 +479,9 @@ fn test_by_month() {
474479
///
475480
/// Groups an iterator of dates by week.
476481
///
477-
fn by_week<It>(it: It)
478-
-> impl Iterator<Item=(u32, impl DateIterator)> + Clone
479-
where It: DateIterator {
482+
fn by_week(it: impl DateIterator)
483+
-> impl Iterator<Item=(u32, impl DateIterator)> + Clone
484+
{
480485
// We go forward one day because `isoweekdate` considers the week to start on a Monday.
481486
it.group_by(|d| d.succ().isoweekdate().1)
482487
}
@@ -548,8 +553,7 @@ const COLS_PER_WEEK: u32 = 7 * COLS_PER_DAY;
548553
///
549554
/// Formats an iterator of weeks into an iterator of strings.
550555
///
551-
fn format_weeks<It>(it: It) -> impl Iterator<Item=String>
552-
where It: Iterator, It::Item: DateIterator {
556+
fn format_weeks(it: impl Iterator<Item = impl DateIterator>) -> impl Iterator<Item=String> {
553557
it.map(|week| {
554558
let mut buf = String::with_capacity((COLS_PER_DAY * COLS_PER_WEEK + 2) as usize);
555559

@@ -627,7 +631,7 @@ fn test_month_title() {
627631
///
628632
/// Formats a month.
629633
///
630-
fn format_month<It: DateIterator>(it: It) -> impl Iterator<Item=String> {
634+
fn format_month(it: impl DateIterator) -> impl Iterator<Item=String> {
631635
let mut month_days = it.peekable();
632636
let title = month_title(month_days.peek().unwrap().month());
633637

@@ -659,8 +663,9 @@ fn test_format_month() {
659663
///
660664
/// Formats an iterator of months.
661665
///
662-
fn format_months<It>(it: It) -> impl Iterator<Item=impl Iterator<Item=String>>
663-
where It: Iterator, It::Item: DateIterator {
666+
fn format_months(it: impl Iterator<Item = impl DateIterator>)
667+
-> impl Iterator<Item=impl Iterator<Item=String>>
668+
{
664669
it.map(format_month)
665670
}
666671

src/test/rustdoc/issue-43869.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ pub fn test_44731_1() -> Result<Box<impl Clone>, ()> {
5555
Ok(Box::new(j()))
5656
}
5757

58-
pub fn test_44731_2() -> Box<Fn(impl Clone)> {
59-
Box::new(|_: u32| {})
60-
}
6158

6259
pub fn test_44731_3() -> Box<Fn() -> impl Clone> {
6360
Box::new(|| 0u32)
@@ -78,6 +75,5 @@ pub fn test_44731_4() -> Box<Iterator<Item=impl Clone>> {
7875
// @has issue_43869/fn.o.html
7976
// @has issue_43869/fn.test_44731_0.html
8077
// @has issue_43869/fn.test_44731_1.html
81-
// @has issue_43869/fn.test_44731_2.html
8278
// @has issue_43869/fn.test_44731_3.html
8379
// @has issue_43869/fn.test_44731_4.html

0 commit comments

Comments
 (0)