diff --git a/src/doc/trpl/traits.md b/src/doc/trpl/traits.md index 2986de4179b89..25f5c7cacc771 100644 --- a/src/doc/trpl/traits.md +++ b/src/doc/trpl/traits.md @@ -273,8 +273,8 @@ not, because both the trait and the type aren't in our crate. One last thing about traits: generic functions with a trait bound use *monomorphization* (*mono*: one, *morph*: form), so they are statically -dispatched. What's that mean? Check out the chapter on [static and dynamic -dispatch](static-and-dynamic-dispatch.html) for more. +dispatched. What's that mean? Check out the chapter on [trait +objects](trait-objects.html) for more. ## Multiple trait bounds diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 527a7297f85b9..9f378748d2007 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -2197,13 +2197,9 @@ impl Iterator for Fuse where I: Iterator { if self.done { None } else { - match self.iter.next() { - None => { - self.done = true; - None - } - x => x - } + let next = self.iter.next(); + self.done = next.is_none(); + next } } @@ -2224,13 +2220,9 @@ impl DoubleEndedIterator for Fuse where I: DoubleEndedIterator { if self.done { None } else { - match self.iter.next_back() { - None => { - self.done = true; - None - } - x => x - } + let next = self.iter.next_back(); + self.done = next.is_none(); + next } } } diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 4e4a928d91f72..3fd179cf86f3a 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2705,7 +2705,7 @@ macro_rules! from_str_radix_float_impl { /// /// # Return value /// - /// `Err(ParseIntError)` if the string did not represent a valid number. Otherwise, + /// `Err(ParseFloatError)` if the string did not represent a valid number. /// Otherwise, `Ok(n)` where `n` is the floating-point number represented by `src`. #[inline] #[allow(deprecated)] @@ -2734,7 +2734,7 @@ macro_rules! from_str_radix_float_impl { /// /// # Return value /// - /// `Err(ParseIntError)` if the string did not represent a valid number. Otherwise, + /// `Err(ParseFloatError)` if the string did not represent a valid number. /// Otherwise, `Ok(n)` where `n` is the floating-point number represented by `src`. fn from_str_radix(src: &str, radix: u32) -> Result<$T, ParseFloatError> { diff --git a/src/librustc/middle/infer/error_reporting.rs b/src/librustc/middle/infer/error_reporting.rs index 36229a558e955..03e76f5ee2573 100644 --- a/src/librustc/middle/infer/error_reporting.rs +++ b/src/librustc/middle/infer/error_reporting.rs @@ -373,8 +373,9 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> { fn report_and_explain_type_error(&self, trace: TypeTrace<'tcx>, terr: &ty::type_err<'tcx>) { + let span = trace.origin.span(); self.report_type_error(trace, terr); - ty::note_and_explain_type_err(self.tcx, terr); + ty::note_and_explain_type_err(self.tcx, terr, span); } /// Returns a string of the form "expected `{}`, found `{}`", or None if this is a derived @@ -812,7 +813,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> { } self.give_suggestion(same_regions); for &(ref trace, terr) in trace_origins { - self.report_type_error(trace.clone(), &terr); + self.report_and_explain_type_error(trace.clone(), &terr); } } diff --git a/src/librustc/middle/infer/mod.rs b/src/librustc/middle/infer/mod.rs index b11e25c059d08..0f62b440bf32d 100644 --- a/src/librustc/middle/infer/mod.rs +++ b/src/librustc/middle/infer/mod.rs @@ -987,7 +987,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { error_str)); if let Some(err) = err { - ty::note_and_explain_type_err(self.tcx, err) + ty::note_and_explain_type_err(self.tcx, err, sp) } } } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 952996c90f52e..0a747ba881fd6 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -5103,7 +5103,7 @@ pub fn type_err_to_str<'tcx>(cx: &ctxt<'tcx>, err: &type_err<'tcx>) -> String { } } -pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) { +pub fn note_and_explain_type_err<'tcx>(cx: &ctxt<'tcx>, err: &type_err<'tcx>, sp: Span) { match *err { terr_regions_does_not_outlive(subregion, superregion) => { note_and_explain_region(cx, "", subregion, "..."); @@ -5134,6 +5134,16 @@ pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) { "expected concrete lifetime is ", conc_region, ""); } + terr_sorts(values) => { + let expected_str = ty_sort_string(cx, values.expected); + let found_str = ty_sort_string(cx, values.found); + if expected_str == found_str && expected_str == "closure" { + cx.sess.span_note(sp, &format!("no two closures, even if identical, have the same \ + type")); + cx.sess.span_help(sp, &format!("consider boxing your closure and/or \ + using it as a trait object")); + } + } _ => {} } } diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 88faf1cb68ae4..500af5fc772f5 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -163,7 +163,7 @@ impl Session { self.diagnostic().handler().note(msg) } pub fn help(&self, msg: &str) { - self.diagnostic().handler().note(msg) + self.diagnostic().handler().help(msg) } pub fn opt_span_bug(&self, opt_sp: Option, msg: &str) -> ! { match opt_sp { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 31039e3abca6e..862c454a38833 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3575,7 +3575,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, .ty_to_string( actual_structure_type), type_error_description); - ty::note_and_explain_type_err(tcx, &type_error); + ty::note_and_explain_type_err(tcx, &type_error, path.span); } } } diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 9d6c04b1ad49d..be3fc860b2b12 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -200,7 +200,7 @@ fn require_same_types<'a, 'tcx, M>(tcx: &ty::ctxt<'tcx>, msg(), ty::type_err_to_str(tcx, terr)); - ty::note_and_explain_type_err(tcx, terr); + ty::note_and_explain_type_err(tcx, terr, span); false } } diff --git a/src/test/compile-fail/issue-24036.rs b/src/test/compile-fail/issue-24036.rs new file mode 100644 index 0000000000000..3c8a64eaf7de4 --- /dev/null +++ b/src/test/compile-fail/issue-24036.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn closure_to_loc() { + let mut x = |c| c + 1; + x = |c| c + 1; + //~^ ERROR mismatched types + //~| NOTE no two closures, even if identical, have the same type + //~| HELP consider boxing your closure and/or using it as a trait object +} + +fn closure_from_match() { + let x = match 1usize { + 1 => |c| c + 1, + 2 => |c| c - 1, + _ => |c| c - 1 + }; + //~^^^^^ ERROR match arms have incompatible types + //~| NOTE no two closures, even if identical, have the same type + //~| HELP consider boxing your closure and/or using it as a trait object +} + +fn main() { } diff --git a/src/test/run-pass/issue-16602-1.rs b/src/test/run-pass/issue-16602-1.rs new file mode 100644 index 0000000000000..ee638edad6cb5 --- /dev/null +++ b/src/test/run-pass/issue-16602-1.rs @@ -0,0 +1,15 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let mut t = [1; 2]; + t = [t[1] * 2, t[0] * 2]; + assert_eq!(&t[..], &[2, 2]); +} diff --git a/src/test/run-pass/issue-16602-2.rs b/src/test/run-pass/issue-16602-2.rs new file mode 100644 index 0000000000000..742eb6c280e39 --- /dev/null +++ b/src/test/run-pass/issue-16602-2.rs @@ -0,0 +1,21 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct A { + pub x: u32, + pub y: u32, +} + +fn main() { + let mut a = A { x: 1, y: 1 }; + a = A { x: a.y * 2, y: a.x * 2 }; + assert_eq!(a.x, 2); + assert_eq!(a.y, 2); +} diff --git a/src/test/run-pass/issue-16602-3.rs b/src/test/run-pass/issue-16602-3.rs new file mode 100644 index 0000000000000..d29932dcf6836 --- /dev/null +++ b/src/test/run-pass/issue-16602-3.rs @@ -0,0 +1,34 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[derive(Debug)] +enum Foo { + Bar(u32, u32), + Baz(&'static u32, &'static u32) +} + +static NUM: u32 = 100; + +fn main () { + let mut b = Foo::Baz(&NUM, &NUM); + b = Foo::Bar(f(&b), g(&b)); +} + +static FNUM: u32 = 1; + +fn f (b: &Foo) -> u32 { + FNUM +} + +static GNUM: u32 = 2; + +fn g (b: &Foo) -> u32 { + GNUM +}