Skip to content

Commit 9d5cf0f

Browse files
committed
Remove the InternIteratorElement impl for &'a T.
`InternIteratorElement` is a trait used to intern values produces by iterators. There are three impls, corresponding to iterators that produce different types: - One for `T`, which operates straightforwardly. - One for `Result<T, E>`, which is fallible, and will fail early with an error result if any of the iterator elements are errors. - One for `&'a T`, which clones the items as it iterates. That last one is bad: it's extremely easy to use it without realizing that it clones, which goes against Rust's normal "explicit is better" approach to cloning. So this commit just removes it. In practice, there weren't many use sites. For all but one of them `into_iter()` could be used, which avoids the need for cloning. And for the one remaining case `copied()` is used.
1 parent 28184e7 commit 9d5cf0f

File tree

7 files changed

+8
-19
lines changed

7 files changed

+8
-19
lines changed

compiler/rustc_hir_typeck/src/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fn check_lang_start_fn<'tcx>(
264264
let fn_generic = generics.param_at(0, tcx);
265265
let generic_ty = tcx.mk_ty_param(fn_generic.index, fn_generic.name);
266266
let expected_fn_sig =
267-
tcx.mk_fn_sig([].iter(), &generic_ty, false, hir::Unsafety::Normal, Abi::Rust);
267+
tcx.mk_fn_sig([].into_iter(), generic_ty, false, hir::Unsafety::Normal, Abi::Rust);
268268
let expected_ty = tcx.mk_fn_ptr(Binder::dummy(expected_fn_sig));
269269

270270
// we emit the same error to suggest changing the arg no matter what's wrong with the arg

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub fn resolve_interior<'a, 'tcx>(
312312

313313
// Extract type components to build the witness type.
314314
let type_list = fcx.tcx.mk_type_list(type_causes.iter().map(|cause| cause.ty));
315-
let bound_vars = fcx.tcx.mk_bound_variable_kinds(bound_vars.iter());
315+
let bound_vars = fcx.tcx.mk_bound_variable_kinds(bound_vars.into_iter());
316316
let witness =
317317
fcx.tcx.mk_generator_witness(ty::Binder::bind_with_vars(type_list, bound_vars.clone()));
318318

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ impl<'tcx> TyCtxt<'tcx> {
23872387
.unwrap_or_else(|| {
23882388
bug!("No bound vars found for {}", self.hir().node_to_string(id))
23892389
})
2390-
.iter(),
2390+
.into_iter(),
23912391
)
23922392
}
23932393

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,8 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
781781
let output = transform_ty(tcx, fn_sig.skip_binder().output(), options);
782782
ty = tcx.mk_fn_ptr(ty::Binder::bind_with_vars(
783783
tcx.mk_fn_sig(
784-
parameters.iter(),
785-
&output,
784+
parameters.into_iter(),
785+
output,
786786
fn_sig.c_variadic(),
787787
fn_sig.unsafety(),
788788
fn_sig.abi(),

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
359359
let b_last_ty = b_tys.last().unwrap();
360360

361361
// Substitute just the tail field of B., and require that they're equal.
362-
let unsized_a_ty = tcx.mk_tup(a_rest_tys.iter().chain([b_last_ty]));
362+
let unsized_a_ty = tcx.mk_tup(a_rest_tys.iter().chain([b_last_ty]).copied());
363363
let mut nested_goals = ecx.infcx.eq(goal.param_env, unsized_a_ty, b_ty)?;
364364

365365
// Similar to ADTs, require that the rest of the fields are equal.

compiler/rustc_ty_utils/src/abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ fn fn_sig_for_fn_abi<'tcx>(
141141

142142
ty::Binder::bind_with_vars(
143143
tcx.mk_fn_sig(
144-
[env_ty, resume_ty].iter(),
145-
&ret_ty,
144+
[env_ty, resume_ty].into_iter(),
145+
ret_ty,
146146
false,
147147
hir::Unsafety::Normal,
148148
rustc_target::spec::abi::Abi::Rust,

compiler/rustc_type_ir/src/lib.rs

-11
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,6 @@ impl<T, R> InternIteratorElement<T, R> for T {
127127
}
128128
}
129129

130-
impl<'a, T, R> InternIteratorElement<T, R> for &'a T
131-
where
132-
T: Clone + 'a,
133-
{
134-
type Output = R;
135-
fn intern_with<I: Iterator<Item = Self>, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output {
136-
// This code isn't hot.
137-
f(&iter.cloned().collect::<SmallVec<[_; 8]>>())
138-
}
139-
}
140-
141130
impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
142131
type Output = Result<R, E>;
143132
fn intern_with<I: Iterator<Item = Self>, F: FnOnce(&[T]) -> R>(

0 commit comments

Comments
 (0)