Skip to content

Commit 4322a78

Browse files
committed
Auto merge of #97697 - WaffleLapkin:no_ref_vec, r=WaffleLapkin
Replace `&Vec<_>`s with `&[_]`s It's generally preferable to use `&[_]` since it's one less indirection and it can be created from types other that `Vec`. I've left `&Vec` in some locals where it doesn't really matter, in cases where `TypeFoldable` is expected (`TypeFoldable: Clone` so slice can't implement it) and in cases where it's `&TypeAliasThatIsActiallyVec`. Nothing important, really, I was just a little annoyed by `visit_generic_param_vec` :D r? `@compiler-errors`
2 parents a2da4af + cae3c78 commit 4322a78

File tree

14 files changed

+53
-45
lines changed

14 files changed

+53
-45
lines changed

compiler/rustc_errors/src/diagnostic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ impl Diagnostic {
827827
self
828828
}
829829

830-
pub fn styled_message(&self) -> &Vec<(DiagnosticMessage, Style)> {
830+
pub fn styled_message(&self) -> &[(DiagnosticMessage, Style)] {
831831
&self.message
832832
}
833833

@@ -888,11 +888,11 @@ impl Diagnostic {
888888
&self,
889889
) -> (
890890
&Level,
891-
&Vec<(DiagnosticMessage, Style)>,
891+
&[(DiagnosticMessage, Style)],
892892
&Option<DiagnosticId>,
893893
&MultiSpan,
894894
&Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
895-
Option<&Vec<SubDiagnostic>>,
895+
Option<&[SubDiagnostic]>,
896896
) {
897897
(
898898
&self.level,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ pub fn same_type_modulo_infer<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
352352
}
353353

354354
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
355-
pub fn report_region_errors(&self, errors: &Vec<RegionResolutionError<'tcx>>) {
355+
pub fn report_region_errors(&self, errors: &[RegionResolutionError<'tcx>]) {
356356
debug!("report_region_errors(): {} errors to start", errors.len());
357357

358358
// try to pre-process the errors, which will group some of them

compiler/rustc_middle/src/middle/region.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_query_system::ich::StableHashingContext;
1616
use rustc_span::{Span, DUMMY_SP};
1717

1818
use std::fmt;
19+
use std::ops::Deref;
1920

2021
/// Represents a statically-describable scope that can be used to
2122
/// bound the lifetime/region for values.
@@ -407,8 +408,8 @@ impl ScopeTree {
407408

408409
/// Checks whether the given scope contains a `yield`. If so,
409410
/// returns `Some(YieldData)`. If not, returns `None`.
410-
pub fn yield_in_scope(&self, scope: Scope) -> Option<&Vec<YieldData>> {
411-
self.yield_in_scope.get(&scope)
411+
pub fn yield_in_scope(&self, scope: Scope) -> Option<&[YieldData]> {
412+
self.yield_in_scope.get(&scope).map(Deref::deref)
412413
}
413414

414415
/// Gives the number of expressions visited in a body.

compiler/rustc_middle/src/mir/traversal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ pub fn reachable_as_bitset<'tcx>(body: &Body<'tcx>) -> BitSet<BasicBlock> {
310310
#[derive(Clone)]
311311
pub struct ReversePostorderIter<'a, 'tcx> {
312312
body: &'a Body<'tcx>,
313-
blocks: &'a Vec<BasicBlock>,
313+
blocks: &'a [BasicBlock],
314314
idx: usize,
315315
}
316316

@@ -358,9 +358,9 @@ impl PostorderCache {
358358
self.cache = OnceCell::new();
359359
}
360360

361-
/// Returns the &Vec<BasicBlocks> represents the postorder graph for this MIR.
361+
/// Returns the `&[BasicBlocks]` represents the postorder graph for this MIR.
362362
#[inline]
363-
pub(super) fn compute(&self, body: &Body<'_>) -> &Vec<BasicBlock> {
363+
pub(super) fn compute(&self, body: &Body<'_>) -> &[BasicBlock] {
364364
self.cache.get_or_init(|| Postorder::new(body, START_BLOCK).map(|(bb, _)| bb).collect())
365365
}
366366
}

compiler/rustc_middle/src/ty/sty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,13 @@ impl<'tcx, T> Binder<'tcx, T> {
10321032
Binder(&self.0, self.1)
10331033
}
10341034

1035+
pub fn as_deref(&self) -> Binder<'tcx, &T::Target>
1036+
where
1037+
T: Deref,
1038+
{
1039+
Binder(&self.0, self.1)
1040+
}
1041+
10351042
pub fn map_bound_ref_unchecked<F, U>(&self, f: F) -> Binder<'tcx, U>
10361043
where
10371044
F: FnOnce(&T) -> U,

compiler/rustc_mir_build/src/build/expr/as_place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn convert_to_hir_projections_and_truncate_for_capture<'tcx>(
133133
/// ancestor of `foo.x.y`. It's the caller's responsibility to ensure that both projections
134134
/// list are being applied to the same root variable.
135135
fn is_ancestor_or_same_capture(
136-
proj_possible_ancestor: &Vec<HirProjectionKind>,
136+
proj_possible_ancestor: &[HirProjectionKind],
137137
proj_capture: &[HirProjectionKind],
138138
) -> bool {
139139
// We want to make sure `is_ancestor_or_same_capture("x.0.0", "x.0")` to return false.
@@ -187,7 +187,7 @@ fn find_capture_matching_projections<'a, 'tcx>(
187187
// If an ancestor is found, `idx` is the index within the list of captured places
188188
// for root variable `var_hir_id` and `capture` is the `ty::CapturedPlace` itself.
189189
let (idx, capture) = root_variable_min_captures.iter().enumerate().find(|(_, capture)| {
190-
let possible_ancestor_proj_kinds =
190+
let possible_ancestor_proj_kinds: Vec<_> =
191191
capture.place.projections.iter().map(|proj| proj.kind).collect();
192192
is_ancestor_or_same_capture(&possible_ancestor_proj_kinds, &hir_projections)
193193
})?;

compiler/rustc_mir_build/src/build/matches/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
409409
outer_source_info: SourceInfo,
410410
candidate: Candidate<'_, 'tcx>,
411411
guard: Option<&Guard<'tcx>>,
412-
fake_borrow_temps: &Vec<(Place<'tcx>, Local)>,
412+
fake_borrow_temps: &[(Place<'tcx>, Local)],
413413
scrutinee_span: Span,
414414
arm_span: Option<Span>,
415415
arm_scope: Option<region::Scope>,
@@ -1826,7 +1826,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
18261826
candidate: Candidate<'pat, 'tcx>,
18271827
parent_bindings: &[(Vec<Binding<'tcx>>, Vec<Ascription<'tcx>>)],
18281828
guard: Option<&Guard<'tcx>>,
1829-
fake_borrows: &Vec<(Place<'tcx>, Local)>,
1829+
fake_borrows: &[(Place<'tcx>, Local)],
18301830
scrutinee_span: Span,
18311831
arm_span: Option<Span>,
18321832
match_scope: Option<region::Scope>,

compiler/rustc_mir_transform/src/coverage/counters.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl CoverageCounters {
4343
pub fn make_bcb_counters(
4444
&mut self,
4545
basic_coverage_blocks: &mut CoverageGraph,
46-
coverage_spans: &Vec<CoverageSpan>,
46+
coverage_spans: &[CoverageSpan],
4747
) -> Result<Vec<CoverageKind>, Error> {
4848
let mut bcb_counters = BcbCounters::new(self, basic_coverage_blocks);
4949
bcb_counters.make_bcb_counters(coverage_spans)
@@ -349,7 +349,7 @@ impl<'a> BcbCounters<'a> {
349349
// counters and/or expressions of its incoming edges. This will recursively get or create
350350
// counters for those incoming edges first, then call `make_expression()` to sum them up,
351351
// with additional intermediate expressions as needed.
352-
let mut predecessors = self.bcb_predecessors(bcb).clone().into_iter();
352+
let mut predecessors = self.bcb_predecessors(bcb).to_owned().into_iter();
353353
debug!(
354354
"{}{:?} has multiple incoming edges and will get an expression that sums them up...",
355355
NESTED_INDENT.repeat(debug_indent_level),
@@ -571,12 +571,12 @@ impl<'a> BcbCounters<'a> {
571571
}
572572

573573
#[inline]
574-
fn bcb_predecessors(&self, bcb: BasicCoverageBlock) -> &Vec<BasicCoverageBlock> {
574+
fn bcb_predecessors(&self, bcb: BasicCoverageBlock) -> &[BasicCoverageBlock] {
575575
&self.basic_coverage_blocks.predecessors[bcb]
576576
}
577577

578578
#[inline]
579-
fn bcb_successors(&self, bcb: BasicCoverageBlock) -> &Vec<BasicCoverageBlock> {
579+
fn bcb_successors(&self, bcb: BasicCoverageBlock) -> &[BasicCoverageBlock] {
580580
&self.basic_coverage_blocks.successors[bcb]
581581
}
582582

compiler/rustc_mir_transform/src/coverage/debug.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ use rustc_span::Span;
124124

125125
use std::iter;
126126
use std::lazy::SyncOnceCell;
127+
use std::ops::Deref;
127128

128129
pub const NESTED_INDENT: &str = " ";
129130

@@ -434,11 +435,11 @@ impl GraphvizData {
434435
pub fn get_bcb_coverage_spans_with_counters(
435436
&self,
436437
bcb: BasicCoverageBlock,
437-
) -> Option<&Vec<(CoverageSpan, CoverageKind)>> {
438+
) -> Option<&[(CoverageSpan, CoverageKind)]> {
438439
if let Some(bcb_to_coverage_spans_with_counters) =
439440
self.some_bcb_to_coverage_spans_with_counters.as_ref()
440441
{
441-
bcb_to_coverage_spans_with_counters.get(&bcb)
442+
bcb_to_coverage_spans_with_counters.get(&bcb).map(Deref::deref)
442443
} else {
443444
None
444445
}
@@ -457,12 +458,9 @@ impl GraphvizData {
457458
}
458459
}
459460

460-
pub fn get_bcb_dependency_counters(
461-
&self,
462-
bcb: BasicCoverageBlock,
463-
) -> Option<&Vec<CoverageKind>> {
461+
pub fn get_bcb_dependency_counters(&self, bcb: BasicCoverageBlock) -> Option<&[CoverageKind]> {
464462
if let Some(bcb_to_dependency_counters) = self.some_bcb_to_dependency_counters.as_ref() {
465-
bcb_to_dependency_counters.get(&bcb)
463+
bcb_to_dependency_counters.get(&bcb).map(Deref::deref)
466464
} else {
467465
None
468466
}
@@ -571,11 +569,11 @@ impl UsedExpressions {
571569
/// associated with a coverage span).
572570
pub fn validate(
573571
&mut self,
574-
bcb_counters_without_direct_coverage_spans: &Vec<(
572+
bcb_counters_without_direct_coverage_spans: &[(
575573
Option<BasicCoverageBlock>,
576574
BasicCoverageBlock,
577575
CoverageKind,
578-
)>,
576+
)],
579577
) {
580578
if self.is_enabled() {
581579
let mut not_validated = bcb_counters_without_direct_coverage_spans
@@ -634,7 +632,7 @@ pub(super) fn dump_coverage_spanview<'tcx>(
634632
basic_coverage_blocks: &CoverageGraph,
635633
pass_name: &str,
636634
body_span: Span,
637-
coverage_spans: &Vec<CoverageSpan>,
635+
coverage_spans: &[CoverageSpan],
638636
) {
639637
let mir_source = mir_body.source;
640638
let def_id = mir_source.def_id();
@@ -654,7 +652,7 @@ fn span_viewables<'tcx>(
654652
tcx: TyCtxt<'tcx>,
655653
mir_body: &mir::Body<'tcx>,
656654
basic_coverage_blocks: &CoverageGraph,
657-
coverage_spans: &Vec<CoverageSpan>,
655+
coverage_spans: &[CoverageSpan],
658656
) -> Vec<SpanViewable> {
659657
let mut span_viewables = Vec::new();
660658
for coverage_span in coverage_spans {
@@ -676,7 +674,7 @@ pub(super) fn dump_coverage_graphviz<'tcx>(
676674
basic_coverage_blocks: &CoverageGraph,
677675
debug_counters: &DebugCounters,
678676
graphviz_data: &GraphvizData,
679-
intermediate_expressions: &Vec<CoverageKind>,
677+
intermediate_expressions: &[CoverageKind],
680678
debug_used_expressions: &UsedExpressions,
681679
) {
682680
let mir_source = mir_body.source;
@@ -753,9 +751,9 @@ fn bcb_to_string_sections<'tcx>(
753751
mir_body: &mir::Body<'tcx>,
754752
debug_counters: &DebugCounters,
755753
bcb_data: &BasicCoverageBlockData,
756-
some_coverage_spans_with_counters: Option<&Vec<(CoverageSpan, CoverageKind)>>,
757-
some_dependency_counters: Option<&Vec<CoverageKind>>,
758-
some_intermediate_expressions: Option<&Vec<CoverageKind>>,
754+
some_coverage_spans_with_counters: Option<&[(CoverageSpan, CoverageKind)]>,
755+
some_dependency_counters: Option<&[CoverageKind]>,
756+
some_intermediate_expressions: Option<&[CoverageKind]>,
759757
) -> Vec<String> {
760758
let len = bcb_data.basic_blocks.len();
761759
let mut sections = Vec::new();

compiler/rustc_resolve/src/late.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
631631
span,
632632
},
633633
|this| {
634-
this.visit_generic_param_vec(&bare_fn.generic_params, false);
634+
this.visit_generic_params(&bare_fn.generic_params, false);
635635
this.with_lifetime_rib(
636636
LifetimeRibKind::AnonymousPassThrough(ty.id, false),
637637
|this| walk_list!(this, visit_param, &bare_fn.decl.inputs),
@@ -662,7 +662,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
662662
span,
663663
},
664664
|this| {
665-
this.visit_generic_param_vec(&tref.bound_generic_params, false);
665+
this.visit_generic_params(&tref.bound_generic_params, false);
666666
this.smart_resolve_path(
667667
tref.trait_ref.ref_id,
668668
None,
@@ -833,7 +833,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
833833
}
834834

835835
fn visit_generics(&mut self, generics: &'ast Generics) {
836-
self.visit_generic_param_vec(
836+
self.visit_generic_params(
837837
&generics.params,
838838
self.diagnostic_metadata.current_self_item.is_some(),
839839
);
@@ -941,7 +941,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
941941
span,
942942
},
943943
|this| {
944-
this.visit_generic_param_vec(&bound_generic_params, false);
944+
this.visit_generic_params(&bound_generic_params, false);
945945
this.visit_ty(bounded_ty);
946946
for bound in bounds {
947947
this.visit_param_bound(bound, BoundKind::Bound)
@@ -1116,7 +1116,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11161116
}
11171117
}
11181118

1119-
fn visit_generic_param_vec(&mut self, params: &'ast Vec<GenericParam>, add_self_upper: bool) {
1119+
fn visit_generic_params(&mut self, params: &'ast [GenericParam], add_self_upper: bool) {
11201120
// For type parameter defaults, we have to ban access
11211121
// to following type parameters, as the InternalSubsts can only
11221122
// provide previous type parameters as they're built. We
@@ -1870,7 +1870,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18701870

18711871
fn with_generic_param_rib<'c, F>(
18721872
&'c mut self,
1873-
params: &'c Vec<GenericParam>,
1873+
params: &'c [GenericParam],
18741874
kind: RibKind<'a>,
18751875
lifetime_kind: LifetimeRibKind,
18761876
f: F,

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,13 @@ impl<'tcx, 'a> GeneratorData<'tcx, 'a> {
146146
/// that are live across the yield of this generator
147147
fn get_generator_interior_types(
148148
&self,
149-
) -> ty::Binder<'tcx, &Vec<GeneratorInteriorTypeCause<'tcx>>> {
149+
) -> ty::Binder<'tcx, &[GeneratorInteriorTypeCause<'tcx>]> {
150150
match self {
151-
GeneratorData::Local(typeck_result) => typeck_result.generator_interior_types.as_ref(),
151+
GeneratorData::Local(typeck_result) => {
152+
typeck_result.generator_interior_types.as_deref()
153+
}
152154
GeneratorData::Foreign(generator_diagnostic_data) => {
153-
generator_diagnostic_data.generator_interior_types.as_ref()
155+
generator_diagnostic_data.generator_interior_types.as_deref()
154156
}
155157
}
156158
}

compiler/rustc_trait_selection/src/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ pub struct PlaceholderReplacer<'me, 'tcx> {
773773
mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
774774
mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
775775
mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
776-
universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
776+
universe_indices: &'me [Option<ty::UniverseIndex>],
777777
current_index: ty::DebruijnIndex,
778778
}
779779

@@ -783,7 +783,7 @@ impl<'me, 'tcx> PlaceholderReplacer<'me, 'tcx> {
783783
mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
784784
mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
785785
mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
786-
universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
786+
universe_indices: &'me [Option<ty::UniverseIndex>],
787787
value: T,
788788
) -> T {
789789
let mut replacer = PlaceholderReplacer {

compiler/rustc_typeck/src/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2448,7 +2448,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24482448
&self,
24492449
span: Span,
24502450
base_t: Ty<'tcx>,
2451-
) -> Option<(&Vec<ty::FieldDef>, SubstsRef<'tcx>)> {
2451+
) -> Option<(&[ty::FieldDef], SubstsRef<'tcx>)> {
24522452
debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_t);
24532453

24542454
for (base_t, _) in self.autoderef(span, base_t) {

compiler/rustc_typeck/src/check/upvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ fn should_do_rust_2021_incompatible_closure_captures_analysis(
20392039
/// - s2: Comma separated names of the variables being migrated.
20402040
fn migration_suggestion_for_2229(
20412041
tcx: TyCtxt<'_>,
2042-
need_migrations: &Vec<NeededMigration>,
2042+
need_migrations: &[NeededMigration],
20432043
) -> (String, String) {
20442044
let need_migrations_variables = need_migrations
20452045
.iter()

0 commit comments

Comments
 (0)