Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ fn expand_preparsed_asm(
if args.options.contains(ast::InlineAsmOptions::RAW) {
template.push(ast::InlineAsmTemplatePiece::String(template_str.to_string().into()));
let template_num_lines = 1 + template_str.matches('\n').count();
line_spans.extend(std::iter::repeat(template_sp).take(template_num_lines));
line_spans.extend(std::iter::repeat_n(template_sp, template_num_lines));
continue;
}

Expand Down Expand Up @@ -523,7 +523,7 @@ fn expand_preparsed_asm(

if parser.line_spans.is_empty() {
let template_num_lines = 1 + template_str.matches('\n').count();
line_spans.extend(std::iter::repeat(template_sp).take(template_num_lines));
line_spans.extend(std::iter::repeat_n(template_sp, template_num_lines));
} else {
line_spans.extend(
parser
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ fn data_id_for_static(
let mut data = DataDescription::new();
data.set_align(align);
let data_gv = module.declare_data_in_data(data_id, &mut data);
data.define(std::iter::repeat(0).take(pointer_ty(tcx).bytes() as usize).collect());
data.define(std::iter::repeat_n(0, pointer_ty(tcx).bytes() as usize).collect());
data.write_data_addr(0, data_gv, 0);
match module.define_data(ref_data_id, &data) {
// Every time the static is referenced there will be another definition of this global,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/const_eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn get_span_and_frames<'tcx>(
if frame.times < 3 {
let times = frame.times;
frame.times = 0;
frames.extend(std::iter::repeat(frame).take(times as usize));
frames.extend(std::iter::repeat_n(frame, times as usize));
} else {
frames.push(frame);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
.compute_size_in_bytes(layout.size, count)
.ok_or_else(|| err_ub_custom!(fluent::const_eval_size_overflow, name = name))?;

let bytes = std::iter::repeat(byte).take(len.bytes_usize());
let bytes = std::iter::repeat_n(byte, len.bytes_usize());
self.write_bytes_ptr(dst, bytes)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn check_validity_requirement_strict<'tcx>(
if kind == ValidityRequirement::Zero {
cx.write_bytes_ptr(
allocated.ptr(),
std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()),
std::iter::repeat_n(0_u8, ty.layout.size().bytes_usize()),
)
.expect("failed to write bytes for zero valid check");
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ fn get_new_lifetime_name<'tcx>(
let a_to_z_repeat_n = |n| {
(b'a'..=b'z').map(move |c| {
let mut s = '\''.to_string();
s.extend(std::iter::repeat(char::from(c)).take(n));
s.extend(std::iter::repeat_n(char::from(c), n));
s
})
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
hir::GenericArg::Lifetime(lt) => Some(lt),
_ => None,
}) {
return std::iter::repeat(lt.to_string())
.take(num_params_to_take)
return std::iter::repeat_n(lt.to_string(), num_params_to_take)
.collect::<Vec<_>>()
.join(", ");
}
Expand All @@ -362,8 +361,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
matches!(fn_decl.output, hir::FnRetTy::Return(ty) if ty.hir_id == ty_id);

if in_arg || (in_ret && fn_decl.lifetime_elision_allowed) {
return std::iter::repeat("'_".to_owned())
.take(num_params_to_take)
return std::iter::repeat_n("'_".to_owned(), num_params_to_take)
.collect::<Vec<_>>()
.join(", ");
}
Expand All @@ -388,10 +386,12 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
})
| hir::Node::AnonConst(..) = node
{
return std::iter::repeat("'static".to_owned())
.take(num_params_to_take.saturating_sub(ret.len()))
.collect::<Vec<_>>()
.join(", ");
return std::iter::repeat_n(
"'static".to_owned(),
num_params_to_take.saturating_sub(ret.len()),
)
.collect::<Vec<_>>()
.join(", ");
}

let params = if let Some(generics) = node.generics() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let suggestion = |name, args| {
format!(
"::{name}({})",
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", ")
)
};
match &items[..] {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/if_let_rescope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::iter::repeat;
use std::iter::repeat_n;
use std::ops::ControlFlow;

use hir::intravisit::{self, Visitor};
Expand Down Expand Up @@ -351,7 +351,7 @@ impl Subdiagnostic for IfLetRescopeRewrite {
.then_some(" _ => {}".chars())
.into_iter()
.flatten()
.chain(repeat('}').take(closing_brackets.count))
.chain(repeat_n('}', closing_brackets.count))
.collect(),
));
let msg = diag.eagerly_translate(crate::fluent_generated::lint_suggestion);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
};

let fn_sig = ty::Binder::dummy(tcx.mk_fn_sig(
std::iter::repeat(err).take(arity),
std::iter::repeat_n(err, arity),
err,
false,
rustc_hir::Safety::Safe,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/check_tail_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl<'tcx> TailCallCkVisitor<'_, 'tcx> {
fn report_calling_closure(&mut self, fun: &Expr<'_>, tupled_args: Ty<'_>, expr: &Expr<'_>) {
let underscored_args = match tupled_args.kind() {
ty::Tuple(tys) if tys.is_empty() => "".to_owned(),
ty::Tuple(tys) => std::iter::repeat("_, ").take(tys.len() - 1).chain(["_"]).collect(),
ty::Tuple(tys) => std::iter::repeat_n("_, ", tys.len() - 1).chain(["_"]).collect(),
_ => "_".to_owned(),
};

Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2019,10 +2019,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}

let expected_sig = tcx.mk_fn_sig(
std::iter::repeat(token_stream).take(match kind {
ProcMacroKind::Attribute => 2,
ProcMacroKind::Derive | ProcMacroKind::FunctionLike => 1,
}),
std::iter::repeat_n(
token_stream,
match kind {
ProcMacroKind::Attribute => 2,
ProcMacroKind::Derive | ProcMacroKind::FunctionLike => 1,
},
),
token_stream,
false,
Safety::Safe,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_query_system/src/dep_graph/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ impl<D: Deps> EncoderState<D> {
edge_count: 0,
node_count: 0,
encoder: MemEncoder::new(),
kind_stats: iter::repeat(0).take(D::DEP_KIND_MAX as usize + 1).collect(),
kind_stats: iter::repeat_n(0, D::DEP_KIND_MAX as usize + 1).collect(),
})
}),
marker: PhantomData,
Expand Down Expand Up @@ -735,7 +735,7 @@ impl<D: Deps> EncoderState<D> {

let mut encoder = self.file.lock().take().unwrap();

let mut kind_stats: Vec<u32> = iter::repeat(0).take(D::DEP_KIND_MAX as usize + 1).collect();
let mut kind_stats: Vec<u32> = iter::repeat_n(0, D::DEP_KIND_MAX as usize + 1).collect();

let mut node_max = 0;
let mut node_count = 0;
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2214,10 +2214,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
.collect::<Vec<_>>();
items.sort_by_key(|(order, _, _)| *order);
let suggestion = |name, args| {
format!(
"::{name}({})",
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
)
format!("::{name}({})", std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", "))
};
match &items[..] {
[] => {}
Expand Down Expand Up @@ -3485,17 +3482,14 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
(lt.span.shrink_to_hi(), format!("{existing_name} "))
}
MissingLifetimeKind::Comma => {
let sugg: String = std::iter::repeat([existing_name.as_str(), ", "])
.take(lt.count)
let sugg: String = std::iter::repeat_n([existing_name.as_str(), ", "], lt.count)
.flatten()
.collect();
(lt.span.shrink_to_hi(), sugg)
}
MissingLifetimeKind::Brackets => {
let sugg: String = std::iter::once("<")
.chain(
std::iter::repeat(existing_name.as_str()).take(lt.count).intersperse(", "),
)
.chain(std::iter::repeat_n(existing_name.as_str(), lt.count).intersperse(", "))
.chain([">"])
.collect();
(lt.span.shrink_to_hi(), sugg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn make_elided_region_spans_suggs<'a>(
consecutive_brackets += 1;
} else if let Some(bracket_span) = bracket_span.take() {
let sugg = std::iter::once("<")
.chain(std::iter::repeat(name).take(consecutive_brackets).intersperse(", "))
.chain(std::iter::repeat_n(name, consecutive_brackets).intersperse(", "))
.chain([">"])
.collect();
spans_suggs.push((bracket_span.shrink_to_hi(), sugg));
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_transmute/src/layout/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,7 @@ pub(crate) mod rustc {
let inner_layout = layout_of(cx, *inner_ty)?;
assert_eq!(*stride, inner_layout.size);
let elt = Tree::from_ty(*inner_ty, cx)?;
Ok(std::iter::repeat(elt)
.take(*count as usize)
Ok(std::iter::repeat_n(elt, *count as usize)
.fold(Tree::unit(), |tree, elt| tree.then(elt)))
}

Expand Down
4 changes: 4 additions & 0 deletions library/core/src/iter/sources/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ use crate::num::NonZero;
/// Infinite iterators like `repeat()` are often used with adapters like
/// [`Iterator::take()`], in order to make them finite.
///
/// If you know the number of repetitions in advance, consider using [`repeat_n()`]
/// instead, as it is more efficient and conveys the intent more clearly.
///
/// Use [`str::repeat()`] instead of this function if you just want to repeat
/// a char/string `n` times.
///
/// If the element type of the iterator you need does not implement `Clone`,
/// or if you do not want to keep the repeated element in memory, you can
/// instead use the [`repeat_with()`] function.
///
/// [`repeat_n()`]: crate::iter::repeat_n
/// [`repeat_with()`]: crate::iter::repeat_with
/// [`str::repeat()`]: ../../std/primitive.str.html#method.repeat
///
Expand Down
Loading