Skip to content

Commit

Permalink
Auto merge of #86505 - JohnTitor:fix-86483, r=jackh726
Browse files Browse the repository at this point in the history
Do not panic in `return_type_impl_trait`

Fixes #86483
  • Loading branch information
bors committed Jun 25, 2021
2 parents 50e0cc5 + 9323a28 commit 117799b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
19 changes: 8 additions & 11 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
}
}
Expand All @@ -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);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/generic-associated-types/issue-86483.rs
Original file line number Diff line number Diff line change
@@ -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() {}
36 changes: 36 additions & 0 deletions src/test/ui/generic-associated-types/issue-86483.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 117799b

Please sign in to comment.