Skip to content

Commit f338a1f

Browse files
authored
Rollup merge of #114301 - compiler-errors:dont-error-on-missing-region-outlives, r=spastorino
Don't check unnecessarily that impl trait is RPIT We have this random `return_type_impl_trait` function to detect if a function returns an RPIT which is used in outlives suggestions, but removing it doesn't actually change any diagnostics. Let's just remove it. Also, suppress a spurious outlives error from a ReError. Fixes #114274
2 parents bb3dee1 + 1c35634 commit f338a1f

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
555555
for (region_a, region_a_idx) in &regions {
556556
// Ignore `'static` lifetimes for the purpose of this lint: it's
557557
// because we know it outlives everything and so doesn't give meaningful
558-
// clues
559-
if let ty::ReStatic = **region_a {
558+
// clues. Also ignore `ReError`, to avoid knock-down errors.
559+
if let ty::ReStatic | ty::ReError(_) = **region_a {
560560
continue;
561561
}
562562
// For each region argument (e.g., `'a` in our example), check for a
@@ -599,8 +599,9 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
599599
// on the GAT itself.
600600
for (region_b, region_b_idx) in &regions {
601601
// Again, skip `'static` because it outlives everything. Also, we trivially
602-
// know that a region outlives itself.
603-
if ty::ReStatic == **region_b || region_a == region_b {
602+
// know that a region outlives itself. Also ignore `ReError`, to avoid
603+
// knock-down errors.
604+
if matches!(**region_b, ty::ReStatic | ty::ReError(_)) || region_a == region_b {
604605
continue;
605606
}
606607
if region_known_to_outlive(

Diff for: 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
_ => {

Diff for: compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,10 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
942942
generic_ty: Ty<'tcx>,
943943
min: ty::Region<'tcx>,
944944
) -> bool {
945+
if let ty::ReError(_) = *min {
946+
return true;
947+
}
948+
945949
match bound {
946950
VerifyBound::IfEq(verify_if_eq_b) => {
947951
let verify_if_eq_b = var_values.normalize(self.region_rels.tcx, *verify_if_eq_b);

Diff for: 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,12 @@
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+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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: aborting due to previous error
22+
23+
For more information about this error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)