Skip to content

Commit 34466a2

Browse files
committed
Remove Option from the return type of def_kind.
1 parent e5addfa commit 34466a2

File tree

23 files changed

+74
-110
lines changed

23 files changed

+74
-110
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
@@ -229,15 +229,14 @@ impl<'hir> Map<'hir> {
229229
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
230230
}
231231

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

237238
let hir_id = self.local_def_id_to_hir_id(local_def_id);
238-
let node = self.find(hir_id)?;
239-
240-
Some(match node {
239+
match self.get(hir_id) {
241240
Node::Item(item) => match item.kind {
242241
ItemKind::Static(..) => DefKind::Static,
243242
ItemKind::Const(..) => DefKind::Const,
@@ -275,7 +274,7 @@ impl<'hir> Map<'hir> {
275274
Node::Variant(_) => DefKind::Variant,
276275
Node::Ctor(variant_data) => {
277276
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
278-
variant_data.ctor_hir_id()?;
277+
assert_ne!(variant_data.ctor_hir_id(), None);
279278

280279
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
281280
Some(Node::Item(..)) => def::CtorOf::Struct,
@@ -310,7 +309,7 @@ impl<'hir> Map<'hir> {
310309
| Node::Visibility(_)
311310
| Node::Block(_)
312311
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
313-
})
312+
}
314313
}
315314

316315
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
@@ -630,7 +630,7 @@ rustc_queries! {
630630
cache_on_disk_if { true }
631631
}
632632

633-
query def_kind(_: DefId) -> Option<DefKind> {}
633+
query def_kind(_: DefId) -> DefKind {}
634634
query def_span(_: DefId) -> Span {
635635
// FIXME(mw): DefSpans are not really inputs since they are derived from
636636
// 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
@@ -50,7 +50,7 @@ use rustc_errors::ErrorReported;
5050
use rustc_hir as hir;
5151
use rustc_hir::def::{DefKind, Res};
5252
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
53-
use rustc_hir::definitions::{DefPathData, DefPathHash, Definitions};
53+
use rustc_hir::definitions::{DefPathHash, Definitions};
5454
use rustc_hir::lang_items;
5555
use rustc_hir::lang_items::PanicLocationLangItem;
5656
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: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,16 +2670,9 @@ impl<'tcx> TyCtxt<'tcx> {
26702670
}
26712671

26722672
pub fn opt_associated_item(self, def_id: DefId) -> Option<AssocItem> {
2673-
let is_associated_item = if let Some(hir_id) = self.hir().as_local_hir_id(def_id) {
2674-
match self.hir().get(hir_id) {
2675-
Node::TraitItem(_) | Node::ImplItem(_) => true,
2676-
_ => false,
2677-
}
2678-
} else {
2679-
match self.def_kind(def_id) {
2680-
Some(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy) => true,
2681-
_ => false,
2682-
}
2673+
let is_associated_item = match self.def_kind(def_id) {
2674+
DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy => true,
2675+
_ => false,
26832676
};
26842677

26852678
is_associated_item.then(|| self.associated_item(def_id))

src/librustc_middle/ty/print/pretty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -909,9 +909,9 @@ pub trait PrettyPrinter<'tcx>:
909909
p!(write("::{:?}", promoted));
910910
} else {
911911
match self.tcx().def_kind(did) {
912-
Some(DefKind::Static)
913-
| Some(DefKind::Const)
914-
| Some(DefKind::AssocConst) => p!(print_value_path(did, substs)),
912+
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
913+
p!(print_value_path(did, substs))
914+
}
915915
_ => {
916916
if did.is_local() {
917917
let span = self.tcx().def_span(did);

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};
@@ -448,24 +447,24 @@ impl<'tcx> TyCtxt<'tcx> {
448447
/// those are not yet phased out). The parent of the closure's
449448
/// `DefId` will also be the context where it appears.
450449
pub fn is_closure(self, def_id: DefId) -> bool {
451-
self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr
450+
matches!(self.def_kind(def_id), DefKind::Closure | DefKind::Generator)
452451
}
453452

454453
/// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
455454
pub fn is_trait(self, def_id: DefId) -> bool {
456-
self.def_kind(def_id) == Some(DefKind::Trait)
455+
self.def_kind(def_id) == DefKind::Trait
457456
}
458457

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

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

471470
/// 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
@@ -345,7 +345,7 @@ pub fn const_eval_raw_provider<'tcx>(
345345
// because any code that existed before validation could not have failed
346346
// validation thus preventing such a hard error from being a backwards
347347
// compatibility hazard
348-
Some(DefKind::Const) | Some(DefKind::AssocConst) => {
348+
DefKind::Const | DefKind::AssocConst => {
349349
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
350350
err.report_as_lint(
351351
tcx.at(tcx.def_span(def_id)),

0 commit comments

Comments
 (0)