fix(rustc): suggest items
be borrowed in for i in items[x..]
Fixes #87994
This commit is contained in:
parent
e3c71f1e33
commit
733bdd079a
3 changed files with 57 additions and 4 deletions
|
@ -23,7 +23,7 @@ use rustc_middle::ty::{
|
||||||
use rustc_middle::ty::{TypeAndMut, TypeckResults};
|
use rustc_middle::ty::{TypeAndMut, TypeckResults};
|
||||||
use rustc_span::def_id::LOCAL_CRATE;
|
use rustc_span::def_id::LOCAL_CRATE;
|
||||||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||||
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};
|
use rustc_span::{BytePos, DesugaringKind, ExpnKind, ForLoopLoc, MultiSpan, Span, DUMMY_SP};
|
||||||
use rustc_target::spec::abi;
|
use rustc_target::spec::abi;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
@ -680,7 +680,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
points_at_arg: bool,
|
points_at_arg: bool,
|
||||||
has_custom_message: bool,
|
has_custom_message: bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
if !points_at_arg {
|
let span = obligation.cause.span;
|
||||||
|
let points_at_for_iter = matches!(
|
||||||
|
span.ctxt().outer_expn_data().kind,
|
||||||
|
ExpnKind::Desugaring(DesugaringKind::ForLoop(ForLoopLoc::IntoIter))
|
||||||
|
);
|
||||||
|
|
||||||
|
if !points_at_arg && !points_at_for_iter {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -695,7 +701,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
|
|
||||||
never_suggest_borrow.push(self.tcx.get_diagnostic_item(sym::send_trait).unwrap());
|
never_suggest_borrow.push(self.tcx.get_diagnostic_item(sym::send_trait).unwrap());
|
||||||
|
|
||||||
let span = obligation.cause.span;
|
|
||||||
let param_env = obligation.param_env;
|
let param_env = obligation.param_env;
|
||||||
let trait_ref = trait_ref.skip_binder();
|
let trait_ref = trait_ref.skip_binder();
|
||||||
|
|
||||||
|
@ -754,7 +759,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
);
|
);
|
||||||
|
|
||||||
// This if is to prevent a special edge-case
|
// This if is to prevent a special edge-case
|
||||||
if !span.from_expansion() {
|
if matches!(
|
||||||
|
span.ctxt().outer_expn_data().kind,
|
||||||
|
ExpnKind::Root
|
||||||
|
| ExpnKind::Desugaring(DesugaringKind::ForLoop(ForLoopLoc::IntoIter))
|
||||||
|
) {
|
||||||
// We don't want a borrowing suggestion on the fields in structs,
|
// We don't want a borrowing suggestion on the fields in structs,
|
||||||
// ```
|
// ```
|
||||||
// struct Foo {
|
// struct Foo {
|
||||||
|
|
7
src/test/ui/suggestions/slice-issue-87994.rs
Normal file
7
src/test/ui/suggestions/slice-issue-87994.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
fn main() {
|
||||||
|
let v = vec![1i32, 2, 3];
|
||||||
|
for _ in v[1..] {
|
||||||
|
//~^ ERROR [i32]` is not an iterator [E0277]
|
||||||
|
//~^^ ERROR known at compilation time
|
||||||
|
}
|
||||||
|
}
|
37
src/test/ui/suggestions/slice-issue-87994.stderr
Normal file
37
src/test/ui/suggestions/slice-issue-87994.stderr
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
|
||||||
|
--> $DIR/slice-issue-87994.rs:3:12
|
||||||
|
|
|
||||||
|
LL | for _ in v[1..] {
|
||||||
|
| ^^^^^^
|
||||||
|
| |
|
||||||
|
| expected an implementor of trait `IntoIterator`
|
||||||
|
| help: consider borrowing here: `&v[1..]`
|
||||||
|
|
|
||||||
|
= note: the trait bound `[i32]: IntoIterator` is not satisfied
|
||||||
|
= note: required because of the requirements on the impl of `IntoIterator` for `[i32]`
|
||||||
|
note: required by `into_iter`
|
||||||
|
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
|
||||||
|
|
|
||||||
|
LL | fn into_iter(self) -> Self::IntoIter;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error[E0277]: `[i32]` is not an iterator
|
||||||
|
--> $DIR/slice-issue-87994.rs:3:12
|
||||||
|
|
|
||||||
|
LL | for _ in v[1..] {
|
||||||
|
| ^^^^^^
|
||||||
|
| |
|
||||||
|
| expected an implementor of trait `IntoIterator`
|
||||||
|
| help: consider borrowing here: `&v[1..]`
|
||||||
|
|
|
||||||
|
= note: the trait bound `[i32]: IntoIterator` is not satisfied
|
||||||
|
= note: required because of the requirements on the impl of `IntoIterator` for `[i32]`
|
||||||
|
note: required by `into_iter`
|
||||||
|
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
|
||||||
|
|
|
||||||
|
LL | fn into_iter(self) -> Self::IntoIter;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0277`.
|
Loading…
Add table
Add a link
Reference in a new issue