Skip to content

Commit

Permalink
Prefer "allow list" structure to check a type
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed Jun 24, 2021
1 parent 462c740 commit 9323a28
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +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,
TraitItemKind,
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 @@ -1499,24 +1499,14 @@ impl<'tcx> TyCtxt<'tcx> {
}

pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
// HACK: `type_of()` 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()` will work */ }
_ => {
return None;
}
}
}
Node::TraitItem(item) => {
// #86483: Return early if it doesn't have a concrete type.
if let TraitItemKind::Type(_, None) = item.kind {
return None;
}
}
_ => { /* `type_of()` 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

0 comments on commit 9323a28

Please sign in to comment.