Skip to content

Commit 71a34d7

Browse files
committed
Merge branch 'master' of github.com:theypsilon/rust
2 parents 0e8ec43 + f013914 commit 71a34d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+691
-193
lines changed

src/libcollections/slice.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,8 @@ impl<T> [T] {
973973
/// ```
974974
#[stable(feature = "rust1", since = "1.0.0")]
975975
#[inline]
976-
pub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
977-
where F: FnMut(&T) -> Ordering
976+
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
977+
where F: FnMut(&'a T) -> Ordering
978978
{
979979
core_slice::SliceExt::binary_search_by(self, f)
980980
}
@@ -1009,8 +1009,8 @@ impl<T> [T] {
10091009
/// ```
10101010
#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
10111011
#[inline]
1012-
pub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
1013-
where F: FnMut(&T) -> B,
1012+
pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
1013+
where F: FnMut(&'a T) -> B,
10141014
B: Ord
10151015
{
10161016
core_slice::SliceExt::binary_search_by_key(self, b, f)

src/libcollections/string.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,27 @@ impl<'a> From<String> for Cow<'a, str> {
18741874
}
18751875
}
18761876

1877+
#[stable(feature = "cow_str_from_iter", since = "1.12.0")]
1878+
impl<'a> FromIterator<char> for Cow<'a, str> {
1879+
fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> {
1880+
Cow::Owned(FromIterator::from_iter(it))
1881+
}
1882+
}
1883+
1884+
#[stable(feature = "cow_str_from_iter", since = "1.12.0")]
1885+
impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> {
1886+
fn from_iter<I: IntoIterator<Item = &'b str>>(it: I) -> Cow<'a, str> {
1887+
Cow::Owned(FromIterator::from_iter(it))
1888+
}
1889+
}
1890+
1891+
#[stable(feature = "cow_str_from_iter", since = "1.12.0")]
1892+
impl<'a> FromIterator<String> for Cow<'a, str> {
1893+
fn from_iter<I: IntoIterator<Item = String>>(it: I) -> Cow<'a, str> {
1894+
Cow::Owned(FromIterator::from_iter(it))
1895+
}
1896+
}
1897+
18771898
#[stable(feature = "rust1", since = "1.0.0")]
18781899
impl Into<Vec<u8>> for String {
18791900
fn into(self) -> Vec<u8> {

src/libcore/cell.rs

+116-9
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,13 @@
147147
use clone::Clone;
148148
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
149149
use default::Default;
150-
use marker::{Copy, Send, Sync, Sized, Unsize};
150+
use fmt::{self, Debug, Display};
151+
use marker::{Copy, PhantomData, Send, Sync, Sized, Unsize};
151152
use ops::{Deref, DerefMut, Drop, FnOnce, CoerceUnsized};
152153
use option::Option;
153154
use option::Option::{None, Some};
155+
use result::Result;
156+
use result::Result::{Ok, Err};
154157

155158
/// A mutable memory location that admits only `Copy` data.
156159
///
@@ -347,6 +350,46 @@ pub enum BorrowState {
347350
Unused,
348351
}
349352

353+
/// An error returned by [`RefCell::try_borrow`](struct.RefCell.html#method.try_borrow).
354+
#[unstable(feature = "try_borrow", issue = "35070")]
355+
pub struct BorrowError<'a, T: 'a + ?Sized> {
356+
marker: PhantomData<&'a RefCell<T>>,
357+
}
358+
359+
#[unstable(feature = "try_borrow", issue = "35070")]
360+
impl<'a, T: ?Sized> Debug for BorrowError<'a, T> {
361+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
362+
f.debug_struct("BorrowError").finish()
363+
}
364+
}
365+
366+
#[unstable(feature = "try_borrow", issue = "35070")]
367+
impl<'a, T: ?Sized> Display for BorrowError<'a, T> {
368+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
369+
Display::fmt("already mutably borrowed", f)
370+
}
371+
}
372+
373+
/// An error returned by [`RefCell::try_borrow_mut`](struct.RefCell.html#method.try_borrow_mut).
374+
#[unstable(feature = "try_borrow", issue = "35070")]
375+
pub struct BorrowMutError<'a, T: 'a + ?Sized> {
376+
marker: PhantomData<&'a RefCell<T>>,
377+
}
378+
379+
#[unstable(feature = "try_borrow", issue = "35070")]
380+
impl<'a, T: ?Sized> Debug for BorrowMutError<'a, T> {
381+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
382+
f.debug_struct("BorrowMutError").finish()
383+
}
384+
}
385+
386+
#[unstable(feature = "try_borrow", issue = "35070")]
387+
impl<'a, T: ?Sized> Display for BorrowMutError<'a, T> {
388+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
389+
Display::fmt("already borrowed", f)
390+
}
391+
}
392+
350393
// Values [1, MAX-1] represent the number of `Ref` active
351394
// (will not outgrow its range since `usize` is the size of the address space)
352395
type BorrowFlag = usize;
@@ -432,7 +475,8 @@ impl<T: ?Sized> RefCell<T> {
432475
///
433476
/// # Panics
434477
///
435-
/// Panics if the value is currently mutably borrowed.
478+
/// Panics if the value is currently mutably borrowed. For a non-panicking variant, use
479+
/// [`try_borrow`](#method.try_borrow).
436480
///
437481
/// # Examples
438482
///
@@ -463,12 +507,45 @@ impl<T: ?Sized> RefCell<T> {
463507
#[stable(feature = "rust1", since = "1.0.0")]
464508
#[inline]
465509
pub fn borrow(&self) -> Ref<T> {
510+
self.try_borrow().expect("already mutably borrowed")
511+
}
512+
513+
/// Immutably borrows the wrapped value, returning an error if the value is currently mutably
514+
/// borrowed.
515+
///
516+
/// The borrow lasts until the returned `Ref` exits scope. Multiple immutable borrows can be
517+
/// taken out at the same time.
518+
///
519+
/// This is the non-panicking variant of [`borrow`](#method.borrow).
520+
///
521+
/// # Examples
522+
///
523+
/// ```
524+
/// #![feature(try_borrow)]
525+
///
526+
/// use std::cell::RefCell;
527+
///
528+
/// let c = RefCell::new(5);
529+
///
530+
/// {
531+
/// let m = c.borrow_mut();
532+
/// assert!(c.try_borrow().is_err());
533+
/// }
534+
///
535+
/// {
536+
/// let m = c.borrow();
537+
/// assert!(c.try_borrow().is_ok());
538+
/// }
539+
/// ```
540+
#[unstable(feature = "try_borrow", issue = "35070")]
541+
#[inline]
542+
pub fn try_borrow(&self) -> Result<Ref<T>, BorrowError<T>> {
466543
match BorrowRef::new(&self.borrow) {
467-
Some(b) => Ref {
544+
Some(b) => Ok(Ref {
468545
value: unsafe { &*self.value.get() },
469546
borrow: b,
470-
},
471-
None => panic!("RefCell<T> already mutably borrowed"),
547+
}),
548+
None => Err(BorrowError { marker: PhantomData }),
472549
}
473550
}
474551

@@ -479,7 +556,8 @@ impl<T: ?Sized> RefCell<T> {
479556
///
480557
/// # Panics
481558
///
482-
/// Panics if the value is currently borrowed.
559+
/// Panics if the value is currently borrowed. For a non-panicking variant, use
560+
/// [`try_borrow_mut`](#method.try_borrow_mut).
483561
///
484562
/// # Examples
485563
///
@@ -511,12 +589,41 @@ impl<T: ?Sized> RefCell<T> {
511589
#[stable(feature = "rust1", since = "1.0.0")]
512590
#[inline]
513591
pub fn borrow_mut(&self) -> RefMut<T> {
592+
self.try_borrow_mut().expect("already borrowed")
593+
}
594+
595+
/// Mutably borrows the wrapped value, returning an error if the value is currently borrowed.
596+
///
597+
/// The borrow lasts until the returned `RefMut` exits scope. The value cannot be borrowed
598+
/// while this borrow is active.
599+
///
600+
/// This is the non-panicking variant of [`borrow_mut`](#method.borrow_mut).
601+
///
602+
/// # Examples
603+
///
604+
/// ```
605+
/// #![feature(try_borrow)]
606+
///
607+
/// use std::cell::RefCell;
608+
///
609+
/// let c = RefCell::new(5);
610+
///
611+
/// {
612+
/// let m = c.borrow();
613+
/// assert!(c.try_borrow_mut().is_err());
614+
/// }
615+
///
616+
/// assert!(c.try_borrow_mut().is_ok());
617+
/// ```
618+
#[unstable(feature = "try_borrow", issue = "35070")]
619+
#[inline]
620+
pub fn try_borrow_mut(&self) -> Result<RefMut<T>, BorrowMutError<T>> {
514621
match BorrowRefMut::new(&self.borrow) {
515-
Some(b) => RefMut {
622+
Some(b) => Ok(RefMut {
516623
value: unsafe { &mut *self.value.get() },
517624
borrow: b,
518-
},
519-
None => panic!("RefCell<T> already borrowed"),
625+
}),
626+
None => Err(BorrowMutError { marker: PhantomData }),
520627
}
521628
}
522629

src/libcore/iter/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ pub trait ExactSizeIterator: Iterator {
548548
/// assert_eq!(one_element.next(), None);
549549
/// ```
550550
#[inline]
551-
#[unstable(feature = "exact_size_is_empty", issue = "0")]
551+
#[unstable(feature = "exact_size_is_empty", issue = "35428")]
552552
fn is_empty(&self) -> bool {
553553
self.len() == 0
554554
}

src/libcore/slice.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ pub trait SliceExt {
105105
fn binary_search(&self, x: &Self::Item) -> Result<usize, usize>
106106
where Self::Item: Ord;
107107
#[stable(feature = "core", since = "1.6.0")]
108-
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
109-
where F: FnMut(&Self::Item) -> Ordering;
108+
fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
109+
where F: FnMut(&'a Self::Item) -> Ordering;
110110
#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
111-
fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
112-
where F: FnMut(&Self::Item) -> B,
111+
fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
112+
where F: FnMut(&'a Self::Item) -> B,
113113
B: Ord;
114114
#[stable(feature = "core", since = "1.6.0")]
115115
fn len(&self) -> usize;
@@ -301,8 +301,8 @@ impl<T> SliceExt for [T] {
301301
self as *const [T] as *const T
302302
}
303303

304-
fn binary_search_by<F>(&self, mut f: F) -> Result<usize, usize> where
305-
F: FnMut(&T) -> Ordering
304+
fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
305+
where F: FnMut(&'a T) -> Ordering
306306
{
307307
let mut base = 0usize;
308308
let mut s = self;
@@ -514,8 +514,8 @@ impl<T> SliceExt for [T] {
514514
}
515515

516516
#[inline]
517-
fn binary_search_by_key<B, F>(&self, b: &B, mut f: F) -> Result<usize, usize>
518-
where F: FnMut(&Self::Item) -> B,
517+
fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize>
518+
where F: FnMut(&'a Self::Item) -> B,
519519
B: Ord
520520
{
521521
self.binary_search_by(|k| f(k).cmp(b))

src/librustc/traits/error_reporting.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
870870

871871

872872
fn need_type_info(&self, span: Span, ty: Ty<'tcx>) {
873-
span_err!(self.tcx.sess, span, E0282,
874-
"unable to infer enough type information about `{}`; \
875-
type annotations or generic parameter binding required",
876-
ty);
873+
let mut err = struct_span_err!(self.tcx.sess, span, E0282,
874+
"unable to infer enough type information about `{}`",
875+
ty);
876+
err.note("type annotations or generic parameter binding required");
877+
err.span_label(span, &format!("cannot infer type for `{}`", ty));
878+
err.emit()
877879
}
878880

879881
fn note_obligation_cause<T>(&self,

src/librustc_const_eval/check_match.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,12 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &hir::Expr) {
235235
.flat_map(|arm| &arm.0)
236236
.map(|pat| vec![wrap_pat(cx, &pat)])
237237
.collect();
238-
check_exhaustive(cx, ex.span, &matrix, source);
238+
let match_span = Span {
239+
lo: ex.span.lo,
240+
hi: scrut.span.hi,
241+
expn_id: ex.span.expn_id
242+
};
243+
check_exhaustive(cx, match_span, &matrix, source);
239244
},
240245
_ => ()
241246
}

src/librustc_mir/transform/qualify_consts.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,10 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
686686
Rvalue::Box(_) => {
687687
self.add(Qualif::NOT_CONST);
688688
if self.mode != Mode::Fn {
689-
span_err!(self.tcx.sess, self.span, E0010,
690-
"allocations are not allowed in {}s", self.mode);
689+
struct_span_err!(self.tcx.sess, self.span, E0010,
690+
"allocations are not allowed in {}s", self.mode)
691+
.span_label(self.span, &format!("allocation not allowed in {}s", self.mode))
692+
.emit();
691693
}
692694
}
693695

src/librustc_resolve/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3375,7 +3375,11 @@ impl<'a> Resolver<'a> {
33753375
(true, _) | (_, true) => struct_span_err!(self.session, span, E0260, "{}", msg),
33763376
_ => match (old_binding.is_import(), binding.is_import()) {
33773377
(false, false) => struct_span_err!(self.session, span, E0428, "{}", msg),
3378-
(true, true) => struct_span_err!(self.session, span, E0252, "{}", msg),
3378+
(true, true) => {
3379+
let mut e = struct_span_err!(self.session, span, E0252, "{}", msg);
3380+
e.span_label(span, &format!("already imported"));
3381+
e
3382+
},
33793383
_ => {
33803384
let mut e = struct_span_err!(self.session, span, E0255, "{}", msg);
33813385
e.span_label(span, &format!("`{}` was already imported", name));

src/librustc_typeck/astconv.rs

+29-17
Original file line numberDiff line numberDiff line change
@@ -1215,10 +1215,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
12151215
type_str: &str,
12161216
trait_str: &str,
12171217
name: &str) {
1218-
span_err!(self.tcx().sess, span, E0223,
1219-
"ambiguous associated type; specify the type using the syntax \
1220-
`<{} as {}>::{}`",
1221-
type_str, trait_str, name);
1218+
struct_span_err!(self.tcx().sess, span, E0223, "ambiguous associated type")
1219+
.span_label(span, &format!("ambiguous associated type"))
1220+
.note(&format!("specify the type using the syntax `<{} as {}>::{}`",
1221+
type_str, trait_str, name))
1222+
.emit();
1223+
12221224
}
12231225

12241226
// Search for a bound on a type parameter which includes the associated item
@@ -2095,8 +2097,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
20952097

20962098
if !trait_bounds.is_empty() {
20972099
let b = &trait_bounds[0];
2098-
span_err!(self.tcx().sess, b.trait_ref.path.span, E0225,
2099-
"only the builtin traits can be used as closure or object bounds");
2100+
let span = b.trait_ref.path.span;
2101+
struct_span_err!(self.tcx().sess, span, E0225,
2102+
"only the builtin traits can be used as closure or object bounds")
2103+
.span_label(span, &format!("non-builtin trait used as bounds"))
2104+
.emit();
21002105
}
21012106

21022107
let region_bound =
@@ -2255,20 +2260,27 @@ fn check_type_argument_count(tcx: TyCtxt, span: Span, supplied: usize,
22552260
} else {
22562261
"expected"
22572262
};
2258-
span_err!(tcx.sess, span, E0243,
2259-
"wrong number of type arguments: {} {}, found {}",
2260-
expected, required, supplied);
2263+
struct_span_err!(tcx.sess, span, E0243, "wrong number of type arguments")
2264+
.span_label(
2265+
span,
2266+
&format!("{} {} type arguments, found {}", expected, required, supplied)
2267+
)
2268+
.emit();
22612269
} else if supplied > accepted {
2262-
let expected = if required < accepted {
2263-
"expected at most"
2270+
let expected = if required == 0 {
2271+
"expected no".to_string()
2272+
} else if required < accepted {
2273+
format!("expected at most {}", accepted)
22642274
} else {
2265-
"expected"
2275+
format!("expected {}", accepted)
22662276
};
2267-
span_err!(tcx.sess, span, E0244,
2268-
"wrong number of type arguments: {} {}, found {}",
2269-
expected,
2270-
accepted,
2271-
supplied);
2277+
2278+
struct_span_err!(tcx.sess, span, E0244, "wrong number of type arguments")
2279+
.span_label(
2280+
span,
2281+
&format!("{} type arguments, found {}", expected, supplied)
2282+
)
2283+
.emit();
22722284
}
22732285
}
22742286

0 commit comments

Comments
 (0)