1
Fork 0

Don't check unnecessarily that impl trait is RPIT

This commit is contained in:
Michael Goulet 2023-07-31 18:13:48 +00:00
parent b321edd1b2
commit 9295817bad
4 changed files with 53 additions and 31 deletions

View file

@ -2567,15 +2567,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
None, None,
); );
if let Some(infer::RelateParamBound(_, t, _)) = origin { if let Some(infer::RelateParamBound(_, t, _)) = origin {
let return_impl_trait =
self.tcx.return_type_impl_trait(generic_param_scope).is_some();
let t = self.resolve_vars_if_possible(t); let t = self.resolve_vars_if_possible(t);
match t.kind() { match t.kind() {
// We've got: // We've got:
// fn get_later<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ // fn get_later<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
// suggest: // suggest:
// fn get_later<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a // fn get_later<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
ty::Closure(..) | ty::Alias(ty::Opaque, ..) if return_impl_trait => { ty::Closure(..) | ty::Alias(ty::Opaque, ..) => {
new_binding_suggestion(&mut err, type_param_span); new_binding_suggestion(&mut err, type_param_span);
} }
_ => { _ => {

View file

@ -50,9 +50,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions; use rustc_hir::definitions::Definitions;
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_hir::{ use rustc_hir::{Constness, HirId, Node, TraitCandidate};
Constness, ExprKind, HirId, ImplItemKind, ItemKind, Node, TraitCandidate, TraitItemKind,
};
use rustc_index::IndexVec; use rustc_index::IndexVec;
use rustc_macros::HashStable; use rustc_macros::HashStable;
use rustc_query_system::dep_graph::DepNodeIndex; use rustc_query_system::dep_graph::DepNodeIndex;
@ -1077,31 +1075,6 @@ impl<'tcx> TyCtxt<'tcx> {
return None; return None;
} }
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
match self.hir().get_by_def_id(scope_def_id) {
Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {}
Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {}
Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {}
Node::Expr(&hir::Expr { kind: ExprKind::Closure { .. }, .. }) => {}
_ => return None,
}
let ret_ty = self.type_of(scope_def_id).instantiate_identity();
match ret_ty.kind() {
ty::FnDef(_, _) => {
let sig = ret_ty.fn_sig(self);
let output = self.erase_late_bound_regions(sig.output());
output.is_impl_trait().then(|| {
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap();
(output, fn_decl.output.span())
})
}
_ => None,
}
}
/// Checks if the bound region is in Impl Item. /// Checks if the bound region is in Impl Item.
pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool { pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool {
let container_id = self.parent(suitable_region_binding_scope.to_def_id()); let container_id = self.parent(suitable_region_binding_scope.to_def_id());

View file

@ -0,0 +1,13 @@
#![feature(return_position_impl_trait_in_trait)]
trait Iterable {
type Item<'a>
where
Self: 'a;
fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
//~^ ERROR use of undeclared lifetime name `'missing`
//~| ERROR the parameter type `Self` may not live long enough
}
fn main() {}

View file

@ -0,0 +1,38 @@
error[E0261]: use of undeclared lifetime name `'missing`
--> $DIR/missing-lt-outlives-in-rpitit-114274.rs:8:55
|
LL | fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
| ^^^^^^^^ undeclared lifetime
|
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'missing` lifetime
|
LL | fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missing>>;
| +++++++++++++
help: consider introducing lifetime `'missing` here
|
LL | fn iter<'missing>(&self) -> impl Iterator<Item = Self::Item<'missing>>;
| ++++++++++
help: consider introducing lifetime `'missing` here
|
LL | trait Iterable<'missing> {
| ++++++++++
error[E0311]: the parameter type `Self` may not live long enough
--> $DIR/missing-lt-outlives-in-rpitit-114274.rs:8:37
|
LL | fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `Self: 'a`...
= note: ...so that the type `Self` will meet its required lifetime bounds...
note: ...that is required by this bound
--> $DIR/missing-lt-outlives-in-rpitit-114274.rs:6:15
|
LL | Self: 'a;
| ^^
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0261, E0311.
For more information about an error, try `rustc --explain E0261`.