Skip to content

Commit 5ebfe69

Browse files
committed
Fix the issue of invalid suggestion for a reference of iterator
1 parent a6123db commit 5ebfe69

File tree

3 files changed

+4
-9
lines changed

3 files changed

+4
-9
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
12841284
if self.in_move_closure(expr) {
12851285
return false;
12861286
}
1287+
12871288
// Try to find predicates on *generic params* that would allow copying `ty`
12881289
let mut suggestion =
12891290
if let Some(symbol) = tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
466466
&& let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr)
467467
{
468468
// Suggest dereferencing the argument to a function/method call if possible
469-
470469
let mut real_trait_pred = trait_pred;
471470
while let Some((parent_code, parent_trait_pred)) = code.parent() {
472471
code = parent_code;
@@ -553,6 +552,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
553552
);
554553
if self.predicate_may_hold(&obligation)
555554
&& self.predicate_must_hold_modulo_regions(&sized_obligation)
555+
// Do not suggest * if it is already a reference,
556+
// will suggest removing the borrow instead in that case.
557+
&& !matches!(expr.kind, hir::ExprKind::AddrOf(..))
556558
{
557559
let call_node = self.tcx.hir_node(*call_hir_id);
558560
let msg = "consider dereferencing here";

tests/ui/traits/suggest-dereferences/invalid-suggest-deref-issue-127590.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ LL | for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
1010
= note: required for `&std::slice::Iter<'_, {integer}>` to implement `IntoIterator`
1111
note: required by a bound in `std::iter::zip`
1212
--> $SRC_DIR/core/src/iter/adapters/zip.rs:LL:COL
13-
help: consider dereferencing here
14-
|
15-
LL | for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter()) {
16-
| +
1713
help: consider removing the leading `&`-reference
1814
|
1915
LL - for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
@@ -43,10 +39,6 @@ LL | for (src, dest) in std::iter::zip(fields.iter(), &variant.iter().clone(
4339
= note: required for `&std::slice::Iter<'_, {integer}>` to implement `IntoIterator`
4440
note: required by a bound in `std::iter::zip`
4541
--> $SRC_DIR/core/src/iter/adapters/zip.rs:LL:COL
46-
help: consider dereferencing here
47-
|
48-
LL | for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter().clone()) {
49-
| +
5042
help: consider removing the leading `&`-reference
5143
|
5244
LL - for (src, dest) in std::iter::zip(fields.iter(), &variant.iter().clone()) {

0 commit comments

Comments
 (0)