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

+1-6
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

+1-1
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

+6-7
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

+1-1
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

+1-1
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

+8-16
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

+1-1
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

+1-1
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

+4-5
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

+1-1
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)),

src/librustc_mir/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
632632
// FIXME: The above is likely untrue. See
633633
// <https://github.com/rust-lang/rust/pull/70004#issuecomment-602022110>. Is it
634634
// okay to ignore `StorageDead`/`StorageLive` annotations during CTFE?
635-
Some(DefKind::Static | DefKind::Const | DefKind::AssocConst) => {}
635+
DefKind::Static | DefKind::Const | DefKind::AssocConst => {}
636636
_ => {
637637
// Mark locals that use `Storage*` annotations as dead on function entry.
638638
let always_live = AlwaysLiveLocals::new(self.body());

src/librustc_mir/monomorphize/partitioning.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ fn compute_codegen_unit_name(
779779
cgu_def_id = Some(DefId { krate: def_id.krate, index: CRATE_DEF_INDEX });
780780
}
781781
break;
782-
} else if tcx.def_kind(current_def_id) == Some(DefKind::Mod) {
782+
} else if tcx.def_kind(current_def_id) == DefKind::Mod {
783783
if cgu_def_id.is_none() {
784784
cgu_def_id = Some(current_def_id);
785785
}

src/librustc_mir/transform/const_prop.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
6969
let hir_id = tcx.hir().as_local_hir_id(source.def_id().expect_local());
7070

7171
let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some();
72-
let is_assoc_const = match tcx.def_kind(source.def_id()) {
73-
Some(DefKind::AssocConst) => true,
74-
_ => false,
75-
};
72+
let is_assoc_const = tcx.def_kind(source.def_id()) == DefKind::AssocConst;
7673

7774
// Only run const prop on functions, methods, closures and associated constants
7875
if !is_fn_like && !is_assoc_const {

src/librustc_mir/util/pretty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -807,17 +807,17 @@ fn write_mir_sig(
807807
trace!("write_mir_sig: {:?}", src.instance);
808808
let kind = tcx.def_kind(src.def_id());
809809
let is_function = match kind {
810-
Some(DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..)) => true,
810+
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) => true,
811811
_ => tcx.is_closure(src.def_id()),
812812
};
813813
match (kind, src.promoted) {
814814
(_, Some(i)) => write!(w, "{:?} in ", i)?,
815-
(Some(DefKind::Const | DefKind::AssocConst), _) => write!(w, "const ")?,
816-
(Some(DefKind::Static), _) => {
815+
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
816+
(DefKind::Static, _) => {
817817
write!(w, "static {}", if tcx.is_mutable_static(src.def_id()) { "mut " } else { "" })?
818818
}
819819
(_, _) if is_function => write!(w, "fn ")?,
820-
(Some(DefKind::AnonConst), _) | (None, _) => {} // things like anon const, not an item
820+
(DefKind::AnonConst, _) => {} // things like anon const, not an item
821821
_ => bug!("Unexpected def kind {:?}", kind),
822822
}
823823

src/librustc_privacy/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,10 @@ impl EmbargoVisitor<'tcx> {
538538
for item_id in module.item_ids {
539539
let hir_id = item_id.id;
540540
let item_def_id = self.tcx.hir().local_def_id(hir_id);
541-
if let Some(def_kind) = self.tcx.def_kind(item_def_id) {
542-
let item = self.tcx.hir().expect_item(hir_id);
543-
let vis = ty::Visibility::from_hir(&item.vis, hir_id, self.tcx);
544-
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
545-
}
541+
let def_kind = self.tcx.def_kind(item_def_id);
542+
let item = self.tcx.hir().expect_item(hir_id);
543+
let vis = ty::Visibility::from_hir(&item.vis, hir_id, self.tcx);
544+
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
546545
}
547546
if let Some(exports) = self.tcx.module_exports(module_def_id) {
548547
for export in exports {

src/librustc_resolve/build_reduced_graph.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
917917
| DefKind::LifetimeParam
918918
| DefKind::GlobalAsm
919919
| DefKind::Closure
920-
| DefKind::Impl,
920+
| DefKind::Impl
921+
| DefKind::Generator,
921922
_,
922923
)
923924
| Res::Local(..)

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14871487
// ```
14881488
debug!("parent_def_kind: {:?}", self.tcx.def_kind(parent_did));
14891489
let is_raw_borrow_inside_fn_like_call = match self.tcx.def_kind(parent_did) {
1490-
Some(DefKind::Fn | DefKind::Ctor(..)) => target_ty.is_unsafe_ptr(),
1490+
DefKind::Fn | DefKind::Ctor(..) => target_ty.is_unsafe_ptr(),
14911491
_ => false,
14921492
};
14931493

src/librustc_traits/lowering/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,22 @@ crate fn program_clauses_for(tcx: TyCtxt<'_>, def_id: DefId) -> Clauses<'_> {
150150
// FIXME(eddyb) this should only be using `def_kind`.
151151
match tcx.def_key(def_id).disambiguated_data.data {
152152
DefPathData::TypeNs(..) => match tcx.def_kind(def_id) {
153-
Some(DefKind::Trait | DefKind::TraitAlias) => program_clauses_for_trait(tcx, def_id),
153+
DefKind::Trait | DefKind::TraitAlias => program_clauses_for_trait(tcx, def_id),
154154
// FIXME(eddyb) deduplicate this `associated_item` call with
155155
// `program_clauses_for_associated_type_{value,def}`.
156-
Some(DefKind::AssocTy) => match tcx.associated_item(def_id).container {
156+
DefKind::AssocTy => match tcx.associated_item(def_id).container {
157157
ty::AssocItemContainer::ImplContainer(_) => {
158158
program_clauses_for_associated_type_value(tcx, def_id)
159159
}
160160
ty::AssocItemContainer::TraitContainer(_) => {
161161
program_clauses_for_associated_type_def(tcx, def_id)
162162
}
163163
},
164-
Some(
165-
DefKind::Struct
166-
| DefKind::Enum
167-
| DefKind::TyAlias
168-
| DefKind::Union
169-
| DefKind::OpaqueTy,
170-
) => program_clauses_for_type_def(tcx, def_id),
164+
DefKind::Struct
165+
| DefKind::Enum
166+
| DefKind::TyAlias
167+
| DefKind::Union
168+
| DefKind::OpaqueTy => program_clauses_for_type_def(tcx, def_id),
171169
_ => List::empty(),
172170
},
173171
DefPathData::Impl => program_clauses_for_impl(tcx, def_id),

src/librustc_typeck/check/dropck.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
9494
}
9595
Err(_) => {
9696
let item_span = tcx.def_span(self_type_did);
97-
let self_descr = tcx
98-
.def_kind(self_type_did)
99-
.map(|kind| kind.descr(self_type_did))
100-
.unwrap_or("type");
97+
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did);
10198
struct_span_err!(
10299
tcx.sess,
103100
drop_impl_span,
@@ -244,10 +241,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
244241

245242
if !assumptions_in_impl_context.iter().any(predicate_matches_closure) {
246243
let item_span = tcx.hir().span(self_type_hir_id);
247-
let self_descr = tcx
248-
.def_kind(self_type_did)
249-
.map(|kind| kind.descr(self_type_did.to_def_id()))
250-
.unwrap_or("type");
244+
let self_descr = tcx.def_kind(self_type_did).descr(self_type_did.to_def_id());
251245
struct_span_err!(
252246
tcx.sess,
253247
*predicate_sp,

src/librustc_typeck/check/expr.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1564,10 +1564,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15641564
base_did: DefId,
15651565
) {
15661566
let struct_path = self.tcx().def_path_str(base_did);
1567-
let kind_name = match self.tcx().def_kind(base_did) {
1568-
Some(def_kind) => def_kind.descr(base_did),
1569-
_ => " ",
1570-
};
1567+
let kind_name = self.tcx().def_kind(base_did).descr(base_did);
15711568
let mut err = struct_span_err!(
15721569
self.tcx().sess,
15731570
field.span,

src/librustc_typeck/check/mod.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -831,18 +831,22 @@ fn primary_body_of(
831831
}
832832

833833
fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
834+
// FIXME(#71104) some `LocalDefId` do not seem to have a corresponding `HirId`.
835+
if let Some(def_id) = def_id.as_local() {
836+
if tcx.hir().opt_local_def_id_to_hir_id(def_id).is_none() {
837+
return false;
838+
}
839+
}
840+
834841
// Closures' tables come from their outermost function,
835842
// as they are part of the same "inference environment".
836843
let outer_def_id = tcx.closure_base_def_id(def_id);
837844
if outer_def_id != def_id {
838845
return tcx.has_typeck_tables(outer_def_id);
839846
}
840847

841-
// FIXME(#71104) Should really be using just `as_local_hir_id` but
842-
// some `LocalDefId` do not seem to have a corresponding HirId.
843-
if let Some(id) =
844-
def_id.as_local().and_then(|def_id| tcx.hir().opt_local_def_id_to_hir_id(def_id))
845-
{
848+
if let Some(def_id) = def_id.as_local() {
849+
let id = tcx.hir().local_def_id_to_hir_id(def_id);
846850
primary_body_of(tcx, id).is_some()
847851
} else {
848852
false
@@ -4972,12 +4976,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
49724976
sugg_call = fields.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
49734977
match def_id
49744978
.as_local()
4975-
.and_then(|def_id| hir.def_kind(def_id))
4979+
.map(|def_id| hir.def_kind(def_id))
49764980
{
4977-
Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => {
4981+
Some(DefKind::Ctor(hir::def::CtorOf::Variant, _)) => {
49784982
msg = "instantiate this tuple variant";
49794983
}
4980-
Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Struct, _)) => {
4984+
Some(DefKind::Ctor(CtorOf::Struct, _)) => {
49814985
msg = "instantiate this tuple struct";
49824986
}
49834987
_ => {}

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn build_type_alias_type(cx: &DocContext<'_>, did: DefId) -> Option<clean::Type>
278278
}
279279

280280
pub fn build_ty(cx: &DocContext, did: DefId) -> Option<clean::Type> {
281-
match cx.tcx.def_kind(did)? {
281+
match cx.tcx.def_kind(did) {
282282
DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::Const | DefKind::Static => {
283283
Some(cx.tcx.type_of(did).clean(cx))
284284
}

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ impl Clean<Vec<Item>> for doctree::Impl<'_> {
21342134

21352135
let for_ = self.for_.clean(cx);
21362136
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
2137-
Some(DefKind::TyAlias) => Some(cx.tcx.type_of(did).clean(cx)),
2137+
DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
21382138
_ => None,
21392139
});
21402140
let make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| Item {

0 commit comments

Comments
 (0)