Skip to content

Commit e17d6db

Browse files
authored
Auto merge of #36049 - jonathandturner:rollup, r=jonathandturner
Rollup of 6 pull requests - Successful merges: #35657, #35980, #35985, #35989, #36003, #36044 - Failed merges:
2 parents a23064a + d00a89a commit e17d6db

39 files changed

+469
-32
lines changed

src/librustc/diagnostics.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,50 @@ fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
17211721
```
17221722
"##,
17231723

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+
17241768
}
17251769

17261770

@@ -1760,5 +1804,4 @@ register_diagnostics! {
17601804
E0490, // a value of type `..` is borrowed for too long
17611805
E0491, // in type `..`, reference has a longer lifetime than the data it...
17621806
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
1763-
E0525 // expected a closure that implements `..` but this closure only implements `..`
17641807
}

src/librustc/lint/context.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,12 @@ pub trait LintContext: Sized {
606606
"{}({}) overruled by outer forbid({})",
607607
level.as_str(), lint_name,
608608
lint_name);
609+
diag_builder.span_label(span, &format!("overruled by previous forbid"));
609610
match now_source {
610611
LintSource::Default => &mut diag_builder,
611612
LintSource::Node(forbid_source_span) => {
612-
diag_builder.span_note(forbid_source_span,
613-
"`forbid` lint level set here")
613+
diag_builder.span_label(forbid_source_span,
614+
&format!("`forbid` level set here"))
614615
},
615616
LintSource::CommandLine => {
616617
diag_builder.note("`forbid` lint level was set on command line")

src/librustc/traits/error_reporting.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
477477
return;
478478
}
479479

480-
let mut err = struct_span_err!(
481-
self.tcx.sess, span, E0277,
480+
let mut err = struct_span_err!(self.tcx.sess, span, E0277,
482481
"the trait bound `{}` is not satisfied",
483482
trait_ref.to_predicate());
483+
err.span_label(span, &format!("trait `{}` not satisfied",
484+
trait_ref.to_predicate()));
484485

485486
// Try to report a help message
486487

src/librustc_borrowck/borrowck/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -926,9 +926,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
926926
err
927927
}
928928
mc::AliasableBorrowed => {
929-
struct_span_err!(
929+
let mut e = struct_span_err!(
930930
self.tcx.sess, span, E0389,
931-
"{} in a `&` reference", prefix)
931+
"{} in a `&` reference", prefix);
932+
e.span_label(span, &"assignment into an immutable reference");
933+
e
932934
}
933935
};
934936

src/librustc_lint/types.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ let Wrapping(x) = x;
5656
let y: usize = 1.wrapping_neg();
5757
assert_eq!(x, y);
5858
```
59-
6059
"##
6160
}
6261

src/librustc_privacy/lib.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,32 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
444444
}), ..}) => ty,
445445
_ => expr_ty
446446
}.ty_adt_def().unwrap();
447-
let any_priv = def.struct_variant().fields.iter().any(|f| {
448-
!f.vis.is_accessible_from(self.curitem, &self.tcx.map)
449-
});
450-
if any_priv {
451-
span_err!(self.tcx.sess, expr.span, E0450,
452-
"cannot invoke tuple struct constructor with private \
453-
fields");
447+
448+
let private_indexes : Vec<_> = def.struct_variant().fields.iter().enumerate()
449+
.filter(|&(_,f)| {
450+
!f.vis.is_accessible_from(self.curitem, &self.tcx.map)
451+
}).map(|(n,&_)|n).collect();
452+
453+
if !private_indexes.is_empty() {
454+
455+
let mut error = struct_span_err!(self.tcx.sess, expr.span, E0450,
456+
"cannot invoke tuple struct constructor \
457+
with private fields");
458+
error.span_label(expr.span,
459+
&format!("cannot construct with a private field"));
460+
461+
if let Some(def_id) = self.tcx.map.as_local_node_id(def.did) {
462+
if let Some(hir::map::NodeItem(node)) = self.tcx.map.find(def_id) {
463+
if let hir::Item_::ItemStruct(ref tuple_data, _) = node.node {
464+
465+
for i in private_indexes {
466+
error.span_label(tuple_data.fields()[i].span,
467+
&format!("private field declared here"));
468+
}
469+
}
470+
}
471+
}
472+
error.emit();
454473
}
455474
}
456475
}

src/librustc_trans/diagnostics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ extern "platform-intrinsic" {
2323
fn simd_add<T>(a: T, b: T) -> T;
2424
}
2525
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+
}
2830
```
2931
3032
The generic type has to be a SIMD type. Example:

src/librustc_typeck/check/intrinsic.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ fn equate_intrinsic_type<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
5151
}));
5252
let i_n_tps = i_ty.generics.types.len();
5353
if i_n_tps != n_tps {
54-
struct_span_err!(tcx.sess, it.span, E0094,
55-
"intrinsic has wrong number of type \
56-
parameters: found {}, expected {}",
57-
i_n_tps, n_tps)
58-
.span_label(it.span, &format!("expected {} type parameter", n_tps))
59-
.emit();
54+
let span = match it.node {
55+
hir::ForeignItemFn(_, ref generics) => generics.span().unwrap_or(it.span),
56+
hir::ForeignItemStatic(_, _) => it.span
57+
};
58+
59+
struct_span_err!(tcx.sess, span, E0094,
60+
"intrinsic has wrong number of type \
61+
parameters: found {}, expected {}",
62+
i_n_tps, n_tps)
63+
.span_label(span, &format!("expected {} type parameter", n_tps))
64+
.emit();
6065
} else {
6166
require_same_types(ccx,
6267
TypeOrigin::IntrinsicType(it.span),

src/test/compile-fail-fulldeps/lint-plugin-forbid-attrs.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#![plugin(lint_plugin_test)]
1616
#![forbid(test_lint)]
1717
//~^ NOTE lint level defined here
18-
//~| NOTE `forbid` lint level set here
18+
//~| NOTE `forbid` level set here
1919

2020
fn lintme() { } //~ ERROR item is named 'lintme'
2121

22-
#[allow(test_lint)] //~ ERROR allow(test_lint) overruled by outer forbid(test_lint)
22+
#[allow(test_lint)]
23+
//~^ ERROR allow(test_lint) overruled by outer forbid(test_lint)
24+
//~| NOTE overruled by previous forbid
2325
pub fn main() {
2426
lintme();
2527
}

src/test/compile-fail/E0277.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ fn some_func<T: Foo>(foo: T) {
1717
}
1818

1919
fn main() {
20-
some_func(5i32); //~ ERROR E0277
20+
some_func(5i32);
21+
//~^ ERROR the trait bound `i32: Foo` is not satisfied
22+
//~| NOTE trait `i32: Foo` not satisfied
23+
//~| NOTE required by `some_func`
2124
}

src/test/compile-fail/E0389.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ fn main() {
1616
let mut fancy = FancyNum{ num: 5 };
1717
let fancy_ref = &(&mut fancy);
1818
fancy_ref.num = 6; //~ ERROR E0389
19+
//~^ NOTE assignment into an immutable reference
1920
println!("{}", fancy_ref.num);
2021
}

src/test/compile-fail/E0450.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
// except according to those terms.
1010

1111
mod Bar {
12-
pub struct Foo(isize);
12+
pub struct Foo( bool, pub i32, f32, bool);
13+
//~^ NOTE private field declared here
14+
//~| NOTE private field declared here
15+
//~| NOTE private field declared here
1316
}
1417

1518
fn main() {
16-
let f = Bar::Foo(0); //~ ERROR E0450
19+
let f = Bar::Foo(false,1,0.1, true); //~ ERROR E0450
20+
//~^ NOTE cannot construct with a private field
1721
}

src/test/compile-fail/E0453.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
// except according to those terms.
1010

1111
#![forbid(non_snake_case)]
12+
//~^ NOTE `forbid` level set here
1213

13-
#[allow(non_snake_case)] //~ ERROR E0453
14+
#[allow(non_snake_case)]
15+
//~^ ERROR allow(non_snake_case) overruled by outer forbid(non_snake_case)
16+
//~| NOTE overruled by previous forbid
1417
fn main() {
1518
}

src/test/compile-fail/E0502.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

src/test/compile-fail/E0503.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

src/test/compile-fail/E0504.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

src/test/compile-fail/E0505.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

src/test/compile-fail/E0506.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

src/test/compile-fail/E0507.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

src/test/compile-fail/E0508.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

0 commit comments

Comments
 (0)