Auto merge of #86505 - JohnTitor:fix-86483, r=jackh726

Do not panic in `return_type_impl_trait`

Fixes #86483
This commit is contained in:
bors 2021-06-25 09:28:17 +00:00
commit 117799b73c
4 changed files with 61 additions and 13 deletions

View File

@ -43,7 +43,8 @@ use rustc_hir::definitions::Definitions;
use rustc_hir::intravisit::Visitor;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{
Constness, HirId, ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet, Node, TraitCandidate,
Constness, ExprKind, HirId, ImplItemKind, ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet,
Node, TraitCandidate, TraitItemKind,
};
use rustc_index::vec::{Idx, IndexVec};
use rustc_macros::HashStable;
@ -1498,18 +1499,14 @@ impl<'tcx> TyCtxt<'tcx> {
}
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
// HACK: `type_of_def_id()` will fail on these (#55796), so return `None`.
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
match self.hir().get(hir_id) {
Node::Item(item) => {
match item.kind {
ItemKind::Fn(..) => { /* `type_of_def_id()` will work */ }
_ => {
return None;
}
}
}
_ => { /* `type_of_def_id()` will work or panic */ }
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);

View File

@ -492,7 +492,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
x => tcx.ty_error_with_message(
DUMMY_SP,
&format!("unexpected const parent in type_of_def_id(): {:?}", x),
&format!("unexpected const parent in type_of(): {:?}", x),
),
}
}
@ -504,7 +504,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
},
x => {
bug!("unexpected sort of node in type_of_def_id(): {:?}", x);
bug!("unexpected sort of node in type_of(): {:?}", x);
}
}
}

View File

@ -0,0 +1,15 @@
// Regression test of #86483.
#![feature(generic_associated_types)]
#![allow(incomplete_features)]
pub trait IceIce<T> //~ ERROR: the parameter type `T` may not live long enough
where
for<'a> T: 'a,
{
type Ice<'v>: IntoIterator<Item = &'v T>;
//~^ ERROR: the parameter type `T` may not live long enough
//~| ERROR: the parameter type `T` may not live long enough
}
fn main() {}

View File

@ -0,0 +1,36 @@
error[E0311]: the parameter type `T` may not live long enough
--> $DIR/issue-86483.rs:6:1
|
LL | pub trait IceIce<T>
| ^ - help: consider adding an explicit lifetime bound...: `T: 'a`
| _|
| |
LL | | where
LL | | for<'a> T: 'a,
LL | | {
... |
LL | |
LL | | }
| |_^ ...so that the type `T` will meet its required lifetime bounds
error[E0311]: the parameter type `T` may not live long enough
--> $DIR/issue-86483.rs:10:5
|
LL | pub trait IceIce<T>
| - help: consider adding an explicit lifetime bound...: `T: 'a`
...
LL | type Ice<'v>: IntoIterator<Item = &'v T>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/issue-86483.rs:10:32
|
LL | pub trait IceIce<T>
| - help: consider adding an explicit lifetime bound...: `T: 'v`
...
LL | type Ice<'v>: IntoIterator<Item = &'v T>;
| ^^^^^^^^^^^^ ...so that the reference type `&'v T` does not outlive the data it points at
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0309`.