Skip to content

Commit

Permalink
Rollup merge of rust-lang#36003 - GuillaumeGomez:err_codes, r=jonatha…
Browse files Browse the repository at this point in the history
…ndturner

Err codes

r? @jonathandturner
  • Loading branch information
Jonathan Turner authored Aug 27, 2016
2 parents 1dbd49f + 3e1b33d commit 3bab26e
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 4 deletions.
45 changes: 44 additions & 1 deletion src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,50 @@ fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
```
"##,

E0525: r##"
A closure was attempted to get used whereas it doesn't implement the expected
trait.
Erroneous code example:
```compile_fail,E0525
struct X;
fn foo<T>(_: T) {}
fn bar<T: Fn(u32)>(_: T) {}
fn main() {
let x = X;
let closure = |_| foo(x); // error: expected a closure that implements
// the `Fn` trait, but this closure only
// implements `FnOnce`
bar(closure);
}
```
In the example above, `closure` is an `FnOnce` closure whereas the `bar`
function expected an `Fn` closure. In this case, it's simple to fix the issue,
you just have to implement `Copy` and `Clone` traits on `struct X` and it'll
be ok:
```
#[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
struct X;
fn foo<T>(_: T) {}
fn bar<T: Fn(u32)>(_: T) {}
fn main() {
let x = X;
let closure = |_| foo(x);
bar(closure); // ok!
}
```
To understand better how closures work in Rust, read:
https://doc.rust-lang.org/book/closures.html
"##,

}


Expand Down Expand Up @@ -1760,5 +1804,4 @@ register_diagnostics! {
E0490, // a value of type `..` is borrowed for too long
E0491, // in type `..`, reference has a longer lifetime than the data it...
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
E0525 // expected a closure that implements `..` but this closure only implements `..`
}
1 change: 0 additions & 1 deletion src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ let Wrapping(x) = x;
let y: usize = 1.wrapping_neg();
assert_eq!(x, y);
```
"##
}

Expand Down
6 changes: 4 additions & 2 deletions src/librustc_trans/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ extern "platform-intrinsic" {
fn simd_add<T>(a: T, b: T) -> T;
}
unsafe { simd_add(0, 1); }
// error: invalid monomorphization of `simd_add` intrinsic
fn main() {
unsafe { simd_add(0, 1); }
// error: invalid monomorphization of `simd_add` intrinsic
}
```
The generic type has to be a SIMD type. Example:
Expand Down
18 changes: 18 additions & 0 deletions src/test/compile-fail/E0502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2016 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.

fn bar(x: &mut i32) {}
fn foo(a: &mut i32) {
let ref y = a;
bar(a); //~ ERROR E0502
}

fn main() {
}
15 changes: 15 additions & 0 deletions src/test/compile-fail/E0503.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2016 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.

fn main() {
let mut value = 3;
let _borrow = &mut value;
let _sum = value + 1; //~ ERROR E0503
}
25 changes: 25 additions & 0 deletions src/test/compile-fail/E0504.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2016 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.

struct FancyNum {
num: u8,
}

fn main() {
let fancy_num = FancyNum { num: 5 };
let fancy_ref = &fancy_num;

let x = move || {
println!("child function: {}", fancy_num.num); //~ ERROR E0504
};

x();
println!("main function: {}", fancy_ref.num);
}
21 changes: 21 additions & 0 deletions src/test/compile-fail/E0505.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2016 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.

struct Value {}

fn eat(val: Value) {}

fn main() {
let x = Value{};
{
let _ref_to_val: &Value = &x;
eat(x); //~ ERROR E0505
}
}
21 changes: 21 additions & 0 deletions src/test/compile-fail/E0506.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2016 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.

struct FancyNum {
num: u8,
}

fn main() {
let mut fancy_num = FancyNum { num: 5 };
let fancy_ref = &fancy_num;
fancy_num = FancyNum { num: 6 }; //~ ERROR E0506

println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
}
23 changes: 23 additions & 0 deletions src/test/compile-fail/E0507.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 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.

use std::cell::RefCell;

struct TheDarkKnight;

impl TheDarkKnight {
fn nothing_is_true(self) {}
}

fn main() {
let x = RefCell::new(TheDarkKnight);

x.borrow().nothing_is_true(); //~ ERROR E0507
}
16 changes: 16 additions & 0 deletions src/test/compile-fail/E0508.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2016 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.

struct NonCopy;

fn main() {
let array = [NonCopy; 1];
let _value = array[0]; //~ ERROR E0508
}
28 changes: 28 additions & 0 deletions src/test/compile-fail/E0509.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2016 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.

struct FancyNum {
num: usize
}

struct DropStruct {
fancy: FancyNum
}

impl Drop for DropStruct {
fn drop(&mut self) {
}
}

fn main() {
let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
let fancy_field = drop_struct.fancy; //~ ERROR E0509
println!("Fancy: {}", fancy_field.num);
}
19 changes: 19 additions & 0 deletions src/test/compile-fail/E0511.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2016 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(platform_intrinsics)]

extern "platform-intrinsic" {
fn simd_add<T>(a: T, b: T) -> T;
}

fn main() {
unsafe { simd_add(0, 1); } //~ ERROR E0511
}
15 changes: 15 additions & 0 deletions src/test/compile-fail/E0512.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2016 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.

fn takes_u8(_: u8) {}

fn main() {
unsafe { takes_u8(::std::mem::transmute(0u16)); } //~ ERROR E0512
}
13 changes: 13 additions & 0 deletions src/test/compile-fail/E0516.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2016 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.

fn main() {
let x: typeof(92) = 92; //~ ERROR E0516
}
25 changes: 25 additions & 0 deletions src/test/compile-fail/E0517.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2016 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.

#[repr(C)] //~ ERROR E0517
type Foo = u8;

#[repr(packed)] //~ ERROR E0517
enum Foo2 {Bar, Baz}

#[repr(u8)] //~ ERROR E0517
struct Foo3 {bar: bool, baz: bool}

#[repr(C)] //~ ERROR E0517
impl Foo3 {
}

fn main() {
}
19 changes: 19 additions & 0 deletions src/test/compile-fail/E0518.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2016 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.

#[inline(always)] //~ ERROR E0518
struct Foo;

#[inline(never)] //~ ERROR E0518
impl Foo {
}

fn main() {
}
30 changes: 30 additions & 0 deletions src/test/compile-fail/E0520.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2016 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(specialization)]

trait SpaceLlama {
fn fly(&self);
}

impl<T> SpaceLlama for T {
default fn fly(&self) {}
}

impl<T: Clone> SpaceLlama for T {
fn fly(&self) {}
}

impl SpaceLlama for i32 {
default fn fly(&self) {} //~ ERROR E0520
}

fn main() {
}
16 changes: 16 additions & 0 deletions src/test/compile-fail/E0522.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2016 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(lang_items)]

#[lang = "cookie"]
fn cookie() -> ! { //~ E0522
loop {}
}
Loading

0 comments on commit 3bab26e

Please sign in to comment.