diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index a74070100f413..77f955e93b9dd 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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); diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 78fa8074a64cf..ee84974cb73c2 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -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); } } } diff --git a/src/test/ui/generic-associated-types/issue-86483.rs b/src/test/ui/generic-associated-types/issue-86483.rs new file mode 100644 index 0000000000000..9d03c9dab8d90 --- /dev/null +++ b/src/test/ui/generic-associated-types/issue-86483.rs @@ -0,0 +1,15 @@ +// Regression test of #86483. + +#![feature(generic_associated_types)] +#![allow(incomplete_features)] + +pub trait IceIce //~ ERROR: the parameter type `T` may not live long enough +where + for<'a> T: 'a, +{ + type Ice<'v>: IntoIterator; + //~^ ERROR: the parameter type `T` may not live long enough + //~| ERROR: the parameter type `T` may not live long enough +} + +fn main() {} diff --git a/src/test/ui/generic-associated-types/issue-86483.stderr b/src/test/ui/generic-associated-types/issue-86483.stderr new file mode 100644 index 0000000000000..c8efc2ed88264 --- /dev/null +++ b/src/test/ui/generic-associated-types/issue-86483.stderr @@ -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 + | ^ - 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 + | - help: consider adding an explicit lifetime bound...: `T: 'a` +... +LL | type Ice<'v>: IntoIterator; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...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 + | - help: consider adding an explicit lifetime bound...: `T: 'v` +... +LL | type Ice<'v>: IntoIterator; + | ^^^^^^^^^^^^ ...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`.