Skip to content

Commit 95b3c42

Browse files
eddybmark-i-m
authored andcommitted
Remove Option from the return type of def_kind.
1 parent d1db782 commit 95b3c42

File tree

24 files changed

+69
-100
lines changed

24 files changed

+69
-100
lines changed

src/librustc_infer/infer/error_reporting/need_type_info.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
192192
.get_opt_name()
193193
.map(|parent_symbol| parent_symbol.to_string());
194194

195-
let type_parent_desc = self
196-
.tcx
197-
.def_kind(parent_def_id)
198-
.map(|parent_def_kind| parent_def_kind.descr(parent_def_id));
199-
200-
(parent_name, type_parent_desc)
195+
(parent_name, Some(self.tcx.def_kind(parent_def_id).descr(parent_def_id)))
201196
} else {
202197
(None, None)
203198
};

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
127127
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
128128
static_mutability => { cdata.static_mutability(def_id.index) }
129129
generator_kind => { cdata.generator_kind(def_id.index) }
130-
def_kind => { Some(cdata.def_kind(def_id.index)) }
130+
def_kind => { cdata.def_kind(def_id.index) }
131131
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
132132
lookup_stability => {
133133
cdata.get_stability(def_id.index).map(|s| tcx.intern_stability(s))

src/librustc_middle/hir/map/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,14 @@ impl<'hir> Map<'hir> {
227227
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
228228
}
229229

230-
pub fn def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
230+
pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
231+
// FIXME(eddyb) support `find` on the crate root.
231232
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
232-
return Some(DefKind::Mod);
233+
return DefKind::Mod;
233234
}
234235

235236
let hir_id = self.local_def_id_to_hir_id(local_def_id);
236-
let node = self.find(hir_id)?;
237-
238-
Some(match node {
237+
match self.get(hir_id) {
239238
Node::Item(item) => match item.kind {
240239
ItemKind::Static(..) => DefKind::Static,
241240
ItemKind::Const(..) => DefKind::Const,
@@ -273,7 +272,7 @@ impl<'hir> Map<'hir> {
273272
Node::Variant(_) => DefKind::Variant,
274273
Node::Ctor(variant_data) => {
275274
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
276-
variant_data.ctor_hir_id()?;
275+
assert_ne!(variant_data.ctor_hir_id(), None);
277276

278277
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
279278
Some(Node::Item(..)) => def::CtorOf::Struct,
@@ -308,7 +307,7 @@ impl<'hir> Map<'hir> {
308307
| Node::Visibility(_)
309308
| Node::Block(_)
310309
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
311-
})
310+
}
312311
}
313312

314313
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {

src/librustc_middle/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub enum EvalResult {
246246
fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, mut def_id: DefId) -> bool {
247247
// Check if `def_id` is a trait method.
248248
match tcx.def_kind(def_id) {
249-
Some(DefKind::AssocFn) | Some(DefKind::AssocTy) | Some(DefKind::AssocConst) => {
249+
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
250250
if let ty::TraitContainer(trait_def_id) = tcx.associated_item(def_id).container {
251251
// Trait methods do not declare visibility (even
252252
// for visibility info in cstore). Use containing

src/librustc_middle/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ rustc_queries! {
620620
cache_on_disk_if { true }
621621
}
622622

623-
query def_kind(_: DefId) -> Option<DefKind> {}
623+
query def_kind(_: DefId) -> DefKind {}
624624
query def_span(_: DefId) -> Span {
625625
// FIXME(mw): DefSpans are not really inputs since they are derived from
626626
// HIR. But at the moment HIR hashing still contains some hacks that allow

src/librustc_middle/ty/context.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_errors::ErrorReported;
4848
use rustc_hir as hir;
4949
use rustc_hir::def::{DefKind, Res};
5050
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
51-
use rustc_hir::definitions::{DefPathData, DefPathHash, Definitions};
51+
use rustc_hir::definitions::{DefPathHash, Definitions};
5252
use rustc_hir::lang_items;
5353
use rustc_hir::lang_items::PanicLocationLangItem;
5454
use rustc_hir::{HirId, Node, TraitCandidate};
@@ -1492,21 +1492,13 @@ impl<'tcx> TyCtxt<'tcx> {
14921492

14931493
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "struct")`).
14941494
pub fn article_and_description(&self, def_id: DefId) -> (&'static str, &'static str) {
1495-
self.def_kind(def_id)
1496-
.map(|def_kind| (def_kind.article(), def_kind.descr(def_id)))
1497-
.unwrap_or_else(|| match self.def_key(def_id).disambiguated_data.data {
1498-
DefPathData::ClosureExpr => match self.generator_kind(def_id) {
1499-
None => ("a", "closure"),
1500-
Some(rustc_hir::GeneratorKind::Async(..)) => ("an", "async closure"),
1501-
Some(rustc_hir::GeneratorKind::Gen) => ("a", "generator"),
1502-
},
1503-
DefPathData::LifetimeNs(..) => ("a", "lifetime"),
1504-
DefPathData::Impl => ("an", "implementation"),
1505-
DefPathData::TypeNs(..) | DefPathData::ValueNs(..) | DefPathData::MacroNs(..) => {
1506-
unreachable!()
1507-
}
1508-
_ => bug!("article_and_description called on def_id {:?}", def_id),
1509-
})
1495+
match self.def_kind(def_id) {
1496+
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
1497+
rustc_hir::GeneratorKind::Async(..) => ("an", "async closure"),
1498+
rustc_hir::GeneratorKind::Gen => ("a", "generator"),
1499+
},
1500+
def_kind => (def_kind.article(), def_kind.descr(def_id)),
1501+
}
15101502
}
15111503
}
15121504

src/librustc_middle/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2680,7 +2680,7 @@ impl<'tcx> TyCtxt<'tcx> {
26802680
}
26812681
} else {
26822682
match self.def_kind(def_id) {
2683-
Some(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy) => true,
2683+
DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy => true,
26842684
_ => false,
26852685
}
26862686
};

src/librustc_middle/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ pub trait PrettyPrinter<'tcx>:
891891
p!(write("::{:?}", promoted));
892892
} else {
893893
match self.tcx().def_kind(did) {
894-
Some(DefKind::Static | DefKind::Const | DefKind::AssocConst) => {
894+
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
895895
p!(print_value_path(did, substs))
896896
}
897897
_ => {

src/librustc_middle/ty/util.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_errors::ErrorReported;
1616
use rustc_hir as hir;
1717
use rustc_hir::def::DefKind;
1818
use rustc_hir::def_id::DefId;
19-
use rustc_hir::definitions::DefPathData;
2019
use rustc_macros::HashStable;
2120
use rustc_span::Span;
2221
use rustc_target::abi::{Integer, Size, TargetDataLayout};
@@ -446,24 +445,24 @@ impl<'tcx> TyCtxt<'tcx> {
446445
/// those are not yet phased out). The parent of the closure's
447446
/// `DefId` will also be the context where it appears.
448447
pub fn is_closure(self, def_id: DefId) -> bool {
449-
self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr
448+
matches!(self.def_kind(def_id), DefKind::Closure | DefKind::Generator)
450449
}
451450

452451
/// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
453452
pub fn is_trait(self, def_id: DefId) -> bool {
454-
self.def_kind(def_id) == Some(DefKind::Trait)
453+
self.def_kind(def_id) == DefKind::Trait
455454
}
456455

457456
/// Returns `true` if `def_id` refers to a trait alias (i.e., `trait Foo = ...;`),
458457
/// and `false` otherwise.
459458
pub fn is_trait_alias(self, def_id: DefId) -> bool {
460-
self.def_kind(def_id) == Some(DefKind::TraitAlias)
459+
self.def_kind(def_id) == DefKind::TraitAlias
461460
}
462461

463462
/// Returns `true` if this `DefId` refers to the implicit constructor for
464463
/// a tuple struct like `struct Foo(u32)`, and `false` otherwise.
465464
pub fn is_constructor(self, def_id: DefId) -> bool {
466-
self.def_key(def_id).disambiguated_data.data == DefPathData::Ctor
465+
matches!(self.def_kind(def_id), DefKind::Ctor(..))
467466
}
468467

469468
/// Given the def-ID of a fn or closure, returns the def-ID of

src/librustc_mir/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub fn const_eval_raw_provider<'tcx>(
341341
// because any code that existed before validation could not have failed
342342
// validation thus preventing such a hard error from being a backwards
343343
// compatibility hazard
344-
Some(DefKind::Const | DefKind::AssocConst) => {
344+
DefKind::Const | DefKind::AssocConst => {
345345
let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local());
346346
err.report_as_lint(
347347
tcx.at(tcx.def_span(def_id)),

0 commit comments

Comments
 (0)