Skip to content

Commit 117799b

Browse files
committed
Auto merge of rust-lang#86505 - JohnTitor:fix-86483, r=jackh726
Do not panic in `return_type_impl_trait` Fixes rust-lang#86483
2 parents 50e0cc5 + 9323a28 commit 117799b

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

compiler/rustc_middle/src/ty/context.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use rustc_hir::definitions::Definitions;
4343
use rustc_hir::intravisit::Visitor;
4444
use rustc_hir::lang_items::LangItem;
4545
use rustc_hir::{
46-
Constness, HirId, ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet, Node, TraitCandidate,
46+
Constness, ExprKind, HirId, ImplItemKind, ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet,
47+
Node, TraitCandidate, TraitItemKind,
4748
};
4849
use rustc_index::vec::{Idx, IndexVec};
4950
use rustc_macros::HashStable;
@@ -1498,18 +1499,14 @@ impl<'tcx> TyCtxt<'tcx> {
14981499
}
14991500

15001501
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
1501-
// HACK: `type_of_def_id()` will fail on these (#55796), so return `None`.
1502+
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
15021503
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
15031504
match self.hir().get(hir_id) {
1504-
Node::Item(item) => {
1505-
match item.kind {
1506-
ItemKind::Fn(..) => { /* `type_of_def_id()` will work */ }
1507-
_ => {
1508-
return None;
1509-
}
1510-
}
1511-
}
1512-
_ => { /* `type_of_def_id()` will work or panic */ }
1505+
Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {}
1506+
Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {}
1507+
Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {}
1508+
Node::Expr(&hir::Expr { kind: ExprKind::Closure(..), .. }) => {}
1509+
_ => return None,
15131510
}
15141511

15151512
let ret_ty = self.type_of(scope_def_id);

compiler/rustc_typeck/src/collect/type_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
492492

493493
x => tcx.ty_error_with_message(
494494
DUMMY_SP,
495-
&format!("unexpected const parent in type_of_def_id(): {:?}", x),
495+
&format!("unexpected const parent in type_of(): {:?}", x),
496496
),
497497
}
498498
}
@@ -504,7 +504,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
504504
},
505505

506506
x => {
507-
bug!("unexpected sort of node in type_of_def_id(): {:?}", x);
507+
bug!("unexpected sort of node in type_of(): {:?}", x);
508508
}
509509
}
510510
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test of #86483.
2+
3+
#![feature(generic_associated_types)]
4+
#![allow(incomplete_features)]
5+
6+
pub trait IceIce<T> //~ ERROR: the parameter type `T` may not live long enough
7+
where
8+
for<'a> T: 'a,
9+
{
10+
type Ice<'v>: IntoIterator<Item = &'v T>;
11+
//~^ ERROR: the parameter type `T` may not live long enough
12+
//~| ERROR: the parameter type `T` may not live long enough
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0311]: the parameter type `T` may not live long enough
2+
--> $DIR/issue-86483.rs:6:1
3+
|
4+
LL | pub trait IceIce<T>
5+
| ^ - help: consider adding an explicit lifetime bound...: `T: 'a`
6+
| _|
7+
| |
8+
LL | | where
9+
LL | | for<'a> T: 'a,
10+
LL | | {
11+
... |
12+
LL | |
13+
LL | | }
14+
| |_^ ...so that the type `T` will meet its required lifetime bounds
15+
16+
error[E0311]: the parameter type `T` may not live long enough
17+
--> $DIR/issue-86483.rs:10:5
18+
|
19+
LL | pub trait IceIce<T>
20+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
21+
...
22+
LL | type Ice<'v>: IntoIterator<Item = &'v T>;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
24+
25+
error[E0309]: the parameter type `T` may not live long enough
26+
--> $DIR/issue-86483.rs:10:32
27+
|
28+
LL | pub trait IceIce<T>
29+
| - help: consider adding an explicit lifetime bound...: `T: 'v`
30+
...
31+
LL | type Ice<'v>: IntoIterator<Item = &'v T>;
32+
| ^^^^^^^^^^^^ ...so that the reference type `&'v T` does not outlive the data it points at
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0309`.

0 commit comments

Comments
 (0)