Skip to content

Rollup of 5 pull requests #64313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 9, 2019
2 changes: 1 addition & 1 deletion config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
# default.
#extended = false

# Installs chosen set of extended tools if enables. By default builds all.
# Installs chosen set of extended tools if enabled. By default builds all.
# If chosen tool failed to build the installation fails.
#tools = ["cargo", "rls", "clippy", "rustfmt", "analysis", "src"]

Expand Down
8 changes: 8 additions & 0 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,14 @@ def update_submodules(self):
if (not os.path.exists(os.path.join(self.rust_root, ".git"))) or \
self.get_toml('submodules') == "false":
return

# check the existence of 'git' command
try:
subprocess.check_output(['git', '--version'])
except (subprocess.CalledProcessError, OSError):
print("error: `git` is not found, please make sure it's installed and in the path.")
sys.exit(1)

slow_submodules = self.get_toml('fast-submodules') == "false"
start_time = time()
if slow_submodules:
Expand Down
44 changes: 43 additions & 1 deletion src/libcore/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::usize;
use crate::intrinsics;

use super::{Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedLen};
use super::LoopState;
use super::{LoopState, from_fn};

mod chain;
mod flatten;
Expand Down Expand Up @@ -534,6 +534,26 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
self.iter.nth(nth - 1);
}
}

fn try_fold<Acc, F, R>(&mut self, mut acc: Acc, mut f: F) -> R
where
F: FnMut(Acc, Self::Item) -> R,
R: Try<Ok = Acc>,
{
#[inline]
fn nth<I: Iterator>(iter: &mut I, step: usize) -> impl FnMut() -> Option<I::Item> + '_ {
move || iter.nth(step)
}

if self.first_take {
self.first_take = false;
match self.iter.next() {
None => return Try::from_ok(acc),
Some(x) => acc = f(acc, x)?,
}
}
from_fn(nth(&mut self.iter, self.step)).try_fold(acc, f)
}
}

impl<I> StepBy<I> where I: ExactSizeIterator {
Expand Down Expand Up @@ -567,6 +587,28 @@ impl<I> DoubleEndedIterator for StepBy<I> where I: DoubleEndedIterator + ExactSi
.saturating_add(self.next_back_index());
self.iter.nth_back(n)
}

fn try_rfold<Acc, F, R>(&mut self, init: Acc, mut f: F) -> R
where
F: FnMut(Acc, Self::Item) -> R,
R: Try<Ok = Acc>,
{
#[inline]
fn nth_back<I: DoubleEndedIterator>(
iter: &mut I,
step: usize,
) -> impl FnMut() -> Option<I::Item> + '_ {
move || iter.nth_back(step)
}

match self.next_back() {
None => Try::from_ok(init),
Some(x) => {
let acc = f(init, x)?;
from_fn(nth_back(&mut self.iter, self.step)).try_fold(acc, f)
}
}
}
}

// StepBy can only make the iterator shorter, so the len will still fit.
Expand Down
35 changes: 35 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,23 @@ fn test_iterator_step_by_nth_overflow() {
assert_eq!(it.0, (usize::MAX as Bigger) * 1);
}

#[test]
fn test_iterator_step_by_nth_try_fold() {
let mut it = (0..).step_by(10);
assert_eq!(it.try_fold(0, i8::checked_add), None);
assert_eq!(it.next(), Some(60));
assert_eq!(it.try_fold(0, i8::checked_add), None);
assert_eq!(it.next(), Some(90));

let mut it = (100..).step_by(10);
assert_eq!(it.try_fold(50, i8::checked_add), None);
assert_eq!(it.next(), Some(110));

let mut it = (100..=100).step_by(10);
assert_eq!(it.next(), Some(100));
assert_eq!(it.try_fold(0, i8::checked_add), Some(0));
}

#[test]
fn test_iterator_step_by_nth_back() {
let mut it = (0..16).step_by(5);
Expand All @@ -410,6 +427,24 @@ fn test_iterator_step_by_nth_back() {
assert_eq!(it().nth_back(42), None);
}

#[test]
fn test_iterator_step_by_nth_try_rfold() {
let mut it = (0..100).step_by(10);
assert_eq!(it.try_rfold(0, i8::checked_add), None);
assert_eq!(it.next_back(), Some(70));
assert_eq!(it.next(), Some(0));
assert_eq!(it.try_rfold(0, i8::checked_add), None);
assert_eq!(it.next_back(), Some(30));

let mut it = (0..100).step_by(10);
assert_eq!(it.try_rfold(50, i8::checked_add), None);
assert_eq!(it.next_back(), Some(80));

let mut it = (100..=100).step_by(10);
assert_eq!(it.next_back(), Some(100));
assert_eq!(it.try_fold(0, i8::checked_add), Some(0));
}

#[test]
#[should_panic]
fn test_iterator_step_by_zero() {
Expand Down
58 changes: 57 additions & 1 deletion src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
}

fn visit_variant(&mut self, v: &'a Variant) {
if v.is_placeholder {
return self.visit_macro_invoc(v.id);
}
let def = self.create_def(v.id,
DefPathData::TypeNs(v.ident.as_interned_str()),
v.span);
Expand All @@ -168,16 +171,24 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {

fn visit_variant_data(&mut self, data: &'a VariantData) {
for (index, field) in data.fields().iter().enumerate() {
if field.is_placeholder {
self.visit_macro_invoc(field.id);
continue;
}
let name = field.ident.map(|ident| ident.name)
.unwrap_or_else(|| sym::integer(index));
let def = self.create_def(field.id,
DefPathData::ValueNs(name.as_interned_str()),
field.span);
self.with_parent(def, |this| this.visit_struct_field(field));
self.with_parent(def, |this| visit::walk_struct_field(this, field));
}
}

fn visit_generic_param(&mut self, param: &'a GenericParam) {
if param.is_placeholder {
self.visit_macro_invoc(param.id);
return;
}
let name = param.ident.as_interned_str();
let def_path_data = match param.kind {
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
Expand Down Expand Up @@ -294,4 +305,49 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
}
}
}

fn visit_arm(&mut self, arm: &'a Arm) {
if arm.is_placeholder {
self.visit_macro_invoc(arm.id)
} else {
visit::walk_arm(self, arm)
}
}

fn visit_field(&mut self, f: &'a Field) {
if f.is_placeholder {
self.visit_macro_invoc(f.id)
} else {
visit::walk_field(self, f)
}
}

fn visit_field_pattern(&mut self, fp: &'a FieldPat) {
if fp.is_placeholder {
self.visit_macro_invoc(fp.id)
} else {
visit::walk_field_pattern(self, fp)
}
}

fn visit_param(&mut self, p: &'a Param) {
if p.is_placeholder {
self.visit_macro_invoc(p.id)
} else {
visit::walk_param(self, p)
}
}

fn visit_struct_field(&mut self, sf: &'a StructField) {
if sf.is_placeholder {
self.visit_macro_invoc(sf.id)
} else {
let name = sf.ident.map(|ident| ident.name)
.unwrap_or_else(|| panic!("don't know the field number in this context"));
let def = self.create_def(sf.id,
DefPathData::ValueNs(name.as_interned_str()),
sf.span);
self.with_parent(def, |this| visit::walk_struct_field(this, sf));
}
}
}
Loading