Skip to content
/ rust Public
forked from rust-lang/rust

Commit 9295817

Browse files
Don't check unnecessarily that impl trait is RPIT
1 parent b321edd commit 9295817

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2567,15 +2567,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25672567
None,
25682568
);
25692569
if let Some(infer::RelateParamBound(_, t, _)) = origin {
2570-
let return_impl_trait =
2571-
self.tcx.return_type_impl_trait(generic_param_scope).is_some();
25722570
let t = self.resolve_vars_if_possible(t);
25732571
match t.kind() {
25742572
// We've got:
25752573
// fn get_later<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
25762574
// suggest:
25772575
// fn get_later<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
2578-
ty::Closure(..) | ty::Alias(ty::Opaque, ..) if return_impl_trait => {
2576+
ty::Closure(..) | ty::Alias(ty::Opaque, ..) => {
25792577
new_binding_suggestion(&mut err, type_param_span);
25802578
}
25812579
_ => {

compiler/rustc_middle/src/ty/context.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
5050
use rustc_hir::definitions::Definitions;
5151
use rustc_hir::intravisit::Visitor;
5252
use rustc_hir::lang_items::LangItem;
53-
use rustc_hir::{
54-
Constness, ExprKind, HirId, ImplItemKind, ItemKind, Node, TraitCandidate, TraitItemKind,
55-
};
53+
use rustc_hir::{Constness, HirId, Node, TraitCandidate};
5654
use rustc_index::IndexVec;
5755
use rustc_macros::HashStable;
5856
use rustc_query_system::dep_graph::DepNodeIndex;
@@ -1077,31 +1075,6 @@ impl<'tcx> TyCtxt<'tcx> {
10771075
return None;
10781076
}
10791077

1080-
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
1081-
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
1082-
match self.hir().get_by_def_id(scope_def_id) {
1083-
Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {}
1084-
Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {}
1085-
Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {}
1086-
Node::Expr(&hir::Expr { kind: ExprKind::Closure { .. }, .. }) => {}
1087-
_ => return None,
1088-
}
1089-
1090-
let ret_ty = self.type_of(scope_def_id).instantiate_identity();
1091-
match ret_ty.kind() {
1092-
ty::FnDef(_, _) => {
1093-
let sig = ret_ty.fn_sig(self);
1094-
let output = self.erase_late_bound_regions(sig.output());
1095-
output.is_impl_trait().then(|| {
1096-
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
1097-
let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap();
1098-
(output, fn_decl.output.span())
1099-
})
1100-
}
1101-
_ => None,
1102-
}
1103-
}
1104-
11051078
/// Checks if the bound region is in Impl Item.
11061079
pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool {
11071080
let container_id = self.parent(suitable_region_binding_scope.to_def_id());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(return_position_impl_trait_in_trait)]
2+
3+
trait Iterable {
4+
type Item<'a>
5+
where
6+
Self: 'a;
7+
8+
fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
9+
//~^ ERROR use of undeclared lifetime name `'missing`
10+
//~| ERROR the parameter type `Self` may not live long enough
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0261]: use of undeclared lifetime name `'missing`
2+
--> $DIR/missing-lt-outlives-in-rpitit-114274.rs:8:55
3+
|
4+
LL | fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
5+
| ^^^^^^^^ undeclared lifetime
6+
|
7+
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
8+
help: consider making the bound lifetime-generic with a new `'missing` lifetime
9+
|
10+
LL | fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missing>>;
11+
| +++++++++++++
12+
help: consider introducing lifetime `'missing` here
13+
|
14+
LL | fn iter<'missing>(&self) -> impl Iterator<Item = Self::Item<'missing>>;
15+
| ++++++++++
16+
help: consider introducing lifetime `'missing` here
17+
|
18+
LL | trait Iterable<'missing> {
19+
| ++++++++++
20+
21+
error[E0311]: the parameter type `Self` may not live long enough
22+
--> $DIR/missing-lt-outlives-in-rpitit-114274.rs:8:37
23+
|
24+
LL | fn iter(&self) -> impl Iterator<Item = Self::Item<'missing>>;
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
= help: consider adding an explicit lifetime bound `Self: 'a`...
28+
= note: ...so that the type `Self` will meet its required lifetime bounds...
29+
note: ...that is required by this bound
30+
--> $DIR/missing-lt-outlives-in-rpitit-114274.rs:6:15
31+
|
32+
LL | Self: 'a;
33+
| ^^
34+
35+
error: aborting due to 2 previous errors
36+
37+
Some errors have detailed explanations: E0261, E0311.
38+
For more information about an error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)