File tree 19 files changed +372
-4
lines changed 19 files changed +372
-4
lines changed Original file line number Diff line number Diff line change @@ -1721,6 +1721,50 @@ fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
1721
1721
```
1722
1722
"## ,
1723
1723
1724
+ E0525 : r##"
1725
+ A closure was attempted to get used whereas it doesn't implement the expected
1726
+ trait.
1727
+
1728
+ Erroneous code example:
1729
+
1730
+ ```compile_fail,E0525
1731
+ struct X;
1732
+
1733
+ fn foo<T>(_: T) {}
1734
+ fn bar<T: Fn(u32)>(_: T) {}
1735
+
1736
+ fn main() {
1737
+ let x = X;
1738
+ let closure = |_| foo(x); // error: expected a closure that implements
1739
+ // the `Fn` trait, but this closure only
1740
+ // implements `FnOnce`
1741
+ bar(closure);
1742
+ }
1743
+ ```
1744
+
1745
+ In the example above, `closure` is an `FnOnce` closure whereas the `bar`
1746
+ function expected an `Fn` closure. In this case, it's simple to fix the issue,
1747
+ you just have to implement `Copy` and `Clone` traits on `struct X` and it'll
1748
+ be ok:
1749
+
1750
+ ```
1751
+ #[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
1752
+ struct X;
1753
+
1754
+ fn foo<T>(_: T) {}
1755
+ fn bar<T: Fn(u32)>(_: T) {}
1756
+
1757
+ fn main() {
1758
+ let x = X;
1759
+ let closure = |_| foo(x);
1760
+ bar(closure); // ok!
1761
+ }
1762
+ ```
1763
+
1764
+ To understand better how closures work in Rust, read:
1765
+ https://doc.rust-lang.org/book/closures.html
1766
+ "## ,
1767
+
1724
1768
}
1725
1769
1726
1770
@@ -1760,5 +1804,4 @@ register_diagnostics! {
1760
1804
E0490 , // a value of type `..` is borrowed for too long
1761
1805
E0491 , // in type `..`, reference has a longer lifetime than the data it...
1762
1806
E0495 , // cannot infer an appropriate lifetime due to conflicting requirements
1763
- E0525 // expected a closure that implements `..` but this closure only implements `..`
1764
1807
}
Original file line number Diff line number Diff line change @@ -56,7 +56,6 @@ let Wrapping(x) = x;
56
56
let y: usize = 1.wrapping_neg();
57
57
assert_eq!(x, y);
58
58
```
59
-
60
59
"##
61
60
}
62
61
Original file line number Diff line number Diff line change @@ -23,8 +23,10 @@ extern "platform-intrinsic" {
23
23
fn simd_add<T>(a: T, b: T) -> T;
24
24
}
25
25
26
- unsafe { simd_add(0, 1); }
27
- // error: invalid monomorphization of `simd_add` intrinsic
26
+ fn main() {
27
+ unsafe { simd_add(0, 1); }
28
+ // error: invalid monomorphization of `simd_add` intrinsic
29
+ }
28
30
```
29
31
30
32
The generic type has to be a SIMD type. Example:
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ fn bar ( x : & mut i32 ) { }
12
+ fn foo ( a : & mut i32 ) {
13
+ let ref y = a;
14
+ bar ( a) ; //~ ERROR E0502
15
+ }
16
+
17
+ fn main ( ) {
18
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ fn main ( ) {
12
+ let mut value = 3 ;
13
+ let _borrow = & mut value;
14
+ let _sum = value + 1 ; //~ ERROR E0503
15
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ struct FancyNum {
12
+ num : u8 ,
13
+ }
14
+
15
+ fn main ( ) {
16
+ let fancy_num = FancyNum { num : 5 } ;
17
+ let fancy_ref = & fancy_num;
18
+
19
+ let x = move || {
20
+ println ! ( "child function: {}" , fancy_num. num) ; //~ ERROR E0504
21
+ } ;
22
+
23
+ x ( ) ;
24
+ println ! ( "main function: {}" , fancy_ref. num) ;
25
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ struct Value { }
12
+
13
+ fn eat ( val : Value ) { }
14
+
15
+ fn main ( ) {
16
+ let x = Value { } ;
17
+ {
18
+ let _ref_to_val: & Value = & x;
19
+ eat ( x) ; //~ ERROR E0505
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ struct FancyNum {
12
+ num : u8 ,
13
+ }
14
+
15
+ fn main ( ) {
16
+ let mut fancy_num = FancyNum { num : 5 } ;
17
+ let fancy_ref = & fancy_num;
18
+ fancy_num = FancyNum { num : 6 } ; //~ ERROR E0506
19
+
20
+ println ! ( "Num: {}, Ref: {}" , fancy_num. num, fancy_ref. num) ;
21
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ use std:: cell:: RefCell ;
12
+
13
+ struct TheDarkKnight ;
14
+
15
+ impl TheDarkKnight {
16
+ fn nothing_is_true ( self ) { }
17
+ }
18
+
19
+ fn main ( ) {
20
+ let x = RefCell :: new ( TheDarkKnight ) ;
21
+
22
+ x. borrow ( ) . nothing_is_true ( ) ; //~ ERROR E0507
23
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ struct NonCopy ;
12
+
13
+ fn main ( ) {
14
+ let array = [ NonCopy ; 1 ] ;
15
+ let _value = array[ 0 ] ; //~ ERROR E0508
16
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ struct FancyNum {
12
+ num : usize
13
+ }
14
+
15
+ struct DropStruct {
16
+ fancy : FancyNum
17
+ }
18
+
19
+ impl Drop for DropStruct {
20
+ fn drop ( & mut self ) {
21
+ }
22
+ }
23
+
24
+ fn main ( ) {
25
+ let drop_struct = DropStruct { fancy : FancyNum { num : 5 } } ;
26
+ let fancy_field = drop_struct. fancy ; //~ ERROR E0509
27
+ println ! ( "Fancy: {}" , fancy_field. num) ;
28
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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( platform_intrinsics) ]
12
+
13
+ extern "platform-intrinsic" {
14
+ fn simd_add < T > ( a : T , b : T ) -> T ;
15
+ }
16
+
17
+ fn main ( ) {
18
+ unsafe { simd_add ( 0 , 1 ) ; } //~ ERROR E0511
19
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ fn takes_u8 ( _: u8 ) { }
12
+
13
+ fn main ( ) {
14
+ unsafe { takes_u8 ( :: std:: mem:: transmute ( 0u16 ) ) ; } //~ ERROR E0512
15
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ fn main ( ) {
12
+ let x: typeof ( 92 ) = 92 ; //~ ERROR E0516
13
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ #[ repr( C ) ] //~ ERROR E0517
12
+ type Foo = u8 ;
13
+
14
+ #[ repr( packed) ] //~ ERROR E0517
15
+ enum Foo2 { Bar , Baz }
16
+
17
+ #[ repr( u8 ) ] //~ ERROR E0517
18
+ struct Foo3 { bar : bool , baz : bool }
19
+
20
+ #[ repr( C ) ] //~ ERROR E0517
21
+ impl Foo3 {
22
+ }
23
+
24
+ fn main ( ) {
25
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ #[ inline( always) ] //~ ERROR E0518
12
+ struct Foo ;
13
+
14
+ #[ inline( never) ] //~ ERROR E0518
15
+ impl Foo {
16
+ }
17
+
18
+ fn main ( ) {
19
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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( specialization) ]
12
+
13
+ trait SpaceLlama {
14
+ fn fly ( & self ) ;
15
+ }
16
+
17
+ impl < T > SpaceLlama for T {
18
+ default fn fly ( & self ) { }
19
+ }
20
+
21
+ impl < T : Clone > SpaceLlama for T {
22
+ fn fly ( & self ) { }
23
+ }
24
+
25
+ impl SpaceLlama for i32 {
26
+ default fn fly ( & self ) { } //~ ERROR E0520
27
+ }
28
+
29
+ fn main ( ) {
30
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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( lang_items) ]
12
+
13
+ #[ lang = "cookie" ]
14
+ fn cookie ( ) -> ! { //~ E0522
15
+ loop { }
16
+ }
You can’t perform that action at this time.
0 commit comments