Skip to content

Commit 2fbb63a

Browse files
committed
Auto merge of #48476 - Manishearth:rollup, r=Manishearth
Rollup of 12 pull requests - Successful merges: #47933, #48072, #48083, #48123, #48157, #48219, #48221, #48245, #48429, #48436, #48438, #48472 - Failed merges:
2 parents 063deba + b26442a commit 2fbb63a

File tree

35 files changed

+1035
-313
lines changed

35 files changed

+1035
-313
lines changed

src/Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/liballoc/linked_list.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,8 @@ impl<T> LinkedList<T> {
747747
/// Creates an iterator which uses a closure to determine if an element should be removed.
748748
///
749749
/// If the closure returns true, then the element is removed and yielded.
750-
/// If the closure returns false, it will try again, and call the closure on the next element,
751-
/// seeing if it passes the test.
750+
/// If the closure returns false, the element will remain in the list and will not be yielded
751+
/// by the iterator.
752752
///
753753
/// Note that `drain_filter` lets you mutate every element in the filter closure, regardless of
754754
/// whether you choose to keep or remove it.

src/liballoc/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl String {
364364
///
365365
/// Given that the `String` is empty, this will not allocate any initial
366366
/// buffer. While that means that this initial operation is very
367-
/// inexpensive, but may cause excessive allocation later, when you add
367+
/// inexpensive, it may cause excessive allocation later when you add
368368
/// data. If you have an idea of how much data the `String` will hold,
369369
/// consider the [`with_capacity`] method to prevent excessive
370370
/// re-allocation.

src/liballoc/vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1966,8 +1966,8 @@ impl<T> Vec<T> {
19661966
/// Creates an iterator which uses a closure to determine if an element should be removed.
19671967
///
19681968
/// If the closure returns true, then the element is removed and yielded.
1969-
/// If the closure returns false, it will try again, and call the closure
1970-
/// on the next element, seeing if it passes the test.
1969+
/// If the closure returns false, the element will remain in the vector and will not be yielded
1970+
/// by the iterator.
19711971
///
19721972
/// Using this method is equivalent to the following code:
19731973
///

src/libcore/iter/iterator.rs

+42-6
Original file line numberDiff line numberDiff line change
@@ -1366,9 +1366,9 @@ pub trait Iterator {
13661366
///
13671367
/// In particular, try to have this call `try_fold()` on the internal parts
13681368
/// from which this iterator is composed. If multiple calls are needed,
1369-
/// the `?` operator be convenient for chaining the accumulator value along,
1370-
/// but beware any invariants that need to be upheld before those early
1371-
/// returns. This is a `&mut self` method, so iteration needs to be
1369+
/// the `?` operator may be convenient for chaining the accumulator value
1370+
/// along, but beware any invariants that need to be upheld before those
1371+
/// early returns. This is a `&mut self` method, so iteration needs to be
13721372
/// resumable after hitting an error here.
13731373
///
13741374
/// # Examples
@@ -1414,6 +1414,42 @@ pub trait Iterator {
14141414
Try::from_ok(accum)
14151415
}
14161416

1417+
/// An iterator method that applies a fallible function to each item in the
1418+
/// iterator, stopping at the first error and returning that error.
1419+
///
1420+
/// This can also be thought of as the fallible form of [`for_each()`]
1421+
/// or as the stateless version of [`try_fold()`].
1422+
///
1423+
/// [`for_each()`]: #method.for_each
1424+
/// [`try_fold()`]: #method.try_fold
1425+
///
1426+
/// # Examples
1427+
///
1428+
/// ```
1429+
/// #![feature(iterator_try_fold)]
1430+
/// use std::fs::rename;
1431+
/// use std::io::{stdout, Write};
1432+
/// use std::path::Path;
1433+
///
1434+
/// let data = ["no_tea.txt", "stale_bread.json", "torrential_rain.png"];
1435+
///
1436+
/// let res = data.iter().try_for_each(|x| writeln!(stdout(), "{}", x));
1437+
/// assert!(res.is_ok());
1438+
///
1439+
/// let mut it = data.iter().cloned();
1440+
/// let res = it.try_for_each(|x| rename(x, Path::new(x).with_extension("old")));
1441+
/// assert!(res.is_err());
1442+
/// // It short-circuited, so the remaining items are still in the iterator:
1443+
/// assert_eq!(it.next(), Some("stale_bread.json"));
1444+
/// ```
1445+
#[inline]
1446+
#[unstable(feature = "iterator_try_fold", issue = "45594")]
1447+
fn try_for_each<F, R>(&mut self, mut f: F) -> R where
1448+
Self: Sized, F: FnMut(Self::Item) -> R, R: Try<Ok=()>
1449+
{
1450+
self.try_fold((), move |(), x| f(x))
1451+
}
1452+
14171453
/// An iterator method that applies a function, producing a single, final value.
14181454
///
14191455
/// `fold()` takes two arguments: an initial value, and a closure with two
@@ -1532,7 +1568,7 @@ pub trait Iterator {
15321568
fn all<F>(&mut self, mut f: F) -> bool where
15331569
Self: Sized, F: FnMut(Self::Item) -> bool
15341570
{
1535-
self.try_fold((), move |(), x| {
1571+
self.try_for_each(move |x| {
15361572
if f(x) { LoopState::Continue(()) }
15371573
else { LoopState::Break(()) }
15381574
}) == LoopState::Continue(())
@@ -1581,7 +1617,7 @@ pub trait Iterator {
15811617
Self: Sized,
15821618
F: FnMut(Self::Item) -> bool
15831619
{
1584-
self.try_fold((), move |(), x| {
1620+
self.try_for_each(move |x| {
15851621
if f(x) { LoopState::Break(()) }
15861622
else { LoopState::Continue(()) }
15871623
}) == LoopState::Break(())
@@ -1635,7 +1671,7 @@ pub trait Iterator {
16351671
Self: Sized,
16361672
P: FnMut(&Self::Item) -> bool,
16371673
{
1638-
self.try_fold((), move |(), x| {
1674+
self.try_for_each(move |x| {
16391675
if predicate(&x) { LoopState::Break(x) }
16401676
else { LoopState::Continue(()) }
16411677
}).break_value()

src/libproc_macro/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,12 @@ pub mod __internal {
844844
})
845845
}
846846

847+
pub fn in_sess() -> bool
848+
{
849+
let p = CURRENT_SESS.with(|p| p.get());
850+
!p.0.is_null()
851+
}
852+
847853
pub fn with_sess<F, R>(f: F) -> R
848854
where F: FnOnce((&ParseSess, Mark)) -> R
849855
{

src/librustc/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ bitflags = "1.0"
1414
fmt_macros = { path = "../libfmt_macros" }
1515
graphviz = { path = "../libgraphviz" }
1616
jobserver = "0.1"
17+
lazy_static = "1.0.0"
1718
log = { version = "0.4", features = ["release_max_level_info", "std"] }
19+
proc_macro = { path = "../libproc_macro" }
1820
rustc_apfloat = { path = "../librustc_apfloat" }
1921
rustc_back = { path = "../librustc_back" }
2022
rustc_const_math = { path = "../librustc_const_math" }

src/librustc/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#![feature(never_type)]
6161
#![feature(non_exhaustive)]
6262
#![feature(nonzero)]
63+
#![feature(proc_macro_internals)]
6364
#![feature(quote)]
6465
#![feature(refcell_replace_swap)]
6566
#![feature(rustc_diagnostic_macros)]
@@ -81,6 +82,7 @@ extern crate core;
8182
extern crate fmt_macros;
8283
extern crate getopts;
8384
extern crate graphviz;
85+
#[macro_use] extern crate lazy_static;
8486
#[cfg(windows)]
8587
extern crate libc;
8688
extern crate rustc_back;
@@ -92,6 +94,7 @@ extern crate rustc_errors as errors;
9294
#[macro_use] extern crate syntax;
9395
extern crate syntax_pos;
9496
extern crate jobserver;
97+
extern crate proc_macro;
9598

9699
extern crate serialize as rustc_serialize; // used by deriving
97100

src/librustc/middle/resolve_lifetime.rs

+71-11
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ enum Scope<'a> {
270270
/// we should use for an early-bound region?
271271
next_early_index: u32,
272272

273+
/// Whether or not this binder would serve as the parent
274+
/// binder for abstract types introduced within. For example:
275+
///
276+
/// fn foo<'a>() -> impl for<'b> Trait<Item = impl Trait2<'a>>
277+
///
278+
/// Here, the abstract types we create for the `impl Trait`
279+
/// and `impl Trait2` references will both have the `foo` item
280+
/// as their parent. When we get to `impl Trait2`, we find
281+
/// that it is nested within the `for<>` binder -- this flag
282+
/// allows us to skip that when looking for the parent binder
283+
/// of the resulting abstract type.
284+
abstract_type_parent: bool,
285+
273286
s: ScopeRef<'a>,
274287
},
275288

@@ -498,6 +511,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
498511
let scope = Scope::Binder {
499512
lifetimes,
500513
next_early_index,
514+
abstract_type_parent: true,
501515
s: ROOT_SCOPE,
502516
};
503517
self.with(scope, |old_scope, this| {
@@ -541,6 +555,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
541555
.collect(),
542556
s: self.scope,
543557
next_early_index,
558+
abstract_type_parent: false,
544559
};
545560
self.with(scope, |old_scope, this| {
546561
// a bare fn has no bounds, so everything
@@ -614,7 +629,10 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
614629
ref generics,
615630
ref bounds,
616631
} = *exist_ty;
617-
let mut index = self.next_early_index();
632+
633+
// We want to start our early-bound indices at the end of the parent scope,
634+
// not including any parent `impl Trait`s.
635+
let mut index = self.next_early_index_for_abstract_type();
618636
debug!("visit_ty: index = {}", index);
619637

620638
let mut elision = None;
@@ -638,7 +656,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
638656
s: self.scope
639657
};
640658
self.with(scope, |_old_scope, this| {
641-
let scope = Scope::Binder { lifetimes, next_early_index, s: this.scope };
659+
let scope = Scope::Binder {
660+
lifetimes,
661+
next_early_index,
662+
s: this.scope,
663+
abstract_type_parent: false,
664+
};
642665
this.with(scope, |_old_scope, this| {
643666
this.visit_generics(generics);
644667
for bound in bounds {
@@ -647,7 +670,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
647670
});
648671
});
649672
} else {
650-
let scope = Scope::Binder { lifetimes, next_early_index, s: self.scope };
673+
let scope = Scope::Binder {
674+
lifetimes,
675+
next_early_index,
676+
s: self.scope,
677+
abstract_type_parent: false,
678+
};
651679
self.with(scope, |_old_scope, this| {
652680
this.visit_generics(generics);
653681
for bound in bounds {
@@ -681,7 +709,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
681709
.collect();
682710

683711
let next_early_index = index + generics.ty_params().count() as u32;
684-
let scope = Scope::Binder { lifetimes, next_early_index, s: self.scope };
712+
let scope = Scope::Binder {
713+
lifetimes,
714+
next_early_index,
715+
s: self.scope,
716+
abstract_type_parent: true,
717+
};
685718
self.with(scope, |_old_scope, this| {
686719
this.visit_generics(generics);
687720
for bound in bounds {
@@ -721,7 +754,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
721754
.collect();
722755

723756
let next_early_index = index + generics.ty_params().count() as u32;
724-
let scope = Scope::Binder { lifetimes, next_early_index, s: self.scope };
757+
let scope = Scope::Binder {
758+
lifetimes,
759+
next_early_index,
760+
s: self.scope,
761+
abstract_type_parent: true,
762+
};
725763
self.with(scope, |_old_scope, this| {
726764
this.visit_generics(generics);
727765
this.visit_ty(ty);
@@ -792,6 +830,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
792830
.collect(),
793831
s: self.scope,
794832
next_early_index,
833+
abstract_type_parent: false,
795834
};
796835
let result = self.with(scope, |old_scope, this| {
797836
this.check_lifetime_params(old_scope, &bound_generic_params);
@@ -853,6 +892,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
853892
.collect(),
854893
s: self.scope,
855894
next_early_index,
895+
abstract_type_parent: false,
856896
};
857897
self.with(scope, |old_scope, this| {
858898
this.check_lifetime_params(old_scope, &trait_ref.bound_generic_params);
@@ -1046,6 +1086,7 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body) {
10461086
ref lifetimes,
10471087
s,
10481088
next_early_index: _,
1089+
abstract_type_parent: _,
10491090
} => {
10501091
// FIXME (#24278): non-hygienic comparison
10511092
if let Some(def) = lifetimes.get(&hir::LifetimeName::Name(label)) {
@@ -1303,32 +1344,49 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13031344
lifetimes,
13041345
next_early_index,
13051346
s: self.scope,
1347+
abstract_type_parent: true,
13061348
};
13071349
self.with(scope, move |old_scope, this| {
13081350
this.check_lifetime_params(old_scope, &generics.params);
13091351
this.hack(walk); // FIXME(#37666) workaround in place of `walk(this)`
13101352
});
13111353
}
13121354

1313-
/// Returns the next index one would use for an early-bound-region
1314-
/// if extending the current scope.
1315-
fn next_early_index(&self) -> u32 {
1355+
fn next_early_index_helper(&self, only_abstract_type_parent: bool) -> u32 {
13161356
let mut scope = self.scope;
13171357
loop {
13181358
match *scope {
13191359
Scope::Root => return 0,
13201360

13211361
Scope::Binder {
1322-
next_early_index, ..
1323-
} => return next_early_index,
1362+
next_early_index,
1363+
abstract_type_parent,
1364+
..
1365+
} if (!only_abstract_type_parent || abstract_type_parent)
1366+
=> return next_early_index,
13241367

1325-
Scope::Body { s, .. }
1368+
Scope::Binder { s, .. }
1369+
| Scope::Body { s, .. }
13261370
| Scope::Elision { s, .. }
13271371
| Scope::ObjectLifetimeDefault { s, .. } => scope = s,
13281372
}
13291373
}
13301374
}
13311375

1376+
/// Returns the next index one would use for an early-bound-region
1377+
/// if extending the current scope.
1378+
fn next_early_index(&self) -> u32 {
1379+
self.next_early_index_helper(true)
1380+
}
1381+
1382+
/// Returns the next index one would use for an `impl Trait` that
1383+
/// is being converted into an `abstract type`. This will be the
1384+
/// next early index from the enclosing item, for the most
1385+
/// part. See the `abstract_type_parent` field for more info.
1386+
fn next_early_index_for_abstract_type(&self) -> u32 {
1387+
self.next_early_index_helper(false)
1388+
}
1389+
13321390
fn resolve_lifetime_ref(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
13331391
debug!("resolve_lifetime_ref(lifetime_ref={:?})", lifetime_ref);
13341392
// Walk up the scope chain, tracking the number of fn scopes
@@ -1353,6 +1411,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13531411
ref lifetimes,
13541412
s,
13551413
next_early_index: _,
1414+
abstract_type_parent: _,
13561415
} => {
13571416
if let Some(&def) = lifetimes.get(&lifetime_ref.name) {
13581417
break Some(def.shifted(late_depth));
@@ -2102,6 +2161,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21022161
ref lifetimes,
21032162
s,
21042163
next_early_index: _,
2164+
abstract_type_parent: _,
21052165
} => {
21062166
if let Some(&def) = lifetimes.get(&lifetime.name) {
21072167
let node_id = self.tcx.hir.as_local_node_id(def.id().unwrap()).unwrap();

0 commit comments

Comments
 (0)