Skip to content

Commit c867cbf

Browse files
committed
Auto merge of rust-lang#14216 - Veykril:simplify, r=Veykril
minor: Simplify
2 parents b38fcde + 29a4453 commit c867cbf

File tree

7 files changed

+62
-71
lines changed

7 files changed

+62
-71
lines changed

crates/hir-ty/src/method_resolution.rs

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,9 @@ pub fn iterate_method_candidates_dyn(
821821

822822
let mut table = InferenceTable::new(db, env.clone());
823823
let ty = table.instantiate_canonical(ty.clone());
824-
let (deref_chain, adj) = autoderef_method_receiver(&mut table, ty);
824+
let deref_chain = autoderef_method_receiver(&mut table, ty);
825825

826-
let result = deref_chain.into_iter().zip(adj).try_for_each(|(receiver_ty, adj)| {
826+
let result = deref_chain.into_iter().try_for_each(|(receiver_ty, adj)| {
827827
iterate_method_candidates_with_autoref(
828828
&receiver_ty,
829829
adj,
@@ -867,33 +867,28 @@ fn iterate_method_candidates_with_autoref(
867867
return ControlFlow::Continue(());
868868
}
869869

870-
iterate_method_candidates_by_receiver(
871-
receiver_ty,
872-
first_adjustment.clone(),
873-
db,
874-
env.clone(),
875-
traits_in_scope,
876-
visible_from_module,
877-
name,
878-
&mut callback,
879-
)?;
870+
let mut iterate_method_candidates_by_receiver = move |receiver_ty, first_adjustment| {
871+
iterate_method_candidates_by_receiver(
872+
receiver_ty,
873+
first_adjustment,
874+
db,
875+
env.clone(),
876+
traits_in_scope,
877+
visible_from_module,
878+
name,
879+
&mut callback,
880+
)
881+
};
882+
883+
iterate_method_candidates_by_receiver(receiver_ty, first_adjustment.clone())?;
880884

881885
let refed = Canonical {
882886
value: TyKind::Ref(Mutability::Not, static_lifetime(), receiver_ty.value.clone())
883887
.intern(Interner),
884888
binders: receiver_ty.binders.clone(),
885889
};
886890

887-
iterate_method_candidates_by_receiver(
888-
&refed,
889-
first_adjustment.with_autoref(Mutability::Not),
890-
db,
891-
env.clone(),
892-
traits_in_scope,
893-
visible_from_module,
894-
name,
895-
&mut callback,
896-
)?;
891+
iterate_method_candidates_by_receiver(&refed, first_adjustment.with_autoref(Mutability::Not))?;
897892

898893
let ref_muted = Canonical {
899894
value: TyKind::Ref(Mutability::Mut, static_lifetime(), receiver_ty.value.clone())
@@ -904,12 +899,6 @@ fn iterate_method_candidates_with_autoref(
904899
iterate_method_candidates_by_receiver(
905900
&ref_muted,
906901
first_adjustment.with_autoref(Mutability::Mut),
907-
db,
908-
env,
909-
traits_in_scope,
910-
visible_from_module,
911-
name,
912-
&mut callback,
913902
)
914903
}
915904

@@ -1210,8 +1199,8 @@ pub fn resolve_indexing_op(
12101199
) -> Option<ReceiverAdjustments> {
12111200
let mut table = InferenceTable::new(db, env.clone());
12121201
let ty = table.instantiate_canonical(ty);
1213-
let (deref_chain, adj) = autoderef_method_receiver(&mut table, ty);
1214-
for (ty, adj) in deref_chain.into_iter().zip(adj) {
1202+
let deref_chain = autoderef_method_receiver(&mut table, ty);
1203+
for (ty, adj) in deref_chain {
12151204
let goal = generic_implements_goal(db, env.clone(), index_trait, &ty);
12161205
if db.trait_solve(env.krate, goal.cast(Interner)).is_some() {
12171206
return Some(adj);
@@ -1421,25 +1410,24 @@ fn generic_implements_goal(
14211410
fn autoderef_method_receiver(
14221411
table: &mut InferenceTable<'_>,
14231412
ty: Ty,
1424-
) -> (Vec<Canonical<Ty>>, Vec<ReceiverAdjustments>) {
1425-
let (mut deref_chain, mut adjustments): (Vec<_>, Vec<_>) = (Vec::new(), Vec::new());
1413+
) -> Vec<(Canonical<Ty>, ReceiverAdjustments)> {
1414+
let mut deref_chain: Vec<_> = Vec::new();
14261415
let mut autoderef = autoderef::Autoderef::new(table, ty);
14271416
while let Some((ty, derefs)) = autoderef.next() {
1428-
deref_chain.push(autoderef.table.canonicalize(ty).value);
1429-
adjustments.push(ReceiverAdjustments {
1430-
autoref: None,
1431-
autoderefs: derefs,
1432-
unsize_array: false,
1433-
});
1417+
deref_chain.push((
1418+
autoderef.table.canonicalize(ty).value,
1419+
ReceiverAdjustments { autoref: None, autoderefs: derefs, unsize_array: false },
1420+
));
14341421
}
14351422
// As a last step, we can do array unsizing (that's the only unsizing that rustc does for method receivers!)
1436-
if let (Some((TyKind::Array(parameters, _), binders)), Some(adj)) = (
1437-
deref_chain.last().map(|ty| (ty.value.kind(Interner), ty.binders.clone())),
1438-
adjustments.last().cloned(),
1439-
) {
1423+
if let Some((TyKind::Array(parameters, _), binders, adj)) =
1424+
deref_chain.last().map(|(ty, adj)| (ty.value.kind(Interner), ty.binders.clone(), adj))
1425+
{
14401426
let unsized_ty = TyKind::Slice(parameters.clone()).intern(Interner);
1441-
deref_chain.push(Canonical { value: unsized_ty, binders });
1442-
adjustments.push(ReceiverAdjustments { unsize_array: true, ..adj });
1427+
deref_chain.push((
1428+
Canonical { value: unsized_ty, binders },
1429+
ReceiverAdjustments { unsize_array: true, ..adj.clone() },
1430+
));
14431431
}
1444-
(deref_chain, adjustments)
1432+
deref_chain
14451433
}

crates/hir/src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,12 +3339,10 @@ impl Type {
33393339
.map(move |ty| self.derived(ty))
33403340
}
33413341

3342-
pub fn iterate_method_candidates<T>(
3342+
pub fn iterate_method_candidates_with_traits<T>(
33433343
&self,
33443344
db: &dyn HirDatabase,
33453345
scope: &SemanticsScope<'_>,
3346-
// FIXME this can be retrieved from `scope`, except autoimport uses this
3347-
// to specify a different set, so the method needs to be split
33483346
traits_in_scope: &FxHashSet<TraitId>,
33493347
with_local_impls: Option<Module>,
33503348
name: Option<&Name>,
@@ -3372,6 +3370,24 @@ impl Type {
33723370
slot
33733371
}
33743372

3373+
pub fn iterate_method_candidates<T>(
3374+
&self,
3375+
db: &dyn HirDatabase,
3376+
scope: &SemanticsScope<'_>,
3377+
with_local_impls: Option<Module>,
3378+
name: Option<&Name>,
3379+
callback: impl FnMut(Function) -> Option<T>,
3380+
) -> Option<T> {
3381+
self.iterate_method_candidates_with_traits(
3382+
db,
3383+
scope,
3384+
&scope.visible_traits().0,
3385+
with_local_impls,
3386+
name,
3387+
callback,
3388+
)
3389+
}
3390+
33753391
fn iterate_method_candidates_dyn(
33763392
&self,
33773393
db: &dyn HirDatabase,

crates/hir/src/semantics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,7 @@ impl<'a> SemanticsScope<'a> {
16731673
}
16741674
}
16751675

1676+
#[derive(Debug)]
16761677
pub struct VisibleTraits(pub FxHashSet<TraitId>);
16771678

16781679
impl ops::Deref for VisibleTraits {

crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,12 @@ fn is_ref_and_impls_iter_method(
157157
let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?;
158158

159159
let has_wanted_method = ty
160-
.iterate_method_candidates(
161-
sema.db,
162-
&scope,
163-
&scope.visible_traits().0,
164-
None,
165-
Some(&wanted_method),
166-
|func| {
167-
if func.ret_type(sema.db).impls_trait(sema.db, iter_trait, &[]) {
168-
return Some(());
169-
}
170-
None
171-
},
172-
)
160+
.iterate_method_candidates(sema.db, &scope, None, Some(&wanted_method), |func| {
161+
if func.ret_type(sema.db).impls_trait(sema.db, iter_trait, &[]) {
162+
return Some(());
163+
}
164+
None
165+
})
173166
.is_some();
174167
if !has_wanted_method {
175168
return None;

crates/ide-assists/src/handlers/generate_is_empty_from_len.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,7 @@ fn get_impl_method(
9595

9696
let scope = ctx.sema.scope(impl_.syntax())?;
9797
let ty = impl_def.self_ty(db);
98-
ty.iterate_method_candidates(
99-
db,
100-
&scope,
101-
&scope.visible_traits().0,
102-
None,
103-
Some(fn_name),
104-
|func| Some(func),
105-
)
98+
ty.iterate_method_candidates(db, &scope, None, Some(fn_name), |func| Some(func))
10699
}
107100

108101
#[cfg(test)]

crates/ide-completion/src/completions/dot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn complete_methods(
122122
mut f: impl FnMut(hir::Function),
123123
) {
124124
let mut seen_methods = FxHashSet::default();
125-
receiver.iterate_method_candidates(
125+
receiver.iterate_method_candidates_with_traits(
126126
ctx.db,
127127
&ctx.scope,
128128
&ctx.traits_in_scope(),

crates/ide-db/src/imports/import_assets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ fn trait_applicable_items(
528528
},
529529
)
530530
} else {
531-
trait_candidate.receiver_ty.iterate_method_candidates(
531+
trait_candidate.receiver_ty.iterate_method_candidates_with_traits(
532532
db,
533533
scope,
534534
&trait_candidates,

0 commit comments

Comments
 (0)