Skip to content

Commit f002610

Browse files
committed
Reduce verbosity when calling for-loop on non-Iterator expression
1 parent f4f3b2d commit f002610

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ pub enum ObligationCauseCode<'tcx> {
350350

351351
AwaitableExpr,
352352

353+
ForLoopIterator,
354+
353355
/// Well-formed checking. If a `WellFormedLoc` is provided,
354356
/// then it will be used to eprform HIR-based wf checking
355357
/// after an error occurs, in order to generate a more precise error span.

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
887887
let span = obligation.cause.span;
888888

889889
if let ObligationCauseCode::AwaitableExpr = obligation.cause.code {
890-
// FIXME: use `trait_ref.self_ty().no_bound_vars()` to typecheck if `()` and if not
891-
// maybe suggest returning instead?
890+
// FIXME: use `obligation.predicate.kind()...trait_ref.self_ty()` to see if we have `()`
891+
// and if not maybe suggest doing something else? If we kept the expression around we
892+
// could also check if it is an fn call (very likely) and suggest changing *that*, if
893+
// it is from the local crate.
892894
err.span_suggestion_verbose(
893895
span,
894896
"do not `.await` the expression",
@@ -1961,6 +1963,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19611963
| ObligationCauseCode::ReturnValue(_)
19621964
| ObligationCauseCode::BlockTailExpression(_)
19631965
| ObligationCauseCode::AwaitableExpr
1966+
| ObligationCauseCode::ForLoopIterator
19641967
| ObligationCauseCode::LetElse => {}
19651968
ObligationCauseCode::SliceOrArrayElem => {
19661969
err.note("slice and array elements must have `Sized` type");

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
816816
&substs,
817817
match lang_item {
818818
hir::LangItem::FuturePoll => ObligationCauseCode::AwaitableExpr,
819-
// FIXME: see if there are other obligation specializations we could do here beyond
820-
// what we do above for `.await`.
819+
hir::LangItem::IntoIterIntoIter => ObligationCauseCode::ForLoopIterator,
820+
// FIXME: This could also be used for `?`. See if there are others.
821821
_ => traits::ItemObligation(def_id),
822822
},
823823
);

src/test/ui/issues/issue-33941.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ error[E0271]: type mismatch resolving `<std::collections::hash_map::Iter<'_, _,
1616
--> $DIR/issue-33941.rs:4:14
1717
|
1818
LL | for _ in HashMap::new().iter().cloned() {}
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found tuple
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected tuple, found reference
2020
|
21-
= note: expected reference `&_`
22-
found tuple `(&_, &_)`
21+
= note: expected tuple `(&_, &_)`
22+
found reference `&_`
2323
= note: required because of the requirements on the impl of `Iterator` for `Cloned<std::collections::hash_map::Iter<'_, _, _>>`
2424
= note: required because of the requirements on the impl of `IntoIterator` for `Cloned<std::collections::hash_map::Iter<'_, _, _>>`
2525

0 commit comments

Comments
 (0)