-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Suggest impl Iterator
when possible for _
return type
#106172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,14 +24,18 @@ use rustc_hir as hir; | |
use rustc_hir::def_id::{DefId, LocalDefId}; | ||
use rustc_hir::intravisit::{self, Visitor}; | ||
use rustc_hir::{GenericParamKind, Node}; | ||
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; | ||
use rustc_infer::infer::TyCtxtInferExt; | ||
use rustc_middle::hir::nested_filter; | ||
use rustc_middle::ty::query::Providers; | ||
use rustc_middle::ty::util::{Discr, IntTypeExt}; | ||
use rustc_middle::ty::{self, AdtKind, Const, IsSuggestable, ToPredicate, Ty, TyCtxt}; | ||
use rustc_span::symbol::{kw, sym, Ident, Symbol}; | ||
use rustc_span::Span; | ||
use rustc_target::spec::abi; | ||
use rustc_trait_selection::infer::InferCtxtExt; | ||
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName; | ||
use rustc_trait_selection::traits::ObligationCtxt; | ||
use std::iter; | ||
|
||
mod generics_of; | ||
|
@@ -1224,7 +1228,17 @@ fn infer_return_ty_for_fn_sig<'tcx>( | |
// to prevent the user from getting a papercut while trying to use the unique closure | ||
// syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`). | ||
diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound"); | ||
diag.note("for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html"); | ||
diag.note( | ||
"for more information on `Fn` traits and closure types, see \ | ||
https://doc.rust-lang.org/book/ch13-01-closures.html", | ||
); | ||
} else if let Some(i_ty) = suggest_impl_iterator(tcx, ret_ty, ty.span, hir_id, def_id) { | ||
diag.span_suggestion( | ||
ty.span, | ||
"replace with an appropriate return type", | ||
format!("impl Iterator<Item = {}>", i_ty), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
diag.emit(); | ||
|
||
|
@@ -1242,6 +1256,51 @@ fn infer_return_ty_for_fn_sig<'tcx>( | |
} | ||
} | ||
|
||
fn suggest_impl_iterator<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
ret_ty: Ty<'tcx>, | ||
span: Span, | ||
hir_id: hir::HirId, | ||
def_id: LocalDefId, | ||
) -> Option<Ty<'tcx>> { | ||
let Some(iter_trait) = tcx.get_diagnostic_item(sym::Iterator) else { return None; }; | ||
let Some(iterator_item) = tcx.get_diagnostic_item(sym::IteratorItem) else { return None; }; | ||
if !tcx | ||
.infer_ctxt() | ||
.build() | ||
.type_implements_trait(iter_trait, [ret_ty], tcx.param_env(def_id)) | ||
.must_apply_modulo_regions() | ||
{ | ||
return None; | ||
} | ||
let infcx = tcx.infer_ctxt().build(); | ||
let ocx = ObligationCtxt::new_in_snapshot(&infcx); | ||
// Find the type of `Iterator::Item`. | ||
let origin = TypeVariableOrigin { kind: TypeVariableOriginKind::TypeInference, span }; | ||
let ty_var = infcx.next_ty_var(origin); | ||
let projection = ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::Projection( | ||
ty::ProjectionPredicate { | ||
projection_ty: tcx.mk_alias_ty(iterator_item, tcx.mk_substs([ret_ty.into()].iter())), | ||
term: ty_var.into(), | ||
}, | ||
))); | ||
// Add `<ret_ty as Iterator>::Item = _` obligation. | ||
ocx.register_obligation(crate::traits::Obligation::misc( | ||
tcx, | ||
span, | ||
hir_id, | ||
tcx.param_env(def_id), | ||
projection, | ||
)); | ||
Comment on lines
+1280
to
+1294
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just construct a projection type then pass it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'll fix this in a follow-up PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried making that change, but I must have done it incorrectly and it didn't work. Let's land this and clean it up soon. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't worry about it at all, I got it working on a local branch in any case. I do wonder what was wrong, though. |
||
if ocx.select_where_possible().is_empty() | ||
&& let item_ty = infcx.resolve_vars_if_possible(ty_var) | ||
&& item_ty.is_suggestable(tcx, false) | ||
{ | ||
return Some(item_ty); | ||
} | ||
None | ||
} | ||
|
||
fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> { | ||
let icx = ItemCtxt::new(tcx, def_id); | ||
let item = tcx.hir().expect_item(def_id.expect_local()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,6 +213,7 @@ symbols! { | |
Is, | ||
ItemContext, | ||
Iterator, | ||
IteratorItem, | ||
Layout, | ||
Left, | ||
LinkedList, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures | |
LL | const C: _ = || 42; | ||
| ^ not allowed in type signatures | ||
| | ||
note: however, the inferred type `[closure@$DIR/unnamable-types.rs:17:14: 17:16]` cannot be named | ||
note: however, the inferred type `[[email protected]:17:14]` cannot be named | ||
--> $DIR/unnamable-types.rs:17:14 | ||
| | ||
LL | const C: _ = || 42; | ||
|
@@ -31,7 +31,7 @@ error: missing type for `const` item | |
LL | const D = S { t: { let i = 0; move || -> i32 { i } } }; | ||
| ^ | ||
| | ||
note: however, the inferred type `S<[closure@$DIR/unnamable-types.rs:23:31: 23:45]>` cannot be named | ||
note: however, the inferred type `S<[[email protected]:23:31]>` cannot be named | ||
--> $DIR/unnamable-types.rs:23:11 | ||
| | ||
LL | const D = S { t: { let i = 0; move || -> i32 { i } } }; | ||
|
Uh oh!
There was an error while loading. Please reload this page.