From 8da26e036630352a21a9ddc14bf53ad31e9b9ba0 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 26 Mar 2020 05:32:52 -0400 Subject: [PATCH 1/7] Use `call` instead of `invoke` for functions that cannot unwind The `FnAbi` now knows if the function is allowed to unwind. If a function isn't allowed to unwind, we can use a `call` instead of an `invoke`. This resolves an issue when calling LLVM intrinsics which cannot unwind LLVM will generate an error if you attempt to invoke them so we need to ignore cleanup blocks in codegen and generate a call instead. --- src/librustc_codegen_ssa/mir/block.rs | 4 +++- src/test/codegen/c-variadic.rs | 16 +++++++------- src/test/codegen/call-llvm-intrinsics.rs | 27 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 src/test/codegen/call-llvm-intrinsics.rs diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index 219d5aa77ea0d..4913138650880 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -111,7 +111,9 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> { destination: Option<(ReturnDest<'tcx, Bx::Value>, mir::BasicBlock)>, cleanup: Option, ) { - if let Some(cleanup) = cleanup { + // If there is a cleanup block and the function we're calling can unwind, then + // do an invoke, otherwise do a call. + if let Some(cleanup) = cleanup.filter(|_| fn_abi.can_unwind) { let ret_bx = if let Some((_, target)) = destination { fx.blocks[target] } else { diff --git a/src/test/codegen/c-variadic.rs b/src/test/codegen/c-variadic.rs index 971f4e3e12ea8..29c82686731ca 100644 --- a/src/test/codegen/c-variadic.rs +++ b/src/test/codegen/c-variadic.rs @@ -16,13 +16,13 @@ extern "C" { #[unwind(aborts)] // FIXME(#58794) pub unsafe extern "C" fn use_foreign_c_variadic_0() { // Ensure that we correctly call foreign C-variadic functions. - // CHECK: invoke void (i32, ...) @foreign_c_variadic_0([[PARAM:i32( signext)?]] 0) + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM:i32( signext)?]] 0) foreign_c_variadic_0(0); - // CHECK: invoke void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42) + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42) foreign_c_variadic_0(0, 42i32); - // CHECK: invoke void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024) + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024) foreign_c_variadic_0(0, 42i32, 1024i32); - // CHECK: invoke void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024, [[PARAM]] 0) + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024, [[PARAM]] 0) foreign_c_variadic_0(0, 42i32, 1024i32, 0i32); } @@ -30,24 +30,24 @@ pub unsafe extern "C" fn use_foreign_c_variadic_0() { // removing the "spoofed" `VaListImpl` that is used by Rust defined C-variadics. #[unwind(aborts)] // FIXME(#58794) pub unsafe extern "C" fn use_foreign_c_variadic_1_0(ap: VaList) { - // CHECK: invoke void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap) + // CHECK: call void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap) foreign_c_variadic_1(ap); } #[unwind(aborts)] // FIXME(#58794) pub unsafe extern "C" fn use_foreign_c_variadic_1_1(ap: VaList) { - // CHECK: invoke void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 42) + // CHECK: call void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 42) foreign_c_variadic_1(ap, 42i32); } #[unwind(aborts)] // FIXME(#58794) pub unsafe extern "C" fn use_foreign_c_variadic_1_2(ap: VaList) { - // CHECK: invoke void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 2, [[PARAM]] 42) + // CHECK: call void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 2, [[PARAM]] 42) foreign_c_variadic_1(ap, 2i32, 42i32); } #[unwind(aborts)] // FIXME(#58794) pub unsafe extern "C" fn use_foreign_c_variadic_1_3(ap: VaList) { - // CHECK: invoke void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 2, [[PARAM]] 42, [[PARAM]] 0) + // CHECK: call void ({{.*}}*, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 2, [[PARAM]] 42, [[PARAM]] 0) foreign_c_variadic_1(ap, 2i32, 42i32, 0i32); } diff --git a/src/test/codegen/call-llvm-intrinsics.rs b/src/test/codegen/call-llvm-intrinsics.rs new file mode 100644 index 0000000000000..c7a464a9b0ef2 --- /dev/null +++ b/src/test/codegen/call-llvm-intrinsics.rs @@ -0,0 +1,27 @@ +// compile-flags: -C no-prepopulate-passes + +#![feature(link_llvm_intrinsics)] +#![crate_type = "lib"] + +struct A; + +impl Drop for A { + fn drop(&mut self) { + println!("A"); + } +} + +extern { + #[link_name = "llvm.sqrt.f32"] + fn sqrt(x: f32) -> f32; +} + +pub fn do_call() { + let _a = A; + + unsafe { + // Ensure that we `call` LLVM intrinsics instead of trying to `invoke` them + // CHECK: call float @llvm.sqrt.f32(float 4.000000e+00 + sqrt(4.0); + } +} From 3e246bb3d04a6784a4024aa80662c873f7024edd Mon Sep 17 00:00:00 2001 From: marmeladema Date: Thu, 9 Apr 2020 09:43:00 +0100 Subject: [PATCH 2/7] librustc_middle: return LocalDefId instead of DefId in local_def_id --- .../back/symbol_export.rs | 10 +- src/librustc_driver/pretty.rs | 2 +- src/librustc_incremental/assert_dep_graph.rs | 4 +- .../persist/dirty_clean.rs | 6 +- src/librustc_interface/proc_macro_decls.rs | 2 +- src/librustc_lint/builtin.rs | 9 +- src/librustc_metadata/foreign_modules.rs | 8 +- src/librustc_metadata/native_libs.rs | 2 +- src/librustc_metadata/rmeta/encoder.rs | 102 ++++--- src/librustc_middle/hir/map/mod.rs | 24 +- src/librustc_middle/hir/mod.rs | 1 - src/librustc_middle/middle/stability.rs | 2 +- .../ty/inhabitedness/def_id_forest.rs | 2 +- src/librustc_middle/ty/sty.rs | 5 +- src/librustc_middle/ty/trait_def.rs | 2 +- .../borrow_check/universal_regions.rs | 2 +- src/librustc_mir/monomorphize/collector.rs | 24 +- src/librustc_mir/monomorphize/partitioning.rs | 4 +- src/librustc_mir/transform/mod.rs | 2 +- src/librustc_mir_build/build/mod.rs | 10 +- src/librustc_mir_build/hair/cx/expr.rs | 6 +- src/librustc_mir_build/hair/cx/mod.rs | 4 +- src/librustc_passes/diagnostic_items.rs | 2 +- src/librustc_passes/entry.rs | 8 +- src/librustc_passes/hir_id_validator.rs | 4 +- src/librustc_passes/lang_items.rs | 2 +- src/librustc_passes/layout_test.rs | 6 +- src/librustc_passes/liveness.rs | 6 +- src/librustc_plugin_impl/build.rs | 2 +- src/librustc_privacy/lib.rs | 21 +- src/librustc_resolve/late/lifetimes.rs | 14 +- src/librustc_symbol_mangling/test.rs | 6 +- src/librustc_trait_selection/opaque_types.rs | 3 +- src/librustc_ty/ty.rs | 18 +- src/librustc_typeck/astconv.rs | 6 +- src/librustc_typeck/check/closure.rs | 65 ++-- src/librustc_typeck/check/method/mod.rs | 4 +- src/librustc_typeck/check/method/suggest.rs | 2 +- src/librustc_typeck/check/mod.rs | 89 +++--- src/librustc_typeck/check/regionck.rs | 2 +- src/librustc_typeck/check/upvar.rs | 2 +- src/librustc_typeck/check/wfcheck.rs | 28 +- src/librustc_typeck/check/writeback.rs | 3 +- src/librustc_typeck/check_unused.rs | 4 +- src/librustc_typeck/coherence/builtin.rs | 288 +++++++++--------- .../coherence/inherent_impls.rs | 10 +- src/librustc_typeck/coherence/mod.rs | 24 +- src/librustc_typeck/coherence/orphan.rs | 2 +- src/librustc_typeck/collect.rs | 57 ++-- src/librustc_typeck/collect/type_of.rs | 8 +- src/librustc_typeck/expr_use_visitor.rs | 2 +- src/librustc_typeck/impl_wf_check.rs | 8 +- src/librustc_typeck/lib.rs | 4 +- .../outlives/implicit_infer.rs | 2 +- src/librustc_typeck/outlives/test.rs | 2 +- src/librustc_typeck/variance/constraints.rs | 2 +- src/librustc_typeck/variance/solve.rs | 2 +- src/librustc_typeck/variance/test.rs | 2 +- src/librustdoc/clean/mod.rs | 86 +++--- src/librustdoc/core.rs | 2 +- src/librustdoc/passes/collect_trait_impls.rs | 2 +- src/librustdoc/visit_ast.rs | 4 +- 62 files changed, 545 insertions(+), 492 deletions(-) diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index cb39bf3820933..346b425c4d3bf 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -94,7 +94,7 @@ fn reachable_non_generics_provider( if !generics.requires_monomorphization(tcx) && // Functions marked with #[inline] are only ever codegened // with "internal" linkage and are never exported. - !Instance::mono(tcx, def_id).def.generates_cgu_internal_copy(tcx) + !Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx) { Some(def_id) } else { @@ -107,7 +107,7 @@ fn reachable_non_generics_provider( }) .map(|def_id| { let export_level = if special_runtime_crate { - let name = tcx.symbol_name(Instance::mono(tcx, def_id)).name.as_str(); + let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name.as_str(); // We can probably do better here by just ensuring that // it has hidden visibility rather than public // visibility, as this is primarily here to ensure it's @@ -124,14 +124,14 @@ fn reachable_non_generics_provider( SymbolExportLevel::Rust } } else { - symbol_export_level(tcx, def_id) + symbol_export_level(tcx, def_id.to_def_id()) }; debug!( "EXPORTED SYMBOL (local): {} ({:?})", - tcx.symbol_name(Instance::mono(tcx, def_id)), + tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())), export_level ); - (def_id, export_level) + (def_id.to_def_id(), export_level) }) .collect(); diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 78a271810b288..610e9b9051040 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -322,7 +322,7 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> { } fn node_path(&self, id: hir::HirId) -> Option { - Some(self.tcx.def_path_str(self.tcx.hir().local_def_id(id))) + Some(self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id())) } } diff --git a/src/librustc_incremental/assert_dep_graph.rs b/src/librustc_incremental/assert_dep_graph.rs index 5135820ac507f..673d6e92b7ee3 100644 --- a/src/librustc_incremental/assert_dep_graph.rs +++ b/src/librustc_incremental/assert_dep_graph.rs @@ -115,7 +115,7 @@ impl IfThisChanged<'tcx> { fn process_attrs(&mut self, hir_id: hir::HirId, attrs: &[ast::Attribute]) { let def_id = self.tcx.hir().local_def_id(hir_id); - let def_path_hash = self.tcx.def_path_hash(def_id); + let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id()); for attr in attrs { if attr.check_name(sym::rustc_if_this_changed) { let dep_node_interned = self.argument(attr); @@ -131,7 +131,7 @@ impl IfThisChanged<'tcx> { } }, }; - self.if_this_changed.push((attr.span, def_id, dep_node)); + self.if_this_changed.push((attr.span, def_id.to_def_id(), dep_node)); } else if attr.check_name(sym::rustc_then_this_would_need) { let dep_node_interned = self.argument(attr); let dep_node = match dep_node_interned { diff --git a/src/librustc_incremental/persist/dirty_clean.rs b/src/librustc_incremental/persist/dirty_clean.rs index 804d1744d6872..27aebf7b1b98d 100644 --- a/src/librustc_incremental/persist/dirty_clean.rs +++ b/src/librustc_incremental/persist/dirty_clean.rs @@ -434,16 +434,16 @@ impl DirtyCleanVisitor<'tcx> { fn check_item(&mut self, item_id: hir::HirId, item_span: Span) { let def_id = self.tcx.hir().local_def_id(item_id); - for attr in self.tcx.get_attrs(def_id).iter() { + for attr in self.tcx.get_attrs(def_id.to_def_id()).iter() { let assertion = match self.assertion_maybe(item_id, attr) { Some(a) => a, None => continue, }; self.checked_attrs.insert(attr.id); - for dep_node in self.dep_nodes(&assertion.clean, def_id) { + for dep_node in self.dep_nodes(&assertion.clean, def_id.to_def_id()) { self.assert_clean(item_span, dep_node); } - for dep_node in self.dep_nodes(&assertion.dirty, def_id) { + for dep_node in self.dep_nodes(&assertion.dirty, def_id.to_def_id()) { self.assert_dirty(item_span, dep_node); } } diff --git a/src/librustc_interface/proc_macro_decls.rs b/src/librustc_interface/proc_macro_decls.rs index 47b53aeba725d..c74cba81ca907 100644 --- a/src/librustc_interface/proc_macro_decls.rs +++ b/src/librustc_interface/proc_macro_decls.rs @@ -16,7 +16,7 @@ fn proc_macro_decls_static(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option { let mut finder = Finder { decls: None }; tcx.hir().krate().visit_all_item_likes(&mut finder); - finder.decls.map(|id| tcx.hir().local_def_id(id)) + finder.decls.map(|id| tcx.hir().local_def_id(id).to_def_id()) } struct Finder { diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 627a438c2c3b1..0fba510e101a1 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -461,7 +461,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { }; let def_id = cx.tcx.hir().local_def_id(it.hir_id); - let (article, desc) = cx.tcx.article_and_description(def_id); + let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id()); self.check_missing_docs_attrs(cx, Some(it.hir_id), &it.attrs, it.span, article, desc); } @@ -472,7 +472,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { } let def_id = cx.tcx.hir().local_def_id(trait_item.hir_id); - let (article, desc) = cx.tcx.article_and_description(def_id); + let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id()); self.check_missing_docs_attrs( cx, @@ -491,7 +491,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { } let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id); - let (article, desc) = cx.tcx.article_and_description(def_id); + let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id()); self.check_missing_docs_attrs( cx, Some(impl_item.hir_id), @@ -1531,7 +1531,8 @@ impl ExplicitOutlivesRequirements { inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)], ty_generics: &'tcx ty::Generics, ) -> Vec> { - let index = ty_generics.param_def_id_to_index[&tcx.hir().local_def_id(param.hir_id)]; + let index = + ty_generics.param_def_id_to_index[&tcx.hir().local_def_id(param.hir_id).to_def_id()]; match param.kind { hir::GenericParamKind::Lifetime { .. } => { diff --git a/src/librustc_metadata/foreign_modules.rs b/src/librustc_metadata/foreign_modules.rs index b312aa37d46b0..8675197656a48 100644 --- a/src/librustc_metadata/foreign_modules.rs +++ b/src/librustc_metadata/foreign_modules.rs @@ -22,9 +22,11 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> { }; let foreign_items = - fm.items.iter().map(|it| self.tcx.hir().local_def_id(it.hir_id)).collect(); - self.modules - .push(ForeignModule { foreign_items, def_id: self.tcx.hir().local_def_id(it.hir_id) }); + fm.items.iter().map(|it| self.tcx.hir().local_def_id(it.hir_id).to_def_id()).collect(); + self.modules.push(ForeignModule { + foreign_items, + def_id: self.tcx.hir().local_def_id(it.hir_id).to_def_id(), + }); } fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {} diff --git a/src/librustc_metadata/native_libs.rs b/src/librustc_metadata/native_libs.rs index 4b92a2205c211..51c9950a5dfed 100644 --- a/src/librustc_metadata/native_libs.rs +++ b/src/librustc_metadata/native_libs.rs @@ -51,7 +51,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> { name: None, kind: cstore::NativeUnknown, cfg: None, - foreign_module: Some(self.tcx.hir().local_def_id(it.hir_id)), + foreign_module: Some(self.tcx.hir().local_def_id(it.hir_id).to_def_id()), wasm_import_module: None, }; let mut kind_specified = false; diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index 9c9869c85571f..5a1ba0bbeeabd 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -604,9 +604,8 @@ impl EncodeContext<'tcx> { record!(self.tables.ty[def_id] <- self.tcx.type_of(def_id)); } - fn encode_enum_variant_info(&mut self, enum_did: DefId, index: VariantIdx) { + fn encode_enum_variant_info(&mut self, def: &ty::AdtDef, index: VariantIdx) { let tcx = self.tcx; - let def = tcx.adt_def(enum_did); let variant = &def.variants[index]; let def_id = variant.def_id; debug!("EncodeContext::encode_enum_variant_info({:?})", def_id); @@ -617,7 +616,7 @@ impl EncodeContext<'tcx> { ctor: variant.ctor_def_id.map(|did| did.index), }; - let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap(); + let enum_id = tcx.hir().as_local_hir_id(def.did).unwrap(); let enum_vis = &tcx.hir().expect_item(enum_id).vis; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); @@ -648,9 +647,8 @@ impl EncodeContext<'tcx> { self.encode_promoted_mir(def_id); } - fn encode_enum_variant_ctor(&mut self, enum_did: DefId, index: VariantIdx) { + fn encode_enum_variant_ctor(&mut self, def: &ty::AdtDef, index: VariantIdx) { let tcx = self.tcx; - let def = tcx.adt_def(enum_did); let variant = &def.variants[index]; let def_id = variant.ctor_def_id.unwrap(); debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id); @@ -664,7 +662,7 @@ impl EncodeContext<'tcx> { // Variant constructors have the same visibility as the parent enums, unless marked as // non-exhaustive, in which case they are lowered to `pub(crate)`. - let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap(); + let enum_id = tcx.hir().as_local_hir_id(def.did).unwrap(); let enum_vis = &tcx.hir().expect_item(enum_id).vis; let mut ctor_vis = ty::Visibility::from_hir(enum_vis, enum_id, tcx); if variant.is_field_list_non_exhaustive() && ctor_vis == ty::Visibility::Public { @@ -696,7 +694,7 @@ impl EncodeContext<'tcx> { vis: &hir::Visibility<'_>, ) { let tcx = self.tcx; - let def_id = tcx.hir().local_def_id(id); + let def_id = tcx.hir().local_def_id(id).to_def_id(); debug!("EncodeContext::encode_info_for_mod({:?})", def_id); let data = ModData { @@ -711,15 +709,20 @@ impl EncodeContext<'tcx> { record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- attrs); record!(self.tables.children[def_id] <- md.item_ids.iter().map(|item_id| { - tcx.hir().local_def_id(item_id.id).index + tcx.hir().local_def_id(item_id.id).local_def_index })); self.encode_stability(def_id); self.encode_deprecation(def_id); } - fn encode_field(&mut self, adt_def_id: DefId, variant_index: VariantIdx, field_index: usize) { + fn encode_field( + &mut self, + adt_def: &ty::AdtDef, + variant_index: VariantIdx, + field_index: usize, + ) { let tcx = self.tcx; - let variant = &tcx.adt_def(adt_def_id).variants[variant_index]; + let variant = &adt_def.variants[variant_index]; let field = &variant.fields[field_index]; let def_id = field.did; @@ -741,10 +744,9 @@ impl EncodeContext<'tcx> { self.encode_inferred_outlives(def_id); } - fn encode_struct_ctor(&mut self, adt_def_id: DefId, def_id: DefId) { + fn encode_struct_ctor(&mut self, adt_def: &ty::AdtDef, def_id: DefId) { debug!("EncodeContext::encode_struct_ctor({:?})", def_id); let tcx = self.tcx; - let adt_def = tcx.adt_def(adt_def_id); let variant = adt_def.non_enum_variant(); let data = VariantData { @@ -753,7 +755,7 @@ impl EncodeContext<'tcx> { ctor: Some(def_id.index), }; - let struct_id = tcx.hir().as_local_hir_id(adt_def_id).unwrap(); + let struct_id = tcx.hir().as_local_hir_id(adt_def.did).unwrap(); let struct_vis = &tcx.hir().expect_item(struct_id).vis; let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx); for field in &variant.fields { @@ -1100,7 +1102,7 @@ impl EncodeContext<'tcx> { // for methods, write all the stuff get_trait_method // needs to know let ctor = struct_def.ctor_hir_id().map(|ctor_hir_id| { - self.tcx.hir().local_def_id(ctor_hir_id).index + self.tcx.hir().local_def_id(ctor_hir_id).local_def_index }); EntryKind::Struct(self.lazy(VariantData { @@ -1181,7 +1183,7 @@ impl EncodeContext<'tcx> { fm.items .iter() .map(|foreign_item| tcx.hir().local_def_id( - foreign_item.hir_id).index) + foreign_item.hir_id).local_def_index) ), hir::ItemKind::Enum(..) => record!(self.tables.children[def_id] <- self.tcx.adt_def(def_id).variants.iter().map(|v| { @@ -1286,7 +1288,7 @@ impl EncodeContext<'tcx> { /// Serialize the text of exported macros fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) { - let def_id = self.tcx.hir().local_def_id(macro_def.hir_id); + let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); record!(self.tables.visibility[def_id] <- ty::Visibility::Public); record!(self.tables.span[def_id] <- macro_def.span); @@ -1305,7 +1307,8 @@ impl EncodeContext<'tcx> { } } - fn encode_info_for_closure(&mut self, def_id: DefId) { + fn encode_info_for_closure(&mut self, def_id: LocalDefId) { + let def_id = def_id.to_def_id(); debug!("EncodeContext::encode_info_for_closure({:?})", def_id); // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, @@ -1335,7 +1338,8 @@ impl EncodeContext<'tcx> { self.encode_promoted_mir(def_id); } - fn encode_info_for_anon_const(&mut self, def_id: DefId) { + fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) { + let def_id = def_id.to_def_id(); debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id); let id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); let body_id = self.tcx.hir().body_owned_by(id); @@ -1572,14 +1576,14 @@ impl Visitor<'tcx> for EncodeContext<'tcx> { let def_id = self.tcx.hir().local_def_id(item.hir_id); match item.kind { hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => {} // ignore these - _ => self.encode_info_for_item(def_id, item), + _ => self.encode_info_for_item(def_id.to_def_id(), item), } self.encode_addl_info_for_item(item); } fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem<'tcx>) { intravisit::walk_foreign_item(self, ni); let def_id = self.tcx.hir().local_def_id(ni.hir_id); - self.encode_info_for_foreign_item(def_id, ni); + self.encode_info_for_foreign_item(def_id.to_def_id(), ni); } fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) { intravisit::walk_generics(self, generics); @@ -1591,13 +1595,10 @@ impl Visitor<'tcx> for EncodeContext<'tcx> { } impl EncodeContext<'tcx> { - fn encode_fields(&mut self, adt_def_id: DefId) { - let def = self.tcx.adt_def(adt_def_id); - for (variant_index, variant) in def.variants.iter_enumerated() { + fn encode_fields(&mut self, adt_def: &ty::AdtDef) { + for (variant_index, variant) in adt_def.variants.iter_enumerated() { for (field_index, _field) in variant.fields.iter().enumerate() { - // FIXME(eddyb) `adt_def_id` is leftover from incremental isolation, - // pass `def`, `variant` or `field` instead. - self.encode_field(adt_def_id, variant_index, field_index); + self.encode_field(adt_def, variant_index, field_index); } } } @@ -1609,13 +1610,17 @@ impl EncodeContext<'tcx> { GenericParamKind::Lifetime { .. } => continue, GenericParamKind::Type { ref default, .. } => { self.encode_info_for_generic_param( - def_id, + def_id.to_def_id(), EntryKind::TypeParam, default.is_some(), ); } GenericParamKind::Const { .. } => { - self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true); + self.encode_info_for_generic_param( + def_id.to_def_id(), + EntryKind::ConstParam, + true, + ); } } } @@ -1653,40 +1658,40 @@ impl EncodeContext<'tcx> { // no sub-item recording needed in these cases } hir::ItemKind::Enum(..) => { - self.encode_fields(def_id); + let def = self.tcx.adt_def(def_id.to_def_id()); + self.encode_fields(def); - let def = self.tcx.adt_def(def_id); for (i, variant) in def.variants.iter_enumerated() { - // FIXME(eddyb) `def_id` is leftover from incremental isolation, - // pass `def` or `variant` instead. - self.encode_enum_variant_info(def_id, i); + self.encode_enum_variant_info(def, i); - // FIXME(eddyb) `def_id` is leftover from incremental isolation, - // pass `def`, `variant` or `ctor_def_id` instead. if let Some(_ctor_def_id) = variant.ctor_def_id { - self.encode_enum_variant_ctor(def_id, i); + self.encode_enum_variant_ctor(def, i); } } } hir::ItemKind::Struct(ref struct_def, _) => { - self.encode_fields(def_id); + let def = self.tcx.adt_def(def_id.to_def_id()); + self.encode_fields(def); // If the struct has a constructor, encode it. if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { let ctor_def_id = self.tcx.hir().local_def_id(ctor_hir_id); - self.encode_struct_ctor(def_id, ctor_def_id); + self.encode_struct_ctor(def, ctor_def_id.to_def_id()); } } hir::ItemKind::Union(..) => { - self.encode_fields(def_id); + let def = self.tcx.adt_def(def_id.to_def_id()); + self.encode_fields(def); } hir::ItemKind::Impl { .. } => { - for &trait_item_def_id in self.tcx.associated_item_def_ids(def_id).iter() { + for &trait_item_def_id in + self.tcx.associated_item_def_ids(def_id.to_def_id()).iter() + { self.encode_info_for_impl_item(trait_item_def_id); } } hir::ItemKind::Trait(..) => { - for &item_def_id in self.tcx.associated_item_def_ids(def_id).iter() { + for &item_def_id in self.tcx.associated_item_def_ids(def_id.to_def_id()).iter() { self.encode_info_for_trait_item(item_def_id); } } @@ -1703,8 +1708,8 @@ impl<'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { if let hir::ItemKind::Impl { .. } = item.kind { let impl_id = self.tcx.hir().local_def_id(item.hir_id); - if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id) { - self.impls.entry(trait_ref.def_id).or_default().push(impl_id.index); + if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id.to_def_id()) { + self.impls.entry(trait_ref.def_id).or_default().push(impl_id.local_def_index); } } } @@ -1724,7 +1729,8 @@ struct PrefetchVisitor<'tcx> { } impl<'tcx> PrefetchVisitor<'tcx> { - fn prefetch_mir(&self, def_id: DefId) { + fn prefetch_mir(&self, def_id: LocalDefId) { + let def_id = def_id.to_def_id(); if self.mir_keys.contains(&def_id) { self.tcx.optimized_mir(def_id); self.tcx.promoted_mir(def_id); @@ -1742,9 +1748,9 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> { } hir::ItemKind::Fn(ref sig, ..) => { let def_id = tcx.hir().local_def_id(item.hir_id); - let generics = tcx.generics_of(def_id); + let generics = tcx.generics_of(def_id.to_def_id()); let needs_inline = generics.requires_monomorphization(tcx) - || tcx.codegen_fn_attrs(def_id).requests_inline(); + || tcx.codegen_fn_attrs(def_id.to_def_id()).requests_inline(); if needs_inline || sig.header.constness == hir::Constness::Const { self.prefetch_mir(def_id) } @@ -1767,9 +1773,9 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> { } hir::ImplItemKind::Fn(ref sig, _) => { let def_id = tcx.hir().local_def_id(impl_item.hir_id); - let generics = tcx.generics_of(def_id); + let generics = tcx.generics_of(def_id.to_def_id()); let needs_inline = generics.requires_monomorphization(tcx) - || tcx.codegen_fn_attrs(def_id).requests_inline(); + || tcx.codegen_fn_attrs(def_id.to_def_id()).requests_inline(); let is_const_fn = sig.header.constness == hir::Constness::Const; if needs_inline || is_const_fn { self.prefetch_mir(def_id) diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index ead8529fad8be..1f25a2f6cfba8 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -171,16 +171,14 @@ impl<'hir> Map<'hir> { // FIXME(eddyb) this function can and should return `LocalDefId`. #[inline] - pub fn local_def_id(&self, hir_id: HirId) -> DefId { - self.opt_local_def_id(hir_id) - .unwrap_or_else(|| { - bug!( - "local_def_id: no entry for `{:?}`, which has a map of `{:?}`", - hir_id, - self.find_entry(hir_id) - ) - }) - .to_def_id() + pub fn local_def_id(&self, hir_id: HirId) -> LocalDefId { + self.opt_local_def_id(hir_id).unwrap_or_else(|| { + bug!( + "local_def_id: no entry for `{:?}`, which has a map of `{:?}`", + hir_id, + self.find_entry(hir_id) + ) + }) } #[inline] @@ -378,7 +376,7 @@ impl<'hir> Map<'hir> { } pub fn body_owner_def_id(&self, id: BodyId) -> LocalDefId { - self.local_def_id(self.body_owner(id)).expect_local() + self.local_def_id(self.body_owner(id)) } /// Given a `HirId`, returns the `BodyId` associated with it, @@ -731,7 +729,7 @@ impl<'hir> Map<'hir> { } pub fn get_parent_did(&self, id: HirId) -> LocalDefId { - self.local_def_id(self.get_parent_item(id)).expect_local() + self.local_def_id(self.get_parent_item(id)) } pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi { @@ -997,7 +995,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String { crate::ty::tls::with_opt(|tcx| { if let Some(tcx) = tcx { let def_id = map.local_def_id(id); - tcx.def_path_str(def_id) + tcx.def_path_str(def_id.to_def_id()) } else if let Some(path) = map.def_path_from_hir_id(id) { path.data .into_iter() diff --git a/src/librustc_middle/hir/mod.rs b/src/librustc_middle/hir/mod.rs index ce8e1f48daa77..3d3d9dcf41da5 100644 --- a/src/librustc_middle/hir/mod.rs +++ b/src/librustc_middle/hir/mod.rs @@ -69,7 +69,6 @@ pub fn provide(providers: &mut Providers<'_>) { providers.parent_module_from_def_id = |tcx, id| { let hir = tcx.hir(); hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id.to_def_id()).unwrap())) - .expect_local() }; providers.hir_crate = |tcx, _| tcx.untracked_crate; providers.index_hir = map::index_hir; diff --git a/src/librustc_middle/middle/stability.rs b/src/librustc_middle/middle/stability.rs index 1dd14b7c4ffda..9d95a700313d6 100644 --- a/src/librustc_middle/middle/stability.rs +++ b/src/librustc_middle/middle/stability.rs @@ -286,7 +286,7 @@ impl<'tcx> TyCtxt<'tcx> { if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) { let parent_def_id = self.hir().local_def_id(self.hir().get_parent_item(id)); let skip = self - .lookup_deprecation_entry(parent_def_id) + .lookup_deprecation_entry(parent_def_id.to_def_id()) .map_or(false, |parent_depr| parent_depr.same_origin(&depr_entry)); if !skip { diff --git a/src/librustc_middle/ty/inhabitedness/def_id_forest.rs b/src/librustc_middle/ty/inhabitedness/def_id_forest.rs index 14ead77653c32..ee6b06a1cc803 100644 --- a/src/librustc_middle/ty/inhabitedness/def_id_forest.rs +++ b/src/librustc_middle/ty/inhabitedness/def_id_forest.rs @@ -32,7 +32,7 @@ impl<'tcx> DefIdForest { #[inline] pub fn full(tcx: TyCtxt<'tcx>) -> DefIdForest { let crate_id = tcx.hir().local_def_id(CRATE_HIR_ID); - DefIdForest::from_id(crate_id) + DefIdForest::from_id(crate_id.to_def_id()) } /// Creates a forest containing a `DefId` and all its descendants. diff --git a/src/librustc_middle/ty/sty.rs b/src/librustc_middle/ty/sty.rs index 248a2095d0a2c..d99a96316ae78 100644 --- a/src/librustc_middle/ty/sty.rs +++ b/src/librustc_middle/ty/sty.rs @@ -2264,8 +2264,9 @@ impl<'tcx> Const<'tcx> { let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); let item_id = tcx.hir().get_parent_node(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); - let generics = tcx.generics_of(item_def_id); - let index = generics.param_def_id_to_index[&tcx.hir().local_def_id(hir_id)]; + let generics = tcx.generics_of(item_def_id.to_def_id()); + let index = + generics.param_def_id_to_index[&tcx.hir().local_def_id(hir_id).to_def_id()]; let name = tcx.hir().name(hir_id); ty::ConstKind::Param(ty::ParamConst::new(index, name)) } diff --git a/src/librustc_middle/ty/trait_def.rs b/src/librustc_middle/ty/trait_def.rs index 912f8be1d3342..2686759ab971b 100644 --- a/src/librustc_middle/ty/trait_def.rs +++ b/src/librustc_middle/ty/trait_def.rs @@ -217,7 +217,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> &Trai } for &hir_id in tcx.hir().trait_impls(trait_id) { - add_impl(tcx.hir().local_def_id(hir_id)); + add_impl(tcx.hir().local_def_id(hir_id).to_def_id()); } } diff --git a/src/librustc_mir/borrow_check/universal_regions.rs b/src/librustc_mir/borrow_check/universal_regions.rs index 4d67d7204ca6e..2dbfcb690179e 100644 --- a/src/librustc_mir/borrow_check/universal_regions.rs +++ b/src/librustc_mir/borrow_check/universal_regions.rs @@ -777,7 +777,7 @@ fn for_each_late_bound_region_defined_on<'tcx>( let region_def_id = tcx.hir().local_def_id(hir_id); let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion { scope: fn_def_id, - bound_region: ty::BoundRegion::BrNamed(region_def_id, name), + bound_region: ty::BoundRegion::BrNamed(region_def_id.to_def_id(), name), })); f(liberated_region); } diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index d8ceda96a25e1..d85e3bdedd4ff 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -179,7 +179,7 @@ use crate::monomorphize; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator}; use rustc_hir as hir; -use rustc_hir::def_id::{DefId, DefIdMap, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem}; use rustc_index::bit_set::GrowableBitSet; @@ -949,8 +949,8 @@ impl ItemLikeVisitor<'v> for RootCollector<'_, 'v> { def_id_to_string(self.tcx, def_id) ); - let ty = - Instance::new(def_id, InternalSubsts::empty()).monomorphic_ty(self.tcx); + let ty = Instance::new(def_id.to_def_id(), InternalSubsts::empty()) + .monomorphic_ty(self.tcx); visit_drop_use(self.tcx, ty, true, self.output); } } @@ -965,7 +965,7 @@ impl ItemLikeVisitor<'v> for RootCollector<'_, 'v> { hir::ItemKind::Static(..) => { let def_id = self.tcx.hir().local_def_id(item.hir_id); debug!("RootCollector: ItemKind::Static({})", def_id_to_string(self.tcx, def_id)); - self.output.push(MonoItem::Static(def_id)); + self.output.push(MonoItem::Static(def_id.to_def_id())); } hir::ItemKind::Const(..) => { // const items only generate mono items if they are @@ -974,7 +974,7 @@ impl ItemLikeVisitor<'v> for RootCollector<'_, 'v> { // but even just declaring them must collect the items they refer to let def_id = self.tcx.hir().local_def_id(item.hir_id); - if let Ok(val) = self.tcx.const_eval_poly(def_id) { + if let Ok(val) = self.tcx.const_eval_poly(def_id.to_def_id()) { collect_const_value(self.tcx, val, &mut self.output); } } @@ -999,12 +999,12 @@ impl ItemLikeVisitor<'v> for RootCollector<'_, 'v> { } impl RootCollector<'_, 'v> { - fn is_root(&self, def_id: DefId) -> bool { + fn is_root(&self, def_id: LocalDefId) -> bool { !item_requires_monomorphization(self.tcx, def_id) && match self.mode { MonoItemCollectionMode::Eager => true, MonoItemCollectionMode::Lazy => { - self.entry_fn.map(|(id, _)| id) == Some(def_id) + self.entry_fn.map(|(id, _)| id) == Some(def_id.to_def_id()) || self.tcx.is_reachable_non_generic(def_id) || self .tcx @@ -1017,11 +1017,11 @@ impl RootCollector<'_, 'v> { /// If `def_id` represents a root, pushes it onto the list of /// outputs. (Note that all roots must be monomorphic.) - fn push_if_root(&mut self, def_id: DefId) { + fn push_if_root(&mut self, def_id: LocalDefId) { if self.is_root(def_id) { debug!("RootCollector::push_if_root: found root def_id={:?}", def_id); - let instance = Instance::mono(self.tcx, def_id); + let instance = Instance::mono(self.tcx, def_id.to_def_id()); self.output.push(create_fn_mono_item(instance)); } } @@ -1062,7 +1062,7 @@ impl RootCollector<'_, 'v> { } } -fn item_requires_monomorphization(tcx: TyCtxt<'_>, def_id: DefId) -> bool { +fn item_requires_monomorphization(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { let generics = tcx.generics_of(def_id); generics.requires_monomorphization(tcx) } @@ -1165,10 +1165,10 @@ fn collect_neighbours<'tcx>( MirNeighborCollector { tcx, body: &body, output, instance }.visit_body(&body); } -fn def_id_to_string(tcx: TyCtxt<'_>, def_id: DefId) -> String { +fn def_id_to_string(tcx: TyCtxt<'_>, def_id: LocalDefId) -> String { let mut output = String::new(); let printer = DefPathBasedNames::new(tcx, false, false); - printer.push_def_path(def_id, &mut output); + printer.push_def_path(def_id.to_def_id(), &mut output); output } diff --git a/src/librustc_mir/monomorphize/partitioning.rs b/src/librustc_mir/monomorphize/partitioning.rs index 5f75633ae591b..cad5b114ae455 100644 --- a/src/librustc_mir/monomorphize/partitioning.rs +++ b/src/librustc_mir/monomorphize/partitioning.rs @@ -306,7 +306,7 @@ fn mono_item_visibility( let def_id = tcx.hir().local_def_id(*hir_id); return if tcx.is_reachable_non_generic(def_id) { *can_be_internalized = false; - default_visibility(tcx, def_id, false) + default_visibility(tcx, def_id.to_def_id(), false) } else { Visibility::Hidden }; @@ -755,7 +755,7 @@ fn characteristic_def_id_of_mono_item<'tcx>( Some(def_id) } MonoItem::Static(def_id) => Some(def_id), - MonoItem::GlobalAsm(hir_id) => Some(tcx.hir().local_def_id(hir_id)), + MonoItem::GlobalAsm(hir_id) => Some(tcx.hir().local_def_id(hir_id).to_def_id()), } } diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs index 8db0b39a497a9..8aa371a383381 100644 --- a/src/librustc_mir/transform/mod.rs +++ b/src/librustc_mir/transform/mod.rs @@ -80,7 +80,7 @@ fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> &DefIdSet { _: Span, ) { if let hir::VariantData::Tuple(_, hir_id) = *v { - self.set.insert(self.tcx.hir().local_def_id(hir_id)); + self.set.insert(self.tcx.hir().local_def_id(hir_id).to_def_id()); } intravisit::walk_struct_def(self, v) } diff --git a/src/librustc_mir_build/build/mod.rs b/src/librustc_mir_build/build/mod.rs index 04cb509d44e4b..5374000d5a8bb 100644 --- a/src/librustc_mir_build/build/mod.rs +++ b/src/librustc_mir_build/build/mod.rs @@ -4,7 +4,7 @@ use crate::hair::cx::Cx; use crate::hair::{BindingMode, LintLevel, PatKind}; use rustc_attr::{self as attr, UnwindAttr}; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::lang_items; use rustc_hir::{GeneratorKind, HirIdMap, Node}; use rustc_index::vec::{Idx, IndexVec}; @@ -521,9 +521,9 @@ macro_rules! unpack { }}; } -fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: DefId, _abi: Abi) -> bool { +fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: LocalDefId, _abi: Abi) -> bool { // Validate `#[unwind]` syntax regardless of platform-specific panic strategy. - let attrs = &tcx.get_attrs(fn_def_id); + let attrs = &tcx.get_attrs(fn_def_id.to_def_id()); let unwind_attr = attr::find_unwind_attr(Some(tcx.sess.diagnostic()), attrs); // We never unwind, so it's not relevant to stop an unwind. @@ -611,7 +611,7 @@ where builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| { builder.args_and_body( block, - fn_def_id, + fn_def_id.to_def_id(), &arguments, arg_scope, &body.value, @@ -641,7 +641,7 @@ where } else { None }; - debug!("fn_id {:?} has attrs {:?}", fn_def_id, tcx.get_attrs(fn_def_id)); + debug!("fn_id {:?} has attrs {:?}", fn_def_id, tcx.get_attrs(fn_def_id.to_def_id())); let mut body = builder.finish(); body.spread_arg = spread_arg; diff --git a/src/librustc_mir_build/hair/cx/expr.rs b/src/librustc_mir_build/hair/cx/expr.rs index d2d99cf030d96..73306d89a2e71 100644 --- a/src/librustc_mir_build/hair/cx/expr.rs +++ b/src/librustc_mir_build/hair/cx/expr.rs @@ -408,7 +408,7 @@ fn make_mirror_unadjusted<'a, 'tcx>( // Now comes the rote stuff: hir::ExprKind::Repeat(ref v, ref count) => { - let count_def_id = cx.tcx.hir().local_def_id(count.hir_id).expect_local(); + let count_def_id = cx.tcx.hir().local_def_id(count.hir_id); let count = ty::Const::from_anon_const(cx.tcx, count_def_id); ExprKind::Repeat { value: v.to_ref(), count } @@ -695,7 +695,7 @@ fn convert_path_expr<'a, 'tcx>( let item_def_id = cx.tcx.hir().local_def_id(item_id); let generics = cx.tcx.generics_of(item_def_id); let local_def_id = cx.tcx.hir().local_def_id(hir_id); - let index = generics.param_def_id_to_index[&local_def_id]; + let index = generics.param_def_id_to_index[&local_def_id.to_def_id()]; let name = cx.tcx.hir().name(hir_id); let val = ty::ConstKind::Param(ty::ParamConst::new(index, name)); ExprKind::Literal { @@ -968,7 +968,7 @@ fn capture_upvar<'tcx>( ) -> ExprRef<'tcx> { let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_hir_id }, - closure_expr_id: cx.tcx.hir().local_def_id(closure_expr.hir_id).expect_local(), + closure_expr_id: cx.tcx.hir().local_def_id(closure_expr.hir_id), }; let upvar_capture = cx.tables().upvar_capture(upvar_id); let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id); diff --git a/src/librustc_mir_build/hair/cx/mod.rs b/src/librustc_mir_build/hair/cx/mod.rs index 18a981dfea1bd..46607fd07cdd7 100644 --- a/src/librustc_mir_build/hair/cx/mod.rs +++ b/src/librustc_mir_build/hair/cx/mod.rs @@ -82,11 +82,11 @@ impl<'a, 'tcx> Cx<'a, 'tcx> { infcx, root_lint_level: src_id, param_env: tcx.param_env(src_def_id), - identity_substs: InternalSubsts::identity_for_item(tcx, src_def_id), + identity_substs: InternalSubsts::identity_for_item(tcx, src_def_id.to_def_id()), region_scope_tree: tcx.region_scope_tree(src_def_id), tables, constness, - body_owner: src_def_id, + body_owner: src_def_id.to_def_id(), body_owner_kind, check_overflow, control_flow_destroyed: Vec::new(), diff --git a/src/librustc_passes/diagnostic_items.rs b/src/librustc_passes/diagnostic_items.rs index c7210432b1d90..bed2221fe6076 100644 --- a/src/librustc_passes/diagnostic_items.rs +++ b/src/librustc_passes/diagnostic_items.rs @@ -47,7 +47,7 @@ impl<'tcx> DiagnosticItemCollector<'tcx> { if let Some(name) = extract(attrs) { let def_id = self.tcx.hir().local_def_id(hir_id); // insert into our table - collect_item(self.tcx, &mut self.items, name, def_id); + collect_item(self.tcx, &mut self.items, name, def_id.to_def_id()); } } } diff --git a/src/librustc_passes/entry.rs b/src/librustc_passes/entry.rs index 678859219eaab..35805db8d59ef 100644 --- a/src/librustc_passes/entry.rs +++ b/src/librustc_passes/entry.rs @@ -34,7 +34,7 @@ struct EntryContext<'a, 'tcx> { impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> { fn visit_item(&mut self, item: &'tcx Item<'tcx>) { let def_id = self.map.local_def_id(item.hir_id); - let def_key = self.map.def_key(def_id.expect_local()); + let def_key = self.map.def_key(def_id); let at_root = def_key.parent == Some(CRATE_DEF_INDEX); find_item(item, self, at_root); } @@ -145,11 +145,11 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(DefId, EntryFnType)> { if let Some((hir_id, _)) = visitor.start_fn { - Some((tcx.hir().local_def_id(hir_id), EntryFnType::Start)) + Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Start)) } else if let Some((hir_id, _)) = visitor.attr_main_fn { - Some((tcx.hir().local_def_id(hir_id), EntryFnType::Main)) + Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Main)) } else if let Some((hir_id, _)) = visitor.main_fn { - Some((tcx.hir().local_def_id(hir_id), EntryFnType::Main)) + Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Main)) } else { no_main_err(tcx, visitor); None diff --git a/src/librustc_passes/hir_id_validator.rs b/src/librustc_passes/hir_id_validator.rs index 1e31b7c74b6f0..80dfcd9c2417a 100644 --- a/src/librustc_passes/hir_id_validator.rs +++ b/src/librustc_passes/hir_id_validator.rs @@ -17,7 +17,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) { par_iter(&hir_map.krate().modules).for_each(|(module_id, _)| { let local_def_id = hir_map.local_def_id(*module_id); hir_map.visit_item_likes_in_module( - local_def_id, + local_def_id.to_def_id(), &mut OuterVisitor { hir_map, errors: &errors }, ); }); @@ -79,7 +79,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { fn check)>(&mut self, hir_id: HirId, walk: F) { assert!(self.owner.is_none()); - let owner = self.hir_map.local_def_id(hir_id).expect_local(); + let owner = self.hir_map.local_def_id(hir_id); self.owner = Some(owner); walk(self); diff --git a/src/librustc_passes/lang_items.rs b/src/librustc_passes/lang_items.rs index f1aa76cd223ff..c48e6f9133bcd 100644 --- a/src/librustc_passes/lang_items.rs +++ b/src/librustc_passes/lang_items.rs @@ -34,7 +34,7 @@ impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> { // Known lang item with attribute on correct target. Some((item_index, expected_target)) if actual_target == expected_target => { let def_id = self.tcx.hir().local_def_id(item.hir_id); - self.collect_item(item_index, def_id); + self.collect_item(item_index, def_id.to_def_id()); } // Known lang item with attribute on incorrect target. Some((_, expected_target)) => { diff --git a/src/librustc_passes/layout_test.rs b/src/librustc_passes/layout_test.rs index dbc39169f2b70..c0826f8cc605f 100644 --- a/src/librustc_passes/layout_test.rs +++ b/src/librustc_passes/layout_test.rs @@ -1,6 +1,6 @@ use rustc_ast::ast::Attribute; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::LocalDefId; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::ItemKind; use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, TyAndLayout}; @@ -29,7 +29,7 @@ impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> { | ItemKind::Struct(..) | ItemKind::Union(..) | ItemKind::OpaqueTy(..) => { - for attr in self.tcx.get_attrs(item_def_id).iter() { + for attr in self.tcx.get_attrs(item_def_id.to_def_id()).iter() { if attr.check_name(sym::rustc_layout) { self.dump_layout_of(item_def_id, item, attr); } @@ -44,7 +44,7 @@ impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> { } impl LayoutTest<'tcx> { - fn dump_layout_of(&self, item_def_id: DefId, item: &hir::Item<'tcx>, attr: &Attribute) { + fn dump_layout_of(&self, item_def_id: LocalDefId, item: &hir::Item<'tcx>, attr: &Attribute) { let tcx = self.tcx; let param_env = self.tcx.param_env(item_def_id); let ty = self.tcx.type_of(item_def_id); diff --git a/src/librustc_passes/liveness.rs b/src/librustc_passes/liveness.rs index 24f6d1a9c5894..25d53485bc54d 100644 --- a/src/librustc_passes/liveness.rs +++ b/src/librustc_passes/liveness.rs @@ -361,7 +361,7 @@ fn visit_fn<'tcx>( // swap in a new set of IR maps for this function body: let def_id = ir.tcx.hir().local_def_id(id); - let mut fn_maps = IrMaps::new(ir.tcx, def_id); + let mut fn_maps = IrMaps::new(ir.tcx, def_id.to_def_id()); // Don't run unused pass for #[derive()] if let FnKind::Method(..) = fk { @@ -398,7 +398,7 @@ fn visit_fn<'tcx>( intravisit::walk_fn(&mut fn_maps, fk, decl, body_id, sp, id); // compute liveness - let mut lsets = Liveness::new(&mut fn_maps, def_id); + let mut lsets = Liveness::new(&mut fn_maps, def_id.to_def_id()); let entry_ln = lsets.compute(&body.value); // check for various error conditions @@ -496,7 +496,7 @@ fn visit_expr<'tcx>(ir: &mut IrMaps<'tcx>, expr: &'tcx Expr<'tcx>) { } ir.set_captures(expr.hir_id, call_caps); let old_body_owner = ir.body_owner; - ir.body_owner = closure_def_id; + ir.body_owner = closure_def_id.to_def_id(); intravisit::walk_expr(ir, expr); ir.body_owner = old_body_owner; } diff --git a/src/librustc_plugin_impl/build.rs b/src/librustc_plugin_impl/build.rs index c7841595fe5a4..34522cfe97f35 100644 --- a/src/librustc_plugin_impl/build.rs +++ b/src/librustc_plugin_impl/build.rs @@ -42,7 +42,7 @@ fn plugin_registrar_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option { 0 => None, 1 => { let (hir_id, _) = finder.registrars.pop().unwrap(); - Some(tcx.hir().local_def_id(hir_id)) + Some(tcx.hir().local_def_id(hir_id).to_def_id()) } _ => { let diagnostic = tcx.sess.diagnostic(); diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 51e1588c71c42..e8d016aa07a8d 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -268,11 +268,11 @@ fn def_id_visibility<'tcx>( Node::Variant(..) => { let parent_did = tcx.hir().local_def_id(parent_hir_id); let (mut ctor_vis, mut span, mut descr) = - def_id_visibility(tcx, parent_did); + def_id_visibility(tcx, parent_did.to_def_id()); let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id()); let ctor_did = tcx.hir().local_def_id(vdata.ctor_hir_id().unwrap()); - let variant = adt_def.variant_with_ctor_id(ctor_did); + let variant = adt_def.variant_with_ctor_id(ctor_did.to_def_id()); if variant.is_field_list_non_exhaustive() && ctor_vis == ty::Visibility::Public @@ -514,7 +514,7 @@ impl EmbargoVisitor<'tcx> { ) -> ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { ReachEverythingInTheInterfaceVisitor { access_level: cmp::min(access_level, Some(AccessLevel::Reachable)), - item_def_id: self.tcx.hir().local_def_id(item_id), + item_def_id: self.tcx.hir().local_def_id(item_id).to_def_id(), ev: self, } } @@ -532,7 +532,7 @@ impl EmbargoVisitor<'tcx> { fn update_macro_reachable_mod(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) { let module_def_id = self.tcx.hir().local_def_id(reachable_mod); - let module = self.tcx.hir().get_module(module_def_id).0; + let module = self.tcx.hir().get_module(module_def_id.to_def_id()).0; for item_id in module.item_ids { let hir_id = item_id.id; let item_def_id = self.tcx.hir().local_def_id(hir_id); @@ -661,7 +661,7 @@ impl EmbargoVisitor<'tcx> { for item_id in m.item_ids { let item = self.tcx.hir().expect_item(item_id.id); let def_id = self.tcx.hir().local_def_id(item_id.id); - if !self.tcx.hygienic_eq(segment.ident, item.ident, def_id) { + if !self.tcx.hygienic_eq(segment.ident, item.ident, def_id.to_def_id()) { continue; } if let hir::ItemKind::Use(..) = item.kind { @@ -927,7 +927,8 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> { } let macro_module_def_id = - ty::DefIdTree::parent(self.tcx, self.tcx.hir().local_def_id(md.hir_id)).unwrap(); + ty::DefIdTree::parent(self.tcx, self.tcx.hir().local_def_id(md.hir_id).to_def_id()) + .unwrap(); // FIXME(#71104) Should really be using just `as_local_hir_id` but // some `DefId` do not seem to have a corresponding HirId. let hir_id = macro_module_def_id @@ -1370,8 +1371,10 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> { // Check types in item interfaces. fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { - let orig_current_item = - mem::replace(&mut self.current_item, self.tcx.hir().local_def_id(item.hir_id)); + let orig_current_item = mem::replace( + &mut self.current_item, + self.tcx.hir().local_def_id(item.hir_id).to_def_id(), + ); let orig_in_body = mem::replace(&mut self.in_body, false); let orig_tables = mem::replace(&mut self.tables, item_tables(self.tcx, item.hir_id, self.empty_tables)); @@ -1913,7 +1916,7 @@ impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> { SearchInterfaceForPrivateItemsVisitor { tcx: self.tcx, item_id, - item_def_id: self.tcx.hir().local_def_id(item_id), + item_def_id: self.tcx.hir().local_def_id(item_id).to_def_id(), span: self.tcx.hir().span(item_id), required_visibility, has_pub_restricted: self.has_pub_restricted, diff --git a/src/librustc_resolve/late/lifetimes.rs b/src/librustc_resolve/late/lifetimes.rs index 5bfb5aa2440b7..98a8ab9a7665a 100644 --- a/src/librustc_resolve/late/lifetimes.rs +++ b/src/librustc_resolve/late/lifetimes.rs @@ -62,7 +62,7 @@ impl RegionExt for Region { let def_id = hir_map.local_def_id(param.hir_id); let origin = LifetimeDefOrigin::from_param(param); debug!("Region::early: index={} def_id={:?}", i, def_id); - (param.name.normalize_to_macros_2_0(), Region::EarlyBound(i, def_id, origin)) + (param.name.normalize_to_macros_2_0(), Region::EarlyBound(i, def_id.to_def_id(), origin)) } fn late(hir_map: &Map<'_>, param: &GenericParam<'_>) -> (ParamName, Region) { @@ -73,7 +73,7 @@ impl RegionExt for Region { "Region::late: param={:?} depth={:?} def_id={:?} origin={:?}", param, depth, def_id, origin, ); - (param.name.normalize_to_macros_2_0(), Region::LateBound(depth, def_id, origin)) + (param.name.normalize_to_macros_2_0(), Region::LateBound(depth, def_id.to_def_id(), origin)) } fn late_anon(index: &Cell) -> Region { @@ -1278,7 +1278,7 @@ fn object_lifetime_defaults_for_item( _ => continue, }; - if res == Res::Def(DefKind::TyParam, param_def_id) { + if res == Res::Def(DefKind::TyParam, param_def_id.to_def_id()) { add_bounds(&mut set, &data.bounds); } } @@ -1304,7 +1304,11 @@ fn object_lifetime_defaults_for_item( .find(|&(_, (_, lt_name, _))| lt_name == name) .map_or(Set1::Many, |(i, (id, _, origin))| { let def_id = tcx.hir().local_def_id(id); - Set1::One(Region::EarlyBound(i as u32, def_id, origin)) + Set1::One(Region::EarlyBound( + i as u32, + def_id.to_def_id(), + origin, + )) }) } } @@ -1812,7 +1816,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { }) | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => { let scope = self.tcx.hir().local_def_id(fn_id); - def = Region::Free(scope, def.id().unwrap()); + def = Region::Free(scope.to_def_id(), def.id().unwrap()); } _ => {} } diff --git a/src/librustc_symbol_mangling/test.rs b/src/librustc_symbol_mangling/test.rs index a20915dd6fcac..5175b692e17b6 100644 --- a/src/librustc_symbol_mangling/test.rs +++ b/src/librustc_symbol_mangling/test.rs @@ -33,10 +33,10 @@ impl SymbolNamesTest<'tcx> { fn process_attrs(&mut self, hir_id: hir::HirId) { let tcx = self.tcx; let def_id = tcx.hir().local_def_id(hir_id); - for attr in tcx.get_attrs(def_id).iter() { + for attr in tcx.get_attrs(def_id.to_def_id()).iter() { if attr.check_name(SYMBOL_NAME) { // for now, can only use on monomorphic names - let instance = Instance::mono(tcx, def_id); + let instance = Instance::mono(tcx, def_id.to_def_id()); let mangled = self.tcx.symbol_name(instance); tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled)); if let Ok(demangling) = rustc_demangle::try_demangle(&mangled.name.as_str()) { @@ -44,7 +44,7 @@ impl SymbolNamesTest<'tcx> { tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling)); } } else if attr.check_name(DEF_PATH) { - let path = tcx.def_path_str(def_id); + let path = tcx.def_path_str(def_id.to_def_id()); tcx.sess.span_err(attr.span, &format!("def-path({})", path)); } diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index 0cb26e082280e..7c4e2155cb48f 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1040,7 +1040,8 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { let parent_def_id = self.parent_def_id; let def_scope_default = || { let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id); - parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id) + parent_def_id + == tcx.hir().local_def_id(opaque_parent_hir_id).to_def_id() }; let (in_definition_scope, origin) = match tcx.hir().find(opaque_hir_id) { Some(Node::Item(item)) => match item.kind { diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index 43ff39f92f757..a3bf297db80b8 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -1,6 +1,6 @@ use rustc_data_structures::svh::Svh; use rustc_hir as hir; -use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_middle::hir::map as hir_map; use rustc_middle::ty::subst::Subst; use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness}; @@ -78,7 +78,7 @@ fn sized_constraint_for_ty<'tcx>( fn associated_item_from_trait_item_ref( tcx: TyCtxt<'_>, - parent_def_id: DefId, + parent_def_id: LocalDefId, parent_vis: &hir::Visibility<'_>, trait_item_ref: &hir::TraitItemRef, ) -> ty::AssocItem { @@ -96,15 +96,15 @@ fn associated_item_from_trait_item_ref( // Visibility of trait items is inherited from their traits. vis: ty::Visibility::from_hir(parent_vis, trait_item_ref.id.hir_id, tcx), defaultness: trait_item_ref.defaultness, - def_id, - container: ty::TraitContainer(parent_def_id), + def_id: def_id.to_def_id(), + container: ty::TraitContainer(parent_def_id.to_def_id()), fn_has_self_parameter: has_self, } } fn associated_item_from_impl_item_ref( tcx: TyCtxt<'_>, - parent_def_id: DefId, + parent_def_id: LocalDefId, impl_item_ref: &hir::ImplItemRef<'_>, ) -> ty::AssocItem { let def_id = tcx.hir().local_def_id(impl_item_ref.id.hir_id); @@ -121,8 +121,8 @@ fn associated_item_from_impl_item_ref( // Visibility of trait impl items doesn't matter. vis: ty::Visibility::from_hir(&impl_item_ref.vis, impl_item_ref.id.hir_id, tcx), defaultness: impl_item_ref.defaultness, - def_id, - container: ty::ImplContainer(parent_def_id), + def_id: def_id.to_def_id(), + container: ty::ImplContainer(parent_def_id.to_def_id()), fn_has_self_parameter: has_self, } } @@ -207,13 +207,13 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { trait_item_refs .iter() .map(|trait_item_ref| trait_item_ref.id) - .map(|id| tcx.hir().local_def_id(id.hir_id)), + .map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()), ), hir::ItemKind::Impl { ref items, .. } => tcx.arena.alloc_from_iter( items .iter() .map(|impl_item_ref| impl_item_ref.id) - .map(|id| tcx.hir().local_def_id(id.hir_id)), + .map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()), ), hir::ItemKind::TraitAlias(..) => &[], _ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"), diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 87e5baa57e963..b8cd850717526 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -782,7 +782,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } } (GenericParamDefKind::Const, GenericArg::Const(ct)) => { - let ct_def_id = tcx.hir().local_def_id(ct.value.hir_id).expect_local(); + let ct_def_id = tcx.hir().local_def_id(ct.value.hir_id); ty::Const::from_anon_const(tcx, ct_def_id).into() } _ => unreachable!(), @@ -2754,7 +2754,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } hir::TyKind::Def(item_id, ref lifetimes) => { let did = tcx.hir().local_def_id(item_id.id); - self.impl_trait_ty_to_ty(did, lifetimes) + self.impl_trait_ty_to_ty(did.to_def_id(), lifetimes) } hir::TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => { debug!("ast_ty_to_ty: qself={:?} segment={:?}", qself, segment); @@ -2770,7 +2770,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .unwrap_or(tcx.types.err) } hir::TyKind::Array(ref ty, ref length) => { - let length_def_id = tcx.hir().local_def_id(length.hir_id).expect_local(); + let length_def_id = tcx.hir().local_def_id(length.hir_id); let length = ty::Const::from_anon_const(tcx, length_def_id); let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(&ty), length)); self.normalize_ty(ast_ty.span, array_ty) diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 2ccf7890c3042..fc4ca1e04b97b 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -71,49 +71,52 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let expr_def_id = self.tcx.hir().local_def_id(expr.hir_id); let ClosureSignatures { bound_sig, liberated_sig } = - self.sig_of_closure(expr_def_id, decl, body, expected_sig); + self.sig_of_closure(expr_def_id.to_def_id(), decl, body, expected_sig); debug!("check_closure: ty_of_closure returns {:?}", liberated_sig); let generator_types = check_fn(self, self.param_env, liberated_sig, decl, expr.hir_id, body, gen).1; - let base_substs = - InternalSubsts::identity_for_item(self.tcx, self.tcx.closure_base_def_id(expr_def_id)); + let base_substs = InternalSubsts::identity_for_item( + self.tcx, + self.tcx.closure_base_def_id(expr_def_id.to_def_id()), + ); // HACK(eddyb) this hardcodes indices into substs but it should rely on // `ClosureSubsts` and `GeneratorSubsts` providing constructors, instead. // That would also remove the need for most of the inference variables, // as they immediately unified with the actual type below, including // the `InferCtxt::closure_sig` and `ClosureSubsts::sig_ty` methods. let tupled_upvars_idx = base_substs.len() + if generator_types.is_some() { 4 } else { 2 }; - let substs = base_substs.extend_to(self.tcx, expr_def_id, |param, _| match param.kind { - GenericParamDefKind::Lifetime => span_bug!(expr.span, "closure has lifetime param"), - GenericParamDefKind::Type { .. } => if param.index as usize == tupled_upvars_idx { - self.tcx.mk_tup(self.tcx.upvars(expr_def_id).iter().flat_map(|upvars| { - upvars.iter().map(|(&var_hir_id, _)| { - // Create type variables (for now) to represent the transformed - // types of upvars. These will be unified during the upvar - // inference phase (`upvar.rs`). - self.infcx.next_ty_var(TypeVariableOrigin { - // FIXME(eddyb) distinguish upvar inference variables from the rest. - kind: TypeVariableOriginKind::ClosureSynthetic, - span: self.tcx.hir().span(var_hir_id), + let substs = + base_substs.extend_to(self.tcx, expr_def_id.to_def_id(), |param, _| match param.kind { + GenericParamDefKind::Lifetime => span_bug!(expr.span, "closure has lifetime param"), + GenericParamDefKind::Type { .. } => if param.index as usize == tupled_upvars_idx { + self.tcx.mk_tup(self.tcx.upvars(expr_def_id).iter().flat_map(|upvars| { + upvars.iter().map(|(&var_hir_id, _)| { + // Create type variables (for now) to represent the transformed + // types of upvars. These will be unified during the upvar + // inference phase (`upvar.rs`). + self.infcx.next_ty_var(TypeVariableOrigin { + // FIXME(eddyb) distinguish upvar inference variables from the rest. + kind: TypeVariableOriginKind::ClosureSynthetic, + span: self.tcx.hir().span(var_hir_id), + }) }) + })) + } else { + // Create type variables (for now) to represent the various + // pieces of information kept in `{Closure,Generic}Substs`. + // They will either be unified below, or later during the upvar + // inference phase (`upvar.rs`) + self.infcx.next_ty_var(TypeVariableOrigin { + kind: TypeVariableOriginKind::ClosureSynthetic, + span: expr.span, }) - })) - } else { - // Create type variables (for now) to represent the various - // pieces of information kept in `{Closure,Generic}Substs`. - // They will either be unified below, or later during the upvar - // inference phase (`upvar.rs`) - self.infcx.next_ty_var(TypeVariableOrigin { - kind: TypeVariableOriginKind::ClosureSynthetic, - span: expr.span, - }) - } - .into(), - GenericParamDefKind::Const => span_bug!(expr.span, "closure has const param"), - }); + } + .into(), + GenericParamDefKind::Const => span_bug!(expr.span, "closure has const param"), + }); if let Some(GeneratorTypes { resume_ty, yield_ty, interior, movability }) = generator_types { let generator_substs = substs.as_generator(); @@ -126,7 +129,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // it should rely on `GeneratorSubsts` providing a constructor, instead. let substs = self.resolve_vars_if_possible(&substs); - return self.tcx.mk_generator(expr_def_id, substs, movability); + return self.tcx.mk_generator(expr_def_id.to_def_id(), substs, movability); } // Tuple up the arguments and insert the resulting function type into @@ -157,7 +160,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // it should rely on `ClosureSubsts` providing a constructor, instead. let substs = self.resolve_vars_if_possible(&substs); - let closure_type = self.tcx.mk_closure(expr_def_id, substs); + let closure_type = self.tcx.mk_closure(expr_def_id.to_def_id(), substs); debug!("check_closure: expr.hir_id={:?} closure_type={:?}", expr.hir_id, closure_type); diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index c4f53332cb673..90a9f8a4d6f77 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -198,7 +198,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!("used_trait_import: {:?}", import_def_id); Lrc::get_mut(&mut self.tables.borrow_mut().used_trait_imports) .unwrap() - .insert(import_def_id); + .insert(import_def_id.to_def_id()); } self.tcx.check_stability(pick.item.def_id, Some(call_expr.hir_id), span); @@ -463,7 +463,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { for import_id in pick.import_ids { let import_def_id = tcx.hir().local_def_id(import_id); debug!("resolve_ufcs: used_trait_import: {:?}", import_def_id); - used_trait_imports.insert(import_def_id); + used_trait_imports.insert(import_def_id.to_def_id()); } } diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index edde9b1a1a12f..f8419e1025f89 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -1258,7 +1258,7 @@ fn compute_all_traits(tcx: TyCtxt<'_>) -> Vec { match i.kind { hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) => { let def_id = self.map.local_def_id(i.hir_id); - self.traits.push(def_id); + self.traits.push(def_id.to_def_id()); } _ => (), } diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index ca6bd21fefd39..4b64d116a8285 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1333,7 +1333,7 @@ fn check_fn<'a, 'tcx>( fcx.resume_yield_tys = Some((resume_ty, yield_ty)); } - let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id)); + let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id()); let outer_hir_id = hir.as_local_hir_id(outer_def_id).unwrap(); GatherLocalsVisitor { fcx: &fcx, parent_id: outer_hir_id }.visit_body(body); @@ -1453,7 +1453,7 @@ fn check_fn<'a, 'tcx>( // Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !` if let Some(panic_impl_did) = tcx.lang_items().panic_impl() { - if panic_impl_did == hir.local_def_id(fn_id) { + if panic_impl_did == hir.local_def_id(fn_id).to_def_id() { if let Some(panic_info_did) = tcx.lang_items().panic_info() { if declared_ret_ty.kind != ty::Never { sess.span_err(decl.output.span(), "return type should be `!`"); @@ -1497,7 +1497,7 @@ fn check_fn<'a, 'tcx>( // Check that a function marked as `#[alloc_error_handler]` has signature `fn(Layout) -> !` if let Some(alloc_error_handler_did) = tcx.lang_items().oom() { - if alloc_error_handler_did == hir.local_def_id(fn_id) { + if alloc_error_handler_did == hir.local_def_id(fn_id).to_def_id() { if let Some(alloc_layout_did) = tcx.lang_items().alloc_layout() { if declared_ret_ty.kind != ty::Never { sess.span_err(decl.output.span(), "return type should be `!`"); @@ -1549,8 +1549,8 @@ fn check_struct(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) { check_simd(tcx, span, def_id); } - check_transparent(tcx, span, def_id); - check_packed(tcx, span, def_id); + check_transparent(tcx, span, def); + check_packed(tcx, span, def); } fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) { @@ -1558,14 +1558,14 @@ fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) { let def = tcx.adt_def(def_id); def.destructor(tcx); // force the destructor to be evaluated check_representable(tcx, span, def_id); - check_transparent(tcx, span, def_id); + check_transparent(tcx, span, def); check_union_fields(tcx, span, def_id); - check_packed(tcx, span, def_id); + check_packed(tcx, span, def); } /// When the `#![feature(untagged_unions)]` gate is active, /// check that the fields of the `union` does not contain fields that need dropping. -fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: DefId) -> bool { +fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> bool { let item_type = tcx.type_of(item_def_id); if let ty::Adt(def, substs) = item_type.kind { assert!(def.is_union()); @@ -1597,7 +1597,7 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: DefId) -> bool { /// projections that would result in "inheriting lifetimes". fn check_opaque<'tcx>( tcx: TyCtxt<'tcx>, - def_id: DefId, + def_id: LocalDefId, substs: SubstsRef<'tcx>, span: Span, origin: &hir::OpaqueTyOrigin, @@ -1608,9 +1608,10 @@ fn check_opaque<'tcx>( /// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result /// in "inheriting lifetimes". -fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: DefId, span: Span) { - let item = - tcx.hir().expect_item(tcx.hir().as_local_hir_id(def_id).expect("opaque type is not local")); +fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: LocalDefId, span: Span) { + let item = tcx.hir().expect_item( + tcx.hir().as_local_hir_id(def_id.to_def_id()).expect("opaque type is not local"), + ); debug!( "check_opaque_for_inheriting_lifetimes: def_id={:?} span={:?} item={:?}", def_id, span, item @@ -1642,8 +1643,10 @@ fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: DefId, span: ItemKind::OpaqueTy(hir::OpaqueTy { origin: hir::OpaqueTyOrigin::AsyncFn, .. }) | ItemKind::OpaqueTy(hir::OpaqueTy { origin: hir::OpaqueTyOrigin::FnReturn, .. }) => { let mut visitor = ProhibitOpaqueVisitor { - opaque_identity_ty: tcx - .mk_opaque(def_id, InternalSubsts::identity_for_item(tcx, def_id)), + opaque_identity_ty: tcx.mk_opaque( + def_id.to_def_id(), + InternalSubsts::identity_for_item(tcx, def_id.to_def_id()), + ), generics: tcx.generics_of(def_id), }; debug!("check_opaque_for_inheriting_lifetimes: visitor={:?}", visitor); @@ -1680,12 +1683,13 @@ fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: DefId, span: /// Checks that an opaque type does not contain cycles. fn check_opaque_for_cycles<'tcx>( tcx: TyCtxt<'tcx>, - def_id: DefId, + def_id: LocalDefId, substs: SubstsRef<'tcx>, span: Span, origin: &hir::OpaqueTyOrigin, ) { - if let Err(partially_expanded_type) = tcx.try_expand_impl_trait_type(def_id, substs) { + if let Err(partially_expanded_type) = tcx.try_expand_impl_trait_type(def_id.to_def_id(), substs) + { if let hir::OpaqueTyOrigin::AsyncFn = origin { struct_span_err!(tcx.sess, span, E0733, "recursion in an `async fn` requires boxing",) .span_label(span, "recursive `async fn`") @@ -1717,7 +1721,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) { debug!( "check_item_type(it.hir_id={}, it.name={})", it.hir_id, - tcx.def_path_str(tcx.hir().local_def_id(it.hir_id)) + tcx.def_path_str(tcx.hir().local_def_id(it.hir_id).to_def_id()) ); let _indenter = indenter(); match it.kind { @@ -1745,7 +1749,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) { } hir::ItemKind::Trait(_, _, _, _, ref items) => { let def_id = tcx.hir().local_def_id(it.hir_id); - check_on_unimplemented(tcx, def_id, it); + check_on_unimplemented(tcx, def_id.to_def_id(), it); for item in items.iter() { let item = tcx.hir().trait_item(item.id); @@ -1764,7 +1768,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) { hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => { let def_id = tcx.hir().local_def_id(it.hir_id); - let substs = InternalSubsts::identity_for_item(tcx, def_id); + let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id()); check_opaque(tcx, def_id, substs, it.span, &origin); } hir::ItemKind::TyAlias(..) => { @@ -1827,7 +1831,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) { } } -fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: DefId, span: Span) { +fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId, span: Span) { // Only restricted on wasm32 target for now if !tcx.sess.opts.target_triple.triple().starts_with("wasm32") { return; @@ -1847,7 +1851,7 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: DefId, span: Span) // `#[link_section]` may contain arbitrary, or even undefined bytes, but it is // the consumer's responsibility to ensure all bytes that have been read // have defined values. - match tcx.const_eval_poly(id) { + match tcx.const_eval_poly(id.to_def_id()) { Ok(ConstValue::ByRef { alloc, .. }) => { if alloc.relocations().len() != 0 { let msg = "statics with a custom `#[link_section]` must be a \ @@ -1864,7 +1868,7 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: DefId, span: Span) fn check_on_unimplemented(tcx: TyCtxt<'_>, trait_def_id: DefId, item: &hir::Item<'_>) { let item_def_id = tcx.hir().local_def_id(item.hir_id); // an error would be reported if this fails. - let _ = traits::OnUnimplementedDirective::of_item(tcx, trait_def_id, item_def_id); + let _ = traits::OnUnimplementedDirective::of_item(tcx, trait_def_id, item_def_id.to_def_id()); } fn report_forbidden_specialization( @@ -1968,7 +1972,7 @@ fn check_specialization_validity<'tcx>( fn check_impl_items_against_trait<'tcx>( tcx: TyCtxt<'tcx>, full_impl_span: Span, - impl_id: DefId, + impl_id: LocalDefId, impl_trait_ref: ty::TraitRef<'tcx>, impl_item_refs: &[hir::ImplItemRef<'_>], ) { @@ -2109,13 +2113,19 @@ fn check_impl_items_against_trait<'tcx>( } } - check_specialization_validity(tcx, trait_def, &ty_trait_item, impl_id, impl_item); + check_specialization_validity( + tcx, + trait_def, + &ty_trait_item, + impl_id.to_def_id(), + impl_item, + ); } } // Check for missing items from trait let mut missing_items = Vec::new(); - if let Ok(ancestors) = trait_def.ancestors(tcx, impl_id) { + if let Ok(ancestors) = trait_def.ancestors(tcx, impl_id.to_def_id()) { for trait_item in tcx.associated_items(impl_trait_ref.def_id).in_definition_order() { let is_implemented = ancestors .leaf_def(tcx, trait_item.ident, trait_item.kind) @@ -2325,7 +2335,7 @@ fn suggestion_signature(assoc: &ty::AssocItem, tcx: TyCtxt<'_>) -> String { /// Checks whether a type can be represented in memory. In particular, it /// identifies types that contain themselves without indirection through a /// pointer, which would mean their size is unbounded. -fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: DefId) -> bool { +fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: LocalDefId) -> bool { let rty = tcx.type_of(item_def_id); // Check that it is possible to represent this type. This call identifies @@ -2335,7 +2345,7 @@ fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: DefId) -> bool { // caught by case 1. match rty.is_representable(tcx, sp) { Representability::SelfRecursive(spans) => { - let mut err = recursive_type_with_infinite_size_error(tcx, item_def_id); + let mut err = recursive_type_with_infinite_size_error(tcx, item_def_id.to_def_id()); for span in spans { err.span_label(span, "recursive without indirection"); } @@ -2347,7 +2357,7 @@ fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: DefId) -> bool { true } -pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { +pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) { let t = tcx.type_of(def_id); if let ty::Adt(def, substs) = t.kind { if def.is_struct() { @@ -2381,10 +2391,10 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { } } -fn check_packed(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { - let repr = tcx.adt_def(def_id).repr; +fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: &ty::AdtDef) { + let repr = def.repr; if repr.packed() { - for attr in tcx.get_attrs(def_id).iter() { + for attr in tcx.get_attrs(def.did).iter() { for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) { if let attr::ReprPacked(pack) = r { if let Some(repr_pack) = repr.pack { @@ -2410,7 +2420,7 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { ) .emit(); } else { - if let Some(def_spans) = check_packed_inner(tcx, def_id, &mut vec![]) { + if let Some(def_spans) = check_packed_inner(tcx, def.did, &mut vec![]) { let mut err = struct_span_err!( tcx.sess, sp, @@ -2438,7 +2448,7 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { &if first { format!( "`{}` contains a field of type `{}`", - tcx.type_of(def_id), + tcx.type_of(def.did), ident ) } else { @@ -2531,8 +2541,7 @@ fn bad_non_zero_sized_fields<'tcx>( err.emit(); } -fn check_transparent(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { - let adt = tcx.adt_def(def_id); +fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: &'tcx ty::AdtDef) { if !adt.repr.transparent() { return; } @@ -2549,7 +2558,7 @@ fn check_transparent(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { } if adt.variants.len() != 1 { - bad_variant_count(tcx, adt, sp, def_id); + bad_variant_count(tcx, adt, sp, adt.did); if adt.variants.is_empty() { // Don't bother checking the fields. No variants (and thus no fields) exist. return; @@ -2601,7 +2610,7 @@ pub fn check_enum<'tcx>( def.destructor(tcx); // force the destructor to be evaluated if vs.is_empty() { - let attributes = tcx.get_attrs(def_id); + let attributes = tcx.get_attrs(def_id.to_def_id()); if let Some(attr) = attr::find_by_name(&attributes, sym::repr) { struct_span_err!( tcx.sess, @@ -2681,7 +2690,7 @@ pub fn check_enum<'tcx>( } check_representable(tcx, sp, def_id); - check_transparent(tcx, sp, def_id); + check_transparent(tcx, sp, def); } fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) { @@ -3210,7 +3219,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let (value, opaque_type_map) = self.register_infer_ok_obligations(self.instantiate_opaque_types( - parent_def_id, + parent_def_id.to_def_id(), self.body_id, self.param_env, value, @@ -3315,7 +3324,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } pub fn to_const(&self, ast_c: &hir::AnonConst) -> &'tcx ty::Const<'tcx> { - let const_def_id = self.tcx.hir().local_def_id(ast_c.hir_id).expect_local(); + let const_def_id = self.tcx.hir().local_def_id(ast_c.hir_id); let c = ty::Const::from_anon_const(self.tcx, const_def_id); // HACK(eddyb) emulate what a `WellFormedConst` obligation would do. diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index f7564623946d8..c3614380ad9dc 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -135,7 +135,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self, RepeatingScope(item_id), item_id, - Subject(subject), + Subject(subject.to_def_id()), self.param_env, ); rcx.outlives_environment.add_implied_bounds(self, wf_tys, item_id, span); diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs index 2c9e23d8095ca..14de85b91f172 100644 --- a/src/librustc_typeck/check/upvar.rs +++ b/src/librustc_typeck/check/upvar.rs @@ -226,7 +226,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let upvar_ty = self.node_ty(var_hir_id); let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_hir_id }, - closure_expr_id: closure_def_id.expect_local(), + closure_expr_id: closure_def_id, }; let capture = self.tables.borrow().upvar_capture(upvar_id); diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 32004744ff950..574e5a4f8f1cc 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -5,7 +5,7 @@ use rustc_ast::ast; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder}; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::itemlikevisit::ParItemLikeVisitor; use rustc_hir::lang_items; use rustc_hir::ItemKind; @@ -195,10 +195,10 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: DefId) { check_associated_item(tcx, trait_item.hir_id, trait_item.span, method_sig); } -fn could_be_self(trait_def_id: DefId, ty: &hir::Ty<'_>) -> bool { +fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool { match ty.kind { hir::TyKind::TraitObject([trait_ref], ..) => match trait_ref.trait_ref.path.segments { - [s] => s.res.and_then(|r| r.opt_def_id()) == Some(trait_def_id), + [s] => s.res.and_then(|r| r.opt_def_id()) == Some(trait_def_id.to_def_id()), _ => false, }, _ => false, @@ -330,7 +330,7 @@ fn for_item<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>) -> CheckWfFcxBuilder< } fn for_id(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) -> CheckWfFcxBuilder<'_> { - let def_id = tcx.hir().local_def_id(id).expect_local(); + let def_id = tcx.hir().local_def_id(id); CheckWfFcxBuilder { inherited: Inherited::build(tcx, def_id), id, @@ -413,7 +413,7 @@ fn check_type_defn<'tcx, F>( } } - check_where_clauses(tcx, fcx, item.span, def_id, None); + check_where_clauses(tcx, fcx, item.span, def_id.to_def_id(), None); // No implied bounds in a struct definition. vec![] @@ -441,8 +441,8 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) { } for_item(tcx, item).with_fcx(|fcx, _| { - check_where_clauses(tcx, fcx, item.span, trait_def_id, None); - check_associated_type_defaults(fcx, trait_def_id); + check_where_clauses(tcx, fcx, item.span, trait_def_id.to_def_id(), None); + check_associated_type_defaults(fcx, trait_def_id.to_def_id()); vec![] }); @@ -555,7 +555,15 @@ fn check_item_fn(tcx: TyCtxt<'_>, item: &hir::Item<'_>) { ItemKind::Fn(sig, ..) => sig, _ => bug!("expected `ItemKind::Fn`, found `{:?}`", item.kind), }; - check_fn_or_method(tcx, fcx, item.ident.span, sig, hir_sig, def_id, &mut implied_bounds); + check_fn_or_method( + tcx, + fcx, + item.ident.span, + sig, + hir_sig, + def_id.to_def_id(), + &mut implied_bounds, + ); implied_bounds }) } @@ -631,9 +639,9 @@ fn check_impl<'tcx>( } } - check_where_clauses(tcx, fcx, item.span, item_def_id, None); + check_where_clauses(tcx, fcx, item.span, item_def_id.to_def_id(), None); - fcx.impl_implied_bounds(item_def_id, item.span) + fcx.impl_implied_bounds(item_def_id.to_def_id(), item.span) }); } diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index f9100300e3add..27434b00a98be 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -41,7 +41,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // This attribute causes us to dump some writeback information // in the form of errors, which is uSymbol for unit tests. - let rustc_dump_user_substs = self.tcx.has_attr(item_def_id, sym::rustc_dump_user_substs); + let rustc_dump_user_substs = + self.tcx.has_attr(item_def_id.to_def_id(), sym::rustc_dump_user_substs); let mut wbcx = WritebackCx::new(self, body, rustc_dump_user_substs); for param in body.params { diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index cc99ae2019939..bb2e077ceb03c 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -50,7 +50,7 @@ impl CheckVisitor<'tcx> { return; } - if self.used_trait_imports.contains(&def_id) { + if self.used_trait_imports.contains(&def_id.to_def_id()) { return; } @@ -216,7 +216,7 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> { if let hir::ItemKind::ExternCrate(orig_name) = item.kind { let extern_crate_def_id = self.tcx.hir().local_def_id(item.hir_id); self.crates_to_lint.push(ExternCrateToLint { - def_id: extern_crate_def_id, + def_id: extern_crate_def_id.to_def_id(), span: item.span, orig_name, warn_if_unused: !item.ident.as_str().starts_with('_'), diff --git a/src/librustc_typeck/coherence/builtin.rs b/src/librustc_typeck/coherence/builtin.rs index 384a22d010e56..7cfb7fa712cca 100644 --- a/src/librustc_typeck/coherence/builtin.rs +++ b/src/librustc_typeck/coherence/builtin.rs @@ -3,7 +3,7 @@ use rustc_errors::struct_span_err; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::lang_items::UnsizeTraitLangItem; use rustc_hir::ItemKind; use rustc_infer::infer; @@ -35,7 +35,7 @@ struct Checker<'tcx> { impl<'tcx> Checker<'tcx> { fn check(&self, trait_def_id: Option, mut f: F) -> &Self where - F: FnMut(TyCtxt<'tcx>, DefId), + F: FnMut(TyCtxt<'tcx>, LocalDefId), { if Some(self.trait_def_id) == trait_def_id { for &impl_id in self.tcx.hir().trait_impls(self.trait_def_id) { @@ -47,13 +47,14 @@ impl<'tcx> Checker<'tcx> { } } -fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: DefId) { +fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { // Destructors only work on nominal types. if let ty::Adt(..) | ty::Error = tcx.type_of(impl_did).kind { return; } - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).expect("foreign Drop impl on non-ADT"); + let impl_hir_id = + tcx.hir().as_local_hir_id(impl_did.to_def_id()).expect("foreign Drop impl on non-ADT"); let sp = match tcx.hir().expect_item(impl_hir_id).kind { ItemKind::Impl { self_ty, .. } => self_ty.span, _ => bug!("expected Drop impl item"), @@ -69,10 +70,10 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: DefId) { .emit(); } -fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: DefId) { +fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_copy: impl_did={:?}", impl_did); - let impl_hir_id = if let Some(n) = tcx.hir().as_local_hir_id(impl_did) { + let impl_hir_id = if let Some(n) = tcx.hir().as_local_hir_id(impl_did.to_def_id()) { n } else { debug!("visit_implementation_of_copy(): impl not in this crate"); @@ -137,187 +138,184 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: DefId) { } } -fn visit_implementation_of_coerce_unsized(tcx: TyCtxt<'tcx>, impl_did: DefId) { +fn visit_implementation_of_coerce_unsized(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) { debug!("visit_implementation_of_coerce_unsized: impl_did={:?}", impl_did); // Just compute this for the side-effects, in particular reporting // errors; other parts of the code may demand it for the info of // course. - if impl_did.is_local() { - let span = tcx.def_span(impl_did); - tcx.at(span).coerce_unsized_info(impl_did); - } + let span = tcx.def_span(impl_did); + tcx.at(span).coerce_unsized_info(impl_did); } -fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: DefId) { +fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did); - if impl_did.is_local() { - let dispatch_from_dyn_trait = tcx.lang_items().dispatch_from_dyn_trait().unwrap(); - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).unwrap(); - let span = tcx.hir().span(impl_hir_id); + let dispatch_from_dyn_trait = tcx.lang_items().dispatch_from_dyn_trait().unwrap(); - let source = tcx.type_of(impl_did); - assert!(!source.has_escaping_bound_vars()); - let target = { - let trait_ref = tcx.impl_trait_ref(impl_did).unwrap(); - assert_eq!(trait_ref.def_id, dispatch_from_dyn_trait); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.to_def_id()).unwrap(); + let span = tcx.hir().span(impl_hir_id); - trait_ref.substs.type_at(1) - }; + let source = tcx.type_of(impl_did); + assert!(!source.has_escaping_bound_vars()); + let target = { + let trait_ref = tcx.impl_trait_ref(impl_did).unwrap(); + assert_eq!(trait_ref.def_id, dispatch_from_dyn_trait); - debug!("visit_implementation_of_dispatch_from_dyn: {:?} -> {:?}", source, target); + trait_ref.substs.type_at(1) + }; + + debug!("visit_implementation_of_dispatch_from_dyn: {:?} -> {:?}", source, target); - let param_env = tcx.param_env(impl_did); + let param_env = tcx.param_env(impl_did); - let create_err = |msg: &str| struct_span_err!(tcx.sess, span, E0378, "{}", msg); + let create_err = |msg: &str| struct_span_err!(tcx.sess, span, E0378, "{}", msg); - tcx.infer_ctxt().enter(|infcx| { - let cause = ObligationCause::misc(span, impl_hir_id); + tcx.infer_ctxt().enter(|infcx| { + let cause = ObligationCause::misc(span, impl_hir_id); - use ty::TyKind::*; - match (&source.kind, &target.kind) { - (&Ref(r_a, _, mutbl_a), Ref(r_b, _, mutbl_b)) - if infcx.at(&cause, param_env).eq(r_a, r_b).is_ok() && mutbl_a == *mutbl_b => {} - (&RawPtr(tm_a), &RawPtr(tm_b)) if tm_a.mutbl == tm_b.mutbl => (), - (&Adt(def_a, substs_a), &Adt(def_b, substs_b)) - if def_a.is_struct() && def_b.is_struct() => - { - if def_a != def_b { - let source_path = tcx.def_path_str(def_a.did); - let target_path = tcx.def_path_str(def_b.did); + use ty::TyKind::*; + match (&source.kind, &target.kind) { + (&Ref(r_a, _, mutbl_a), Ref(r_b, _, mutbl_b)) + if infcx.at(&cause, param_env).eq(r_a, r_b).is_ok() && mutbl_a == *mutbl_b => {} + (&RawPtr(tm_a), &RawPtr(tm_b)) if tm_a.mutbl == tm_b.mutbl => (), + (&Adt(def_a, substs_a), &Adt(def_b, substs_b)) + if def_a.is_struct() && def_b.is_struct() => + { + if def_a != def_b { + let source_path = tcx.def_path_str(def_a.did); + let target_path = tcx.def_path_str(def_b.did); - create_err(&format!( - "the trait `DispatchFromDyn` may only be implemented \ + create_err(&format!( + "the trait `DispatchFromDyn` may only be implemented \ for a coercion between structures with the same \ definition; expected `{}`, found `{}`", - source_path, target_path, - )) - .emit(); + source_path, target_path, + )) + .emit(); - return; - } + return; + } - if def_a.repr.c() || def_a.repr.packed() { - create_err( - "structs implementing `DispatchFromDyn` may not have \ + if def_a.repr.c() || def_a.repr.packed() { + create_err( + "structs implementing `DispatchFromDyn` may not have \ `#[repr(packed)]` or `#[repr(C)]`", - ) - .emit(); - } + ) + .emit(); + } - let fields = &def_a.non_enum_variant().fields; + let fields = &def_a.non_enum_variant().fields; - let coerced_fields = fields - .iter() - .filter_map(|field| { - let ty_a = field.ty(tcx, substs_a); - let ty_b = field.ty(tcx, substs_b); + let coerced_fields = fields + .iter() + .filter_map(|field| { + let ty_a = field.ty(tcx, substs_a); + let ty_b = field.ty(tcx, substs_b); - if let Ok(layout) = tcx.layout_of(param_env.and(ty_a)) { - if layout.is_zst() && layout.align.abi.bytes() == 1 { - // ignore ZST fields with alignment of 1 byte - return None; - } + if let Ok(layout) = tcx.layout_of(param_env.and(ty_a)) { + if layout.is_zst() && layout.align.abi.bytes() == 1 { + // ignore ZST fields with alignment of 1 byte + return None; } + } - if let Ok(ok) = infcx.at(&cause, param_env).eq(ty_a, ty_b) { - if ok.obligations.is_empty() { - create_err( - "the trait `DispatchFromDyn` may only be implemented \ + if let Ok(ok) = infcx.at(&cause, param_env).eq(ty_a, ty_b) { + if ok.obligations.is_empty() { + create_err( + "the trait `DispatchFromDyn` may only be implemented \ for structs containing the field being coerced, \ ZST fields with 1 byte alignment, and nothing else", - ) - .note(&format!( - "extra field `{}` of type `{}` is not allowed", - field.ident, ty_a, - )) - .emit(); - - return None; - } + ) + .note(&format!( + "extra field `{}` of type `{}` is not allowed", + field.ident, ty_a, + )) + .emit(); + + return None; } + } - Some(field) - }) - .collect::>(); + Some(field) + }) + .collect::>(); - if coerced_fields.is_empty() { - create_err( - "the trait `DispatchFromDyn` may only be implemented \ + if coerced_fields.is_empty() { + create_err( + "the trait `DispatchFromDyn` may only be implemented \ for a coercion between structures with a single field \ being coerced, none found", - ) - .emit(); - } else if coerced_fields.len() > 1 { - create_err( - "implementing the `DispatchFromDyn` trait requires multiple coercions", - ) - .note( - "the trait `DispatchFromDyn` may only be implemented \ + ) + .emit(); + } else if coerced_fields.len() > 1 { + create_err( + "implementing the `DispatchFromDyn` trait requires multiple coercions", + ) + .note( + "the trait `DispatchFromDyn` may only be implemented \ for a coercion between structures with a single field \ being coerced", - ) - .note(&format!( - "currently, {} fields need coercions: {}", - coerced_fields.len(), - coerced_fields - .iter() - .map(|field| { - format!( - "`{}` (`{}` to `{}`)", - field.ident, - field.ty(tcx, substs_a), - field.ty(tcx, substs_b), - ) - }) - .collect::>() - .join(", ") - )) - .emit(); - } else { - let mut fulfill_cx = TraitEngine::new(infcx.tcx); - - for field in coerced_fields { - let predicate = predicate_for_trait_def( - tcx, - param_env, - cause.clone(), - dispatch_from_dyn_trait, - 0, - field.ty(tcx, substs_a), - &[field.ty(tcx, substs_b).into()], - ); - - fulfill_cx.register_predicate_obligation(&infcx, predicate); - } + ) + .note(&format!( + "currently, {} fields need coercions: {}", + coerced_fields.len(), + coerced_fields + .iter() + .map(|field| { + format!( + "`{}` (`{}` to `{}`)", + field.ident, + field.ty(tcx, substs_a), + field.ty(tcx, substs_b), + ) + }) + .collect::>() + .join(", ") + )) + .emit(); + } else { + let mut fulfill_cx = TraitEngine::new(infcx.tcx); + + for field in coerced_fields { + let predicate = predicate_for_trait_def( + tcx, + param_env, + cause.clone(), + dispatch_from_dyn_trait, + 0, + field.ty(tcx, substs_a), + &[field.ty(tcx, substs_b).into()], + ); - // Check that all transitive obligations are satisfied. - if let Err(errors) = fulfill_cx.select_all_or_error(&infcx) { - infcx.report_fulfillment_errors(&errors, None, false); - } + fulfill_cx.register_predicate_obligation(&infcx, predicate); + } - // Finally, resolve all regions. - let region_scope_tree = region::ScopeTree::default(); - let outlives_env = OutlivesEnvironment::new(param_env); - infcx.resolve_regions_and_report_errors( - impl_did, - ®ion_scope_tree, - &outlives_env, - RegionckMode::default(), - ); + // Check that all transitive obligations are satisfied. + if let Err(errors) = fulfill_cx.select_all_or_error(&infcx) { + infcx.report_fulfillment_errors(&errors, None, false); } + + // Finally, resolve all regions. + let region_scope_tree = region::ScopeTree::default(); + let outlives_env = OutlivesEnvironment::new(param_env); + infcx.resolve_regions_and_report_errors( + impl_did.to_def_id(), + ®ion_scope_tree, + &outlives_env, + RegionckMode::default(), + ); } - _ => { - create_err( - "the trait `DispatchFromDyn` may only be implemented \ + } + _ => { + create_err( + "the trait `DispatchFromDyn` may only be implemented \ for a coercion between structures", - ) - .emit(); - } + ) + .emit(); } - }) - } + } + }) } pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedInfo { diff --git a/src/librustc_typeck/coherence/inherent_impls.rs b/src/librustc_typeck/coherence/inherent_impls.rs index 2e84173477074..6817312e303ae 100644 --- a/src/librustc_typeck/coherence/inherent_impls.rs +++ b/src/librustc_typeck/coherence/inherent_impls.rs @@ -9,7 +9,7 @@ use rustc_errors::struct_span_err; use rustc_hir as hir; -use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_middle::ty::{self, CrateInherentImpls, TyCtxt}; @@ -327,7 +327,7 @@ impl InherentCollect<'tcx> { // the implementation does not have any associated traits. let impl_def_id = self.tcx.hir().local_def_id(item.hir_id); let vec = self.impls_map.inherent_impls.entry(def_id).or_default(); - vec.push(impl_def_id); + vec.push(impl_def_id.to_def_id()); } else { struct_span_err!( self.tcx.sess, @@ -344,7 +344,7 @@ impl InherentCollect<'tcx> { fn check_primitive_impl( &self, - impl_def_id: DefId, + impl_def_id: LocalDefId, lang_def_id: Option, lang_def_id2: Option, lang: &str, @@ -352,10 +352,10 @@ impl InherentCollect<'tcx> { span: Span, ) { match (lang_def_id, lang_def_id2) { - (Some(lang_def_id), _) if lang_def_id == impl_def_id => { + (Some(lang_def_id), _) if lang_def_id == impl_def_id.to_def_id() => { // OK } - (_, Some(lang_def_id)) if lang_def_id == impl_def_id => { + (_, Some(lang_def_id)) if lang_def_id == impl_def_id.to_def_id() => { // OK } _ => { diff --git a/src/librustc_typeck/coherence/mod.rs b/src/librustc_typeck/coherence/mod.rs index 0df08b7bc8b37..d8ffb9f6b4c07 100644 --- a/src/librustc_typeck/coherence/mod.rs +++ b/src/librustc_typeck/coherence/mod.rs @@ -6,7 +6,7 @@ // mappings. That mapping code resides here. use rustc_errors::struct_span_err; -use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, TyCtxt, TypeFoldable}; use rustc_span::Span; @@ -19,15 +19,15 @@ mod orphan; mod unsafety; /// Obtains the span of just the impl header of `impl_def_id`. -fn impl_header_span(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Span { - tcx.sess.source_map().guess_head_span(tcx.span_of_impl(impl_def_id).unwrap()) +fn impl_header_span(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) -> Span { + tcx.sess.source_map().guess_head_span(tcx.span_of_impl(impl_def_id.to_def_id()).unwrap()) } -fn check_impl(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_ref: ty::TraitRef<'_>) { +fn check_impl(tcx: TyCtxt<'_>, impl_def_id: LocalDefId, trait_ref: ty::TraitRef<'_>) { debug!( "(checking implementation) adding impl for trait '{:?}', item '{}'", trait_ref, - tcx.def_path_str(impl_def_id) + tcx.def_path_str(impl_def_id.to_def_id()) ); // Skip impls where one of the self type is an error type. @@ -40,7 +40,11 @@ fn check_impl(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_ref: ty::TraitRef<'_>) enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id); } -fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_def_id: DefId) { +fn enforce_trait_manually_implementable( + tcx: TyCtxt<'_>, + impl_def_id: LocalDefId, + trait_def_id: DefId, +) { let did = Some(trait_def_id); let li = tcx.lang_items(); @@ -117,7 +121,11 @@ fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, tra /// We allow impls of marker traits to overlap, so they can't override impls /// as that could make it ambiguous which associated item to use. -fn enforce_empty_impls_for_marker_traits(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_def_id: DefId) { +fn enforce_empty_impls_for_marker_traits( + tcx: TyCtxt<'_>, + impl_def_id: LocalDefId, + trait_def_id: DefId, +) { if !tcx.trait_def(trait_def_id).is_marker { return; } @@ -177,7 +185,7 @@ pub fn check_coherence(tcx: TyCtxt<'_>) { /// Checks whether an impl overlaps with the automatic `impl Trait for dyn Trait`. fn check_object_overlap<'tcx>( tcx: TyCtxt<'tcx>, - impl_def_id: DefId, + impl_def_id: LocalDefId, trait_ref: ty::TraitRef<'tcx>, ) { let trait_def_id = trait_ref.def_id; diff --git a/src/librustc_typeck/coherence/orphan.rs b/src/librustc_typeck/coherence/orphan.rs index d77f2bd0d8b96..71469770f2a33 100644 --- a/src/librustc_typeck/coherence/orphan.rs +++ b/src/librustc_typeck/coherence/orphan.rs @@ -35,7 +35,7 @@ impl ItemLikeVisitor<'v> for OrphanChecker<'tcx> { let trait_def_id = trait_ref.def_id; let sm = self.tcx.sess.source_map(); let sp = sm.guess_head_span(item.span); - match traits::orphan_check(self.tcx, def_id) { + match traits::orphan_check(self.tcx, def_id.to_def_id()) { Ok(()) => {} Err(traits::OrphanCheckErr::NonLocalInputType(tys)) => { let mut err = struct_span_err!( diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index a327951b3b0dd..ffed17b6b30ce 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -26,7 +26,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::{struct_span_err, Applicability}; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::weak_lang_items; use rustc_hir::{GenericParamKind, Node, Unsafety}; @@ -495,8 +495,11 @@ fn type_param_predicates( let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(param_id)); // Don't look for bounds where the type parameter isn't in scope. - let parent = - if item_def_id == param_owner_def_id { None } else { tcx.generics_of(item_def_id).parent }; + let parent = if item_def_id == param_owner_def_id.to_def_id() { + None + } else { + tcx.generics_of(item_def_id).parent + }; let mut result = parent .map(|parent| { @@ -611,7 +614,7 @@ fn is_param(tcx: TyCtxt<'_>, ast_ty: &hir::Ty<'_>, param_id: hir::HirId) -> bool if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = ast_ty.kind { match path.res { Res::SelfTy(Some(def_id), None) | Res::Def(DefKind::TyParam, def_id) => { - def_id == tcx.hir().local_def_id(param_id) + def_id == tcx.hir().local_def_id(param_id).to_def_id() } _ => false, } @@ -645,7 +648,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::HirId) { tcx.generics_of(def_id); tcx.type_of(def_id); tcx.predicates_of(def_id); - convert_enum_variant_types(tcx, def_id, &enum_definition.variants); + convert_enum_variant_types(tcx, def_id.to_def_id(), &enum_definition.variants); } hir::ItemKind::Impl { .. } => { tcx.generics_of(def_id); @@ -767,7 +770,7 @@ fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId, variants: &[hir::V prev_discr = Some( if let Some(ref e) = variant.disr_expr { let expr_did = tcx.hir().local_def_id(e.hir_id); - def.eval_explicit_discr(tcx, expr_did) + def.eval_explicit_discr(tcx, expr_did.to_def_id()) } else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) { Some(discr) } else { @@ -803,8 +806,8 @@ fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId, variants: &[hir::V fn convert_variant( tcx: TyCtxt<'_>, - variant_did: Option, - ctor_did: Option, + variant_did: Option, + ctor_did: Option, ident: Ident, discr: ty::VariantDiscr, def: &hir::VariantData<'_>, @@ -812,7 +815,10 @@ fn convert_variant( parent_did: DefId, ) -> ty::VariantDef { let mut seen_fields: FxHashMap = Default::default(); - let hir_id = tcx.hir().as_local_hir_id(variant_did.unwrap_or(parent_did)).unwrap(); + let hir_id = tcx + .hir() + .as_local_hir_id(variant_did.map(LocalDefId::to_def_id).unwrap_or(parent_did)) + .unwrap(); let fields = def .fields() .iter() @@ -835,7 +841,7 @@ fn convert_variant( } ty::FieldDef { - did: fid, + did: fid.to_def_id(), ident: f.ident, vis: ty::Visibility::from_hir(&f.vis, hir_id, tcx), } @@ -848,8 +854,8 @@ fn convert_variant( ty::VariantDef::new( tcx, ident, - variant_did, - ctor_did, + variant_did.map(LocalDefId::to_def_id), + ctor_did.map(LocalDefId::to_def_id), discr, fields, CtorKind::from_hir(def), @@ -882,7 +888,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { let discr = if let Some(ref e) = v.disr_expr { distance_from_explicit = 0; - ty::VariantDiscr::Explicit(tcx.hir().local_def_id(e.hir_id)) + ty::VariantDiscr::Explicit(tcx.hir().local_def_id(e.hir_id).to_def_id()) } else { ty::VariantDiscr::Relative(distance_from_explicit) }; @@ -904,7 +910,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { (AdtKind::Enum, variants) } ItemKind::Struct(ref def, _) => { - let variant_did = None; + let variant_did = None::; let ctor_did = def.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id)); let variants = std::iter::once(convert_variant( @@ -1166,7 +1172,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { | Node::Ctor(..) | Node::Field(_) => { let parent_id = tcx.hir().get_parent_item(hir_id); - Some(tcx.hir().local_def_id(parent_id)) + Some(tcx.hir().local_def_id(parent_id).to_def_id()) } // FIXME(#43408) enable this always when we get lazy normalization. Node::AnonConst(_) => { @@ -1177,7 +1183,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { // `feature(const_generics)` is enabled, so that const expressions // used with const generics, e.g. `Foo<{N+1}>`, can work at all. if tcx.features().const_generics { - Some(parent_def_id) + Some(parent_def_id.to_def_id()) } else { let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id)); match parent_node { @@ -1187,7 +1193,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. }) if constant.hir_id == hir_id => { - Some(parent_def_id) + Some(parent_def_id.to_def_id()) } _ => None, @@ -1210,7 +1216,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { if let Node::Item(hir::Item { kind: ItemKind::OpaqueTy(..), .. }) = tcx.hir().get(parent_id) { - Some(tcx.hir().local_def_id(parent_id)) + Some(tcx.hir().local_def_id(parent_id).to_def_id()) } else { None } @@ -1257,7 +1263,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { opt_self = Some(ty::GenericParamDef { index: 0, name: kw::SelfUpper, - def_id: tcx.hir().local_def_id(param_id), + def_id: tcx.hir().local_def_id(param_id).to_def_id(), pure_wrt_drop: false, kind: ty::GenericParamDefKind::Type { has_default: false, @@ -1300,7 +1306,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef { name: param.name.ident().name, index: own_start + i as u32, - def_id: tcx.hir().local_def_id(param.hir_id), + def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(), pure_wrt_drop: param.pure_wrt_drop, kind: ty::GenericParamDefKind::Lifetime, })); @@ -1346,7 +1352,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { let param_def = ty::GenericParamDef { index: type_start + i as u32, name: param.name.ident().name, - def_id: tcx.hir().local_def_id(param.hir_id), + def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(), pure_wrt_drop: param.pure_wrt_drop, kind, }; @@ -1362,7 +1368,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics { let param_def = ty::GenericParamDef { index: type_start + i as u32, name: param.name.ident().name, - def_id: tcx.hir().local_def_id(param.hir_id), + def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(), pure_wrt_drop: param.pure_wrt_drop, kind: ty::GenericParamDefKind::Const, }; @@ -1841,7 +1847,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat let mut index = parent_count + has_own_self as u32; for param in early_bound_lifetimes_from_generics(tcx, ast_generics) { let region = tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion { - def_id: tcx.hir().local_def_id(param.hir_id), + def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(), index, name: param.name.ident().name, })); @@ -2059,12 +2065,13 @@ fn associated_item_predicates( // the `Binder` around the the predicate. // // FIXME(generic_associated_types): Currently only lifetimes are handled. - self_trait_ref.substs.extend_to(tcx, item_def_id, mk_bound_param) + self_trait_ref.substs.extend_to(tcx, item_def_id.to_def_id(), mk_bound_param) } else { self_trait_ref.substs }; - let assoc_ty = tcx.mk_projection(tcx.hir().local_def_id(trait_item.hir_id), bound_substs); + let assoc_ty = + tcx.mk_projection(tcx.hir().local_def_id(trait_item.hir_id).to_def_id(), bound_substs); let bounds = AstConv::compute_bounds( &ItemCtxt::new(tcx, def_id), diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index 985f66694b671..15cc15f9f09a4 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -483,13 +483,13 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) { if let hir::ExprKind::Closure(..) = ex.kind { let def_id = self.tcx.hir().local_def_id(ex.hir_id); - self.check(def_id); + self.check(def_id.to_def_id()); } intravisit::walk_expr(self, ex); } fn visit_item(&mut self, it: &'tcx Item<'tcx>) { debug!("find_existential_constraints: visiting {:?}", it); - let def_id = self.tcx.hir().local_def_id(it.hir_id); + let def_id = self.tcx.hir().local_def_id(it.hir_id).to_def_id(); // The opaque type itself or its children are not within its reveal scope. if def_id != self.def_id { self.check(def_id); @@ -498,7 +498,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } fn visit_impl_item(&mut self, it: &'tcx ImplItem<'tcx>) { debug!("find_existential_constraints: visiting {:?}", it); - let def_id = self.tcx.hir().local_def_id(it.hir_id); + let def_id = self.tcx.hir().local_def_id(it.hir_id).to_def_id(); // The opaque type itself or its children are not within its reveal scope. if def_id != self.def_id { self.check(def_id); @@ -508,7 +508,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { fn visit_trait_item(&mut self, it: &'tcx TraitItem<'tcx>) { debug!("find_existential_constraints: visiting {:?}", it); let def_id = self.tcx.hir().local_def_id(it.hir_id); - self.check(def_id); + self.check(def_id.to_def_id()); intravisit::walk_trait_item(self, it); } } diff --git a/src/librustc_typeck/expr_use_visitor.rs b/src/librustc_typeck/expr_use_visitor.rs index b6d7309f86aac..2244a89f12963 100644 --- a/src/librustc_typeck/expr_use_visitor.rs +++ b/src/librustc_typeck/expr_use_visitor.rs @@ -519,7 +519,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { for &var_id in upvars.keys() { let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_id }, - closure_expr_id: closure_def_id.expect_local(), + closure_expr_id: closure_def_id, }; let upvar_capture = self.mc.tables.upvar_capture(upvar_id); let captured_place = return_if_err!(self.cat_captured_var( diff --git a/src/librustc_typeck/impl_wf_check.rs b/src/librustc_typeck/impl_wf_check.rs index 319f323851372..e13d9ea2b2626 100644 --- a/src/librustc_typeck/impl_wf_check.rs +++ b/src/librustc_typeck/impl_wf_check.rs @@ -14,7 +14,7 @@ use min_specialization::check_min_specialization; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::struct_span_err; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, TyCtxt, TypeFoldable}; @@ -59,7 +59,7 @@ pub fn impl_wf_check(tcx: TyCtxt<'_>) { // but it's one that we must perform earlier than the rest of // WfCheck. for &module in tcx.hir().krate().modules.keys() { - tcx.ensure().check_mod_impl_wf(tcx.hir().local_def_id(module)); + tcx.ensure().check_mod_impl_wf(tcx.hir().local_def_id(module).to_def_id()); } } @@ -85,7 +85,7 @@ impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> { enforce_impl_params_are_constrained(self.tcx, impl_def_id, items); enforce_impl_items_are_distinct(self.tcx, items); if self.min_specialization { - check_min_specialization(self.tcx, impl_def_id, item.span); + check_min_specialization(self.tcx, impl_def_id.to_def_id(), item.span); } } } @@ -97,7 +97,7 @@ impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> { fn enforce_impl_params_are_constrained( tcx: TyCtxt<'_>, - impl_def_id: DefId, + impl_def_id: LocalDefId, impl_item_refs: &[hir::ImplItemRef<'_>], ) { // Every lifetime used in an associated type must be constrained. diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index df8290fd018c5..a2b02ebd27179 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -354,7 +354,7 @@ pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> { // scope. This is derived from the enclosing item-like thing. let env_node_id = tcx.hir().get_parent_item(hir_ty.hir_id); let env_def_id = tcx.hir().local_def_id(env_node_id); - let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id); + let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id()); astconv::AstConv::ast_ty_to_ty(&item_cx, hir_ty) } @@ -369,7 +369,7 @@ pub fn hir_trait_to_predicates<'tcx>( // scope. This is derived from the enclosing item-like thing. let env_hir_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id); let env_def_id = tcx.hir().local_def_id(env_hir_id); - let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id); + let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id()); let mut bounds = Bounds::default(); let _ = AstConv::instantiate_poly_trait_ref_inner( &item_cx, diff --git a/src/librustc_typeck/outlives/implicit_infer.rs b/src/librustc_typeck/outlives/implicit_infer.rs index 2abca30246905..6a9cbc544f857 100644 --- a/src/librustc_typeck/outlives/implicit_infer.rs +++ b/src/librustc_typeck/outlives/implicit_infer.rs @@ -53,7 +53,7 @@ pub struct InferVisitor<'cx, 'tcx> { impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { - let item_did = self.tcx.hir().local_def_id(item.hir_id); + let item_did = self.tcx.hir().local_def_id(item.hir_id).to_def_id(); debug!("InferVisitor::visit_item(item={:?})", item_did); diff --git a/src/librustc_typeck/outlives/test.rs b/src/librustc_typeck/outlives/test.rs index 655f938493abd..abe9319d71c59 100644 --- a/src/librustc_typeck/outlives/test.rs +++ b/src/librustc_typeck/outlives/test.rs @@ -18,7 +18,7 @@ impl ItemLikeVisitor<'tcx> for OutlivesTest<'tcx> { // For unit testing: check for a special "rustc_outlives" // attribute and report an error with various results if found. - if self.tcx.has_attr(item_def_id, sym::rustc_outlives) { + if self.tcx.has_attr(item_def_id.to_def_id(), sym::rustc_outlives) { let inferred_outlives_of = self.tcx.inferred_outlives_of(item_def_id); struct_span_err!(self.tcx.sess, item.span, E0640, "{:?}", inferred_outlives_of).emit(); } diff --git a/src/librustc_typeck/variance/constraints.rs b/src/librustc_typeck/variance/constraints.rs index afa6e49a05cf1..83be3e2c87802 100644 --- a/src/librustc_typeck/variance/constraints.rs +++ b/src/librustc_typeck/variance/constraints.rs @@ -121,7 +121,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { fn visit_node_helper(&mut self, id: hir::HirId) { let tcx = self.terms_cx.tcx; let def_id = tcx.hir().local_def_id(id); - self.build_constraints_for_item(def_id); + self.build_constraints_for_item(def_id.to_def_id()); } fn tcx(&self) -> TyCtxt<'tcx> { diff --git a/src/librustc_typeck/variance/solve.rs b/src/librustc_typeck/variance/solve.rs index 4fc46ce93ee52..7402117a7ebb1 100644 --- a/src/librustc_typeck/variance/solve.rs +++ b/src/librustc_typeck/variance/solve.rs @@ -115,7 +115,7 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> { } } - (def_id, &*variances) + (def_id.to_def_id(), &*variances) }) .collect() } diff --git a/src/librustc_typeck/variance/test.rs b/src/librustc_typeck/variance/test.rs index a25252ccd3d84..1aab89310c6e8 100644 --- a/src/librustc_typeck/variance/test.rs +++ b/src/librustc_typeck/variance/test.rs @@ -18,7 +18,7 @@ impl ItemLikeVisitor<'tcx> for VarianceTest<'tcx> { // For unit testing: check for a special "rustc_variance" // attribute and report an error with various results if found. - if self.tcx.has_attr(item_def_id, sym::rustc_variance) { + if self.tcx.has_attr(item_def_id.to_def_id(), sym::rustc_variance) { let variances_of = self.tcx.variances_of(item_def_id); struct_span_err!(self.tcx.sess, item.span, E0208, "{:?}", variances_of).emit(); } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 6e50264c098b6..6d65e6b1e0a47 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -137,15 +137,16 @@ impl Clean for CrateNum { .filter_map(|&id| { let item = cx.tcx.hir().expect_item(id.id); match item.kind { - hir::ItemKind::Mod(_) => { - as_primitive(Res::Def(DefKind::Mod, cx.tcx.hir().local_def_id(id.id))) - } + hir::ItemKind::Mod(_) => as_primitive(Res::Def( + DefKind::Mod, + cx.tcx.hir().local_def_id(id.id).to_def_id(), + )), hir::ItemKind::Use(ref path, hir::UseKind::Single) if item.vis.node.is_pub() => { as_primitive(path.res).map(|(_, prim, attrs)| { // Pretend the primitive is local. - (cx.tcx.hir().local_def_id(id.id), prim, attrs) + (cx.tcx.hir().local_def_id(id.id).to_def_id(), prim, attrs) }) } _ => None, @@ -191,14 +192,15 @@ impl Clean for CrateNum { .filter_map(|&id| { let item = cx.tcx.hir().expect_item(id.id); match item.kind { - hir::ItemKind::Mod(_) => { - as_keyword(Res::Def(DefKind::Mod, cx.tcx.hir().local_def_id(id.id))) - } + hir::ItemKind::Mod(_) => as_keyword(Res::Def( + DefKind::Mod, + cx.tcx.hir().local_def_id(id.id).to_def_id(), + )), hir::ItemKind::Use(ref path, hir::UseKind::Single) if item.vis.node.is_pub() => { as_keyword(path.res).map(|(_, prim, attrs)| { - (cx.tcx.hir().local_def_id(id.id), prim, attrs) + (cx.tcx.hir().local_def_id(id.id).to_def_id(), prim, attrs) }) } _ => None, @@ -273,7 +275,7 @@ impl Clean for doctree::Module<'_> { visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), inner: ModuleItem(Module { is_crate: self.is_crate, items }), } } @@ -622,7 +624,7 @@ impl Clean for hir::GenericParam<'_> { hir::GenericParamKind::Type { ref default, synthetic } => ( self.name.ident().name.clean(cx), GenericParamDefKind::Type { - did: cx.tcx.hir().local_def_id(self.hir_id), + did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(), bounds: self.bounds.clean(cx), default: default.clean(cx), synthetic, @@ -631,7 +633,7 @@ impl Clean for hir::GenericParam<'_> { hir::GenericParamKind::Const { ref ty } => ( self.name.ident().name.clean(cx), GenericParamDefKind::Const { - did: cx.tcx.hir().local_def_id(self.hir_id), + did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(), ty: ty.clean(cx), }, ), @@ -892,7 +894,7 @@ impl Clean for doctree::Function<'_> { enter_impl_trait(cx, || (self.generics.clean(cx), (self.decl, self.body).clean(cx))); let did = cx.tcx.hir().local_def_id(self.id); - let constness = if is_min_const_fn(cx.tcx, did) { + let constness = if is_min_const_fn(cx.tcx, did.to_def_id()) { hir::Constness::Const } else { hir::Constness::NotConst @@ -905,7 +907,7 @@ impl Clean for doctree::Function<'_> { visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), - def_id: did, + def_id: did.to_def_id(), inner: FunctionItem(Function { decl, generics, @@ -1014,7 +1016,7 @@ impl Clean for doctree::Trait<'_> { name: Some(self.name.clean(cx)), attrs, source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -1037,7 +1039,7 @@ impl Clean for doctree::TraitAlias<'_> { name: Some(self.name.clean(cx)), attrs, source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -1118,10 +1120,10 @@ impl Clean for hir::TraitItem<'_> { name: Some(self.ident.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.span.clean(cx), - def_id: local_did, + def_id: local_did.to_def_id(), visibility: Visibility::Inherited, - stability: get_stability(cx, local_did), - deprecation: get_deprecation(cx, local_did), + stability: get_stability(cx, local_did.to_def_id()), + deprecation: get_deprecation(cx, local_did.to_def_id()), inner, } } @@ -1151,10 +1153,10 @@ impl Clean for hir::ImplItem<'_> { name: Some(self.ident.name.clean(cx)), source: self.span.clean(cx), attrs: self.attrs.clean(cx), - def_id: local_did, + def_id: local_did.to_def_id(), visibility: self.vis.clean(cx), - stability: get_stability(cx, local_did), - deprecation: get_deprecation(cx, local_did), + stability: get_stability(cx, local_did.to_def_id()), + deprecation: get_deprecation(cx, local_did.to_def_id()), inner, } } @@ -1346,7 +1348,7 @@ impl Clean for hir::Ty<'_> { TyKind::Slice(ref ty) => Slice(box ty.clean(cx)), TyKind::Array(ref ty, ref length) => { let def_id = cx.tcx.hir().local_def_id(length.hir_id); - let length = match cx.tcx.const_eval_poly(def_id) { + let length = match cx.tcx.const_eval_poly(def_id.to_def_id()) { Ok(length) => { print_const(cx, ty::Const::from_value(cx.tcx, length, cx.tcx.types.usize)) } @@ -1413,7 +1415,7 @@ impl Clean for hir::Ty<'_> { if let Some(lt) = lifetime.cloned() { if !lt.is_elided() { let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id); - lt_substs.insert(lt_def_id, lt.clean(cx)); + lt_substs.insert(lt_def_id.to_def_id(), lt.clean(cx)); } } indices.lifetimes += 1; @@ -1433,9 +1435,10 @@ impl Clean for hir::Ty<'_> { _ => None, }); if let Some(ty) = type_ { - ty_substs.insert(ty_param_def_id, ty.clean(cx)); + ty_substs.insert(ty_param_def_id.to_def_id(), ty.clean(cx)); } else if let Some(default) = *default { - ty_substs.insert(ty_param_def_id, default.clean(cx)); + ty_substs + .insert(ty_param_def_id.to_def_id(), default.clean(cx)); } indices.types += 1; } @@ -1455,7 +1458,8 @@ impl Clean for hir::Ty<'_> { _ => None, }); if let Some(ct) = const_ { - ct_substs.insert(const_param_def_id, ct.clean(cx)); + ct_substs + .insert(const_param_def_id.to_def_id(), ct.clean(cx)); } // FIXME(const_generics:defaults) indices.consts += 1; @@ -1747,9 +1751,9 @@ impl Clean for hir::StructField<'_> { attrs: self.attrs.clean(cx), source: self.span.clean(cx), visibility: self.vis.clean(cx), - stability: get_stability(cx, local_did), - deprecation: get_deprecation(cx, local_did), - def_id: local_did, + stability: get_stability(cx, local_did.to_def_id()), + deprecation: get_deprecation(cx, local_did.to_def_id()), + def_id: local_did.to_def_id(), inner: StructFieldItem(self.ty.clean(cx)), } } @@ -1797,7 +1801,7 @@ impl Clean for doctree::Struct<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -1817,7 +1821,7 @@ impl Clean for doctree::Union<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -1847,7 +1851,7 @@ impl Clean for doctree::Enum<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -1869,7 +1873,7 @@ impl Clean for doctree::Variant<'_> { visibility: Inherited, stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), inner: VariantItem(Variant { kind: self.def.clean(cx) }), } } @@ -2018,7 +2022,7 @@ impl Clean for doctree::Typedef<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -2033,7 +2037,7 @@ impl Clean for doctree::OpaqueTy<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -2064,7 +2068,7 @@ impl Clean for doctree::Static<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -2085,14 +2089,14 @@ impl Clean for doctree::Constant<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id, + def_id: def_id.to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), inner: ConstantItem(Constant { type_: self.type_.clean(cx), expr: print_const_expr(cx, self.expr), - value: print_evaluated_const(cx, def_id), + value: print_evaluated_const(cx, def_id.to_def_id()), is_literal: is_literal_expr(cx, self.expr.hir_id), }), } @@ -2139,7 +2143,7 @@ impl Clean> for doctree::Impl<'_> { name: None, attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id, + def_id: def_id.to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -2297,7 +2301,7 @@ impl Clean for doctree::ForeignItem<'_> { name: Some(self.name.clean(cx)), attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), @@ -2341,7 +2345,7 @@ impl Clean for doctree::ProcMacro<'_> { visibility: Public, stability: cx.stability(self.id).clean(cx), deprecation: cx.deprecation(self.id).clean(cx), - def_id: cx.tcx.hir().local_def_id(self.id), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), inner: ProcMacroItem(ProcMacro { kind: self.kind, helpers: self.helpers.clean(cx) }), } } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 762ec7e9ac3c6..894cc3e022c96 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -384,7 +384,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt map: access_levels .map .iter() - .map(|(&k, &v)| (tcx.hir().local_def_id(k), v)) + .map(|(&k, &v)| (tcx.hir().local_def_id(k).to_def_id(), v)) .collect(), }; diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index c80967a4b33f2..6ef01c3dec79f 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -120,7 +120,7 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate { for &trait_did in cx.tcx.all_traits(LOCAL_CRATE).iter() { for &impl_node in cx.tcx.hir().trait_impls(trait_did) { let impl_did = cx.tcx.hir().local_def_id(impl_node); - inline::build_impl(cx, impl_did, None, &mut new_items); + inline::build_impl(cx, impl_did.to_def_id(), None, &mut new_items); } } diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index bf4c44879273f..5ef803c992505 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -390,7 +390,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { if item.vis.node.is_pub() { let def_id = self.cx.tcx.hir().local_def_id(item.hir_id); - self.store_path(def_id); + self.store_path(def_id.to_def_id()); } match item.kind { @@ -628,7 +628,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { Macro { hid: def.hir_id, - def_id: self.cx.tcx.hir().local_def_id(def.hir_id), + def_id: self.cx.tcx.hir().local_def_id(def.hir_id).to_def_id(), attrs: &def.attrs, name: renamed.unwrap_or(def.ident.name), whence: def.span, From 3532d6e749e83e7558c8ed828ed20a3ff212fa21 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Sun, 12 Apr 2020 13:45:41 +0100 Subject: [PATCH 3/7] Modify `as_local_hir_id` to accept a `LocalDefId` instead of a `DefId` --- src/librustc_codegen_llvm/callee.rs | 2 +- src/librustc_codegen_llvm/consts.rs | 4 +- .../back/symbol_export.rs | 4 +- src/librustc_hir/definitions.rs | 8 +- .../infer/error_reporting/mod.rs | 5 +- .../nice_region_error/find_anon_type.rs | 4 +- .../nice_region_error/outlives_closure.rs | 4 +- .../error_reporting/nice_region_error/util.rs | 2 +- src/librustc_lint/builtin.rs | 11 +- src/librustc_lint/late.rs | 6 +- src/librustc_lint/lib.rs | 6 +- src/librustc_metadata/rmeta/encoder.rs | 16 +-- src/librustc_middle/dep_graph/mod.rs | 8 +- src/librustc_middle/hir/map/mod.rs | 20 ++- src/librustc_middle/hir/mod.rs | 4 +- src/librustc_middle/middle/region.rs | 4 +- src/librustc_middle/mir/mod.rs | 10 +- src/librustc_middle/mir/mono.rs | 16 ++- src/librustc_middle/query/mod.rs | 2 +- src/librustc_middle/ty/context.rs | 5 +- src/librustc_middle/ty/mod.rs | 18 +-- src/librustc_middle/ty/print/pretty.rs | 8 +- src/librustc_middle/ty/sty.rs | 2 +- .../diagnostics/conflict_errors.rs | 10 +- .../borrow_check/diagnostics/mod.rs | 6 +- .../diagnostics/mutability_errors.rs | 4 +- .../borrow_check/diagnostics/region_name.rs | 10 +- src/librustc_mir/borrow_check/mod.rs | 37 ++++-- src/librustc_mir/borrow_check/nll.rs | 2 +- .../borrow_check/universal_regions.rs | 7 +- src/librustc_mir/const_eval/eval_queries.rs | 4 +- src/librustc_mir/const_eval/fn_queries.rs | 3 +- src/librustc_mir/monomorphize/collector.rs | 4 +- .../transform/check_consts/mod.rs | 8 +- .../transform/check_consts/validation.rs | 2 +- src/librustc_mir/transform/check_unsafety.rs | 21 +-- src/librustc_mir/transform/const_prop.rs | 5 +- src/librustc_mir/transform/inline.rs | 2 +- src/librustc_mir/transform/mod.rs | 2 +- src/librustc_mir/transform/promote_consts.rs | 20 ++- .../transform/qualify_min_const_fn.rs | 2 +- src/librustc_mir/util/liveness.rs | 2 +- src/librustc_mir_build/build/mod.rs | 6 +- src/librustc_mir_build/hair/cx/expr.rs | 2 +- .../hair/pattern/check_match.rs | 4 +- src/librustc_mir_build/lints.rs | 4 +- src/librustc_passes/dead.rs | 13 +- src/librustc_passes/reachable.rs | 22 ++-- src/librustc_passes/region.rs | 2 +- src/librustc_passes/upvars.rs | 2 +- src/librustc_privacy/lib.rs | 41 +++--- src/librustc_resolve/late/lifetimes.rs | 27 ++-- src/librustc_symbol_mangling/lib.rs | 16 +-- src/librustc_trait_selection/opaque_types.rs | 26 +++- .../traits/error_reporting/mod.rs | 3 +- .../traits/error_reporting/suggestions.rs | 10 +- .../traits/specialize/mod.rs | 27 ++-- src/librustc_trait_selection/traits/wf.rs | 6 +- src/librustc_traits/lowering/environment.rs | 2 +- src/librustc_ty/ty.rs | 20 +-- src/librustc_typeck/astconv.rs | 21 +-- src/librustc_typeck/check/coercion.rs | 2 +- src/librustc_typeck/check/compare_method.rs | 84 ++++++------ src/librustc_typeck/check/dropck.rs | 21 +-- src/librustc_typeck/check/expr.rs | 4 +- src/librustc_typeck/check/method/suggest.rs | 124 +++++++++--------- src/librustc_typeck/check/mod.rs | 34 ++--- src/librustc_typeck/check/wfcheck.rs | 14 +- src/librustc_typeck/check/writeback.rs | 2 +- src/librustc_typeck/check_unused.rs | 4 +- src/librustc_typeck/coherence/builtin.rs | 11 +- src/librustc_typeck/collect.rs | 39 +++--- src/librustc_typeck/collect/type_of.rs | 12 +- .../impl_wf_check/min_specialization.rs | 13 +- src/librustc_typeck/lib.rs | 10 +- .../outlives/implicit_infer.rs | 8 +- src/librustc_typeck/outlives/mod.rs | 2 +- src/librustc_typeck/variance/constraints.rs | 12 +- src/librustc_typeck/variance/mod.rs | 2 +- src/librustc_typeck/variance/terms.rs | 4 +- src/librustdoc/clean/inline.rs | 31 ++--- src/librustdoc/clean/mod.rs | 12 +- src/librustdoc/clean/utils.rs | 4 +- src/librustdoc/core.rs | 2 +- .../passes/collect_intra_doc_links.rs | 6 +- src/librustdoc/visit_ast.rs | 4 +- 86 files changed, 588 insertions(+), 447 deletions(-) diff --git a/src/librustc_codegen_llvm/callee.rs b/src/librustc_codegen_llvm/callee.rs index 9db5c40c8e362..6ad75cff3ddb6 100644 --- a/src/librustc_codegen_llvm/callee.rs +++ b/src/librustc_codegen_llvm/callee.rs @@ -116,7 +116,7 @@ pub fn get_fn(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value if cx.tcx.sess.opts.share_generics() { // We are in share_generics mode. - if instance_def_id.is_local() { + if let Some(instance_def_id) = instance_def_id.as_local() { // This is a definition from the current crate. If the // definition is unreachable for downstream crates or // the current crate does not re-export generics, the diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index 9fd22c8b07bdd..ac2f47a740d57 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -209,7 +209,9 @@ impl CodegenCx<'ll, 'tcx> { debug!("get_static: sym={} instance={:?}", sym, instance); - let g = if let Some(id) = self.tcx.hir().as_local_hir_id(def_id) { + let g = if let Some(id) = + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + { let llty = self.layout_of(ty).llvm_type(self); let (g, attrs) = match self.tcx.hir().get(id) { Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => { diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index 346b425c4d3bf..4edf8316028f4 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -359,8 +359,8 @@ fn upstream_drop_glue_for_provider<'tcx>( } fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) { - !tcx.reachable_set(LOCAL_CRATE).contains(&hir_id) + if let Some(def_id) = def_id.as_local() { + !tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().as_local_hir_id(def_id).unwrap()) } else { bug!("is_unreachable_local_definition called with non-local DefId: {:?}", def_id) } diff --git a/src/librustc_hir/definitions.rs b/src/librustc_hir/definitions.rs index 1ac23677d4739..8927814ee04ba 100644 --- a/src/librustc_hir/definitions.rs +++ b/src/librustc_hir/definitions.rs @@ -342,12 +342,8 @@ impl Definitions { } #[inline] - pub fn as_local_hir_id(&self, def_id: DefId) -> Option { - if let Some(def_id) = def_id.as_local() { - Some(self.local_def_id_to_hir_id(def_id)) - } else { - None - } + pub fn as_local_hir_id(&self, def_id: LocalDefId) -> Option { + Some(self.local_def_id_to_hir_id(def_id)) } #[inline] diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs index 4189570a0da58..708518b227456 100644 --- a/src/librustc_infer/infer/error_reporting/mod.rs +++ b/src/librustc_infer/infer/error_reporting/mod.rs @@ -191,7 +191,7 @@ fn msg_span_from_early_bound_and_free_regions( let sm = tcx.sess.source_map(); let scope = region.free_region_binding_scope(tcx); - let node = tcx.hir().as_local_hir_id(scope).unwrap(); + let node = tcx.hir().as_local_hir_id(scope.expect_local()).unwrap(); let tag = match tcx.hir().find(node) { Some(Node::Block(_)) | Some(Node::Expr(_)) => "body", Some(Node::Item(it)) => item_scope_tag(&it), @@ -1780,10 +1780,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { if !(generics.has_self && param.index == 0) { let type_param = generics.type_param(param, self.tcx); let hir = &self.tcx.hir(); - hir.as_local_hir_id(type_param.def_id).map(|id| { + type_param.def_id.as_local().map(|def_id| { // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: 'a'b`, // instead we suggest `T: 'a + 'b` in that case. + let id = hir.as_local_hir_id(def_id).unwrap(); let mut has_bounds = false; if let Node::GenericParam(param) = hir.get(id) { has_bounds = !param.bounds.is_empty(); diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs index 53cab34cd3f17..6d10ec3dcae6f 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs @@ -29,7 +29,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { ) -> Option<(&hir::Ty<'_>, &hir::FnDecl<'_>)> { if let Some(anon_reg) = self.tcx().is_suitable_region(region) { let def_id = anon_reg.def_id; - if let Some(hir_id) = self.tcx().hir().as_local_hir_id(def_id) { + if let Some(hir_id) = + def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id).unwrap()) + { let fndecl = match self.tcx().hir().get(hir_id) { Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. }) | Node::TraitItem(&hir::TraitItem { diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs index 70c302710430b..6bf60db009497 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs @@ -46,7 +46,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { ) = (&sub_origin, sup_region) { let hir = &self.tcx().hir(); - if let Some(hir_id) = hir.as_local_hir_id(free_region.scope) { + if let Some(hir_id) = + free_region.scope.as_local().map(|def_id| hir.as_local_hir_id(def_id).unwrap()) + { if let Node::Expr(Expr { kind: Closure(_, _, _, closure_span, None), .. }) = hir.get(hir_id) { diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs index 7bbd2127bcfdf..ab74513a6755d 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs @@ -51,7 +51,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { }; let hir = &self.tcx().hir(); - let hir_id = hir.as_local_hir_id(id)?; + let hir_id = hir.as_local_hir_id(id.as_local()?)?; let body_id = hir.maybe_body_owned_by(hir_id)?; let body = hir.body(body_id); let owner_id = hir.body_owner(body_id); diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 0fba510e101a1..4040545db968b 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -436,7 +436,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { // If the trait is private, add the impl items to `private_traits` so they don't get // reported for missing docs. let real_trait = trait_ref.path.res.def_id(); - if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(real_trait) { + if let Some(hir_id) = real_trait + .as_local() + .map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + { if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) { if let hir::VisibilityKind::Inherited = item.vis.node { for impl_item_ref in items { @@ -609,7 +612,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations { let mut impls = HirIdSet::default(); cx.tcx.for_each_impl(debug, |d| { if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() { - if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(ty_def.did) { + if let Some(hir_id) = ty_def + .did + .as_local() + .map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + { impls.insert(hir_id); } } diff --git a/src/librustc_lint/late.rs b/src/librustc_lint/late.rs index 1999a8ec5f60d..0a0505e89d468 100644 --- a/src/librustc_lint/late.rs +++ b/src/librustc_lint/late.rs @@ -19,7 +19,7 @@ use rustc_ast::ast; use rustc_ast::walk_list; use rustc_data_structures::sync::{join, par_iter, ParallelIterator}; use rustc_hir as hir; -use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE}; use rustc_hir::intravisit as hir_visit; use rustc_hir::intravisit::Visitor; use rustc_middle::hir::map::Map; @@ -353,7 +353,7 @@ crate::late_lint_methods!(late_lint_pass_impl, [], ['tcx]); fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( tcx: TyCtxt<'tcx>, - module_def_id: DefId, + module_def_id: LocalDefId, pass: T, ) { let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE); @@ -382,7 +382,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( tcx: TyCtxt<'tcx>, - module_def_id: DefId, + module_def_id: LocalDefId, builtin_lints: T, ) { if tcx.sess.opts.debugging_opts.no_interleave_lints { diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index af1fad2c6608b..f9d910cc32547 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -89,7 +89,11 @@ pub fn provide(providers: &mut Providers<'_>) { } fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) { - late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new()); + late::late_lint_mod( + tcx, + module_def_id.expect_local(), + BuiltinCombinedModuleLateLintPass::new(), + ); } macro_rules! pre_expansion_lint_passes { diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index 5a1ba0bbeeabd..505330f481a18 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -616,7 +616,7 @@ impl EncodeContext<'tcx> { ctor: variant.ctor_def_id.map(|did| did.index), }; - let enum_id = tcx.hir().as_local_hir_id(def.did).unwrap(); + let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()).unwrap(); let enum_vis = &tcx.hir().expect_item(enum_id).vis; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); @@ -662,7 +662,7 @@ impl EncodeContext<'tcx> { // Variant constructors have the same visibility as the parent enums, unless marked as // non-exhaustive, in which case they are lowered to `pub(crate)`. - let enum_id = tcx.hir().as_local_hir_id(def.did).unwrap(); + let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()).unwrap(); let enum_vis = &tcx.hir().expect_item(enum_id).vis; let mut ctor_vis = ty::Visibility::from_hir(enum_vis, enum_id, tcx); if variant.is_field_list_non_exhaustive() && ctor_vis == ty::Visibility::Public { @@ -728,7 +728,7 @@ impl EncodeContext<'tcx> { let def_id = field.did; debug!("EncodeContext::encode_field({:?})", def_id); - let variant_id = tcx.hir().as_local_hir_id(variant.def_id).unwrap(); + let variant_id = tcx.hir().as_local_hir_id(variant.def_id.expect_local()).unwrap(); let variant_data = tcx.hir().expect_variant_data(variant_id); record!(self.tables.kind[def_id] <- EntryKind::Field); @@ -755,7 +755,7 @@ impl EncodeContext<'tcx> { ctor: Some(def_id.index), }; - let struct_id = tcx.hir().as_local_hir_id(adt_def.did).unwrap(); + let struct_id = tcx.hir().as_local_hir_id(adt_def.did.expect_local()).unwrap(); let struct_vis = &tcx.hir().expect_item(struct_id).vis; let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx); for field in &variant.fields { @@ -817,7 +817,7 @@ impl EncodeContext<'tcx> { debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id); let tcx = self.tcx; - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let ast_item = tcx.hir().expect_trait_item(hir_id); let trait_item = tcx.associated_item(def_id); @@ -908,7 +908,7 @@ impl EncodeContext<'tcx> { debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id); let tcx = self.tcx; - let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let ast_item = self.tcx.hir().expect_impl_item(hir_id); let impl_item = self.tcx.associated_item(def_id); @@ -1308,7 +1308,6 @@ impl EncodeContext<'tcx> { } fn encode_info_for_closure(&mut self, def_id: LocalDefId) { - let def_id = def_id.to_def_id(); debug!("EncodeContext::encode_info_for_closure({:?})", def_id); // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, @@ -1316,6 +1315,7 @@ impl EncodeContext<'tcx> { let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); let ty = self.tcx.typeck_tables_of(def_id).node_type(hir_id); + let def_id = def_id.to_def_id(); record!(self.tables.kind[def_id] <- match ty.kind { ty::Generator(..) => { let data = self.tcx.generator_kind(def_id).unwrap(); @@ -1339,11 +1339,11 @@ impl EncodeContext<'tcx> { } fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) { - let def_id = def_id.to_def_id(); debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id); let id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); let body_id = self.tcx.hir().body_owned_by(id); let const_data = self.encode_rendered_const_for_body(body_id); + let def_id = def_id.to_def_id(); let qualifs = self.tcx.mir_const_qualif(def_id); record!(self.tables.kind[def_id] <- EntryKind::Const(qualifs, const_data)); diff --git a/src/librustc_middle/dep_graph/mod.rs b/src/librustc_middle/dep_graph/mod.rs index f56df19bfb061..0aca0f6e5b734 100644 --- a/src/librustc_middle/dep_graph/mod.rs +++ b/src/librustc_middle/dep_graph/mod.rs @@ -5,7 +5,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::Diagnostic; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::LocalDefId; mod dep_node; @@ -106,7 +106,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { match dep_node.kind { DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => { if let Some(def_id) = dep_node.extract_def_id(*self) { - if def_id_corresponds_to_hir_dep_node(*self, def_id) { + if def_id_corresponds_to_hir_dep_node(*self, def_id.expect_local()) { if dep_node.kind == DepKind::CrateMetadata { // The `DefPath` has corresponding node, // and that node should have been marked @@ -180,7 +180,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { } } -fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool { +fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); - def_id.index == hir_id.owner.local_def_index + def_id == hir_id.owner } diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index 1f25a2f6cfba8..a3626e790a89b 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -198,7 +198,7 @@ impl<'hir> Map<'hir> { } #[inline] - pub fn as_local_hir_id(&self, def_id: DefId) -> Option { + pub fn as_local_hir_id(&self, def_id: LocalDefId) -> Option { self.tcx.definitions.as_local_hir_id(def_id) } @@ -448,7 +448,7 @@ impl<'hir> Map<'hir> { } } - pub fn get_module(&self, module: DefId) -> (&'hir Mod<'hir>, Span, HirId) { + pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) { let hir_id = self.as_local_hir_id(module).unwrap(); match self.get_entry(hir_id).node { Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id), @@ -482,7 +482,11 @@ impl<'hir> Map<'hir> { } pub fn get_if_local(&self, id: DefId) -> Option> { - self.as_local_hir_id(id).map(|id| self.get(id)) + if let Some(id) = id.as_local() { + self.as_local_hir_id(id).map(|id| self.get(id)) + } else { + None + } } pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> { @@ -885,7 +889,11 @@ impl<'hir> Map<'hir> { } pub fn span_if_local(&self, id: DefId) -> Option { - self.as_local_hir_id(id).map(|id| self.span(id)) + if let Some(id) = id.as_local() { + self.as_local_hir_id(id).map(|id| self.span(id)) + } else { + None + } } pub fn res_span(&self, res: Res) -> Option { @@ -1085,8 +1093,8 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String { pub fn provide(providers: &mut Providers<'_>) { providers.def_kind = |tcx, def_id| { - if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) { - tcx.hir().def_kind(hir_id) + if let Some(def_id) = def_id.as_local() { + tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id).unwrap()) } else { bug!("calling local def_kind query provider for upstream DefId: {:?}", def_id); } diff --git a/src/librustc_middle/hir/mod.rs b/src/librustc_middle/hir/mod.rs index 3d3d9dcf41da5..c44a8b0b12e72 100644 --- a/src/librustc_middle/hir/mod.rs +++ b/src/librustc_middle/hir/mod.rs @@ -68,13 +68,13 @@ impl<'tcx> TyCtxt<'tcx> { pub fn provide(providers: &mut Providers<'_>) { providers.parent_module_from_def_id = |tcx, id| { let hir = tcx.hir(); - hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id.to_def_id()).unwrap())) + hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap())) }; providers.hir_crate = |tcx, _| tcx.untracked_crate; providers.index_hir = map::index_hir; providers.hir_module_items = |tcx, id| { let hir = tcx.hir(); - let module = hir.as_local_hir_id(id.to_def_id()).unwrap(); + let module = hir.as_local_hir_id(id).unwrap(); &tcx.untracked_crate.modules[&module] }; providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature; diff --git a/src/librustc_middle/middle/region.rs b/src/librustc_middle/middle/region.rs index 2ad6fe14ec716..41f712f600aa0 100644 --- a/src/librustc_middle/middle/region.rs +++ b/src/librustc_middle/middle/region.rs @@ -554,7 +554,7 @@ impl<'tcx> ScopeTree { pub fn early_free_scope(&self, tcx: TyCtxt<'tcx>, br: &ty::EarlyBoundRegion) -> Scope { let param_owner = tcx.parent(br.def_id).unwrap(); - let param_owner_id = tcx.hir().as_local_hir_id(param_owner).unwrap(); + let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local()).unwrap(); let scope = tcx .hir() .maybe_body_owned_by(param_owner_id) @@ -595,7 +595,7 @@ impl<'tcx> ScopeTree { // on the same function that they ended up being freed in. assert_eq!(param_owner, fr.scope); - let param_owner_id = tcx.hir().as_local_hir_id(param_owner).unwrap(); + let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local()).unwrap(); let body_id = tcx.hir().body_owned_by(param_owner_id); Scope { id: tcx.hir().body(body_id).value.hir_id.local_id, data: ScopeData::CallSite } } diff --git a/src/librustc_middle/mir/mod.rs b/src/librustc_middle/mir/mod.rs index 212061cfd824f..1019941a523c9 100644 --- a/src/librustc_middle/mir/mod.rs +++ b/src/librustc_middle/mir/mod.rs @@ -2284,7 +2284,10 @@ impl<'tcx> Debug for Rvalue<'tcx> { } AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| { - if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = def_id + .as_local() + .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + { let name = if tcx.sess.opts.debugging_opts.span_free_formats { let substs = tcx.lift(&substs).unwrap(); format!( @@ -2310,7 +2313,10 @@ impl<'tcx> Debug for Rvalue<'tcx> { }), AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| { - if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = def_id + .as_local() + .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + { let name = format!("[generator@{:?}]", tcx.hir().span(hir_id)); let mut struct_fmt = fmt.debug_struct(&name); diff --git a/src/librustc_middle/mir/mono.rs b/src/librustc_middle/mir/mono.rs index 0b64cb479d559..1b18060c88665 100644 --- a/src/librustc_middle/mir/mono.rs +++ b/src/librustc_middle/mir/mono.rs @@ -197,8 +197,12 @@ impl<'tcx> MonoItem<'tcx> { pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option { match *self { - MonoItem::Fn(Instance { def, .. }) => tcx.hir().as_local_hir_id(def.def_id()), - MonoItem::Static(def_id) => tcx.hir().as_local_hir_id(def_id), + MonoItem::Fn(Instance { def, .. }) => { + def.def_id().as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + } + MonoItem::Static(def_id) => { + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + } MonoItem::GlobalAsm(hir_id) => Some(hir_id), } .map(|hir_id| tcx.hir().span(hir_id)) @@ -339,7 +343,9 @@ impl<'tcx> CodegenUnit<'tcx> { // instances into account. The others don't matter for // the codegen tests and can even make item order // unstable. - InstanceDef::Item(def_id) => tcx.hir().as_local_hir_id(def_id), + InstanceDef::Item(def_id) => def_id + .as_local() + .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()), InstanceDef::VtableShim(..) | InstanceDef::ReifyShim(..) | InstanceDef::Intrinsic(..) @@ -350,7 +356,9 @@ impl<'tcx> CodegenUnit<'tcx> { | InstanceDef::CloneShim(..) => None, } } - MonoItem::Static(def_id) => tcx.hir().as_local_hir_id(def_id), + MonoItem::Static(def_id) => { + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + } MonoItem::GlobalAsm(hir_id) => Some(hir_id), }, item.symbol_name(tcx), diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs index 3ddb290fc8d1e..25c7d86ddac2d 100644 --- a/src/librustc_middle/query/mod.rs +++ b/src/librustc_middle/query/mod.rs @@ -287,7 +287,7 @@ rustc_queries! { /// per-type-parameter predicates for resolving `T::AssocTy`. query type_param_predicates(key: (DefId, DefId)) -> ty::GenericPredicates<'tcx> { desc { |tcx| "computing the bounds for type parameter `{}`", { - let id = tcx.hir().as_local_hir_id(key.1).unwrap(); + let id = tcx.hir().as_local_hir_id(key.1.expect_local()).unwrap(); tcx.hir().ty_param_name(id) }} } diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index a49dc105498ed..8a25beb6ae49f 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -1413,7 +1413,8 @@ impl<'tcx> TyCtxt<'tcx> { _ => return None, // not a free region }; - let hir_id = self.hir().as_local_hir_id(suitable_region_binding_scope).unwrap(); + let hir_id = + self.hir().as_local_hir_id(suitable_region_binding_scope.expect_local()).unwrap(); let is_impl_item = match self.hir().find(hir_id) { Some(Node::Item(..)) | Some(Node::TraitItem(..)) => false, Some(Node::ImplItem(..)) => { @@ -1431,7 +1432,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn return_type_impl_trait(&self, scope_def_id: DefId) -> Option<(Ty<'tcx>, Span)> { // HACK: `type_of_def_id()` will fail on these (#55796), so return `None`. - let hir_id = self.hir().as_local_hir_id(scope_def_id).unwrap(); + let hir_id = self.hir().as_local_hir_id(scope_def_id.expect_local()).unwrap(); match self.hir().get(hir_id) { Node::Item(item) => { match item.kind { diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 430ff67d56b68..e63d1bbdc4dcc 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -2666,12 +2666,14 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn opt_item_name(self, def_id: DefId) -> Option { - self.hir().as_local_hir_id(def_id).and_then(|hir_id| self.hir().get(hir_id).ident()) + def_id + .as_local() + .and_then(|def_id| self.hir().get(self.hir().as_local_hir_id(def_id).unwrap()).ident()) } pub fn opt_associated_item(self, def_id: DefId) -> Option { - let is_associated_item = if let Some(hir_id) = self.hir().as_local_hir_id(def_id) { - match self.hir().get(hir_id) { + let is_associated_item = if let Some(def_id) = def_id.as_local() { + match self.hir().get(self.hir().as_local_hir_id(def_id).unwrap()) { Node::TraitItem(_) | Node::ImplItem(_) => true, _ => false, } @@ -2825,8 +2827,8 @@ impl<'tcx> TyCtxt<'tcx> { /// Gets the attributes of a definition. pub fn get_attrs(self, did: DefId) -> Attributes<'tcx> { - if let Some(id) = self.hir().as_local_hir_id(did) { - self.hir().attrs(id) + if let Some(did) = did.as_local() { + self.hir().attrs(self.hir().as_local_hir_id(did).unwrap()) } else { self.item_attrs(did) } @@ -2864,7 +2866,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err` /// with the name of the crate containing the impl. pub fn span_of_impl(self, impl_did: DefId) -> Result { - if impl_did.is_local() { + if let Some(impl_did) = impl_did.as_local() { let hir_id = self.hir().as_local_hir_id(impl_did).unwrap(); Ok(self.hir().span(hir_id)) } else { @@ -2925,8 +2927,8 @@ pub struct AdtSizedConstraint<'tcx>(pub &'tcx [Ty<'tcx>]); /// Yields the parent function's `DefId` if `def_id` is an `impl Trait` definition. pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option { - if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) { - if let Node::Item(item) = tcx.hir().get(hir_id) { + if let Some(def_id) = def_id.as_local() { + if let Node::Item(item) = tcx.hir().get(tcx.hir().as_local_hir_id(def_id).unwrap()) { if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind { return opaque_ty.impl_trait_fn; } diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index a8b7b6a4b97a4..284e609d7e4ff 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -620,7 +620,9 @@ pub trait PrettyPrinter<'tcx>: } // FIXME(eddyb) should use `def_span`. - if let Some(hir_id) = self.tcx().hir().as_local_hir_id(did) { + if let Some(hir_id) = + did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did).unwrap()) + { p!(write("@{:?}", self.tcx().hir().span(hir_id))); if substs.as_generator().is_valid() { @@ -666,7 +668,9 @@ pub trait PrettyPrinter<'tcx>: p!(write("[closure")); // FIXME(eddyb) should use `def_span`. - if let Some(hir_id) = self.tcx().hir().as_local_hir_id(did) { + if let Some(hir_id) = + did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did).unwrap()) + { if self.tcx().sess.opts.debugging_opts.span_free_formats { p!(write("@"), print_def_path(did, substs)); } else { diff --git a/src/librustc_middle/ty/sty.rs b/src/librustc_middle/ty/sty.rs index d99a96316ae78..7ac4b6bb15700 100644 --- a/src/librustc_middle/ty/sty.rs +++ b/src/librustc_middle/ty/sty.rs @@ -2261,7 +2261,7 @@ impl<'tcx> Const<'tcx> { ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => { // Find the name and index of the const parameter by indexing the generics of // the parent item and construct a `ParamConst`. - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let item_id = tcx.hir().get_parent_node(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id.to_def_id()); diff --git a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs index 65561e224dbfd..201660552194c 100644 --- a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs @@ -191,7 +191,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let needs_note = match ty.kind { ty::Closure(id, _) => { let tables = self.infcx.tcx.typeck_tables_of(id); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(id).unwrap(); + let hir_id = self.infcx.tcx.hir().as_local_hir_id(id.expect_local()).unwrap(); tables.closure_kind_origins().get(hir_id).is_none() } @@ -862,7 +862,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { format!("`{}` would have to be valid for `{}`...", name, region_name), ); - if let Some(fn_hir_id) = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id) { + if let Some(fn_hir_id) = self + .mir_def_id + .as_local() + .map(|def_id| self.infcx.tcx.hir().as_local_hir_id(def_id).unwrap()) + { err.span_label( drop_span, format!( @@ -1763,7 +1767,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ) -> Option> { debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig); let is_closure = self.infcx.tcx.is_closure(did); - let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did)?; + let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did.as_local()?)?; let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?; // We need to work out which arguments to highlight. We do this by looking diff --git a/src/librustc_mir/borrow_check/diagnostics/mod.rs b/src/librustc_mir/borrow_check/diagnostics/mod.rs index 404cc0c74679f..c339dd46eece0 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mod.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mod.rs @@ -97,7 +97,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { debug!("add_moved_or_invoked_closure_note: closure={:?}", closure); if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind { - let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap(); + let hir_id = self.infcx.tcx.hir().as_local_hir_id(did.expect_local()).unwrap(); if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did).closure_kind_origins().get(hir_id) @@ -119,7 +119,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { // Check if we are just moving a closure after it has been invoked. if let Some(target) = target { if let ty::Closure(did, _) = self.body.local_decls[target].ty.kind { - let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap(); + let hir_id = self.infcx.tcx.hir().as_local_hir_id(did.expect_local()).unwrap(); if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did).closure_kind_origins().get(hir_id) @@ -804,7 +804,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "closure_span: def_id={:?} target_place={:?} places={:?}", def_id, target_place, places ); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id)?; + let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id.as_local()?)?; let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind; debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr); if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr { diff --git a/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs b/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs index 635c299cf8136..a83b30c4a6ef7 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs @@ -490,7 +490,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { err.span_label(sp, format!("cannot {}", act)); let hir = self.infcx.tcx.hir(); - let closure_id = hir.as_local_hir_id(self.mir_def_id).unwrap(); + let closure_id = hir.as_local_hir_id(self.mir_def_id.expect_local()).unwrap(); let fn_call_id = hir.get_parent_node(closure_id); let node = hir.get(fn_call_id); let item_id = hir.get_parent_item(fn_call_id); @@ -689,7 +689,7 @@ fn annotate_struct_field( if let ty::Adt(def, _) = ty.kind { let field = def.all_fields().nth(field.index())?; // Use the HIR types to construct the diagnostic message. - let hir_id = tcx.hir().as_local_hir_id(field.did)?; + let hir_id = tcx.hir().as_local_hir_id(field.did.as_local()?)?; let node = tcx.hir().find(hir_id)?; // Now we're dealing with the actual struct that we're going to suggest a change to, // we can expect a field that is an immutable reference to a type. diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index a085c2f7f69c9..2088879f8c606 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -241,8 +241,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { .infcx .tcx .hir() - .as_local_hir_id(self.mir_def_id) - .expect("non-local mir"); + .as_local_hir_id(self.mir_def_id.expect_local()) + .unwrap(); let def_ty = self.regioncx.universal_regions().defining_ty; if let DefiningTy::Closure(_, substs) = def_ty { @@ -328,7 +328,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { argument_ty: Ty<'tcx>, argument_index: usize, ) -> Option { - let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id)?; + let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id.as_local()?)?; let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?; let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?; match argument_hir_ty.kind { @@ -636,7 +636,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap()); let type_name = self.infcx.extract_type_name(&return_ty, Some(highlight)).0; - let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id).expect("non-local mir"); + let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local()).unwrap(); let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) { hir::Node::Expr(hir::Expr { @@ -688,7 +688,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap()); let type_name = self.infcx.extract_type_name(&yield_ty, Some(highlight)).0; - let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id).expect("non-local mir"); + let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local()).unwrap(); let yield_span = match tcx.hir().get(mir_hir_id) { hir::Node::Expr(hir::Expr { diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 52847af214f6c..429cfab330b99 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -5,7 +5,10 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::graph::dominators::Dominators; use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder}; use rustc_hir as hir; -use rustc_hir::{def_id::DefId, HirId, Node}; +use rustc_hir::{ + def_id::{DefId, LocalDefId}, + HirId, Node, +}; use rustc_index::bit_set::BitSet; use rustc_index::vec::IndexVec; use rustc_infer::infer::{InferCtxt, TyCtxtInferExt}; @@ -96,7 +99,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def_id: DefId) -> &BorrowCheckResult<'_> { let opt_closure_req = tcx.infer_ctxt().enter(|infcx| { let input_body: &Body<'_> = &input_body.borrow(); let promoted: &IndexVec<_, _> = &promoted.borrow(); - do_mir_borrowck(&infcx, input_body, promoted, def_id) + do_mir_borrowck(&infcx, input_body, promoted, def_id.expect_local()) }); debug!("mir_borrowck done"); @@ -107,13 +110,13 @@ fn do_mir_borrowck<'a, 'tcx>( infcx: &InferCtxt<'a, 'tcx>, input_body: &Body<'tcx>, input_promoted: &IndexVec>, - def_id: DefId, + def_id: LocalDefId, ) -> BorrowCheckResult<'tcx> { debug!("do_mir_borrowck(def_id = {:?})", def_id); let tcx = infcx.tcx; let param_env = tcx.param_env(def_id); - let id = tcx.hir().as_local_hir_id(def_id).expect("do_mir_borrowck: non-local DefId"); + let id = tcx.hir().as_local_hir_id(def_id).unwrap(); let mut local_names = IndexVec::from_elem(None, &input_body.local_decls); for var_debug_info in &input_body.var_debug_info { @@ -140,7 +143,7 @@ fn do_mir_borrowck<'a, 'tcx>( } let upvars: Vec<_> = tables .upvar_list - .get(&def_id) + .get(&def_id.to_def_id()) .into_iter() .flat_map(|v| v.values()) .map(|upvar_id| { @@ -172,7 +175,7 @@ fn do_mir_borrowck<'a, 'tcx>( let mut promoted = input_promoted.clone(); let mut body = BodyAndCache::new(body_clone); let free_regions = - nll::replace_regions_in_mir(infcx, def_id, param_env, &mut body, &mut promoted); + nll::replace_regions_in_mir(infcx, def_id.to_def_id(), param_env, &mut body, &mut promoted); let body = read_only!(body); // no further changes let promoted: IndexVec<_, _> = promoted.iter_mut().map(|body| read_only!(body)).collect(); @@ -188,7 +191,7 @@ fn do_mir_borrowck<'a, 'tcx>( let mdpe = MoveDataParamEnv { move_data, param_env }; let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe) - .into_engine(tcx, &body, def_id) + .into_engine(tcx, &body, def_id.to_def_id()) .iterate_to_fixpoint() .into_results_cursor(&body); @@ -205,7 +208,7 @@ fn do_mir_borrowck<'a, 'tcx>( nll_errors, } = nll::compute_regions( infcx, - def_id, + def_id.to_def_id(), free_regions, body, &promoted, @@ -218,14 +221,20 @@ fn do_mir_borrowck<'a, 'tcx>( // Dump MIR results into a file, if that is enabled. This let us // write unit-tests, as well as helping with debugging. - nll::dump_mir_results(infcx, MirSource::item(def_id), &body, ®ioncx, &opt_closure_req); + nll::dump_mir_results( + infcx, + MirSource::item(def_id.to_def_id()), + &body, + ®ioncx, + &opt_closure_req, + ); // We also have a `#[rustc_regions]` annotation that causes us to dump // information. nll::dump_annotation( infcx, &body, - def_id, + def_id.to_def_id(), ®ioncx, &opt_closure_req, &opaque_type_values, @@ -240,13 +249,13 @@ fn do_mir_borrowck<'a, 'tcx>( let regioncx = Rc::new(regioncx); let flow_borrows = Borrows::new(tcx, &body, regioncx.clone(), &borrow_set) - .into_engine(tcx, &body, def_id) + .into_engine(tcx, &body, def_id.to_def_id()) .iterate_to_fixpoint(); let flow_uninits = MaybeUninitializedPlaces::new(tcx, &body, &mdpe) - .into_engine(tcx, &body, def_id) + .into_engine(tcx, &body, def_id.to_def_id()) .iterate_to_fixpoint(); let flow_ever_inits = EverInitializedPlaces::new(tcx, &body, &mdpe) - .into_engine(tcx, &body, def_id) + .into_engine(tcx, &body, def_id.to_def_id()) .iterate_to_fixpoint(); let movable_generator = match tcx.hir().get(id) { @@ -262,7 +271,7 @@ fn do_mir_borrowck<'a, 'tcx>( let mut mbcx = MirBorrowckCtxt { infcx, body, - mir_def_id: def_id, + mir_def_id: def_id.to_def_id(), move_data: &mdpe.move_data, location_table, movable_generator, diff --git a/src/librustc_mir/borrow_check/nll.rs b/src/librustc_mir/borrow_check/nll.rs index 678ab3ed323ea..71e204f6edd00 100644 --- a/src/librustc_mir/borrow_check/nll.rs +++ b/src/librustc_mir/borrow_check/nll.rs @@ -66,7 +66,7 @@ pub(in crate::borrow_check) fn replace_regions_in_mir<'cx, 'tcx>( debug!("replace_regions_in_mir(def_id={:?})", def_id); // Compute named region information. This also renumbers the inputs/outputs. - let universal_regions = UniversalRegions::new(infcx, def_id, param_env); + let universal_regions = UniversalRegions::new(infcx, def_id.expect_local(), param_env); // Replace all remaining regions with fresh inference variables. renumber::renumber_mir(infcx, body, promoted); diff --git a/src/librustc_mir/borrow_check/universal_regions.rs b/src/librustc_mir/borrow_check/universal_regions.rs index 2dbfcb690179e..de985cb37c56d 100644 --- a/src/librustc_mir/borrow_check/universal_regions.rs +++ b/src/librustc_mir/borrow_check/universal_regions.rs @@ -16,7 +16,7 @@ use either::Either; use rustc_data_structures::fx::FxHashMap; use rustc_errors::DiagnosticBuilder; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::lang_items; use rustc_hir::{BodyOwnerKind, HirId}; use rustc_index::vec::{Idx, IndexVec}; @@ -220,12 +220,13 @@ impl<'tcx> UniversalRegions<'tcx> { /// known between those regions. pub fn new( infcx: &InferCtxt<'_, 'tcx>, - mir_def_id: DefId, + mir_def_id: LocalDefId, param_env: ty::ParamEnv<'tcx>, ) -> Self { let tcx = infcx.tcx; let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id).unwrap(); - UniversalRegionsBuilder { infcx, mir_def_id, mir_hir_id, param_env }.build() + UniversalRegionsBuilder { infcx, mir_def_id: mir_def_id.to_def_id(), mir_hir_id, param_env } + .build() } /// Given a reference to a closure type, extracts all the values diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs index 3f0774767fd7a..588675d8eda84 100644 --- a/src/librustc_mir/const_eval/eval_queries.rs +++ b/src/librustc_mir/const_eval/eval_queries.rs @@ -346,7 +346,7 @@ pub fn const_eval_raw_provider<'tcx>( // validation thus preventing such a hard error from being a backwards // compatibility hazard Some(DefKind::Const) | Some(DefKind::AssocConst) => { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); err.report_as_lint( tcx.at(tcx.def_span(def_id)), "any use of this value will cause an error", @@ -369,7 +369,7 @@ pub fn const_eval_raw_provider<'tcx>( err.report_as_lint( tcx.at(span), "reaching this expression at runtime will panic or abort", - tcx.hir().as_local_hir_id(def_id).unwrap(), + tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(), Some(err.span), ) } diff --git a/src/librustc_mir/const_eval/fn_queries.rs b/src/librustc_mir/const_eval/fn_queries.rs index f1dff4fceb498..c96b65b063eda 100644 --- a/src/librustc_mir/const_eval/fn_queries.rs +++ b/src/librustc_mir/const_eval/fn_queries.rs @@ -90,8 +90,7 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool { /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether /// said intrinsic is on the whitelist for being const callable. fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - let hir_id = - tcx.hir().as_local_hir_id(def_id).expect("Non-local call to local provider is_const_fn"); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let node = tcx.hir().get(hir_id); diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index d85e3bdedd4ff..ace69ad3ae8c0 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -429,7 +429,9 @@ fn check_recursion_limit<'tcx>( // infinite expansion. if adjusted_recursion_depth > *tcx.sess.recursion_limit.get() { let error = format!("reached the recursion limit while instantiating `{}`", instance); - if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + { tcx.sess.span_fatal(tcx.hir().span(hir_id), &error); } else { tcx.sess.fatal(&error); diff --git a/src/librustc_mir/transform/check_consts/mod.rs b/src/librustc_mir/transform/check_consts/mod.rs index bce3a506b1dd9..77ee6e3b29321 100644 --- a/src/librustc_mir/transform/check_consts/mod.rs +++ b/src/librustc_mir/transform/check_consts/mod.rs @@ -5,7 +5,7 @@ //! it finds operations that are invalid in a certain context. use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::mir; use rustc_middle::ty::{self, TyCtxt}; @@ -31,13 +31,13 @@ pub struct Item<'mir, 'tcx> { impl Item<'mir, 'tcx> { pub fn new( tcx: TyCtxt<'tcx>, - def_id: DefId, + def_id: LocalDefId, body: mir::ReadOnlyBodyAndCache<'mir, 'tcx>, ) -> Self { let param_env = tcx.param_env(def_id); let const_kind = ConstKind::for_item(tcx, def_id); - Item { body, tcx, def_id, param_env, const_kind } + Item { body, tcx, def_id: def_id.to_def_id(), param_env, const_kind } } /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.). @@ -64,7 +64,7 @@ pub enum ConstKind { impl ConstKind { /// Returns the validation mode for the item with the given `DefId`, or `None` if this item /// does not require validation (e.g. a non-const `fn`). - pub fn for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option { + pub fn for_item(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Option { use hir::BodyOwnerKind as HirKind; let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); diff --git a/src/librustc_mir/transform/check_consts/validation.rs b/src/librustc_mir/transform/check_consts/validation.rs index e4a0b9cdb48b1..f4ff675c09201 100644 --- a/src/librustc_mir/transform/check_consts/validation.rs +++ b/src/librustc_mir/transform/check_consts/validation.rs @@ -190,7 +190,7 @@ impl Validator<'a, 'mir, 'tcx> { const_kind == Some(ConstKind::Static) && !tcx.has_attr(def_id, sym::thread_local); if should_check_for_sync { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); check_return_ty_is_sync(tcx, &body, hir_id); } } diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 3ce9b875e16cc..4d46c1980db21 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -1,7 +1,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit; use rustc_hir::Node; use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor}; @@ -465,12 +465,11 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'a> { fn check_unused_unsafe( tcx: TyCtxt<'_>, - def_id: DefId, + def_id: LocalDefId, used_unsafe: &FxHashSet, unsafe_blocks: &mut Vec<(hir::HirId, bool)>, ) { - let body_id = - tcx.hir().as_local_hir_id(def_id).and_then(|hir_id| tcx.hir().maybe_body_owned_by(hir_id)); + let body_id = tcx.hir().maybe_body_owned_by(tcx.hir().as_local_hir_id(def_id).unwrap()); let body_id = match body_id { Some(body) => body, @@ -495,7 +494,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult let param_env = tcx.param_env(def_id); - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let (const_context, min_const_fn) = match tcx.hir().body_owner_kind(id) { hir::BodyOwnerKind::Closure => (false, false), hir::BodyOwnerKind::Fn => (is_const_fn(tcx, def_id), is_min_const_fn(tcx, def_id)), @@ -507,7 +506,12 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult let body = body.unwrap_read_only(); checker.visit_body(&body); - check_unused_unsafe(tcx, def_id, &checker.used_unsafe, &mut checker.inherited_blocks); + check_unused_unsafe( + tcx, + def_id.expect_local(), + &checker.used_unsafe, + &mut checker.inherited_blocks, + ); UnsafetyCheckResult { violations: checker.violations.into(), unsafe_blocks: checker.inherited_blocks.into(), @@ -515,10 +519,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult } fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: DefId) { - let lint_hir_id = tcx - .hir() - .as_local_hir_id(def_id) - .unwrap_or_else(|| bug!("checking unsafety for non-local def id {:?}", def_id)); + let lint_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); tcx.struct_span_lint_hir(SAFE_PACKED_BORROWS, lint_hir_id, tcx.def_span(def_id), |lint| { // FIXME: when we make this a hard error, this should have its diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 80c12a30135ff..434cad299235a 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -66,10 +66,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp { } use rustc_middle::hir::map::blocks::FnLikeNode; - let hir_id = tcx - .hir() - .as_local_hir_id(source.def_id()) - .expect("Non-local call to local provider is_const_fn"); + let hir_id = tcx.hir().as_local_hir_id(source.def_id().expect_local()).unwrap(); let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some(); let is_assoc_const = match tcx.def_kind(source.def_id()) { diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 8121d4ead1394..1702120121dbf 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -69,7 +69,7 @@ impl Inliner<'tcx> { let param_env = self.tcx.param_env(self.source.def_id()).with_reveal_all(); // Only do inlining into fn bodies. - let id = self.tcx.hir().as_local_hir_id(self.source.def_id()).unwrap(); + let id = self.tcx.hir().as_local_hir_id(self.source.def_id().expect_local()).unwrap(); if self.tcx.hir().body_owner_kind(id).is_fn_or_closure() && self.source.promoted.is_none() { for (bb, bb_data) in caller_body.basic_blocks().iter_enumerated() { if let Some(callsite) = diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs index 8aa371a383381..ebae5388fe790 100644 --- a/src/librustc_mir/transform/mod.rs +++ b/src/librustc_mir/transform/mod.rs @@ -176,7 +176,7 @@ pub fn run_passes( } fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs { - let const_kind = check_consts::ConstKind::for_item(tcx, def_id); + let const_kind = check_consts::ConstKind::for_item(tcx, def_id.expect_local()); // No need to const-check a non-const `fn`. if const_kind.is_none() { diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs index 71c2e3bf06095..3d85690ed5b2c 100644 --- a/src/librustc_mir/transform/promote_consts.rs +++ b/src/librustc_mir/transform/promote_consts.rs @@ -13,7 +13,7 @@ //! move analysis runs after promotion on broken MIR. use rustc_ast::ast::LitKind; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::mir::traversal::ReversePostorder; use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::*; @@ -64,8 +64,13 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> { let mut rpo = traversal::reverse_postorder(body); let (temps, all_candidates) = collect_temps_and_candidates(tcx, body, &mut rpo); - let promotable_candidates = - validate_candidates(tcx, read_only!(body), def_id, &temps, &all_candidates); + let promotable_candidates = validate_candidates( + tcx, + read_only!(body), + def_id.expect_local(), + &temps, + &all_candidates, + ); let promoted = promote_candidates(def_id, body, tcx, temps, promotable_candidates); self.promoted_fragments.set(promoted); @@ -720,7 +725,7 @@ impl<'tcx> Validator<'_, 'tcx> { pub fn validate_candidates( tcx: TyCtxt<'tcx>, body: ReadOnlyBodyAndCache<'_, 'tcx>, - def_id: DefId, + def_id: LocalDefId, temps: &IndexVec, candidates: &[Candidate], ) -> Vec { @@ -1155,8 +1160,11 @@ crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>( ) -> bool { let mut rpo = traversal::reverse_postorder(&body); let (temps, _) = collect_temps_and_candidates(tcx, &body, &mut rpo); - let validator = - Validator { item: Item::new(tcx, mir_def_id, body), temps: &temps, explicit: false }; + let validator = Validator { + item: Item::new(tcx, mir_def_id.expect_local(), body), + temps: &temps, + explicit: false, + }; let should_promote = validator.validate_operand(operand).is_ok(); let feature_flag = tcx.features().const_in_array_repeat_expressions; diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index 8f7a1b948e3fd..adffd6b84026d 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -13,7 +13,7 @@ type McfResult = Result<(), (Span, Cow<'static, str>)>; pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -> McfResult { // Prevent const trait methods from being annotated as `stable`. if tcx.features().staged_api { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) { return Err((body.span, "trait methods cannot be stable const fn".into())); } diff --git a/src/librustc_mir/util/liveness.rs b/src/librustc_mir/util/liveness.rs index a1b7634f0c618..e722f73b0b8fb 100644 --- a/src/librustc_mir/util/liveness.rs +++ b/src/librustc_mir/util/liveness.rs @@ -285,7 +285,7 @@ fn dump_matched_mir_node<'tcx>( ) { let mut file_path = PathBuf::new(); file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir)); - let item_id = tcx.hir().as_local_hir_id(source.def_id()).unwrap(); + let item_id = tcx.hir().as_local_hir_id(source.def_id().expect_local()).unwrap(); let file_name = format!("rustc.node{}{}-liveness.mir", item_id, pass_name); file_path.push(&file_name); let _ = fs::File::create(&file_path).and_then(|file| { diff --git a/src/librustc_mir_build/build/mod.rs b/src/librustc_mir_build/build/mod.rs index 5374000d5a8bb..5ec7d729862bc 100644 --- a/src/librustc_mir_build/build/mod.rs +++ b/src/librustc_mir_build/build/mod.rs @@ -21,11 +21,11 @@ use rustc_target::spec::PanicStrategy; use super::lints; crate fn mir_built(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::steal::Steal> { - tcx.alloc_steal_mir(mir_build(tcx, def_id)) + tcx.alloc_steal_mir(mir_build(tcx, def_id.expect_local())) } /// Construct the MIR for a given `DefId`. -fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> BodyAndCache<'_> { +fn mir_build(tcx: TyCtxt<'_>, def_id: LocalDefId) -> BodyAndCache<'_> { let id = tcx.hir().as_local_hir_id(def_id).unwrap(); // Figure out what primary body this item has. @@ -178,7 +178,7 @@ fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> BodyAndCache<'_> { build::construct_const(cx, body_id, return_ty, return_ty_span) }; - lints::check(tcx, &body, def_id); + lints::check(tcx, &body, def_id.to_def_id()); let mut body = BodyAndCache::new(body); body.ensure_predecessors(); diff --git a/src/librustc_mir_build/hair/cx/expr.rs b/src/librustc_mir_build/hair/cx/expr.rs index 73306d89a2e71..8edd3ba6f3056 100644 --- a/src/librustc_mir_build/hair/cx/expr.rs +++ b/src/librustc_mir_build/hair/cx/expr.rs @@ -690,7 +690,7 @@ fn convert_path_expr<'a, 'tcx>( } Res::Def(DefKind::ConstParam, def_id) => { - let hir_id = cx.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = cx.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let item_id = cx.tcx.hir().get_parent_node(hir_id); let item_def_id = cx.tcx.hir().local_def_id(item_id); let generics = cx.tcx.generics_of(item_def_id); diff --git a/src/librustc_mir_build/hair/pattern/check_match.rs b/src/librustc_mir_build/hair/pattern/check_match.rs index cdbcaea0befe8..14f36038232a6 100644 --- a/src/librustc_mir_build/hair/pattern/check_match.rs +++ b/src/librustc_mir_build/hair/pattern/check_match.rs @@ -21,9 +21,9 @@ use rustc_span::{sym, Span}; use std::slice; crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) { - let body_id = match tcx.hir().as_local_hir_id(def_id) { + let body_id = match def_id.as_local() { None => return, - Some(id) => tcx.hir().body_owned_by(id), + Some(id) => tcx.hir().body_owned_by(tcx.hir().as_local_hir_id(id).unwrap()), }; let mut visitor = diff --git a/src/librustc_mir_build/lints.rs b/src/librustc_mir_build/lints.rs index 948f1ae0b4277..6cfb8ac2e5154 100644 --- a/src/librustc_mir_build/lints.rs +++ b/src/librustc_mir_build/lints.rs @@ -11,7 +11,7 @@ use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION; use rustc_span::Span; crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); if let Some(fn_like_node) = FnLikeNode::from_node(tcx.hir().get(hir_id)) { if let FnKind::Closure(_) = fn_like_node.kind() { @@ -37,7 +37,7 @@ crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId) { vis.reachable_recursive_calls.sort(); - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span(hir_id)); tcx.struct_span_lint_hir(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| { let mut db = lint.build("function cannot return without recursing"); diff --git a/src/librustc_passes/dead.rs b/src/librustc_passes/dead.rs index 830bd255dfc3f..6389920ba37c3 100644 --- a/src/librustc_passes/dead.rs +++ b/src/librustc_passes/dead.rs @@ -50,7 +50,8 @@ struct MarkSymbolVisitor<'a, 'tcx> { impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { fn check_def_id(&mut self, def_id: DefId) { - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) { + if let Some(def_id) = def_id.as_local() { + let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); if should_explore(self.tcx, hir_id) || self.struct_constructors.contains_key(&hir_id) { self.worklist.push(hir_id); } @@ -59,7 +60,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { } fn insert_def_id(&mut self, def_id: DefId) { - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) { + if let Some(def_id) = def_id.as_local() { + let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); debug_assert!(!should_explore(self.tcx, hir_id)); self.live_symbols.insert(hir_id); } @@ -448,7 +450,8 @@ fn create_and_seed_worklist<'tcx>( ) .chain( // Seed entry point - tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id).unwrap()), + tcx.entry_fn(LOCAL_CRATE) + .map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap()), ) .collect::>(); @@ -532,7 +535,9 @@ impl DeadVisitor<'tcx> { let inherent_impls = self.tcx.inherent_impls(def_id); for &impl_did in inherent_impls.iter() { for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] { - if let Some(item_hir_id) = self.tcx.hir().as_local_hir_id(item_did) { + if let Some(item_hir_id) = + item_did.as_local().map(|did| self.tcx.hir().as_local_hir_id(did).unwrap()) + { if self.live_symbols.contains(&item_hir_id) { return true; } diff --git a/src/librustc_passes/reachable.rs b/src/librustc_passes/reachable.rs index 5114b1bd7472f..a98b2b6f400ef 100644 --- a/src/librustc_passes/reachable.rs +++ b/src/librustc_passes/reachable.rs @@ -53,7 +53,7 @@ fn method_might_be_inlined( return true; } } - if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src.to_def_id()) { + if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) { match tcx.hir().find(impl_hir_id) { Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs), Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"), @@ -108,9 +108,11 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> { } Some(res) => { if let Some((hir_id, def_id)) = res.opt_def_id().and_then(|def_id| { - self.tcx.hir().as_local_hir_id(def_id).map(|hir_id| (hir_id, def_id)) + def_id + .as_local() + .map(|def_id| (self.tcx.hir().as_local_hir_id(def_id).unwrap(), def_id)) }) { - if self.def_id_represents_local_inlined_item(def_id) { + if self.def_id_represents_local_inlined_item(def_id.to_def_id()) { self.worklist.push(hir_id); } else { match res { @@ -141,8 +143,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> { // Returns true if the given def ID represents a local item that is // eligible for inlining and false otherwise. fn def_id_represents_local_inlined_item(&self, def_id: DefId) -> bool { - let hir_id = match self.tcx.hir().as_local_hir_id(def_id) { - Some(hir_id) => hir_id, + let hir_id = match def_id.as_local() { + Some(def_id) => self.tcx.hir().as_local_hir_id(def_id).unwrap(), None => { return false; } @@ -170,7 +172,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> { if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() { true } else { - let impl_did = self.tcx.hir().get_parent_did(hir_id).to_def_id(); + let impl_did = self.tcx.hir().get_parent_did(hir_id); // Check the impl. If the generics on the self // type of the impl require inlining, this method // does too. @@ -360,8 +362,9 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx // FIXME(#53488) remove `let` let tcx = self.tcx; self.worklist.extend( - tcx.provided_trait_methods(trait_def_id) - .map(|assoc| tcx.hir().as_local_hir_id(assoc.def_id).unwrap()), + tcx.provided_trait_methods(trait_def_id).map(|assoc| { + tcx.hir().as_local_hir_id(assoc.def_id.expect_local()).unwrap() + }), ); } } @@ -400,7 +403,8 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, crate_num: CrateNum) -> &'tcx HirIdSet reachable_context.worklist.extend(access_levels.map.iter().map(|(id, _)| *id)); for item in tcx.lang_items().items().iter() { if let Some(did) = *item { - if let Some(hir_id) = tcx.hir().as_local_hir_id(did) { + if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did).unwrap()) + { reachable_context.worklist.push(hir_id); } } diff --git a/src/librustc_passes/region.rs b/src/librustc_passes/region.rs index 4c02127c10669..7fb534def3b8b 100644 --- a/src/librustc_passes/region.rs +++ b/src/librustc_passes/region.rs @@ -805,7 +805,7 @@ fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree { return tcx.region_scope_tree(closure_base_def_id); } - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(id) { let mut visitor = RegionResolutionVisitor { tcx, diff --git a/src/librustc_passes/upvars.rs b/src/librustc_passes/upvars.rs index fd046d9b66bd4..0cdc4f5bd8f74 100644 --- a/src/librustc_passes/upvars.rs +++ b/src/librustc_passes/upvars.rs @@ -15,7 +15,7 @@ pub fn provide(providers: &mut Providers<'_>) { return None; } - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(hir_id)?); let mut local_collector = LocalCollector::default(); diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index e8d016aa07a8d..c268189c5b2e7 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -235,7 +235,7 @@ fn def_id_visibility<'tcx>( tcx: TyCtxt<'tcx>, def_id: DefId, ) -> (ty::Visibility, Span, &'static str) { - match tcx.hir().as_local_hir_id(def_id) { + match def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) { Some(hir_id) => { let vis = match tcx.hir().get(hir_id) { Node::Item(item) => &item.vis, @@ -445,7 +445,9 @@ impl VisibilityLike for Option { const SHALLOW: bool = true; fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self { cmp::min( - if let Some(hir_id) = find.tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = + def_id.as_local().map(|def_id| find.tcx.hir().as_local_hir_id(def_id).unwrap()) + { find.access_levels.map.get(&hir_id).cloned() } else { Self::MAX @@ -532,7 +534,7 @@ impl EmbargoVisitor<'tcx> { fn update_macro_reachable_mod(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) { let module_def_id = self.tcx.hir().local_def_id(reachable_mod); - let module = self.tcx.hir().get_module(module_def_id.to_def_id()).0; + let module = self.tcx.hir().get_module(module_def_id).0; for item_id in module.item_ids { let hir_id = item_id.id; let item_def_id = self.tcx.hir().local_def_id(hir_id); @@ -547,7 +549,10 @@ impl EmbargoVisitor<'tcx> { if export.vis.is_accessible_from(defining_mod, self.tcx) { if let Res::Def(def_kind, def_id) = export.res { let vis = def_id_visibility(self.tcx, def_id).0; - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = def_id + .as_local() + .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + { self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod); } } @@ -654,7 +659,9 @@ impl EmbargoVisitor<'tcx> { // If the module is `self`, i.e. the current crate, // there will be no corresponding item. .filter(|def_id| def_id.index != CRATE_DEF_INDEX || def_id.krate != LOCAL_CRATE) - .and_then(|def_id| self.tcx.hir().as_local_hir_id(def_id)) + .and_then(|def_id| { + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + }) .map(|module_hir_id| self.tcx.hir().expect_item(module_hir_id)) { if let hir::ItemKind::Mod(m) = &item.kind { @@ -908,7 +915,10 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> { for export in exports.iter() { if export.vis == ty::Visibility::Public { if let Some(def_id) = export.res.opt_def_id() { - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = def_id + .as_local() + .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + { self.update(hir_id, Some(AccessLevel::Exported)); } } @@ -996,10 +1006,11 @@ impl DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { self.ev.tcx } fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool { - if let Some(hir_id) = self.ev.tcx.hir().as_local_hir_id(def_id) { + if let Some(def_id) = def_id.as_local() { + let hir_id = self.ev.tcx.hir().as_local_hir_id(def_id).unwrap(); if let ((ty::Visibility::Public, ..), _) | (_, Some(AccessLevel::ReachableFromImplTrait)) = - (def_id_visibility(self.tcx(), def_id), self.access_level) + (def_id_visibility(self.tcx(), def_id.to_def_id()), self.access_level) { self.ev.update(hir_id, self.access_level); } @@ -1443,10 +1454,10 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { // A path can only be private if: // it's in this crate... - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(did) { + if let Some(did) = did.as_local() { // .. and it corresponds to a private type in the AST (this returns // `None` for type parameters). - match self.tcx.hir().find(hir_id) { + match self.tcx.hir().find(self.tcx.hir().as_local_hir_id(did).unwrap()) { Some(Node::Item(ref item)) => !item.vis.node.is_pub(), Some(_) | None => false, } @@ -1564,8 +1575,8 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { |tr| { let did = tr.path.res.def_id(); - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(did) { - self.trait_is_public(hir_id) + if let Some(did) = did.as_local() { + self.trait_is_public(self.tcx.hir().as_local_hir_id(did).unwrap()) } else { true // external traits must be public } @@ -1825,8 +1836,8 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> { ); } - let hir_id = match self.tcx.hir().as_local_hir_id(def_id) { - Some(hir_id) => hir_id, + let hir_id = match def_id.as_local() { + Some(def_id) => self.tcx.hir().as_local_hir_id(def_id).unwrap(), None => return false, }; @@ -2070,7 +2081,7 @@ fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: DefId) { current_item: None, empty_tables: &empty_tables, }; - let (module, span, hir_id) = tcx.hir().get_module(module_def_id); + let (module, span, hir_id) = tcx.hir().get_module(module_def_id.expect_local()); intravisit::walk_mod(&mut visitor, module, hir_id); diff --git a/src/librustc_resolve/late/lifetimes.rs b/src/librustc_resolve/late/lifetimes.rs index 98a8ab9a7665a..a7225aa36da05 100644 --- a/src/librustc_resolve/late/lifetimes.rs +++ b/src/librustc_resolve/late/lifetimes.rs @@ -596,7 +596,10 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { // In the future, this should be fixed and this error should be removed. let def = self.map.defs.get(&lifetime.hir_id).cloned(); if let Some(Region::LateBound(_, def_id, _)) = def { - if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = def_id + .as_local() + .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + { // Ensure that the parent of the def is an item, not HRTB let parent_id = self.tcx.hir().get_parent_node(hir_id); let parent_impl_id = hir::ImplItemId { hir_id: parent_id }; @@ -1166,7 +1169,8 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) { if let Some(def) = lifetimes.get(&hir::ParamName::Plain(label.normalize_to_macros_2_0())) { - let hir_id = tcx.hir().as_local_hir_id(def.id().unwrap()).unwrap(); + let hir_id = + tcx.hir().as_local_hir_id(def.id().unwrap().expect_local()).unwrap(); signal_shadowing_problem( tcx, @@ -1537,7 +1541,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { match lifetimeuseset { Some(LifetimeUseSet::One(lifetime)) => { - let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); debug!("hir id first={:?}", hir_id); if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { Node::Lifetime(hir_lifetime) => Some(( @@ -1556,8 +1560,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } if let Some(parent_def_id) = self.tcx.parent(def_id) { - if let Some(parent_hir_id) = - self.tcx.hir().as_local_hir_id(parent_def_id) + if let Some(parent_hir_id) = parent_def_id + .as_local() + .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) { // lifetimes in `derive` expansions don't count (Issue #53738) if self @@ -1600,7 +1605,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { debug!("not one use lifetime"); } None => { - let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { Node::Lifetime(hir_lifetime) => Some(( hir_lifetime.hir_id, @@ -1950,7 +1955,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { }; let map = &self.map; - let unsubst = if let Some(id) = self.tcx.hir().as_local_hir_id(def_id) { + let unsubst = if let Some(id) = + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + { &map.object_lifetime_defaults[&id] } else { let tcx = self.tcx; @@ -2658,7 +2665,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Scope::Binder { ref lifetimes, s, .. } => { if let Some(&def) = lifetimes.get(¶m.name.normalize_to_macros_2_0()) { - let hir_id = self.tcx.hir().as_local_hir_id(def.id().unwrap()).unwrap(); + let hir_id = self + .tcx + .hir() + .as_local_hir_id(def.id().unwrap().expect_local()) + .unwrap(); signal_shadowing_problem( self.tcx, diff --git a/src/librustc_symbol_mangling/lib.rs b/src/librustc_symbol_mangling/lib.rs index 2ee2ca3ce2881..d16d61454249e 100644 --- a/src/librustc_symbol_mangling/lib.rs +++ b/src/librustc_symbol_mangling/lib.rs @@ -164,22 +164,18 @@ fn compute_symbol_name( debug!("symbol_name(def_id={:?}, substs={:?})", def_id, substs); - let hir_id = tcx.hir().as_local_hir_id(def_id); - - if def_id.is_local() { - if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) { + // FIXME(eddyb) Precompute a custom symbol name based on attributes. + let is_foreign = if let Some(def_id) = def_id.as_local() { + if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id.to_def_id()) { let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_plugin_registrar_symbol(disambiguator); } - if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) { + if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id.to_def_id()) { let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_proc_macro_decls_symbol(disambiguator); } - } - - // FIXME(eddyb) Precompute a custom symbol name based on attributes. - let is_foreign = if let Some(id) = hir_id { - match tcx.hir().get(id) { + let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + match tcx.hir().get(hir_id) { Node::ForeignItem(_) => true, _ => false, } diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index 7c4e2155cb48f..92070731e7405 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -3,7 +3,7 @@ use crate::traits::{self, PredicateObligation}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sync::Lrc; use rustc_hir as hir; -use rustc_hir::def_id::{DefId, DefIdMap}; +use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId}; use rustc_hir::Node; use rustc_infer::infer::error_reporting::unexpected_hidden_region_diagnostic; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; @@ -1036,7 +1036,9 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { // let x = || foo(); // returns the Opaque assoc with `foo` // } // ``` - if let Some(opaque_hir_id) = tcx.hir().as_local_hir_id(def_id) { + if let Some(opaque_hir_id) = + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + { let parent_def_id = self.parent_def_id; let def_scope_default = || { let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id); @@ -1057,14 +1059,22 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { origin, .. }) => ( - may_define_opaque_type(tcx, self.parent_def_id, opaque_hir_id), + may_define_opaque_type( + tcx, + self.parent_def_id.expect_local(), + opaque_hir_id, + ), origin, ), _ => (def_scope_default(), hir::OpaqueTyOrigin::TypeAlias), }, Some(Node::ImplItem(item)) => match item.kind { hir::ImplItemKind::OpaqueTy(_) => ( - may_define_opaque_type(tcx, self.parent_def_id, opaque_hir_id), + may_define_opaque_type( + tcx, + self.parent_def_id.expect_local(), + opaque_hir_id, + ), hir::OpaqueTyOrigin::TypeAlias, ), _ => (def_scope_default(), hir::OpaqueTyOrigin::TypeAlias), @@ -1201,10 +1211,14 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { /// } /// ``` /// -/// Here, `def_id` is the `DefId` of the defining use of the opaque type (e.g., `f1` or `f2`), +/// Here, `def_id` is the `LocalDefId` of the defining use of the opaque type (e.g., `f1` or `f2`), /// and `opaque_hir_id` is the `HirId` of the definition of the opaque type `Baz`. /// For the above example, this function returns `true` for `f1` and `false` for `f2`. -pub fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: DefId, opaque_hir_id: hir::HirId) -> bool { +pub fn may_define_opaque_type( + tcx: TyCtxt<'_>, + def_id: LocalDefId, + opaque_hir_id: hir::HirId, +) -> bool { let mut hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); // Named opaque types can be defined by any siblings or children of siblings. diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index fef7adf02246b..101b53bffa633 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -486,7 +486,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { self.tcx.sess.source_map().guess_head_span( self.tcx.hir().span_if_local(closure_def_id).unwrap(), ); - let hir_id = self.tcx.hir().as_local_hir_id(closure_def_id).unwrap(); + let hir_id = + self.tcx.hir().as_local_hir_id(closure_def_id.expect_local()).unwrap(); let mut err = struct_span_err!( self.tcx.sess, closure_span, diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 254db6cb869b1..e006ac3f3f651 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -427,7 +427,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { }; let hir = self.tcx.hir(); - let hir_id = hir.as_local_hir_id(def_id)?; + let hir_id = hir.as_local_hir_id(def_id.as_local()?)?; let parent_node = hir.get_parent_node(hir_id); match hir.find(parent_node) { Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => { @@ -1179,7 +1179,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let span = self.tcx.def_span(generator_did); // Do not ICE on closure typeck (#66868). - if hir.as_local_hir_id(generator_did).is_none() { + if !generator_did.is_local() { return false; } @@ -1205,8 +1205,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { } }; - let generator_body = hir - .as_local_hir_id(generator_did) + let generator_body = generator_did + .as_local() + .map(|def_id| hir.as_local_hir_id(def_id).unwrap()) .and_then(|hir_id| hir.maybe_body_owned_by(hir_id)) .map(|body_id| hir.body(body_id)); let mut visitor = AwaitsVisitor::default(); @@ -1347,6 +1348,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { GeneratorKind::Async(AsyncGeneratorKind::Fn) => self .tcx .parent(generator_did) + .and_then(|parent_did| parent_did.as_local()) .and_then(|parent_did| hir.as_local_hir_id(parent_did)) .and_then(|parent_hir_id| hir.opt_name(parent_hir_id)) .map(|name| { diff --git a/src/librustc_trait_selection/traits/specialize/mod.rs b/src/librustc_trait_selection/traits/specialize/mod.rs index fabd8c89b72af..95bc64fdaff23 100644 --- a/src/librustc_trait_selection/traits/specialize/mod.rs +++ b/src/librustc_trait_selection/traits/specialize/mod.rs @@ -17,7 +17,7 @@ use crate::traits::select::IntercrateAmbiguityCause; use crate::traits::{self, coherence, FutureCompatOverlapErrorKind, ObligationCause, TraitEngine}; use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::lint::LintDiagnosticBuilder; use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef}; use rustc_middle::ty::{self, TyCtxt}; @@ -271,9 +271,9 @@ pub(super) fn specialization_graph_provider( .sort_unstable_by_key(|def_id| (-(def_id.krate.as_u32() as i64), def_id.index.index())); for impl_def_id in trait_impls { - if impl_def_id.is_local() { + if let Some(impl_def_id) = impl_def_id.as_local() { // This is where impl overlap checking happens: - let insert_result = sg.insert(tcx, impl_def_id); + let insert_result = sg.insert(tcx, impl_def_id.to_def_id()); // Report error if there was one. let (overlap, used_to_be_allowed) = match insert_result { Err(overlap) => (Some(overlap), None), @@ -296,11 +296,11 @@ pub(super) fn specialization_graph_provider( fn report_overlap_conflict( tcx: TyCtxt<'_>, overlap: OverlapError, - impl_def_id: DefId, + impl_def_id: LocalDefId, used_to_be_allowed: Option, sg: &mut specialization_graph::Graph, ) { - let impl_polarity = tcx.impl_polarity(impl_def_id); + let impl_polarity = tcx.impl_polarity(impl_def_id.to_def_id()); let other_polarity = tcx.impl_polarity(overlap.with_impl); match (impl_polarity, other_polarity) { (ty::ImplPolarity::Negative, ty::ImplPolarity::Positive) => { @@ -308,7 +308,7 @@ fn report_overlap_conflict( tcx, &overlap, impl_def_id, - impl_def_id, + impl_def_id.to_def_id(), overlap.with_impl, sg, ); @@ -320,7 +320,7 @@ fn report_overlap_conflict( &overlap, impl_def_id, overlap.with_impl, - impl_def_id, + impl_def_id.to_def_id(), sg, ); } @@ -334,13 +334,15 @@ fn report_overlap_conflict( fn report_negative_positive_conflict( tcx: TyCtxt<'_>, overlap: &OverlapError, - local_impl_def_id: DefId, + local_impl_def_id: LocalDefId, negative_impl_def_id: DefId, positive_impl_def_id: DefId, sg: &mut specialization_graph::Graph, ) { - let impl_span = - tcx.sess.source_map().guess_head_span(tcx.span_of_impl(local_impl_def_id).unwrap()); + let impl_span = tcx + .sess + .source_map() + .guess_head_span(tcx.span_of_impl(local_impl_def_id.to_def_id()).unwrap()); let mut err = struct_span_err!( tcx.sess, @@ -382,11 +384,12 @@ fn report_negative_positive_conflict( fn report_conflicting_impls( tcx: TyCtxt<'_>, overlap: OverlapError, - impl_def_id: DefId, + impl_def_id: LocalDefId, used_to_be_allowed: Option, sg: &mut specialization_graph::Graph, ) { - let impl_span = tcx.sess.source_map().guess_head_span(tcx.span_of_impl(impl_def_id).unwrap()); + let impl_span = + tcx.sess.source_map().guess_head_span(tcx.span_of_impl(impl_def_id.to_def_id()).unwrap()); // Work to be done after we've built the DiagnosticBuilder. We have to define it // now because the struct_lint methods don't return back the DiagnosticBuilder diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index 63a6720b97daf..60c6e761a1cef 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -142,8 +142,10 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( pred: &ty::Predicate<'_>, mut trait_assoc_items: impl Iterator, ) { - let trait_item = - tcx.hir().as_local_hir_id(trait_ref.def_id).and_then(|trait_id| tcx.hir().find(trait_id)); + let trait_item = trait_ref + .def_id + .as_local() + .and_then(|def_id| tcx.hir().find(tcx.hir().as_local_hir_id(def_id).unwrap())); let (trait_name, trait_generics) = match trait_item { Some(hir::Node::Item(hir::Item { ident, diff --git a/src/librustc_traits/lowering/environment.rs b/src/librustc_traits/lowering/environment.rs index 612ab9b70ebc9..b598b6f4fb4e4 100644 --- a/src/librustc_traits/lowering/environment.rs +++ b/src/librustc_traits/lowering/environment.rs @@ -174,7 +174,7 @@ crate fn environment(tcx: TyCtxt<'_>, def_id: DefId) -> Environment<'_> { // could bound lifetimes. .map(Clause::ForAll); - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let node = tcx.hir().get(hir_id); enum NodeKind { diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index a3bf297db80b8..1e215ebc42f1e 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -128,7 +128,7 @@ fn associated_item_from_impl_item_ref( } fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let parent_id = tcx.hir().get_parent_item(id); let parent_def_id = tcx.hir().local_def_id(parent_id); let parent_item = tcx.hir().expect_item(parent_id); @@ -166,7 +166,7 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { } fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let item = tcx.hir().expect_item(hir_id); if let hir::ItemKind::Impl { defaultness, .. } = item.kind { defaultness @@ -200,7 +200,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain } fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let item = tcx.hir().expect_item(id); match item.kind { hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter( @@ -265,9 +265,12 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { let unnormalized_env = ty::ParamEnv::new(tcx.intern_predicates(&predicates), traits::Reveal::UserFacing, None); - let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::CRATE_HIR_ID, |id| { - tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id) - }); + let body_id = def_id + .as_local() + .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + .map_or(hir::CRATE_HIR_ID, |id| { + tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id) + }); let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id); traits::normalize_param_env_or_error(tcx, def_id, unnormalized_env, cause) } @@ -352,10 +355,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { /// Check if a function is async. fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync { - let hir_id = tcx - .hir() - .as_local_hir_id(def_id) - .unwrap_or_else(|| bug!("asyncness: expected local `DefId`, got `{:?}`", def_id)); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let node = tcx.hir().get(hir_id); diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index b8cd850717526..5b2e0935acc04 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -15,7 +15,7 @@ use rustc_errors::ErrorReported; use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, FatalError}; use rustc_hir as hir; use rustc_hir::def::{CtorOf, DefKind, Namespace, Res}; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::{walk_generics, Visitor as _}; use rustc_hir::lang_items::SizedTraitLangItem; use rustc_hir::{Constness, GenericArg, GenericArgs}; @@ -153,7 +153,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { Some(rl::Region::Static) => tcx.lifetimes.re_static, Some(rl::Region::LateBound(debruijn, id, _)) => { - let name = lifetime_name(id); + let name = lifetime_name(id.expect_local()); tcx.mk_region(ty::ReLateBound(debruijn, ty::BrNamed(id, name))) } @@ -162,12 +162,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } Some(rl::Region::EarlyBound(index, id, _)) => { - let name = lifetime_name(id); + let name = lifetime_name(id.expect_local()); tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion { def_id: id, index, name })) } Some(rl::Region::Free(scope, id)) => { - let name = lifetime_name(id); + let name = lifetime_name(id.expect_local()); tcx.mk_region(ty::ReFree(ty::FreeRegion { scope, bound_region: ty::BrNamed(id, name), @@ -1972,7 +1972,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { // any ambiguity. fn find_bound_for_assoc_item( &self, - ty_param_def_id: DefId, + ty_param_def_id: LocalDefId, assoc_name: ast::Ident, span: Span, ) -> Result, ErrorReported> { @@ -1983,7 +1983,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { ty_param_def_id, assoc_name, span, ); - let predicates = &self.get_type_parameter_bounds(span, ty_param_def_id).predicates; + let predicates = + &self.get_type_parameter_bounds(span, ty_param_def_id.to_def_id()).predicates; debug!("find_bound_for_assoc_item: predicates={:#?}", predicates); @@ -2233,7 +2234,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } (&ty::Param(_), Res::SelfTy(Some(param_did), None)) | (&ty::Param(_), Res::Def(DefKind::TyParam, param_did)) => { - self.find_bound_for_assoc_item(param_did, assoc_ident, span)? + self.find_bound_for_assoc_item(param_did.expect_local(), assoc_ident, span)? } _ => { if variant_resolution.is_some() { @@ -2370,7 +2371,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { debug!("qpath_to_ty: self.item_def_id()={:?}", def_id); let parent_def_id = def_id - .and_then(|def_id| tcx.hir().as_local_hir_id(def_id)) + .and_then(|def_id| { + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + }) .map(|hir_id| tcx.hir().get_parent_did(hir_id).to_def_id()); debug!("qpath_to_ty: parent_def_id={:?}", parent_def_id); @@ -2661,7 +2664,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { assert_eq!(opt_self_ty, None); self.prohibit_generics(path.segments); - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let item_id = tcx.hir().get_parent_node(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id); diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index ff3493eb6de6d..6ebd7cea8804b 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -1394,7 +1394,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { let ty = AstConv::ast_ty_to_ty(fcx, ty); // Get the `impl Trait`'s `DefId`. if let ty::Opaque(def_id, _) = ty.kind { - let hir_id = fcx.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = fcx.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); // Get the `impl Trait`'s `Item` so that we can get its trait bounds and // get the `Trait`'s `DefId`. if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) = diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index 82c8a5543eb33..dbdb26afaec2d 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -76,7 +76,7 @@ fn compare_predicate_entailment<'tcx>( // This node-id should be used for the `body_id` field on each // `ObligationCause` (and the `FnCtxt`). This is what // `regionck_item` expects. - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id).unwrap(); + let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()).unwrap(); let cause = ObligationCause { span: impl_m_span, @@ -399,7 +399,7 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( trait_sig: ty::FnSig<'tcx>, ) -> (Span, Option) { let tcx = infcx.tcx; - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id).unwrap(); + let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()).unwrap(); let (impl_m_output, impl_m_iter) = match tcx.hir().expect_impl_item(impl_m_hir_id).kind { ImplItemKind::Fn(ref impl_m_sig, _) => { (&impl_m_sig.decl.output, impl_m_sig.decl.inputs.iter()) @@ -409,7 +409,9 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( match *terr { TypeError::Mutability => { - if let Some(trait_m_hir_id) = tcx.hir().as_local_hir_id(trait_m.def_id) { + if let Some(trait_m_hir_id) = + trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + { let trait_m_iter = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => trait_m_sig.decl.inputs.iter(), _ => bug!("{:?} is not a TraitItemKind::Fn", trait_m), @@ -436,7 +438,9 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( } } TypeError::Sorts(ExpectedFound { .. }) => { - if let Some(trait_m_hir_id) = tcx.hir().as_local_hir_id(trait_m.def_id) { + if let Some(trait_m_hir_id) = + trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + { let (trait_m_output, trait_m_iter) = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => { @@ -587,33 +591,34 @@ fn compare_number_of_generics<'tcx>( if impl_count != trait_count { err_occurred = true; - let (trait_spans, impl_trait_spans) = - if let Some(trait_hir_id) = tcx.hir().as_local_hir_id(trait_.def_id) { - let trait_item = tcx.hir().expect_trait_item(trait_hir_id); - if trait_item.generics.params.is_empty() { - (Some(vec![trait_item.generics.span]), vec![]) - } else { - let arg_spans: Vec = - trait_item.generics.params.iter().map(|p| p.span).collect(); - let impl_trait_spans: Vec = trait_item - .generics - .params - .iter() - .filter_map(|p| match p.kind { - GenericParamKind::Type { - synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), - .. - } => Some(p.span), - _ => None, - }) - .collect(); - (Some(arg_spans), impl_trait_spans) - } + let (trait_spans, impl_trait_spans) = if let Some(trait_hir_id) = + trait_.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + { + let trait_item = tcx.hir().expect_trait_item(trait_hir_id); + if trait_item.generics.params.is_empty() { + (Some(vec![trait_item.generics.span]), vec![]) } else { - (trait_span.map(|s| vec![s]), vec![]) - }; + let arg_spans: Vec = + trait_item.generics.params.iter().map(|p| p.span).collect(); + let impl_trait_spans: Vec = trait_item + .generics + .params + .iter() + .filter_map(|p| match p.kind { + GenericParamKind::Type { + synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), + .. + } => Some(p.span), + _ => None, + }) + .collect(); + (Some(arg_spans), impl_trait_spans) + } + } else { + (trait_span.map(|s| vec![s]), vec![]) + }; - let impl_hir_id = tcx.hir().as_local_hir_id(impl_.def_id).unwrap(); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_.def_id.expect_local()).unwrap(); let impl_item = tcx.hir().expect_impl_item(impl_hir_id); let impl_item_impl_trait_spans: Vec = impl_item .generics @@ -704,8 +709,8 @@ fn compare_number_of_method_arguments<'tcx>( let trait_number_args = trait_m_fty.inputs().skip_binder().len(); let impl_number_args = impl_m_fty.inputs().skip_binder().len(); if trait_number_args != impl_number_args { - let trait_m_hir_id = tcx.hir().as_local_hir_id(trait_m.def_id); - let trait_span = if let Some(trait_id) = trait_m_hir_id { + let trait_span = if let Some(def_id) = trait_m.def_id.as_local() { + let trait_id = tcx.hir().as_local_hir_id(def_id).unwrap(); match tcx.hir().expect_trait_item(trait_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => { let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 }; @@ -728,7 +733,7 @@ fn compare_number_of_method_arguments<'tcx>( } else { trait_item_span }; - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id).unwrap(); + let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()).unwrap(); let impl_span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind { ImplItemKind::Fn(ref impl_m_sig, _) => { let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 }; @@ -810,7 +815,7 @@ fn compare_synthetic_generics<'tcx>( impl_m_type_params.zip(trait_m_type_params) { if impl_synthetic != trait_synthetic { - let impl_hir_id = tcx.hir().as_local_hir_id(impl_def_id).unwrap(); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_def_id.expect_local()).unwrap(); let impl_span = tcx.hir().span(impl_hir_id); let trait_span = tcx.def_span(trait_def_id); let mut err = struct_span_err!( @@ -831,10 +836,10 @@ fn compare_synthetic_generics<'tcx>( // FIXME: this is obviously suboptimal since the name can already be used // as another generic argument let new_name = tcx.sess.source_map().span_to_snippet(trait_span).ok()?; - let trait_m = tcx.hir().as_local_hir_id(trait_m.def_id)?; + let trait_m = tcx.hir().as_local_hir_id(trait_m.def_id.as_local()?)?; let trait_m = tcx.hir().trait_item(hir::TraitItemId { hir_id: trait_m }); - let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id)?; + let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?)?; let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m }); // in case there are no generics, take the spot between the function name @@ -868,7 +873,7 @@ fn compare_synthetic_generics<'tcx>( (None, Some(hir::SyntheticTyParamKind::ImplTrait)) => { err.span_label(impl_span, "expected `impl Trait`, found generic parameter"); (|| { - let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id)?; + let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?)?; let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m }); let input_tys = match impl_m.kind { hir::ImplItemKind::Fn(ref sig, _) => sig.decl.inputs, @@ -961,7 +966,7 @@ crate fn compare_const_impl<'tcx>( // Create a parameter environment that represents the implementation's // method. - let impl_c_hir_id = tcx.hir().as_local_hir_id(impl_c.def_id).unwrap(); + let impl_c_hir_id = tcx.hir().as_local_hir_id(impl_c.def_id.expect_local()).unwrap(); // Compute placeholder form of impl and trait const tys. let impl_ty = tcx.type_of(impl_c.def_id); @@ -1005,7 +1010,8 @@ crate fn compare_const_impl<'tcx>( trait_c.ident ); - let trait_c_hir_id = tcx.hir().as_local_hir_id(trait_c.def_id); + let trait_c_hir_id = + trait_c.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()); let trait_c_span = trait_c_hir_id.map(|trait_c_hir_id| { // Add a label to the Span containing just the type of the const match tcx.hir().expect_trait_item(trait_c_hir_id).kind { @@ -1093,7 +1099,7 @@ fn compare_type_predicate_entailment( // This `HirId` should be used for the `body_id` field on each // `ObligationCause` (and the `FnCtxt`). This is what // `regionck_item` expects. - let impl_ty_hir_id = tcx.hir().as_local_hir_id(impl_ty.def_id).unwrap(); + let impl_ty_hir_id = tcx.hir().as_local_hir_id(impl_ty.def_id.expect_local()).unwrap(); let cause = ObligationCause { span: impl_ty_span, body_id: impl_ty_hir_id, diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index 72220d93d929d..40efbd6f95c28 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -1,6 +1,6 @@ use crate::check::regionck::RegionCtxt; use crate::hir; -use crate::hir::def_id::DefId; +use crate::hir::def_id::{DefId, LocalDefId}; use rustc_errors::{struct_span_err, ErrorReported}; use rustc_infer::infer::outlives::env::OutlivesEnvironment; use rustc_infer::infer::{InferOk, RegionckMode, TyCtxtInferExt}; @@ -39,7 +39,7 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro ty::Adt(adt_def, self_to_impl_substs) => { ensure_drop_params_and_item_params_correspond( tcx, - drop_impl_did, + drop_impl_did.expect_local(), dtor_self_type, adt_def.did, )?; @@ -47,7 +47,7 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro ensure_drop_predicates_are_implied_by_item_defn( tcx, dtor_predicates, - adt_def.did, + adt_def.did.expect_local(), self_to_impl_substs, ) } @@ -67,7 +67,7 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro fn ensure_drop_params_and_item_params_correspond<'tcx>( tcx: TyCtxt<'tcx>, - drop_impl_did: DefId, + drop_impl_did: LocalDefId, drop_impl_ty: Ty<'tcx>, self_type_did: DefId, ) -> Result<(), ErrorReported> { @@ -83,7 +83,8 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>( let named_type = tcx.type_of(self_type_did); let drop_impl_span = tcx.def_span(drop_impl_did); - let fresh_impl_substs = infcx.fresh_substs_for_item(drop_impl_span, drop_impl_did); + let fresh_impl_substs = + infcx.fresh_substs_for_item(drop_impl_span, drop_impl_did.to_def_id()); let fresh_impl_self_ty = drop_impl_ty.subst(tcx, fresh_impl_substs); let cause = &ObligationCause::misc(drop_impl_span, drop_impl_hir_id); @@ -135,7 +136,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>( let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty()); infcx.resolve_regions_and_report_errors( - drop_impl_did, + drop_impl_did.to_def_id(), ®ion_scope_tree, &outlives_env, RegionckMode::default(), @@ -149,7 +150,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>( fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( tcx: TyCtxt<'tcx>, dtor_predicates: ty::GenericPredicates<'tcx>, - self_type_did: DefId, + self_type_did: LocalDefId, self_to_impl_substs: SubstsRef<'tcx>, ) -> Result<(), ErrorReported> { let mut result = Ok(()); @@ -243,8 +244,10 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( if !assumptions_in_impl_context.iter().any(predicate_matches_closure) { let item_span = tcx.hir().span(self_type_hir_id); - let self_descr = - tcx.def_kind(self_type_did).map(|kind| kind.descr(self_type_did)).unwrap_or("type"); + let self_descr = tcx + .def_kind(self_type_did) + .map(|kind| kind.descr(self_type_did.to_def_id())) + .unwrap_or("type"); struct_span_err!( tcx.sess, *predicate_sp, diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 7cb51b4d6d833..bb2bb74340e00 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1624,8 +1624,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return; } let param_def_id = generic_param.def_id; - let param_hir_id = match self.tcx.hir().as_local_hir_id(param_def_id) { - Some(x) => x, + let param_hir_id = match param_def_id.as_local() { + Some(x) => self.tcx.hir().as_local_hir_id(x).unwrap(), None => return, }; let param_span = self.tcx.hir().span(param_hir_id); diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index f8419e1025f89..714330a945bc6 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -579,11 +579,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (&self_ty.kind, parent_pred) { if let ty::Adt(def, _) = p.skip_binder().trait_ref.self_ty().kind { - let node = self - .tcx - .hir() - .as_local_hir_id(def.did) - .map(|id| self.tcx.hir().get(id)); + let node = def.did.as_local().map(|def_id| { + self.tcx + .hir() + .get(self.tcx.hir().as_local_hir_id(def_id).unwrap()) + }); if let Some(hir::Node::Item(hir::Item { kind, .. })) = node { if let Some(g) = kind.generics() { let key = match &g.where_clause.predicates[..] { @@ -857,7 +857,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { candidates: Vec, ) { let module_did = self.tcx.parent_module(self.body_id); - let module_id = self.tcx.hir().as_local_hir_id(module_did.to_def_id()).unwrap(); + let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap(); let krate = self.tcx.hir().krate(); let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id); if let Some(span) = span { @@ -950,62 +950,64 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // implementing a trait would be legal but is rejected // here). !unsatisfied_predicates.iter().any(|(p, _)| match p { - // Hide traits if they are present in predicates as they can be fixed without - // having to implement them. - ty::Predicate::Trait(t, _) => t.def_id() != info.def_id, - ty::Predicate::Projection(p) => p.item_def_id() != info.def_id, - _ => true, - }) && (type_is_local || info.def_id.is_local()) - && self - .associated_item(info.def_id, item_name, Namespace::ValueNS) - .filter(|item| { - if let ty::AssocKind::Fn = item.kind { - let id = self.tcx.hir().as_local_hir_id(item.def_id); - if let Some(hir::Node::TraitItem(hir::TraitItem { - kind: hir::TraitItemKind::Fn(fn_sig, method), - .. - })) = id.map(|id| self.tcx.hir().get(id)) - { - let self_first_arg = match method { - hir::TraitFn::Required([ident, ..]) => { - ident.name == kw::SelfLower - } - hir::TraitFn::Provided(body_id) => { - match &self.tcx.hir().body(*body_id).params[..] { - [hir::Param { - pat: - hir::Pat { - kind: - hir::PatKind::Binding( - _, - _, - ident, - .., - ), - .. - }, - .. - }, ..] => ident.name == kw::SelfLower, - _ => false, - } - } - _ => false, - }; - - if !fn_sig.decl.implicit_self.has_implicit_self() - && self_first_arg + // Hide traits if they are present in predicates as they can be fixed without + // having to implement them. + ty::Predicate::Trait(t, _) => t.def_id() != info.def_id, + ty::Predicate::Projection(p) => p.item_def_id() != info.def_id, + _ => true, + }) && (type_is_local || info.def_id.is_local()) + && self + .associated_item(info.def_id, item_name, Namespace::ValueNS) + .filter(|item| { + if let ty::AssocKind::Fn = item.kind { + let id = item.def_id.as_local().map(|def_id| { + self.tcx.hir().as_local_hir_id(def_id).unwrap() + }); + if let Some(hir::Node::TraitItem(hir::TraitItem { + kind: hir::TraitItemKind::Fn(fn_sig, method), + .. + })) = id.map(|id| self.tcx.hir().get(id)) { - if let Some(ty) = fn_sig.decl.inputs.get(0) { - arbitrary_rcvr.push(ty.span); + let self_first_arg = match method { + hir::TraitFn::Required([ident, ..]) => { + ident.name == kw::SelfLower + } + hir::TraitFn::Provided(body_id) => { + match &self.tcx.hir().body(*body_id).params[..] { + [hir::Param { + pat: + hir::Pat { + kind: + hir::PatKind::Binding( + _, + _, + ident, + .., + ), + .. + }, + .. + }, ..] => ident.name == kw::SelfLower, + _ => false, + } + } + _ => false, + }; + + if !fn_sig.decl.implicit_self.has_implicit_self() + && self_first_arg + { + if let Some(ty) = fn_sig.decl.inputs.get(0) { + arbitrary_rcvr.push(ty.span); + } + return false; } - return false; } } - } - // We only want to suggest public or local traits (#45781). - item.vis == ty::Visibility::Public || info.def_id.is_local() - }) - .is_some() + // We only want to suggest public or local traits (#45781). + item.vis == ty::Visibility::Public || info.def_id.is_local() + }) + .is_some() }) .collect::>(); for span in &arbitrary_rcvr { @@ -1052,7 +1054,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let generics = self.tcx.generics_of(table_owner.to_def_id()); let type_param = generics.type_param(param, self.tcx); let hir = &self.tcx.hir(); - if let Some(id) = hir.as_local_hir_id(type_param.def_id) { + if let Some(id) = type_param + .def_id + .as_local() + .map(|def_id| hir.as_local_hir_id(def_id).unwrap()) + { // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: FooBar`, // instead we suggest `T: Foo + Bar` in that case. diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 4b64d116a8285..15e491b3085e5 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -750,20 +750,20 @@ fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: DefId) { fn typeck_item_bodies(tcx: TyCtxt<'_>, crate_num: CrateNum) { debug_assert!(crate_num == LOCAL_CRATE); tcx.par_body_owners(|body_owner_def_id| { - tcx.ensure().typeck_tables_of(body_owner_def_id.to_def_id()); + tcx.ensure().typeck_tables_of(body_owner_def_id); }); } fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) { - wfcheck::check_item_well_formed(tcx, def_id); + wfcheck::check_item_well_formed(tcx, def_id.expect_local()); } fn check_trait_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) { - wfcheck::check_trait_item(tcx, def_id); + wfcheck::check_trait_item(tcx, def_id.expect_local()); } fn check_impl_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) { - wfcheck::check_impl_item(tcx, def_id); + wfcheck::check_impl_item(tcx, def_id.expect_local()); } pub fn provide(providers: &mut Providers<'_>) { @@ -977,7 +977,7 @@ fn diagnostic_only_typeck_tables_of<'tcx>( ) -> &ty::TypeckTables<'tcx> { assert!(def_id.is_local()); let fallback = move || { - let span = tcx.hir().span(tcx.hir().as_local_hir_id(def_id).unwrap()); + let span = tcx.hir().span(tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap()); tcx.sess.delay_span_bug(span, "diagnostic only typeck table used"); tcx.types.err }; @@ -996,7 +996,7 @@ fn typeck_tables_of_with_fallback<'tcx>( return tcx.typeck_tables_of(outer_def_id); } - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let span = tcx.hir().span(id); // Figure out what primary body this item has. @@ -1334,7 +1334,7 @@ fn check_fn<'a, 'tcx>( } let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id()); - let outer_hir_id = hir.as_local_hir_id(outer_def_id).unwrap(); + let outer_hir_id = hir.as_local_hir_id(outer_def_id.expect_local()).unwrap(); GatherLocalsVisitor { fcx: &fcx, parent_id: outer_hir_id }.visit_body(body); // C-variadic fns also have a `VaList` input that's not listed in `fn_sig` @@ -1431,7 +1431,7 @@ fn check_fn<'a, 'tcx>( // Check that the main return type implements the termination trait. if let Some(term_id) = tcx.lang_items().termination() { if let Some((def_id, EntryFnType::Main)) = tcx.entry_fn(LOCAL_CRATE) { - let main_id = hir.as_local_hir_id(def_id).unwrap(); + let main_id = hir.as_local_hir_id(def_id.expect_local()).unwrap(); if main_id == fn_id { let substs = tcx.mk_substs_trait(declared_ret_ty, &[]); let trait_ref = ty::TraitRef::new(term_id, substs); @@ -1609,9 +1609,7 @@ fn check_opaque<'tcx>( /// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result /// in "inheriting lifetimes". fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: LocalDefId, span: Span) { - let item = tcx.hir().expect_item( - tcx.hir().as_local_hir_id(def_id.to_def_id()).expect("opaque type is not local"), - ); + let item = tcx.hir().expect_item(tcx.hir().as_local_hir_id(def_id).unwrap()); debug!( "check_opaque_for_inheriting_lifetimes: def_id={:?} span={:?} item={:?}", def_id, span, item @@ -2429,7 +2427,7 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: &ty::AdtDef) { ); let hir = tcx.hir(); - if let Some(hir_id) = hir.as_local_hir_id(def_spans[0].0) { + if let Some(hir_id) = hir.as_local_hir_id(def_spans[0].0.expect_local()) { if let Node::Item(Item { ident, .. }) = hir.get(hir_id) { err.span_note( tcx.def_span(def_spans[0].0), @@ -2441,7 +2439,7 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: &ty::AdtDef) { if def_spans.len() > 2 { let mut first = true; for (adt_def, span) in def_spans.iter().skip(1).rev() { - if let Some(hir_id) = hir.as_local_hir_id(*adt_def) { + if let Some(hir_id) = hir.as_local_hir_id(adt_def.expect_local()) { if let Node::Item(Item { ident, .. }) = hir.get(hir_id) { err.span_note( *span, @@ -2665,7 +2663,7 @@ pub fn check_enum<'tcx>( // Check for duplicate discriminant values if let Some(i) = disr_vals.iter().position(|&x| x.val == discr.val) { let variant_did = def.variants[VariantIdx::new(i)].def_id; - let variant_i_hir_id = tcx.hir().as_local_hir_id(variant_did).unwrap(); + let variant_i_hir_id = tcx.hir().as_local_hir_id(variant_did.expect_local()).unwrap(); let variant_i = tcx.hir().expect_variant(variant_i_hir_id); let i_span = match variant_i.disr_expr { Some(ref expr) => tcx.hir().span(expr.hir_id), @@ -2726,7 +2724,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> { fn get_type_parameter_bounds(&self, _: Span, def_id: DefId) -> ty::GenericPredicates<'tcx> { let tcx = self.tcx; - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let item_id = tcx.hir().ty_param_owner(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id); @@ -4943,7 +4941,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } Some(Node::Ctor(hir::VariantData::Tuple(fields, _))) => { sugg_call = fields.iter().map(|_| "_").collect::>().join(", "); - match hir.as_local_hir_id(def_id).and_then(|hir_id| hir.def_kind(hir_id)) { + match def_id + .as_local() + .map(|def_id| hir.as_local_hir_id(def_id).unwrap()) + .and_then(|hir_id| hir.def_kind(hir_id)) + { Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => { msg = "instantiate this tuple variant"; } diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 574e5a4f8f1cc..b3c1b5b23e685 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -70,14 +70,14 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> { /// We do this check as a pre-pass before checking fn bodies because if these constraints are /// not included it frequently leads to confusing errors in fn bodies. So it's better to check /// the types first. -pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) { +pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); let item = tcx.hir().expect_item(hir_id); debug!( "check_item_well_formed(it.hir_id={:?}, it.name={})", item.hir_id, - tcx.def_path_str(def_id) + tcx.def_path_str(def_id.to_def_id()) ); match item.kind { @@ -183,7 +183,7 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) { } } -pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: DefId) { +pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); let trait_item = tcx.hir().expect_trait_item(hir_id); @@ -257,7 +257,7 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem } } -pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: DefId) { +pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); let impl_item = tcx.hir().expect_impl_item(hir_id); @@ -789,7 +789,7 @@ fn check_where_clauses<'tcx, 'fcx>( let mut predicates = predicates.instantiate_identity(fcx.tcx); if let Some((return_ty, span)) = return_ty { - let opaque_types = check_opaque_types(tcx, fcx, def_id, span, return_ty); + let opaque_types = check_opaque_types(tcx, fcx, def_id.expect_local(), span, return_ty); for _ in 0..opaque_types.len() { predicates.spans.push(span); } @@ -862,7 +862,7 @@ fn check_fn_or_method<'fcx, 'tcx>( fn check_opaque_types<'fcx, 'tcx>( tcx: TyCtxt<'tcx>, fcx: &FnCtxt<'fcx, 'tcx>, - fn_def_id: DefId, + fn_def_id: LocalDefId, span: Span, ty: Ty<'tcx>, ) -> Vec> { @@ -878,7 +878,7 @@ fn check_opaque_types<'fcx, 'tcx>( // FIXME(eddyb) is `generics.parent.is_none()` correct? It seems // potentially risky wrt associated types in `impl`s. if generics.parent.is_none() && def_id.is_local() { - let opaque_hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let opaque_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); if may_define_opaque_type(tcx, fn_def_id, opaque_hir_id) { trace!("check_opaque_types: may define, generics={:#?}", generics); let mut seen_params: FxHashMap<_, Vec<_>> = FxHashMap::default(); diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index 27434b00a98be..3a7d10a183203 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -424,7 +424,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { fn visit_opaque_types(&mut self, span: Span) { for (&def_id, opaque_defn) in self.fcx.opaque_types.borrow().iter() { - let hir_id = self.tcx().hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.tcx().hir().as_local_hir_id(def_id.expect_local()).unwrap(); let instantiated_ty = self.resolve(&opaque_defn.concrete_ty, &hir_id); debug_assert!(!instantiated_ty.has_escaping_bound_vars()); diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index bb2e077ceb03c..0c6fc8f32aaf8 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -89,7 +89,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { // Note that if we carry through to the `extern_mod_stmt_cnum` query // below it'll cause a panic because `def_id` is actually bogus at this // point in time otherwise. - if let Some(id) = tcx.hir().as_local_hir_id(def_id) { + if let Some(id) = tcx.hir().as_local_hir_id(def_id.expect_local()) { if tcx.hir().find(id).is_none() { return false; } @@ -115,7 +115,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { }); for extern_crate in &crates_to_lint { - let id = tcx.hir().as_local_hir_id(extern_crate.def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(extern_crate.def_id.expect_local()).unwrap(); let item = tcx.hir().expect_item(id); // If the crate is fully unused, we suggest removing it altogether. diff --git a/src/librustc_typeck/coherence/builtin.rs b/src/librustc_typeck/coherence/builtin.rs index 7cfb7fa712cca..c0613ffdf2ea9 100644 --- a/src/librustc_typeck/coherence/builtin.rs +++ b/src/librustc_typeck/coherence/builtin.rs @@ -53,8 +53,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { return; } - let impl_hir_id = - tcx.hir().as_local_hir_id(impl_did.to_def_id()).expect("foreign Drop impl on non-ADT"); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).expect("foreign Drop impl on non-ADT"); let sp = match tcx.hir().expect_item(impl_hir_id).kind { ItemKind::Impl { self_ty, .. } => self_ty.span, _ => bug!("expected Drop impl item"), @@ -73,7 +72,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_copy: impl_did={:?}", impl_did); - let impl_hir_id = if let Some(n) = tcx.hir().as_local_hir_id(impl_did.to_def_id()) { + let impl_hir_id = if let Some(n) = tcx.hir().as_local_hir_id(impl_did) { n } else { debug!("visit_implementation_of_copy(): impl not in this crate"); @@ -153,7 +152,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef let dispatch_from_dyn_trait = tcx.lang_items().dispatch_from_dyn_trait().unwrap(); - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.to_def_id()).unwrap(); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).unwrap(); let span = tcx.hir().span(impl_hir_id); let source = tcx.type_of(impl_did); @@ -327,9 +326,7 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI }); // this provider should only get invoked for local def-ids - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).unwrap_or_else(|| { - bug!("coerce_unsized_info: invoked for non-local def-id {:?}", impl_did) - }); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.expect_local()).unwrap(); let source = tcx.type_of(impl_did); let trait_ref = tcx.impl_trait_ref(impl_did).unwrap(); diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index ffed17b6b30ce..ed6cdcc38b23a 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -269,10 +269,7 @@ impl ItemCtxt<'tcx> { } pub fn hir_id(&self) -> hir::HirId { - self.tcx - .hir() - .as_local_hir_id(self.item_def_id) - .expect("Non-local call to local provider is_const_fn") + self.tcx.hir().as_local_hir_id(self.item_def_id.expect_local()).unwrap() } pub fn node(&self) -> hir::Node<'tcx> { @@ -487,7 +484,7 @@ fn type_param_predicates( // written inline like `` or in a where-clause like // `where T: Foo`. - let param_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let param_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let param_owner = tcx.hir().ty_param_owner(param_id); let param_owner_def_id = tcx.hir().local_def_id(param_owner); let generics = tcx.generics_of(param_owner_def_id); @@ -509,7 +506,7 @@ fn type_param_predicates( .unwrap_or_default(); let mut extend = None; - let item_hir_id = tcx.hir().as_local_hir_id(item_def_id).unwrap(); + let item_hir_id = tcx.hir().as_local_hir_id(item_def_id.expect_local()).unwrap(); let ast_generics = match tcx.hir().get(item_hir_id) { Node::TraitItem(item) => &item.generics, @@ -812,13 +809,10 @@ fn convert_variant( discr: ty::VariantDiscr, def: &hir::VariantData<'_>, adt_kind: ty::AdtKind, - parent_did: DefId, + parent_did: LocalDefId, ) -> ty::VariantDef { let mut seen_fields: FxHashMap = Default::default(); - let hir_id = tcx - .hir() - .as_local_hir_id(variant_did.map(LocalDefId::to_def_id).unwrap_or(parent_did)) - .unwrap(); + let hir_id = tcx.hir().as_local_hir_id(variant_did.unwrap_or(parent_did)).unwrap(); let fields = def .fields() .iter() @@ -860,7 +854,7 @@ fn convert_variant( fields, CtorKind::from_hir(def), adt_kind, - parent_did, + parent_did.to_def_id(), recovered, ) } @@ -868,13 +862,14 @@ fn convert_variant( fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { use rustc_hir::*; + let def_id = def_id.expect_local(); let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); let item = match tcx.hir().get(hir_id) { Node::Item(item) => item, _ => bug!(), }; - let repr = ReprOptions::new(tcx, def_id); + let repr = ReprOptions::new(tcx, def_id.to_def_id()); let (kind, variants) = match item.kind { ItemKind::Enum(ref def, _) => { let mut distance_from_explicit = 0; @@ -947,7 +942,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { } _ => bug!(), }; - tcx.alloc_adt_def(def_id, kind, variants, repr) + tcx.alloc_adt_def(def_id.to_def_id(), kind, variants, repr) } /// Ensures that the super-predicates of the trait with a `DefId` @@ -955,7 +950,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { /// the transitive super-predicates are converted. fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredicates<'_> { debug!("super_predicates(trait_def_id={:?})", trait_def_id); - let trait_hir_id = tcx.hir().as_local_hir_id(trait_def_id).unwrap(); + let trait_hir_id = tcx.hir().as_local_hir_id(trait_def_id.expect_local()).unwrap(); let item = match tcx.hir().get(trait_hir_id) { Node::Item(item) => item, @@ -1006,7 +1001,7 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi } fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TraitDef { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let item = tcx.hir().expect_item(hir_id); let (is_auto, unsafety) = match item.kind { @@ -1162,7 +1157,7 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option, def_id: DefId) -> &ty::Generics { use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let node = tcx.hir().get(hir_id); let parent_def_id = match node { @@ -1460,7 +1455,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { use rustc_hir::Node::*; use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let icx = ItemCtxt::new(tcx, def_id); @@ -1558,7 +1553,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { let icx = ItemCtxt::new(tcx, def_id); - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { ref of_trait, .. } => of_trait.as_ref().map(|ast_trait_ref| { let selfty = tcx.type_of(def_id); @@ -1569,7 +1564,7 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { } fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl); let item = tcx.hir().expect_item(hir_id); match &item.kind { @@ -1703,7 +1698,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat } } - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let node = tcx.hir().get(hir_id); let mut is_trait = None; @@ -2555,7 +2550,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { if codegen_fn_attrs.flags.intersects(CodegenFnAttrFlags::NO_SANITIZE_ANY) { if codegen_fn_attrs.inline == InlineAttr::Always { if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) { - let hir_id = tcx.hir().as_local_hir_id(id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(id.expect_local()).unwrap(); tcx.struct_span_lint_hir( lint::builtin::INLINE_NO_SANITIZE, hir_id, diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index 15cc15f9f09a4..9dfe6c9d03ec6 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_errors::{struct_span_err, Applicability, StashKey}; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit; use rustc_hir::intravisit::Visitor; use rustc_hir::Node; @@ -21,7 +21,7 @@ use super::{bad_placeholder_type, is_suggestable_infer_ty}; pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); let icx = ItemCtxt::new(tcx, def_id); @@ -63,7 +63,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { report_assoc_ty_on_inherent_impl(tcx, item.span); } - find_opaque_ty_constraints(tcx, def_id) + find_opaque_ty_constraints(tcx, def_id.expect_local()) } ImplItemKind::TyAlias(ref ty) => { if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() { @@ -96,7 +96,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { tcx.mk_adt(def, substs) } ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }) => { - find_opaque_ty_constraints(tcx, def_id) + find_opaque_ty_constraints(tcx, def_id.expect_local()) } // Opaque types desugared from `impl Trait`. ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: Some(owner), origin, .. }) => { @@ -363,7 +363,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } } -fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { +fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> { use rustc_hir::{Expr, ImplItem, Item, TraitItem}; debug!("find_opaque_ty_constraints({:?})", def_id); @@ -515,7 +515,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); let scope = tcx.hir().get_defining_scope(hir_id); - let mut locator = ConstraintLocator { def_id, tcx, found: None }; + let mut locator = ConstraintLocator { def_id: def_id.to_def_id(), tcx, found: None }; debug!("find_opaque_ty_constraints: scope={:?}", scope); diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index ebfb3684eb0d6..f419f052b811f 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -69,7 +69,7 @@ use crate::constrained_generic_params as cgp; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_infer::infer::outlives::env::OutlivesEnvironment; use rustc_infer::infer::{InferCtxt, RegionckMode, TyCtxtInferExt}; use rustc_infer::traits::specialization_graph::Node; @@ -130,7 +130,14 @@ fn check_always_applicable( check_static_lifetimes(tcx, &parent_substs, span); check_duplicate_params(tcx, impl1_substs, &parent_substs, span); - check_predicates(infcx, impl1_def_id, impl1_substs, impl2_node, impl2_substs, span); + check_predicates( + infcx, + impl1_def_id.expect_local(), + impl1_substs, + impl2_node, + impl2_substs, + span, + ); } } @@ -287,7 +294,7 @@ fn check_static_lifetimes<'tcx>( /// including the `Self`-type. fn check_predicates<'tcx>( infcx: &InferCtxt<'_, 'tcx>, - impl1_def_id: DefId, + impl1_def_id: LocalDefId, impl1_substs: SubstsRef<'tcx>, impl2_node: Node, impl2_substs: SubstsRef<'tcx>, diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index a2b02ebd27179..382664a1b4661 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -91,7 +91,7 @@ mod variance; use rustc_errors::{struct_span_err, ErrorReported}; use rustc_hir as hir; -use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE}; use rustc_hir::Node; use rustc_infer::infer::{InferOk, TyCtxtInferExt}; use rustc_infer::traits::TraitEngineExt as _; @@ -152,7 +152,7 @@ fn require_same_types<'tcx>( }) } -fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) { +fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) { let main_id = tcx.hir().as_local_hir_id(main_def_id).unwrap(); let main_span = tcx.def_span(main_def_id); let main_t = tcx.type_of(main_def_id); @@ -219,7 +219,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) { } } -fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) { +fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) { let start_id = tcx.hir().as_local_hir_id(start_def_id).unwrap(); let start_span = tcx.def_span(start_def_id); let start_t = tcx.type_of(start_def_id); @@ -279,8 +279,8 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) { fn check_for_entry_fn(tcx: TyCtxt<'_>) { match tcx.entry_fn(LOCAL_CRATE) { - Some((def_id, EntryFnType::Main)) => check_main_fn_ty(tcx, def_id), - Some((def_id, EntryFnType::Start)) => check_start_fn_ty(tcx, def_id), + Some((def_id, EntryFnType::Main)) => check_main_fn_ty(tcx, def_id.expect_local()), + Some((def_id, EntryFnType::Start)) => check_start_fn_ty(tcx, def_id.expect_local()), _ => {} } } diff --git a/src/librustc_typeck/outlives/implicit_infer.rs b/src/librustc_typeck/outlives/implicit_infer.rs index 6a9cbc544f857..3c579538e64bb 100644 --- a/src/librustc_typeck/outlives/implicit_infer.rs +++ b/src/librustc_typeck/outlives/implicit_infer.rs @@ -53,7 +53,7 @@ pub struct InferVisitor<'cx, 'tcx> { impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { - let item_did = self.tcx.hir().local_def_id(item.hir_id).to_def_id(); + let item_did = self.tcx.hir().local_def_id(item.hir_id); debug!("InferVisitor::visit_item(item={:?})", item_did); @@ -66,7 +66,7 @@ impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> { let mut item_required_predicates = RequiredPredicates::default(); match item.kind { hir::ItemKind::Union(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) => { - let adt_def = self.tcx.adt_def(item_did); + let adt_def = self.tcx.adt_def(item_did.to_def_id()); // Iterate over all fields in item_did for field_def in adt_def.all_fields() { @@ -99,10 +99,10 @@ impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> { // we walk the crates again and re-calculate predicates for all // items. let item_predicates_len: usize = - self.global_inferred_outlives.get(&item_did).map(|p| p.len()).unwrap_or(0); + self.global_inferred_outlives.get(&item_did.to_def_id()).map(|p| p.len()).unwrap_or(0); if item_required_predicates.len() > item_predicates_len { *self.predicates_added = true; - self.global_inferred_outlives.insert(item_did, item_required_predicates); + self.global_inferred_outlives.insert(item_did.to_def_id(), item_required_predicates); } } diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs index 2d9b39a7f6146..71fcd9ddd6bc5 100644 --- a/src/librustc_typeck/outlives/mod.rs +++ b/src/librustc_typeck/outlives/mod.rs @@ -18,7 +18,7 @@ pub fn provide(providers: &mut Providers<'_>) { } fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] { - let id = tcx.hir().as_local_hir_id(item_def_id).expect("expected local def-id"); + let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()).unwrap(); match tcx.hir().get(id) { Node::Item(item) => match item.kind { diff --git a/src/librustc_typeck/variance/constraints.rs b/src/librustc_typeck/variance/constraints.rs index 83be3e2c87802..5b0ccc5169ee0 100644 --- a/src/librustc_typeck/variance/constraints.rs +++ b/src/librustc_typeck/variance/constraints.rs @@ -3,7 +3,7 @@ //! The second pass over the AST determines the set of constraints. //! We walk the set of items and, for each member, generate new constraints. -use hir::def_id::DefId; +use hir::def_id::{DefId, LocalDefId}; use rustc_hir as hir; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_middle::ty::subst::{GenericArgKind, SubstsRef}; @@ -121,16 +121,16 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { fn visit_node_helper(&mut self, id: hir::HirId) { let tcx = self.terms_cx.tcx; let def_id = tcx.hir().local_def_id(id); - self.build_constraints_for_item(def_id.to_def_id()); + self.build_constraints_for_item(def_id); } fn tcx(&self) -> TyCtxt<'tcx> { self.terms_cx.tcx } - fn build_constraints_for_item(&mut self, def_id: DefId) { + fn build_constraints_for_item(&mut self, def_id: LocalDefId) { let tcx = self.tcx(); - debug!("build_constraints_for_item({})", tcx.def_path_str(def_id)); + debug!("build_constraints_for_item({})", tcx.def_path_str(def_id.to_def_id())); // Skip items with no generics - there's nothing to infer in them. if tcx.generics_of(def_id).count() == 0 { @@ -377,7 +377,9 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { return; } - let (local, remote) = if let Some(id) = self.tcx().hir().as_local_hir_id(def_id) { + let (local, remote) = if let Some(id) = + def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id).unwrap()) + { (Some(self.terms_cx.inferred_starts[&id]), None) } else { (None, Some(self.tcx().variances_of(def_id))) diff --git a/src/librustc_typeck/variance/mod.rs b/src/librustc_typeck/variance/mod.rs index 47652b7b6966c..f9e32c2a6b0cd 100644 --- a/src/librustc_typeck/variance/mod.rs +++ b/src/librustc_typeck/variance/mod.rs @@ -38,7 +38,7 @@ fn crate_variances(tcx: TyCtxt<'_>, crate_num: CrateNum) -> &CrateVariancesMap<' } fn variances_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[ty::Variance] { - let id = tcx.hir().as_local_hir_id(item_def_id).expect("expected local def-id"); + let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()).unwrap(); let unsupported = || { // Variance not relevant. span_bug!(tcx.hir().span(id), "asked to compute variance for wrong kind of item") diff --git a/src/librustc_typeck/variance/terms.rs b/src/librustc_typeck/variance/terms.rs index e3ea0bf20e39c..c6cdac5d0ccf6 100644 --- a/src/librustc_typeck/variance/terms.rs +++ b/src/librustc_typeck/variance/terms.rs @@ -94,7 +94,9 @@ fn lang_items(tcx: TyCtxt<'_>) -> Vec<(hir::HirId, Vec)> { all.into_iter() // iterating over (Option, Variance) .filter(|&(ref d, _)| d.is_some()) .map(|(d, v)| (d.unwrap(), v)) // (DefId, Variance) - .filter_map(|(d, v)| tcx.hir().as_local_hir_id(d).map(|n| (n, v))) // (HirId, Variance) + .filter_map(|(d, v)| { + d.as_local().map(|d| tcx.hir().as_local_hir_id(d).unwrap()).map(|n| (n, v)) + }) // (HirId, Variance) .collect() } diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index a82015dea5fc8..0ed9115e77d5e 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -340,14 +340,15 @@ pub fn build_impl( } } - let for_ = if let Some(hir_id) = tcx.hir().as_local_hir_id(did) { - match tcx.hir().expect_item(hir_id).kind { - hir::ItemKind::Impl { self_ty, .. } => self_ty.clean(cx), - _ => panic!("did given to build_impl was not an impl"), - } - } else { - tcx.type_of(did).clean(cx) - }; + let for_ = + if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did).unwrap()) { + match tcx.hir().expect_item(hir_id).kind { + hir::ItemKind::Impl { self_ty, .. } => self_ty.clean(cx), + _ => panic!("did given to build_impl was not an impl"), + } + } else { + tcx.type_of(did).clean(cx) + }; // Only inline impl if the implementing type is // reachable in rustdoc generated documentation @@ -360,7 +361,9 @@ pub fn build_impl( } let predicates = tcx.explicit_predicates_of(did); - let (trait_items, generics) = if let Some(hir_id) = tcx.hir().as_local_hir_id(did) { + let (trait_items, generics) = if let Some(hir_id) = + did.as_local().map(|did| tcx.hir().as_local_hir_id(did).unwrap()) + { match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { ref generics, ref items, .. } => ( items.iter().map(|item| tcx.hir().impl_item(item.id).clean(cx)).collect::>(), @@ -486,7 +489,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet) } pub fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String { - if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(did) { + if let Some(hir_id) = did.as_local().map(|did| cx.tcx.hir().as_local_hir_id(did).unwrap()) { rustc_hir_pretty::id_to_string(&cx.tcx.hir(), hir_id) } else { cx.tcx.rendered_const(did) @@ -498,11 +501,9 @@ fn build_const(cx: &DocContext<'_>, did: DefId) -> clean::Constant { type_: cx.tcx.type_of(did).clean(cx), expr: print_inlined_const(cx, did), value: clean::utils::print_evaluated_const(cx, did), - is_literal: cx - .tcx - .hir() - .as_local_hir_id(did) - .map_or(false, |hir_id| clean::utils::is_literal_expr(cx, hir_id)), + is_literal: did.as_local().map_or(false, |did| { + clean::utils::is_literal_expr(cx, cx.tcx.hir().as_local_hir_id(did).unwrap()) + }), } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 6d65e6b1e0a47..8e5189a80853f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -974,12 +974,7 @@ where impl<'tcx> Clean for (DefId, ty::PolyFnSig<'tcx>) { fn clean(&self, cx: &DocContext<'_>) -> FnDecl { let (did, sig) = *self; - let mut names = if cx.tcx.hir().as_local_hir_id(did).is_some() { - &[] - } else { - cx.tcx.fn_arg_names(did) - } - .iter(); + let mut names = if did.is_local() { &[] } else { cx.tcx.fn_arg_names(did) }.iter(); FnDecl { output: Return(sig.skip_binder().output().clean(cx)), @@ -1382,7 +1377,10 @@ impl Clean for hir::Ty<'_> { let mut alias = None; if let Res::Def(DefKind::TyAlias, def_id) = path.res { // Substitute private type aliases - if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) { + if let Some(hir_id) = def_id + .as_local() + .map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + { if !cx.renderinfo.borrow().access_levels.is_exported(def_id) { alias = Some(&cx.tcx.hir().expect_item(hir_id).kind); } diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 2626ca638e841..be468064368ff 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -473,7 +473,9 @@ pub fn name_from_pat(p: &hir::Pat) -> String { pub fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String { match n.val { ty::ConstKind::Unevaluated(def_id, _, promoted) => { - let mut s = if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) { + let mut s = if let Some(hir_id) = + def_id.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + { print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id)) } else { inline::print_inlined_const(cx, def_id) diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 894cc3e022c96..b740d8e65176d 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -144,7 +144,7 @@ impl<'tcx> DocContext<'tcx> { if self.all_fake_def_ids.borrow().contains(&def_id) { None } else { - self.tcx.hir().as_local_hir_id(def_id) + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) } } diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 1821635bde48f..2f9719774ec47 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -335,7 +335,11 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { fn fold_item(&mut self, mut item: Item) -> Option { let item_hir_id = if item.is_mod() { - if let Some(id) = self.cx.tcx.hir().as_local_hir_id(item.def_id) { + if let Some(id) = item + .def_id + .as_local() + .map(|def_id| self.cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + { Some(id) } else { debug!("attempting to fold on a non-local item: {:?}", item); diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 5ef803c992505..e74a1fe5b8c09 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -330,8 +330,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { return false; } - let res_hir_id = match tcx.hir().as_local_hir_id(res_did) { - Some(n) => n, + let res_hir_id = match res_did.as_local() { + Some(n) => tcx.hir().as_local_hir_id(n).unwrap(), None => return false, }; From 9d5e085feb5868f36ef7bd6bebcefef220a083cd Mon Sep 17 00:00:00 2001 From: marmeladema Date: Thu, 16 Apr 2020 20:36:32 +0100 Subject: [PATCH 4/7] Modify `as_local_hir_id` to return a bare `HirId` --- src/librustc_codegen_llvm/consts.rs | 2 +- .../back/symbol_export.rs | 2 +- src/librustc_hir/definitions.rs | 4 +- .../infer/error_reporting/mod.rs | 4 +- .../nice_region_error/find_anon_type.rs | 2 +- .../nice_region_error/outlives_closure.rs | 2 +- .../error_reporting/nice_region_error/util.rs | 2 +- src/librustc_lint/builtin.rs | 11 ++-- src/librustc_lint/late.rs | 2 +- src/librustc_metadata/rmeta/encoder.rs | 16 ++--- src/librustc_middle/dep_graph/mod.rs | 2 +- src/librustc_middle/hir/map/mod.rs | 18 ++---- src/librustc_middle/hir/mod.rs | 4 +- src/librustc_middle/middle/region.rs | 4 +- src/librustc_middle/mir/mod.rs | 10 ++-- src/librustc_middle/mir/mono.rs | 12 ++-- src/librustc_middle/query/mod.rs | 2 +- src/librustc_middle/ty/context.rs | 5 +- src/librustc_middle/ty/mod.rs | 10 ++-- src/librustc_middle/ty/print/pretty.rs | 4 +- src/librustc_middle/ty/sty.rs | 2 +- .../diagnostics/conflict_errors.rs | 6 +- .../borrow_check/diagnostics/mod.rs | 6 +- .../diagnostics/mutability_errors.rs | 4 +- .../borrow_check/diagnostics/region_name.rs | 14 ++--- src/librustc_mir/borrow_check/mod.rs | 2 +- .../borrow_check/universal_regions.rs | 2 +- src/librustc_mir/const_eval/eval_queries.rs | 4 +- src/librustc_mir/const_eval/fn_queries.rs | 2 +- src/librustc_mir/monomorphize/collector.rs | 4 +- .../transform/check_consts/mod.rs | 2 +- .../transform/check_consts/validation.rs | 2 +- src/librustc_mir/transform/check_unsafety.rs | 6 +- src/librustc_mir/transform/const_prop.rs | 2 +- src/librustc_mir/transform/inline.rs | 2 +- .../transform/qualify_min_const_fn.rs | 2 +- src/librustc_mir/util/liveness.rs | 2 +- src/librustc_mir_build/build/mod.rs | 2 +- src/librustc_mir_build/hair/cx/expr.rs | 2 +- .../hair/pattern/check_match.rs | 2 +- src/librustc_mir_build/lints.rs | 4 +- src/librustc_passes/dead.rs | 8 +-- src/librustc_passes/reachable.rs | 26 +++----- src/librustc_passes/region.rs | 2 +- src/librustc_passes/upvars.rs | 2 +- src/librustc_privacy/lib.rs | 21 ++++--- src/librustc_resolve/late/lifetimes.rs | 20 +++---- src/librustc_symbol_mangling/lib.rs | 2 +- src/librustc_trait_selection/opaque_types.rs | 4 +- .../traits/error_reporting/mod.rs | 3 +- .../traits/error_reporting/suggestions.rs | 6 +- .../traits/specialize/mod.rs | 2 +- src/librustc_trait_selection/traits/wf.rs | 2 +- src/librustc_traits/lowering/environment.rs | 2 +- src/librustc_ty/ty.rs | 10 ++-- src/librustc_typeck/astconv.rs | 8 +-- src/librustc_typeck/check/coercion.rs | 2 +- src/librustc_typeck/check/compare_method.rs | 30 +++++----- src/librustc_typeck/check/dropck.rs | 4 +- src/librustc_typeck/check/expr.rs | 2 +- src/librustc_typeck/check/method/suggest.rs | 14 ++--- src/librustc_typeck/check/mod.rs | 60 +++++++++---------- src/librustc_typeck/check/wfcheck.rs | 8 +-- src/librustc_typeck/check/writeback.rs | 2 +- src/librustc_typeck/check_unused.rs | 8 +-- src/librustc_typeck/coherence/builtin.rs | 13 ++-- src/librustc_typeck/collect.rs | 26 ++++---- src/librustc_typeck/collect/type_of.rs | 4 +- .../impl_wf_check/min_specialization.rs | 2 +- src/librustc_typeck/lib.rs | 4 +- .../outlives/implicit_infer.rs | 2 +- src/librustc_typeck/outlives/mod.rs | 2 +- src/librustc_typeck/variance/constraints.rs | 4 +- src/librustc_typeck/variance/mod.rs | 2 +- src/librustc_typeck/variance/terms.rs | 4 +- src/librustdoc/clean/inline.rs | 23 ++++--- src/librustdoc/clean/mod.rs | 5 +- src/librustdoc/clean/utils.rs | 2 +- src/librustdoc/core.rs | 2 +- .../passes/collect_intra_doc_links.rs | 6 +- src/librustdoc/visit_ast.rs | 2 +- 81 files changed, 245 insertions(+), 298 deletions(-) diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index ac2f47a740d57..c97b547946ebb 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -210,7 +210,7 @@ impl CodegenCx<'ll, 'tcx> { debug!("get_static: sym={} instance={:?}", sym, instance); let g = if let Some(id) = - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) { let llty = self.layout_of(ty).llvm_type(self); let (g, attrs) = match self.tcx.hir().get(id) { diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index 4edf8316028f4..a7d5934f0e9a7 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -360,7 +360,7 @@ fn upstream_drop_glue_for_provider<'tcx>( fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool { if let Some(def_id) = def_id.as_local() { - !tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().as_local_hir_id(def_id).unwrap()) + !tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().as_local_hir_id(def_id)) } else { bug!("is_unreachable_local_definition called with non-local DefId: {:?}", def_id) } diff --git a/src/librustc_hir/definitions.rs b/src/librustc_hir/definitions.rs index 8927814ee04ba..033c2973af6ee 100644 --- a/src/librustc_hir/definitions.rs +++ b/src/librustc_hir/definitions.rs @@ -342,8 +342,8 @@ impl Definitions { } #[inline] - pub fn as_local_hir_id(&self, def_id: LocalDefId) -> Option { - Some(self.local_def_id_to_hir_id(def_id)) + pub fn as_local_hir_id(&self, def_id: LocalDefId) -> hir::HirId { + self.local_def_id_to_hir_id(def_id) } #[inline] diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs index 708518b227456..1e8980efd52f9 100644 --- a/src/librustc_infer/infer/error_reporting/mod.rs +++ b/src/librustc_infer/infer/error_reporting/mod.rs @@ -191,7 +191,7 @@ fn msg_span_from_early_bound_and_free_regions( let sm = tcx.sess.source_map(); let scope = region.free_region_binding_scope(tcx); - let node = tcx.hir().as_local_hir_id(scope.expect_local()).unwrap(); + let node = tcx.hir().as_local_hir_id(scope.expect_local()); let tag = match tcx.hir().find(node) { Some(Node::Block(_)) | Some(Node::Expr(_)) => "body", Some(Node::Item(it)) => item_scope_tag(&it), @@ -1784,7 +1784,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: 'a'b`, // instead we suggest `T: 'a + 'b` in that case. - let id = hir.as_local_hir_id(def_id).unwrap(); + let id = hir.as_local_hir_id(def_id); let mut has_bounds = false; if let Node::GenericParam(param) = hir.get(id) { has_bounds = !param.bounds.is_empty(); diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs index 6d10ec3dcae6f..1181668e4b951 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs @@ -30,7 +30,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { if let Some(anon_reg) = self.tcx().is_suitable_region(region) { let def_id = anon_reg.def_id; if let Some(hir_id) = - def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id)) { let fndecl = match self.tcx().hir().get(hir_id) { Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. }) diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs index 6bf60db009497..ca051cecbb8c4 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs @@ -47,7 +47,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { { let hir = &self.tcx().hir(); if let Some(hir_id) = - free_region.scope.as_local().map(|def_id| hir.as_local_hir_id(def_id).unwrap()) + free_region.scope.as_local().map(|def_id| hir.as_local_hir_id(def_id)) { if let Node::Expr(Expr { kind: Closure(_, _, _, closure_span, None), .. }) = hir.get(hir_id) diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs index ab74513a6755d..5c45f75843606 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs @@ -51,7 +51,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { }; let hir = &self.tcx().hir(); - let hir_id = hir.as_local_hir_id(id.as_local()?)?; + let hir_id = hir.as_local_hir_id(id.as_local()?); let body_id = hir.maybe_body_owned_by(hir_id)?; let body = hir.body(body_id); let owner_id = hir.body_owner(body_id); diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 4040545db968b..5b25204f21b1d 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -436,9 +436,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { // If the trait is private, add the impl items to `private_traits` so they don't get // reported for missing docs. let real_trait = trait_ref.path.res.def_id(); - if let Some(hir_id) = real_trait - .as_local() - .map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + if let Some(hir_id) = + real_trait.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) { if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) { if let hir::VisibilityKind::Inherited = item.vis.node { @@ -612,10 +611,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations { let mut impls = HirIdSet::default(); cx.tcx.for_each_impl(debug, |d| { if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() { - if let Some(hir_id) = ty_def - .did - .as_local() - .map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + if let Some(hir_id) = + ty_def.did.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) { impls.insert(hir_id); } diff --git a/src/librustc_lint/late.rs b/src/librustc_lint/late.rs index 0a0505e89d468..c4ac875fec537 100644 --- a/src/librustc_lint/late.rs +++ b/src/librustc_lint/late.rs @@ -364,7 +364,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( param_env: ty::ParamEnv::empty(), access_levels, lint_store: unerased_lint_store(tcx), - last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(), + last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id), generics: None, only_module: true, }; diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index 505330f481a18..a60c5da4228e9 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -616,7 +616,7 @@ impl EncodeContext<'tcx> { ctor: variant.ctor_def_id.map(|did| did.index), }; - let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()).unwrap(); + let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()); let enum_vis = &tcx.hir().expect_item(enum_id).vis; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); @@ -662,7 +662,7 @@ impl EncodeContext<'tcx> { // Variant constructors have the same visibility as the parent enums, unless marked as // non-exhaustive, in which case they are lowered to `pub(crate)`. - let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()).unwrap(); + let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()); let enum_vis = &tcx.hir().expect_item(enum_id).vis; let mut ctor_vis = ty::Visibility::from_hir(enum_vis, enum_id, tcx); if variant.is_field_list_non_exhaustive() && ctor_vis == ty::Visibility::Public { @@ -728,7 +728,7 @@ impl EncodeContext<'tcx> { let def_id = field.did; debug!("EncodeContext::encode_field({:?})", def_id); - let variant_id = tcx.hir().as_local_hir_id(variant.def_id.expect_local()).unwrap(); + let variant_id = tcx.hir().as_local_hir_id(variant.def_id.expect_local()); let variant_data = tcx.hir().expect_variant_data(variant_id); record!(self.tables.kind[def_id] <- EntryKind::Field); @@ -755,7 +755,7 @@ impl EncodeContext<'tcx> { ctor: Some(def_id.index), }; - let struct_id = tcx.hir().as_local_hir_id(adt_def.did.expect_local()).unwrap(); + let struct_id = tcx.hir().as_local_hir_id(adt_def.did.expect_local()); let struct_vis = &tcx.hir().expect_item(struct_id).vis; let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx); for field in &variant.fields { @@ -817,7 +817,7 @@ impl EncodeContext<'tcx> { debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id); let tcx = self.tcx; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let ast_item = tcx.hir().expect_trait_item(hir_id); let trait_item = tcx.associated_item(def_id); @@ -908,7 +908,7 @@ impl EncodeContext<'tcx> { debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id); let tcx = self.tcx; - let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()); let ast_item = self.tcx.hir().expect_impl_item(hir_id); let impl_item = self.tcx.associated_item(def_id); @@ -1312,7 +1312,7 @@ impl EncodeContext<'tcx> { // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, // including on the signature, which is inferred in `typeck_tables_of. - let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id); let ty = self.tcx.typeck_tables_of(def_id).node_type(hir_id); let def_id = def_id.to_def_id(); @@ -1340,7 +1340,7 @@ impl EncodeContext<'tcx> { fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) { debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id); - let id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = self.tcx.hir().as_local_hir_id(def_id); let body_id = self.tcx.hir().body_owned_by(id); let const_data = self.encode_rendered_const_for_body(body_id); let def_id = def_id.to_def_id(); diff --git a/src/librustc_middle/dep_graph/mod.rs b/src/librustc_middle/dep_graph/mod.rs index 0aca0f6e5b734..4786426792c3d 100644 --- a/src/librustc_middle/dep_graph/mod.rs +++ b/src/librustc_middle/dep_graph/mod.rs @@ -181,6 +181,6 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { } fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); def_id == hir_id.owner } diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index a3626e790a89b..d7c4e6fc4ed4b 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -198,7 +198,7 @@ impl<'hir> Map<'hir> { } #[inline] - pub fn as_local_hir_id(&self, def_id: LocalDefId) -> Option { + pub fn as_local_hir_id(&self, def_id: LocalDefId) -> HirId { self.tcx.definitions.as_local_hir_id(def_id) } @@ -449,7 +449,7 @@ impl<'hir> Map<'hir> { } pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) { - let hir_id = self.as_local_hir_id(module).unwrap(); + let hir_id = self.as_local_hir_id(module); match self.get_entry(hir_id).node { Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id), Node::Crate(item) => (&item.module, item.span, hir_id), @@ -482,11 +482,7 @@ impl<'hir> Map<'hir> { } pub fn get_if_local(&self, id: DefId) -> Option> { - if let Some(id) = id.as_local() { - self.as_local_hir_id(id).map(|id| self.get(id)) - } else { - None - } + if let Some(id) = id.as_local() { Some(self.get(self.as_local_hir_id(id))) } else { None } } pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> { @@ -889,11 +885,7 @@ impl<'hir> Map<'hir> { } pub fn span_if_local(&self, id: DefId) -> Option { - if let Some(id) = id.as_local() { - self.as_local_hir_id(id).map(|id| self.span(id)) - } else { - None - } + if let Some(id) = id.as_local() { Some(self.span(self.as_local_hir_id(id))) } else { None } } pub fn res_span(&self, res: Res) -> Option { @@ -1094,7 +1086,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String { pub fn provide(providers: &mut Providers<'_>) { providers.def_kind = |tcx, def_id| { if let Some(def_id) = def_id.as_local() { - tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id).unwrap()) + tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id)) } else { bug!("calling local def_kind query provider for upstream DefId: {:?}", def_id); } diff --git a/src/librustc_middle/hir/mod.rs b/src/librustc_middle/hir/mod.rs index c44a8b0b12e72..7ab66411b2109 100644 --- a/src/librustc_middle/hir/mod.rs +++ b/src/librustc_middle/hir/mod.rs @@ -68,13 +68,13 @@ impl<'tcx> TyCtxt<'tcx> { pub fn provide(providers: &mut Providers<'_>) { providers.parent_module_from_def_id = |tcx, id| { let hir = tcx.hir(); - hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap())) + hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id))) }; providers.hir_crate = |tcx, _| tcx.untracked_crate; providers.index_hir = map::index_hir; providers.hir_module_items = |tcx, id| { let hir = tcx.hir(); - let module = hir.as_local_hir_id(id).unwrap(); + let module = hir.as_local_hir_id(id); &tcx.untracked_crate.modules[&module] }; providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature; diff --git a/src/librustc_middle/middle/region.rs b/src/librustc_middle/middle/region.rs index 41f712f600aa0..c3eeea7662ba9 100644 --- a/src/librustc_middle/middle/region.rs +++ b/src/librustc_middle/middle/region.rs @@ -554,7 +554,7 @@ impl<'tcx> ScopeTree { pub fn early_free_scope(&self, tcx: TyCtxt<'tcx>, br: &ty::EarlyBoundRegion) -> Scope { let param_owner = tcx.parent(br.def_id).unwrap(); - let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local()).unwrap(); + let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local()); let scope = tcx .hir() .maybe_body_owned_by(param_owner_id) @@ -595,7 +595,7 @@ impl<'tcx> ScopeTree { // on the same function that they ended up being freed in. assert_eq!(param_owner, fr.scope); - let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local()).unwrap(); + let param_owner_id = tcx.hir().as_local_hir_id(param_owner.expect_local()); let body_id = tcx.hir().body_owned_by(param_owner_id); Scope { id: tcx.hir().body(body_id).value.hir_id.local_id, data: ScopeData::CallSite } } diff --git a/src/librustc_middle/mir/mod.rs b/src/librustc_middle/mir/mod.rs index 1019941a523c9..ee6b8a1b661c6 100644 --- a/src/librustc_middle/mir/mod.rs +++ b/src/librustc_middle/mir/mod.rs @@ -2284,9 +2284,8 @@ impl<'tcx> Debug for Rvalue<'tcx> { } AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| { - if let Some(hir_id) = def_id - .as_local() - .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + if let Some(hir_id) = + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { let name = if tcx.sess.opts.debugging_opts.span_free_formats { let substs = tcx.lift(&substs).unwrap(); @@ -2313,9 +2312,8 @@ impl<'tcx> Debug for Rvalue<'tcx> { }), AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| { - if let Some(hir_id) = def_id - .as_local() - .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + if let Some(hir_id) = + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { let name = format!("[generator@{:?}]", tcx.hir().span(hir_id)); let mut struct_fmt = fmt.debug_struct(&name); diff --git a/src/librustc_middle/mir/mono.rs b/src/librustc_middle/mir/mono.rs index 1b18060c88665..fd1d410f05160 100644 --- a/src/librustc_middle/mir/mono.rs +++ b/src/librustc_middle/mir/mono.rs @@ -198,10 +198,10 @@ impl<'tcx> MonoItem<'tcx> { pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option { match *self { MonoItem::Fn(Instance { def, .. }) => { - def.def_id().as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + def.def_id().as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) } MonoItem::Static(def_id) => { - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) } MonoItem::GlobalAsm(hir_id) => Some(hir_id), } @@ -343,9 +343,9 @@ impl<'tcx> CodegenUnit<'tcx> { // instances into account. The others don't matter for // the codegen tests and can even make item order // unstable. - InstanceDef::Item(def_id) => def_id - .as_local() - .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()), + InstanceDef::Item(def_id) => { + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) + } InstanceDef::VtableShim(..) | InstanceDef::ReifyShim(..) | InstanceDef::Intrinsic(..) @@ -357,7 +357,7 @@ impl<'tcx> CodegenUnit<'tcx> { } } MonoItem::Static(def_id) => { - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) } MonoItem::GlobalAsm(hir_id) => Some(hir_id), }, diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs index 25c7d86ddac2d..dca637e42d6be 100644 --- a/src/librustc_middle/query/mod.rs +++ b/src/librustc_middle/query/mod.rs @@ -287,7 +287,7 @@ rustc_queries! { /// per-type-parameter predicates for resolving `T::AssocTy`. query type_param_predicates(key: (DefId, DefId)) -> ty::GenericPredicates<'tcx> { desc { |tcx| "computing the bounds for type parameter `{}`", { - let id = tcx.hir().as_local_hir_id(key.1.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(key.1.expect_local()); tcx.hir().ty_param_name(id) }} } diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index 8a25beb6ae49f..b113e680b6092 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -1413,8 +1413,7 @@ impl<'tcx> TyCtxt<'tcx> { _ => return None, // not a free region }; - let hir_id = - self.hir().as_local_hir_id(suitable_region_binding_scope.expect_local()).unwrap(); + let hir_id = self.hir().as_local_hir_id(suitable_region_binding_scope.expect_local()); let is_impl_item = match self.hir().find(hir_id) { Some(Node::Item(..)) | Some(Node::TraitItem(..)) => false, Some(Node::ImplItem(..)) => { @@ -1432,7 +1431,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn return_type_impl_trait(&self, scope_def_id: DefId) -> Option<(Ty<'tcx>, Span)> { // HACK: `type_of_def_id()` will fail on these (#55796), so return `None`. - let hir_id = self.hir().as_local_hir_id(scope_def_id.expect_local()).unwrap(); + let hir_id = self.hir().as_local_hir_id(scope_def_id.expect_local()); match self.hir().get(hir_id) { Node::Item(item) => { match item.kind { diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index e63d1bbdc4dcc..5d9408d74297c 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -2668,12 +2668,12 @@ impl<'tcx> TyCtxt<'tcx> { pub fn opt_item_name(self, def_id: DefId) -> Option { def_id .as_local() - .and_then(|def_id| self.hir().get(self.hir().as_local_hir_id(def_id).unwrap()).ident()) + .and_then(|def_id| self.hir().get(self.hir().as_local_hir_id(def_id)).ident()) } pub fn opt_associated_item(self, def_id: DefId) -> Option { let is_associated_item = if let Some(def_id) = def_id.as_local() { - match self.hir().get(self.hir().as_local_hir_id(def_id).unwrap()) { + match self.hir().get(self.hir().as_local_hir_id(def_id)) { Node::TraitItem(_) | Node::ImplItem(_) => true, _ => false, } @@ -2828,7 +2828,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Gets the attributes of a definition. pub fn get_attrs(self, did: DefId) -> Attributes<'tcx> { if let Some(did) = did.as_local() { - self.hir().attrs(self.hir().as_local_hir_id(did).unwrap()) + self.hir().attrs(self.hir().as_local_hir_id(did)) } else { self.item_attrs(did) } @@ -2867,7 +2867,7 @@ impl<'tcx> TyCtxt<'tcx> { /// with the name of the crate containing the impl. pub fn span_of_impl(self, impl_did: DefId) -> Result { if let Some(impl_did) = impl_did.as_local() { - let hir_id = self.hir().as_local_hir_id(impl_did).unwrap(); + let hir_id = self.hir().as_local_hir_id(impl_did); Ok(self.hir().span(hir_id)) } else { Err(self.crate_name(impl_did.krate)) @@ -2928,7 +2928,7 @@ pub struct AdtSizedConstraint<'tcx>(pub &'tcx [Ty<'tcx>]); /// Yields the parent function's `DefId` if `def_id` is an `impl Trait` definition. pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option { if let Some(def_id) = def_id.as_local() { - if let Node::Item(item) = tcx.hir().get(tcx.hir().as_local_hir_id(def_id).unwrap()) { + if let Node::Item(item) = tcx.hir().get(tcx.hir().as_local_hir_id(def_id)) { if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind { return opaque_ty.impl_trait_fn; } diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 284e609d7e4ff..0727533223f66 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -621,7 +621,7 @@ pub trait PrettyPrinter<'tcx>: // FIXME(eddyb) should use `def_span`. if let Some(hir_id) = - did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did).unwrap()) + did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did)) { p!(write("@{:?}", self.tcx().hir().span(hir_id))); @@ -669,7 +669,7 @@ pub trait PrettyPrinter<'tcx>: // FIXME(eddyb) should use `def_span`. if let Some(hir_id) = - did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did).unwrap()) + did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did)) { if self.tcx().sess.opts.debugging_opts.span_free_formats { p!(write("@"), print_def_path(did, substs)); diff --git a/src/librustc_middle/ty/sty.rs b/src/librustc_middle/ty/sty.rs index 7ac4b6bb15700..b769f559aae98 100644 --- a/src/librustc_middle/ty/sty.rs +++ b/src/librustc_middle/ty/sty.rs @@ -2261,7 +2261,7 @@ impl<'tcx> Const<'tcx> { ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => { // Find the name and index of the const parameter by indexing the generics of // the parent item and construct a `ParamConst`. - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let item_id = tcx.hir().get_parent_node(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id.to_def_id()); diff --git a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs index 201660552194c..ce0929d0a9191 100644 --- a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs @@ -191,7 +191,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let needs_note = match ty.kind { ty::Closure(id, _) => { let tables = self.infcx.tcx.typeck_tables_of(id); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(id.expect_local()).unwrap(); + let hir_id = self.infcx.tcx.hir().as_local_hir_id(id.expect_local()); tables.closure_kind_origins().get(hir_id).is_none() } @@ -865,7 +865,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { if let Some(fn_hir_id) = self .mir_def_id .as_local() - .map(|def_id| self.infcx.tcx.hir().as_local_hir_id(def_id).unwrap()) + .map(|def_id| self.infcx.tcx.hir().as_local_hir_id(def_id)) { err.span_label( drop_span, @@ -1767,7 +1767,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ) -> Option> { debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig); let is_closure = self.infcx.tcx.is_closure(did); - let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did.as_local()?)?; + let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did.as_local()?); let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?; // We need to work out which arguments to highlight. We do this by looking diff --git a/src/librustc_mir/borrow_check/diagnostics/mod.rs b/src/librustc_mir/borrow_check/diagnostics/mod.rs index c339dd46eece0..80b1ae413fc17 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mod.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mod.rs @@ -97,7 +97,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { debug!("add_moved_or_invoked_closure_note: closure={:?}", closure); if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind { - let hir_id = self.infcx.tcx.hir().as_local_hir_id(did.expect_local()).unwrap(); + let hir_id = self.infcx.tcx.hir().as_local_hir_id(did.expect_local()); if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did).closure_kind_origins().get(hir_id) @@ -119,7 +119,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { // Check if we are just moving a closure after it has been invoked. if let Some(target) = target { if let ty::Closure(did, _) = self.body.local_decls[target].ty.kind { - let hir_id = self.infcx.tcx.hir().as_local_hir_id(did.expect_local()).unwrap(); + let hir_id = self.infcx.tcx.hir().as_local_hir_id(did.expect_local()); if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did).closure_kind_origins().get(hir_id) @@ -804,7 +804,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "closure_span: def_id={:?} target_place={:?} places={:?}", def_id, target_place, places ); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id.as_local()?)?; + let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id.as_local()?); let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind; debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr); if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr { diff --git a/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs b/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs index a83b30c4a6ef7..8973d42a4a7ed 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs @@ -490,7 +490,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { err.span_label(sp, format!("cannot {}", act)); let hir = self.infcx.tcx.hir(); - let closure_id = hir.as_local_hir_id(self.mir_def_id.expect_local()).unwrap(); + let closure_id = hir.as_local_hir_id(self.mir_def_id.expect_local()); let fn_call_id = hir.get_parent_node(closure_id); let node = hir.get(fn_call_id); let item_id = hir.get_parent_item(fn_call_id); @@ -689,7 +689,7 @@ fn annotate_struct_field( if let ty::Adt(def, _) = ty.kind { let field = def.all_fields().nth(field.index())?; // Use the HIR types to construct the diagnostic message. - let hir_id = tcx.hir().as_local_hir_id(field.did.as_local()?)?; + let hir_id = tcx.hir().as_local_hir_id(field.did.as_local()?); let node = tcx.hir().find(hir_id)?; // Now we're dealing with the actual struct that we're going to suggest a change to, // we can expect a field that is an immutable reference to a type. diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 2088879f8c606..209438d72ee2c 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -237,12 +237,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { } ty::BoundRegion::BrEnv => { - let mir_hir_id = self - .infcx - .tcx - .hir() - .as_local_hir_id(self.mir_def_id.expect_local()) - .unwrap(); + let mir_hir_id = + self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id.expect_local()); let def_ty = self.regioncx.universal_regions().defining_ty; if let DefiningTy::Closure(_, substs) = def_ty { @@ -328,7 +324,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { argument_ty: Ty<'tcx>, argument_index: usize, ) -> Option { - let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id.as_local()?)?; + let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id.as_local()?); let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?; let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?; match argument_hir_ty.kind { @@ -636,7 +632,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap()); let type_name = self.infcx.extract_type_name(&return_ty, Some(highlight)).0; - let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local()).unwrap(); + let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local()); let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) { hir::Node::Expr(hir::Expr { @@ -688,7 +684,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap()); let type_name = self.infcx.extract_type_name(&yield_ty, Some(highlight)).0; - let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local()).unwrap(); + let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id.expect_local()); let yield_span = match tcx.hir().get(mir_hir_id) { hir::Node::Expr(hir::Expr { diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 429cfab330b99..8dbe91fcd25ff 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -116,7 +116,7 @@ fn do_mir_borrowck<'a, 'tcx>( let tcx = infcx.tcx; let param_env = tcx.param_env(def_id); - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id); let mut local_names = IndexVec::from_elem(None, &input_body.local_decls); for var_debug_info in &input_body.var_debug_info { diff --git a/src/librustc_mir/borrow_check/universal_regions.rs b/src/librustc_mir/borrow_check/universal_regions.rs index de985cb37c56d..e6099ba919225 100644 --- a/src/librustc_mir/borrow_check/universal_regions.rs +++ b/src/librustc_mir/borrow_check/universal_regions.rs @@ -224,7 +224,7 @@ impl<'tcx> UniversalRegions<'tcx> { param_env: ty::ParamEnv<'tcx>, ) -> Self { let tcx = infcx.tcx; - let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id).unwrap(); + let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id); UniversalRegionsBuilder { infcx, mir_def_id: mir_def_id.to_def_id(), mir_hir_id, param_env } .build() } diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs index 588675d8eda84..ecef6500687a3 100644 --- a/src/librustc_mir/const_eval/eval_queries.rs +++ b/src/librustc_mir/const_eval/eval_queries.rs @@ -346,7 +346,7 @@ pub fn const_eval_raw_provider<'tcx>( // validation thus preventing such a hard error from being a backwards // compatibility hazard Some(DefKind::Const) | Some(DefKind::AssocConst) => { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); err.report_as_lint( tcx.at(tcx.def_span(def_id)), "any use of this value will cause an error", @@ -369,7 +369,7 @@ pub fn const_eval_raw_provider<'tcx>( err.report_as_lint( tcx.at(span), "reaching this expression at runtime will panic or abort", - tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(), + tcx.hir().as_local_hir_id(def_id.expect_local()), Some(err.span), ) } diff --git a/src/librustc_mir/const_eval/fn_queries.rs b/src/librustc_mir/const_eval/fn_queries.rs index c96b65b063eda..0d0caab68e0cb 100644 --- a/src/librustc_mir/const_eval/fn_queries.rs +++ b/src/librustc_mir/const_eval/fn_queries.rs @@ -90,7 +90,7 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool { /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether /// said intrinsic is on the whitelist for being const callable. fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index ace69ad3ae8c0..dfffcfa9cf2f9 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -429,9 +429,7 @@ fn check_recursion_limit<'tcx>( // infinite expansion. if adjusted_recursion_depth > *tcx.sess.recursion_limit.get() { let error = format!("reached the recursion limit while instantiating `{}`", instance); - if let Some(hir_id) = - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) - { + if let Some(hir_id) = def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { tcx.sess.span_fatal(tcx.hir().span(hir_id), &error); } else { tcx.sess.fatal(&error); diff --git a/src/librustc_mir/transform/check_consts/mod.rs b/src/librustc_mir/transform/check_consts/mod.rs index 77ee6e3b29321..db60a8ac40939 100644 --- a/src/librustc_mir/transform/check_consts/mod.rs +++ b/src/librustc_mir/transform/check_consts/mod.rs @@ -67,7 +67,7 @@ impl ConstKind { pub fn for_item(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Option { use hir::BodyOwnerKind as HirKind; - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); let mode = match tcx.hir().body_owner_kind(hir_id) { HirKind::Closure => return None, diff --git a/src/librustc_mir/transform/check_consts/validation.rs b/src/librustc_mir/transform/check_consts/validation.rs index f4ff675c09201..0c738ef9f3f05 100644 --- a/src/librustc_mir/transform/check_consts/validation.rs +++ b/src/librustc_mir/transform/check_consts/validation.rs @@ -190,7 +190,7 @@ impl Validator<'a, 'mir, 'tcx> { const_kind == Some(ConstKind::Static) && !tcx.has_attr(def_id, sym::thread_local); if should_check_for_sync { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); check_return_ty_is_sync(tcx, &body, hir_id); } } diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 4d46c1980db21..f5c50977da418 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -469,7 +469,7 @@ fn check_unused_unsafe( used_unsafe: &FxHashSet, unsafe_blocks: &mut Vec<(hir::HirId, bool)>, ) { - let body_id = tcx.hir().maybe_body_owned_by(tcx.hir().as_local_hir_id(def_id).unwrap()); + let body_id = tcx.hir().maybe_body_owned_by(tcx.hir().as_local_hir_id(def_id)); let body_id = match body_id { Some(body) => body, @@ -494,7 +494,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult let param_env = tcx.param_env(def_id); - let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()); let (const_context, min_const_fn) = match tcx.hir().body_owner_kind(id) { hir::BodyOwnerKind::Closure => (false, false), hir::BodyOwnerKind::Fn => (is_const_fn(tcx, def_id), is_min_const_fn(tcx, def_id)), @@ -519,7 +519,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: DefId) -> UnsafetyCheckResult } fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: DefId) { - let lint_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let lint_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); tcx.struct_span_lint_hir(SAFE_PACKED_BORROWS, lint_hir_id, tcx.def_span(def_id), |lint| { // FIXME: when we make this a hard error, this should have its diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 434cad299235a..704f2df251eee 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -66,7 +66,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp { } use rustc_middle::hir::map::blocks::FnLikeNode; - let hir_id = tcx.hir().as_local_hir_id(source.def_id().expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(source.def_id().expect_local()); let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some(); let is_assoc_const = match tcx.def_kind(source.def_id()) { diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 1702120121dbf..3be09e6004686 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -69,7 +69,7 @@ impl Inliner<'tcx> { let param_env = self.tcx.param_env(self.source.def_id()).with_reveal_all(); // Only do inlining into fn bodies. - let id = self.tcx.hir().as_local_hir_id(self.source.def_id().expect_local()).unwrap(); + let id = self.tcx.hir().as_local_hir_id(self.source.def_id().expect_local()); if self.tcx.hir().body_owner_kind(id).is_fn_or_closure() && self.source.promoted.is_none() { for (bb, bb_data) in caller_body.basic_blocks().iter_enumerated() { if let Some(callsite) = diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index adffd6b84026d..9acb5fa1c1f39 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -13,7 +13,7 @@ type McfResult = Result<(), (Span, Cow<'static, str>)>; pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -> McfResult { // Prevent const trait methods from being annotated as `stable`. if tcx.features().staged_api { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) { return Err((body.span, "trait methods cannot be stable const fn".into())); } diff --git a/src/librustc_mir/util/liveness.rs b/src/librustc_mir/util/liveness.rs index e722f73b0b8fb..66df67429f53b 100644 --- a/src/librustc_mir/util/liveness.rs +++ b/src/librustc_mir/util/liveness.rs @@ -285,7 +285,7 @@ fn dump_matched_mir_node<'tcx>( ) { let mut file_path = PathBuf::new(); file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir)); - let item_id = tcx.hir().as_local_hir_id(source.def_id().expect_local()).unwrap(); + let item_id = tcx.hir().as_local_hir_id(source.def_id().expect_local()); let file_name = format!("rustc.node{}{}-liveness.mir", item_id, pass_name); file_path.push(&file_name); let _ = fs::File::create(&file_path).and_then(|file| { diff --git a/src/librustc_mir_build/build/mod.rs b/src/librustc_mir_build/build/mod.rs index 5ec7d729862bc..e298a0cd15439 100644 --- a/src/librustc_mir_build/build/mod.rs +++ b/src/librustc_mir_build/build/mod.rs @@ -26,7 +26,7 @@ crate fn mir_built(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::steal::Steal, def_id: LocalDefId) -> BodyAndCache<'_> { - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id); // Figure out what primary body this item has. let (body_id, return_ty_span) = match tcx.hir().get(id) { diff --git a/src/librustc_mir_build/hair/cx/expr.rs b/src/librustc_mir_build/hair/cx/expr.rs index 8edd3ba6f3056..f8dd106478992 100644 --- a/src/librustc_mir_build/hair/cx/expr.rs +++ b/src/librustc_mir_build/hair/cx/expr.rs @@ -690,7 +690,7 @@ fn convert_path_expr<'a, 'tcx>( } Res::Def(DefKind::ConstParam, def_id) => { - let hir_id = cx.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = cx.tcx.hir().as_local_hir_id(def_id.expect_local()); let item_id = cx.tcx.hir().get_parent_node(hir_id); let item_def_id = cx.tcx.hir().local_def_id(item_id); let generics = cx.tcx.generics_of(item_def_id); diff --git a/src/librustc_mir_build/hair/pattern/check_match.rs b/src/librustc_mir_build/hair/pattern/check_match.rs index 14f36038232a6..c90634e511bb1 100644 --- a/src/librustc_mir_build/hair/pattern/check_match.rs +++ b/src/librustc_mir_build/hair/pattern/check_match.rs @@ -23,7 +23,7 @@ use std::slice; crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) { let body_id = match def_id.as_local() { None => return, - Some(id) => tcx.hir().body_owned_by(tcx.hir().as_local_hir_id(id).unwrap()), + Some(id) => tcx.hir().body_owned_by(tcx.hir().as_local_hir_id(id)), }; let mut visitor = diff --git a/src/librustc_mir_build/lints.rs b/src/librustc_mir_build/lints.rs index 6cfb8ac2e5154..551071e7fac01 100644 --- a/src/librustc_mir_build/lints.rs +++ b/src/librustc_mir_build/lints.rs @@ -11,7 +11,7 @@ use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION; use rustc_span::Span; crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); if let Some(fn_like_node) = FnLikeNode::from_node(tcx.hir().get(hir_id)) { if let FnKind::Closure(_) = fn_like_node.kind() { @@ -37,7 +37,7 @@ crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId) { vis.reachable_recursive_calls.sort(); - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span(hir_id)); tcx.struct_span_lint_hir(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| { let mut db = lint.build("function cannot return without recursing"); diff --git a/src/librustc_passes/dead.rs b/src/librustc_passes/dead.rs index 6389920ba37c3..26b92bbc72627 100644 --- a/src/librustc_passes/dead.rs +++ b/src/librustc_passes/dead.rs @@ -51,7 +51,7 @@ struct MarkSymbolVisitor<'a, 'tcx> { impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { fn check_def_id(&mut self, def_id: DefId) { if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id); if should_explore(self.tcx, hir_id) || self.struct_constructors.contains_key(&hir_id) { self.worklist.push(hir_id); } @@ -61,7 +61,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> { fn insert_def_id(&mut self, def_id: DefId) { if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id); debug_assert!(!should_explore(self.tcx, hir_id)); self.live_symbols.insert(hir_id); } @@ -451,7 +451,7 @@ fn create_and_seed_worklist<'tcx>( .chain( // Seed entry point tcx.entry_fn(LOCAL_CRATE) - .map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap()), + .map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id.expect_local())), ) .collect::>(); @@ -536,7 +536,7 @@ impl DeadVisitor<'tcx> { for &impl_did in inherent_impls.iter() { for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] { if let Some(item_hir_id) = - item_did.as_local().map(|did| self.tcx.hir().as_local_hir_id(did).unwrap()) + item_did.as_local().map(|did| self.tcx.hir().as_local_hir_id(did)) { if self.live_symbols.contains(&item_hir_id) { return true; diff --git a/src/librustc_passes/reachable.rs b/src/librustc_passes/reachable.rs index a98b2b6f400ef..3ad75d0248b86 100644 --- a/src/librustc_passes/reachable.rs +++ b/src/librustc_passes/reachable.rs @@ -53,13 +53,9 @@ fn method_might_be_inlined( return true; } } - if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) { - match tcx.hir().find(impl_hir_id) { - Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs), - Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"), - } - } else { - span_bug!(impl_item.span, "found a foreign impl as a parent of a local method") + match tcx.hir().find(tcx.hir().as_local_hir_id(impl_src)) { + Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs), + Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"), } } @@ -108,9 +104,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> { } Some(res) => { if let Some((hir_id, def_id)) = res.opt_def_id().and_then(|def_id| { - def_id - .as_local() - .map(|def_id| (self.tcx.hir().as_local_hir_id(def_id).unwrap(), def_id)) + def_id.as_local().map(|def_id| (self.tcx.hir().as_local_hir_id(def_id), def_id)) }) { if self.def_id_represents_local_inlined_item(def_id.to_def_id()) { self.worklist.push(hir_id); @@ -144,7 +138,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> { // eligible for inlining and false otherwise. fn def_id_represents_local_inlined_item(&self, def_id: DefId) -> bool { let hir_id = match def_id.as_local() { - Some(def_id) => self.tcx.hir().as_local_hir_id(def_id).unwrap(), + Some(def_id) => self.tcx.hir().as_local_hir_id(def_id), None => { return false; } @@ -176,7 +170,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> { // Check the impl. If the generics on the self // type of the impl require inlining, this method // does too. - let impl_hir_id = self.tcx.hir().as_local_hir_id(impl_did).unwrap(); + let impl_hir_id = self.tcx.hir().as_local_hir_id(impl_did); match self.tcx.hir().expect_item(impl_hir_id).kind { hir::ItemKind::Impl { .. } => { let generics = self.tcx.generics_of(impl_did); @@ -362,9 +356,8 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx // FIXME(#53488) remove `let` let tcx = self.tcx; self.worklist.extend( - tcx.provided_trait_methods(trait_def_id).map(|assoc| { - tcx.hir().as_local_hir_id(assoc.def_id.expect_local()).unwrap() - }), + tcx.provided_trait_methods(trait_def_id) + .map(|assoc| tcx.hir().as_local_hir_id(assoc.def_id.expect_local())), ); } } @@ -403,8 +396,7 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, crate_num: CrateNum) -> &'tcx HirIdSet reachable_context.worklist.extend(access_levels.map.iter().map(|(id, _)| *id)); for item in tcx.lang_items().items().iter() { if let Some(did) = *item { - if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did).unwrap()) - { + if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did)) { reachable_context.worklist.push(hir_id); } } diff --git a/src/librustc_passes/region.rs b/src/librustc_passes/region.rs index 7fb534def3b8b..2bb0ab97beb32 100644 --- a/src/librustc_passes/region.rs +++ b/src/librustc_passes/region.rs @@ -805,7 +805,7 @@ fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree { return tcx.region_scope_tree(closure_base_def_id); } - let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()); let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(id) { let mut visitor = RegionResolutionVisitor { tcx, diff --git a/src/librustc_passes/upvars.rs b/src/librustc_passes/upvars.rs index 0cdc4f5bd8f74..fb986caa415c9 100644 --- a/src/librustc_passes/upvars.rs +++ b/src/librustc_passes/upvars.rs @@ -15,7 +15,7 @@ pub fn provide(providers: &mut Providers<'_>) { return None; } - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(hir_id)?); let mut local_collector = LocalCollector::default(); diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index c268189c5b2e7..7e1a53d4c3f13 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -235,7 +235,7 @@ fn def_id_visibility<'tcx>( tcx: TyCtxt<'tcx>, def_id: DefId, ) -> (ty::Visibility, Span, &'static str) { - match def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) { + match def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { Some(hir_id) => { let vis = match tcx.hir().get(hir_id) { Node::Item(item) => &item.vis, @@ -446,7 +446,7 @@ impl VisibilityLike for Option { fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self { cmp::min( if let Some(hir_id) = - def_id.as_local().map(|def_id| find.tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| find.tcx.hir().as_local_hir_id(def_id)) { find.access_levels.map.get(&hir_id).cloned() } else { @@ -549,9 +549,8 @@ impl EmbargoVisitor<'tcx> { if export.vis.is_accessible_from(defining_mod, self.tcx) { if let Res::Def(def_kind, def_id) = export.res { let vis = def_id_visibility(self.tcx, def_id).0; - if let Some(hir_id) = def_id - .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + if let Some(hir_id) = + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) { self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod); } @@ -660,7 +659,7 @@ impl EmbargoVisitor<'tcx> { // there will be no corresponding item. .filter(|def_id| def_id.index != CRATE_DEF_INDEX || def_id.krate != LOCAL_CRATE) .and_then(|def_id| { - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) }) .map(|module_hir_id| self.tcx.hir().expect_item(module_hir_id)) { @@ -917,7 +916,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> { if let Some(def_id) = export.res.opt_def_id() { if let Some(hir_id) = def_id .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + .map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) { self.update(hir_id, Some(AccessLevel::Exported)); } @@ -1007,7 +1006,7 @@ impl DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { } fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool { if let Some(def_id) = def_id.as_local() { - let hir_id = self.ev.tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = self.ev.tcx.hir().as_local_hir_id(def_id); if let ((ty::Visibility::Public, ..), _) | (_, Some(AccessLevel::ReachableFromImplTrait)) = (def_id_visibility(self.tcx(), def_id.to_def_id()), self.access_level) @@ -1457,7 +1456,7 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { if let Some(did) = did.as_local() { // .. and it corresponds to a private type in the AST (this returns // `None` for type parameters). - match self.tcx.hir().find(self.tcx.hir().as_local_hir_id(did).unwrap()) { + match self.tcx.hir().find(self.tcx.hir().as_local_hir_id(did)) { Some(Node::Item(ref item)) => !item.vis.node.is_pub(), Some(_) | None => false, } @@ -1576,7 +1575,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { let did = tr.path.res.def_id(); if let Some(did) = did.as_local() { - self.trait_is_public(self.tcx.hir().as_local_hir_id(did).unwrap()) + self.trait_is_public(self.tcx.hir().as_local_hir_id(did)) } else { true // external traits must be public } @@ -1837,7 +1836,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> { } let hir_id = match def_id.as_local() { - Some(def_id) => self.tcx.hir().as_local_hir_id(def_id).unwrap(), + Some(def_id) => self.tcx.hir().as_local_hir_id(def_id), None => return false, }; diff --git a/src/librustc_resolve/late/lifetimes.rs b/src/librustc_resolve/late/lifetimes.rs index a7225aa36da05..3b9331c394461 100644 --- a/src/librustc_resolve/late/lifetimes.rs +++ b/src/librustc_resolve/late/lifetimes.rs @@ -598,7 +598,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { if let Some(Region::LateBound(_, def_id, _)) = def { if let Some(hir_id) = def_id .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + .map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) { // Ensure that the parent of the def is an item, not HRTB let parent_id = self.tcx.hir().get_parent_node(hir_id); @@ -1169,8 +1169,7 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) { if let Some(def) = lifetimes.get(&hir::ParamName::Plain(label.normalize_to_macros_2_0())) { - let hir_id = - tcx.hir().as_local_hir_id(def.id().unwrap().expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def.id().unwrap().expect_local()); signal_shadowing_problem( tcx, @@ -1541,7 +1540,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { match lifetimeuseset { Some(LifetimeUseSet::One(lifetime)) => { - let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()); debug!("hir id first={:?}", hir_id); if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { Node::Lifetime(hir_lifetime) => Some(( @@ -1562,7 +1561,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { if let Some(parent_def_id) = self.tcx.parent(def_id) { if let Some(parent_hir_id) = parent_def_id .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + .map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) { // lifetimes in `derive` expansions don't count (Issue #53738) if self @@ -1605,7 +1604,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { debug!("not one use lifetime"); } None => { - let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()); if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { Node::Lifetime(hir_lifetime) => Some(( hir_lifetime.hir_id, @@ -1956,7 +1955,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { let map = &self.map; let unsubst = if let Some(id) = - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) { &map.object_lifetime_defaults[&id] } else { @@ -2665,11 +2664,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Scope::Binder { ref lifetimes, s, .. } => { if let Some(&def) = lifetimes.get(¶m.name.normalize_to_macros_2_0()) { - let hir_id = self - .tcx - .hir() - .as_local_hir_id(def.id().unwrap().expect_local()) - .unwrap(); + let hir_id = + self.tcx.hir().as_local_hir_id(def.id().unwrap().expect_local()); signal_shadowing_problem( self.tcx, diff --git a/src/librustc_symbol_mangling/lib.rs b/src/librustc_symbol_mangling/lib.rs index d16d61454249e..09cd4efd45dc0 100644 --- a/src/librustc_symbol_mangling/lib.rs +++ b/src/librustc_symbol_mangling/lib.rs @@ -174,7 +174,7 @@ fn compute_symbol_name( let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_proc_macro_decls_symbol(disambiguator); } - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); match tcx.hir().get(hir_id) { Node::ForeignItem(_) => true, _ => false, diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index 92070731e7405..9bc2f2829cf9a 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1037,7 +1037,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { // } // ``` if let Some(opaque_hir_id) = - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { let parent_def_id = self.parent_def_id; let def_scope_default = || { @@ -1219,7 +1219,7 @@ pub fn may_define_opaque_type( def_id: LocalDefId, opaque_hir_id: hir::HirId, ) -> bool { - let mut hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let mut hir_id = tcx.hir().as_local_hir_id(def_id); // Named opaque types can be defined by any siblings or children of siblings. let scope = tcx.hir().get_defining_scope(opaque_hir_id); diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 101b53bffa633..9bc149f09d64f 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -486,8 +486,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { self.tcx.sess.source_map().guess_head_span( self.tcx.hir().span_if_local(closure_def_id).unwrap(), ); - let hir_id = - self.tcx.hir().as_local_hir_id(closure_def_id.expect_local()).unwrap(); + let hir_id = self.tcx.hir().as_local_hir_id(closure_def_id.expect_local()); let mut err = struct_span_err!( self.tcx.sess, closure_span, diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index e006ac3f3f651..e7fe7eb795029 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -427,7 +427,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { }; let hir = self.tcx.hir(); - let hir_id = hir.as_local_hir_id(def_id.as_local()?)?; + let hir_id = hir.as_local_hir_id(def_id.as_local()?); let parent_node = hir.get_parent_node(hir_id); match hir.find(parent_node) { Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => { @@ -1207,7 +1207,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let generator_body = generator_did .as_local() - .map(|def_id| hir.as_local_hir_id(def_id).unwrap()) + .map(|def_id| hir.as_local_hir_id(def_id)) .and_then(|hir_id| hir.maybe_body_owned_by(hir_id)) .map(|body_id| hir.body(body_id)); let mut visitor = AwaitsVisitor::default(); @@ -1349,7 +1349,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { .tcx .parent(generator_did) .and_then(|parent_did| parent_did.as_local()) - .and_then(|parent_did| hir.as_local_hir_id(parent_did)) + .map(|parent_did| hir.as_local_hir_id(parent_did)) .and_then(|parent_hir_id| hir.opt_name(parent_hir_id)) .map(|name| { format!("future returned by `{}` is not {}", name, trait_name) diff --git a/src/librustc_trait_selection/traits/specialize/mod.rs b/src/librustc_trait_selection/traits/specialize/mod.rs index 95bc64fdaff23..61583669c6b5c 100644 --- a/src/librustc_trait_selection/traits/specialize/mod.rs +++ b/src/librustc_trait_selection/traits/specialize/mod.rs @@ -452,7 +452,7 @@ fn report_conflicting_impls( }; tcx.struct_span_lint_hir( lint, - tcx.hir().as_local_hir_id(impl_def_id).unwrap(), + tcx.hir().as_local_hir_id(impl_def_id), impl_span, decorate, ) diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index 60c6e761a1cef..320e3267bc515 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -145,7 +145,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( let trait_item = trait_ref .def_id .as_local() - .and_then(|def_id| tcx.hir().find(tcx.hir().as_local_hir_id(def_id).unwrap())); + .and_then(|def_id| tcx.hir().find(tcx.hir().as_local_hir_id(def_id))); let (trait_name, trait_generics) = match trait_item { Some(hir::Node::Item(hir::Item { ident, diff --git a/src/librustc_traits/lowering/environment.rs b/src/librustc_traits/lowering/environment.rs index b598b6f4fb4e4..e7fa245fd4055 100644 --- a/src/librustc_traits/lowering/environment.rs +++ b/src/librustc_traits/lowering/environment.rs @@ -174,7 +174,7 @@ crate fn environment(tcx: TyCtxt<'_>, def_id: DefId) -> Environment<'_> { // could bound lifetimes. .map(Clause::ForAll); - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); enum NodeKind { diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index 1e215ebc42f1e..16f395a4fec8d 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -128,7 +128,7 @@ fn associated_item_from_impl_item_ref( } fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { - let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()); let parent_id = tcx.hir().get_parent_item(id); let parent_def_id = tcx.hir().local_def_id(parent_id); let parent_item = tcx.hir().expect_item(parent_id); @@ -166,7 +166,7 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { } fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(hir_id); if let hir::ItemKind::Impl { defaultness, .. } = item.kind { defaultness @@ -200,7 +200,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain } fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { - let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(id); match item.kind { hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter( @@ -267,7 +267,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { let body_id = def_id .as_local() - .map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + .map(|def_id| tcx.hir().as_local_hir_id(def_id)) .map_or(hir::CRATE_HIR_ID, |id| { tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id) }); @@ -355,7 +355,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { /// Check if a function is async. fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 5b2e0935acc04..fcfb43fff7423 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -147,7 +147,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { def: Option<&ty::GenericParamDef>, ) -> ty::Region<'tcx> { let tcx = self.tcx(); - let lifetime_name = |def_id| tcx.hir().name(tcx.hir().as_local_hir_id(def_id).unwrap()); + let lifetime_name = |def_id| tcx.hir().name(tcx.hir().as_local_hir_id(def_id)); let r = match tcx.named_region(lifetime.hir_id) { Some(rl::Region::Static) => tcx.lifetimes.re_static, @@ -1988,7 +1988,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { debug!("find_bound_for_assoc_item: predicates={:#?}", predicates); - let param_hir_id = tcx.hir().as_local_hir_id(ty_param_def_id).unwrap(); + let param_hir_id = tcx.hir().as_local_hir_id(ty_param_def_id); let param_name = tcx.hir().ty_param_name(param_hir_id); self.one_bound_for_assoc_type( || { @@ -2372,7 +2372,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { let parent_def_id = def_id .and_then(|def_id| { - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) }) .map(|hir_id| tcx.hir().get_parent_did(hir_id).to_def_id()); @@ -2664,7 +2664,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { assert_eq!(opt_self_ty, None); self.prohibit_generics(path.segments); - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let item_id = tcx.hir().get_parent_node(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id); diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 6ebd7cea8804b..6165a4b1575f3 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -1394,7 +1394,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { let ty = AstConv::ast_ty_to_ty(fcx, ty); // Get the `impl Trait`'s `DefId`. if let ty::Opaque(def_id, _) = ty.kind { - let hir_id = fcx.tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = fcx.tcx.hir().as_local_hir_id(def_id.expect_local()); // Get the `impl Trait`'s `Item` so that we can get its trait bounds and // get the `Trait`'s `DefId`. if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) = diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index dbdb26afaec2d..f834f74d5df18 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -76,7 +76,7 @@ fn compare_predicate_entailment<'tcx>( // This node-id should be used for the `body_id` field on each // `ObligationCause` (and the `FnCtxt`). This is what // `regionck_item` expects. - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()).unwrap(); + let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()); let cause = ObligationCause { span: impl_m_span, @@ -399,7 +399,7 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( trait_sig: ty::FnSig<'tcx>, ) -> (Span, Option) { let tcx = infcx.tcx; - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()).unwrap(); + let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()); let (impl_m_output, impl_m_iter) = match tcx.hir().expect_impl_item(impl_m_hir_id).kind { ImplItemKind::Fn(ref impl_m_sig, _) => { (&impl_m_sig.decl.output, impl_m_sig.decl.inputs.iter()) @@ -410,7 +410,7 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( match *terr { TypeError::Mutability => { if let Some(trait_m_hir_id) = - trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { let trait_m_iter = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => trait_m_sig.decl.inputs.iter(), @@ -439,7 +439,7 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( } TypeError::Sorts(ExpectedFound { .. }) => { if let Some(trait_m_hir_id) = - trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { let (trait_m_output, trait_m_iter) = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { @@ -592,7 +592,7 @@ fn compare_number_of_generics<'tcx>( err_occurred = true; let (trait_spans, impl_trait_spans) = if let Some(trait_hir_id) = - trait_.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()) + trait_.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { let trait_item = tcx.hir().expect_trait_item(trait_hir_id); if trait_item.generics.params.is_empty() { @@ -618,7 +618,7 @@ fn compare_number_of_generics<'tcx>( (trait_span.map(|s| vec![s]), vec![]) }; - let impl_hir_id = tcx.hir().as_local_hir_id(impl_.def_id.expect_local()).unwrap(); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_.def_id.expect_local()); let impl_item = tcx.hir().expect_impl_item(impl_hir_id); let impl_item_impl_trait_spans: Vec = impl_item .generics @@ -710,7 +710,7 @@ fn compare_number_of_method_arguments<'tcx>( let impl_number_args = impl_m_fty.inputs().skip_binder().len(); if trait_number_args != impl_number_args { let trait_span = if let Some(def_id) = trait_m.def_id.as_local() { - let trait_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let trait_id = tcx.hir().as_local_hir_id(def_id); match tcx.hir().expect_trait_item(trait_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => { let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 }; @@ -733,7 +733,7 @@ fn compare_number_of_method_arguments<'tcx>( } else { trait_item_span }; - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()).unwrap(); + let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()); let impl_span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind { ImplItemKind::Fn(ref impl_m_sig, _) => { let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 }; @@ -815,7 +815,7 @@ fn compare_synthetic_generics<'tcx>( impl_m_type_params.zip(trait_m_type_params) { if impl_synthetic != trait_synthetic { - let impl_hir_id = tcx.hir().as_local_hir_id(impl_def_id.expect_local()).unwrap(); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_def_id.expect_local()); let impl_span = tcx.hir().span(impl_hir_id); let trait_span = tcx.def_span(trait_def_id); let mut err = struct_span_err!( @@ -836,10 +836,10 @@ fn compare_synthetic_generics<'tcx>( // FIXME: this is obviously suboptimal since the name can already be used // as another generic argument let new_name = tcx.sess.source_map().span_to_snippet(trait_span).ok()?; - let trait_m = tcx.hir().as_local_hir_id(trait_m.def_id.as_local()?)?; + let trait_m = tcx.hir().as_local_hir_id(trait_m.def_id.as_local()?); let trait_m = tcx.hir().trait_item(hir::TraitItemId { hir_id: trait_m }); - let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?)?; + let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?); let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m }); // in case there are no generics, take the spot between the function name @@ -873,7 +873,7 @@ fn compare_synthetic_generics<'tcx>( (None, Some(hir::SyntheticTyParamKind::ImplTrait)) => { err.span_label(impl_span, "expected `impl Trait`, found generic parameter"); (|| { - let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?)?; + let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?); let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m }); let input_tys = match impl_m.kind { hir::ImplItemKind::Fn(ref sig, _) => sig.decl.inputs, @@ -966,7 +966,7 @@ crate fn compare_const_impl<'tcx>( // Create a parameter environment that represents the implementation's // method. - let impl_c_hir_id = tcx.hir().as_local_hir_id(impl_c.def_id.expect_local()).unwrap(); + let impl_c_hir_id = tcx.hir().as_local_hir_id(impl_c.def_id.expect_local()); // Compute placeholder form of impl and trait const tys. let impl_ty = tcx.type_of(impl_c.def_id); @@ -1011,7 +1011,7 @@ crate fn compare_const_impl<'tcx>( ); let trait_c_hir_id = - trait_c.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id).unwrap()); + trait_c.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)); let trait_c_span = trait_c_hir_id.map(|trait_c_hir_id| { // Add a label to the Span containing just the type of the const match tcx.hir().expect_trait_item(trait_c_hir_id).kind { @@ -1099,7 +1099,7 @@ fn compare_type_predicate_entailment( // This `HirId` should be used for the `body_id` field on each // `ObligationCause` (and the `FnCtxt`). This is what // `regionck_item` expects. - let impl_ty_hir_id = tcx.hir().as_local_hir_id(impl_ty.def_id.expect_local()).unwrap(); + let impl_ty_hir_id = tcx.hir().as_local_hir_id(impl_ty.def_id.expect_local()); let cause = ObligationCause { span: impl_ty_span, body_id: impl_ty_hir_id, diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index 40efbd6f95c28..078401ee6a815 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -71,7 +71,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>( drop_impl_ty: Ty<'tcx>, self_type_did: DefId, ) -> Result<(), ErrorReported> { - let drop_impl_hir_id = tcx.hir().as_local_hir_id(drop_impl_did).unwrap(); + let drop_impl_hir_id = tcx.hir().as_local_hir_id(drop_impl_did); // check that the impl type can be made to match the trait type. @@ -190,7 +190,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( // absent. So we report an error that the Drop impl injected a // predicate that is not present on the struct definition. - let self_type_hir_id = tcx.hir().as_local_hir_id(self_type_did).unwrap(); + let self_type_hir_id = tcx.hir().as_local_hir_id(self_type_did); // We can assume the predicates attached to struct/enum definition // hold. diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index bb2bb74340e00..e3df0739ca3e2 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1625,7 +1625,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } let param_def_id = generic_param.def_id; let param_hir_id = match param_def_id.as_local() { - Some(x) => self.tcx.hir().as_local_hir_id(x).unwrap(), + Some(x) => self.tcx.hir().as_local_hir_id(x), None => return, }; let param_span = self.tcx.hir().span(param_hir_id); diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 714330a945bc6..5ae66e8a9adb5 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -580,9 +580,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { if let ty::Adt(def, _) = p.skip_binder().trait_ref.self_ty().kind { let node = def.did.as_local().map(|def_id| { - self.tcx - .hir() - .get(self.tcx.hir().as_local_hir_id(def_id).unwrap()) + self.tcx.hir().get(self.tcx.hir().as_local_hir_id(def_id)) }); if let Some(hir::Node::Item(hir::Item { kind, .. })) = node { if let Some(g) = kind.generics() { @@ -857,7 +855,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { candidates: Vec, ) { let module_did = self.tcx.parent_module(self.body_id); - let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap(); + let module_id = self.tcx.hir().as_local_hir_id(module_did); let krate = self.tcx.hir().krate(); let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id); if let Some(span) = span { @@ -961,7 +959,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .filter(|item| { if let ty::AssocKind::Fn = item.kind { let id = item.def_id.as_local().map(|def_id| { - self.tcx.hir().as_local_hir_id(def_id).unwrap() + self.tcx.hir().as_local_hir_id(def_id) }); if let Some(hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(fn_sig, method), @@ -1054,10 +1052,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let generics = self.tcx.generics_of(table_owner.to_def_id()); let type_param = generics.type_param(param, self.tcx); let hir = &self.tcx.hir(); - if let Some(id) = type_param - .def_id - .as_local() - .map(|def_id| hir.as_local_hir_id(def_id).unwrap()) + if let Some(id) = + type_param.def_id.as_local().map(|def_id| hir.as_local_hir_id(def_id)) { // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: FooBar`, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 15e491b3085e5..d2713292d055f 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -977,7 +977,7 @@ fn diagnostic_only_typeck_tables_of<'tcx>( ) -> &ty::TypeckTables<'tcx> { assert!(def_id.is_local()); let fallback = move || { - let span = tcx.hir().span(tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap()); + let span = tcx.hir().span(tcx.hir().as_local_hir_id(def_id.expect_local())); tcx.sess.delay_span_bug(span, "diagnostic only typeck table used"); tcx.types.err }; @@ -996,7 +996,7 @@ fn typeck_tables_of_with_fallback<'tcx>( return tcx.typeck_tables_of(outer_def_id); } - let id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id.expect_local()); let span = tcx.hir().span(id); // Figure out what primary body this item has. @@ -1334,7 +1334,7 @@ fn check_fn<'a, 'tcx>( } let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id()); - let outer_hir_id = hir.as_local_hir_id(outer_def_id.expect_local()).unwrap(); + let outer_hir_id = hir.as_local_hir_id(outer_def_id.expect_local()); GatherLocalsVisitor { fcx: &fcx, parent_id: outer_hir_id }.visit_body(body); // C-variadic fns also have a `VaList` input that's not listed in `fn_sig` @@ -1431,7 +1431,7 @@ fn check_fn<'a, 'tcx>( // Check that the main return type implements the termination trait. if let Some(term_id) = tcx.lang_items().termination() { if let Some((def_id, EntryFnType::Main)) = tcx.entry_fn(LOCAL_CRATE) { - let main_id = hir.as_local_hir_id(def_id.expect_local()).unwrap(); + let main_id = hir.as_local_hir_id(def_id.expect_local()); if main_id == fn_id { let substs = tcx.mk_substs_trait(declared_ret_ty, &[]); let trait_ref = ty::TraitRef::new(term_id, substs); @@ -1609,7 +1609,7 @@ fn check_opaque<'tcx>( /// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result /// in "inheriting lifetimes". fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: LocalDefId, span: Span) { - let item = tcx.hir().expect_item(tcx.hir().as_local_hir_id(def_id).unwrap()); + let item = tcx.hir().expect_item(tcx.hir().as_local_hir_id(def_id)); debug!( "check_opaque_for_inheriting_lifetimes: def_id={:?} span={:?} item={:?}", def_id, span, item @@ -2427,34 +2427,32 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: &ty::AdtDef) { ); let hir = tcx.hir(); - if let Some(hir_id) = hir.as_local_hir_id(def_spans[0].0.expect_local()) { - if let Node::Item(Item { ident, .. }) = hir.get(hir_id) { - err.span_note( - tcx.def_span(def_spans[0].0), - &format!("`{}` has a `#[repr(align)]` attribute", ident), - ); - } + let hir_id = hir.as_local_hir_id(def_spans[0].0.expect_local()); + if let Node::Item(Item { ident, .. }) = hir.get(hir_id) { + err.span_note( + tcx.def_span(def_spans[0].0), + &format!("`{}` has a `#[repr(align)]` attribute", ident), + ); } if def_spans.len() > 2 { let mut first = true; for (adt_def, span) in def_spans.iter().skip(1).rev() { - if let Some(hir_id) = hir.as_local_hir_id(adt_def.expect_local()) { - if let Node::Item(Item { ident, .. }) = hir.get(hir_id) { - err.span_note( - *span, - &if first { - format!( - "`{}` contains a field of type `{}`", - tcx.type_of(def.did), - ident - ) - } else { - format!("...which contains a field of type `{}`", ident) - }, - ); - first = false; - } + let hir_id = hir.as_local_hir_id(adt_def.expect_local()); + if let Node::Item(Item { ident, .. }) = hir.get(hir_id) { + err.span_note( + *span, + &if first { + format!( + "`{}` contains a field of type `{}`", + tcx.type_of(def.did), + ident + ) + } else { + format!("...which contains a field of type `{}`", ident) + }, + ); + first = false; } } } @@ -2663,7 +2661,7 @@ pub fn check_enum<'tcx>( // Check for duplicate discriminant values if let Some(i) = disr_vals.iter().position(|&x| x.val == discr.val) { let variant_did = def.variants[VariantIdx::new(i)].def_id; - let variant_i_hir_id = tcx.hir().as_local_hir_id(variant_did.expect_local()).unwrap(); + let variant_i_hir_id = tcx.hir().as_local_hir_id(variant_did.expect_local()); let variant_i = tcx.hir().expect_variant(variant_i_hir_id); let i_span = match variant_i.disr_expr { Some(ref expr) => tcx.hir().span(expr.hir_id), @@ -2724,7 +2722,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> { fn get_type_parameter_bounds(&self, _: Span, def_id: DefId) -> ty::GenericPredicates<'tcx> { let tcx = self.tcx; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let item_id = tcx.hir().ty_param_owner(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id); @@ -4943,7 +4941,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { sugg_call = fields.iter().map(|_| "_").collect::>().join(", "); match def_id .as_local() - .map(|def_id| hir.as_local_hir_id(def_id).unwrap()) + .map(|def_id| hir.as_local_hir_id(def_id)) .and_then(|hir_id| hir.def_kind(hir_id)) { Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => { diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index b3c1b5b23e685..ffe1dcbd44403 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -71,7 +71,7 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> { /// not included it frequently leads to confusing errors in fn bodies. So it's better to check /// the types first. pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); let item = tcx.hir().expect_item(hir_id); debug!( @@ -184,7 +184,7 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { } pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); let trait_item = tcx.hir().expect_trait_item(hir_id); let method_sig = match trait_item.kind { @@ -258,7 +258,7 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem } pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); let impl_item = tcx.hir().expect_impl_item(hir_id); let method_sig = match impl_item.kind { @@ -878,7 +878,7 @@ fn check_opaque_types<'fcx, 'tcx>( // FIXME(eddyb) is `generics.parent.is_none()` correct? It seems // potentially risky wrt associated types in `impl`s. if generics.parent.is_none() && def_id.is_local() { - let opaque_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let opaque_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); if may_define_opaque_type(tcx, fn_def_id, opaque_hir_id) { trace!("check_opaque_types: may define, generics={:#?}", generics); let mut seen_params: FxHashMap<_, Vec<_>> = FxHashMap::default(); diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index 3a7d10a183203..0f2d3f4b47dd3 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -424,7 +424,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { fn visit_opaque_types(&mut self, span: Span) { for (&def_id, opaque_defn) in self.fcx.opaque_types.borrow().iter() { - let hir_id = self.tcx().hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = self.tcx().hir().as_local_hir_id(def_id.expect_local()); let instantiated_ty = self.resolve(&opaque_defn.concrete_ty, &hir_id); debug_assert!(!instantiated_ty.has_escaping_bound_vars()); diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index 0c6fc8f32aaf8..da93462fe2681 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -89,10 +89,8 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { // Note that if we carry through to the `extern_mod_stmt_cnum` query // below it'll cause a panic because `def_id` is actually bogus at this // point in time otherwise. - if let Some(id) = tcx.hir().as_local_hir_id(def_id.expect_local()) { - if tcx.hir().find(id).is_none() { - return false; - } + if tcx.hir().find(tcx.hir().as_local_hir_id(def_id.expect_local())).is_none() { + return false; } true }) @@ -115,7 +113,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { }); for extern_crate in &crates_to_lint { - let id = tcx.hir().as_local_hir_id(extern_crate.def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(extern_crate.def_id.expect_local()); let item = tcx.hir().expect_item(id); // If the crate is fully unused, we suggest removing it altogether. diff --git a/src/librustc_typeck/coherence/builtin.rs b/src/librustc_typeck/coherence/builtin.rs index c0613ffdf2ea9..c01c4d90c8e17 100644 --- a/src/librustc_typeck/coherence/builtin.rs +++ b/src/librustc_typeck/coherence/builtin.rs @@ -53,7 +53,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { return; } - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).expect("foreign Drop impl on non-ADT"); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did); let sp = match tcx.hir().expect_item(impl_hir_id).kind { ItemKind::Impl { self_ty, .. } => self_ty.span, _ => bug!("expected Drop impl item"), @@ -72,12 +72,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_copy: impl_did={:?}", impl_did); - let impl_hir_id = if let Some(n) = tcx.hir().as_local_hir_id(impl_did) { - n - } else { - debug!("visit_implementation_of_copy(): impl not in this crate"); - return; - }; + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did); let self_type = tcx.type_of(impl_did); debug!("visit_implementation_of_copy: self_type={:?} (bound)", self_type); @@ -152,7 +147,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef let dispatch_from_dyn_trait = tcx.lang_items().dispatch_from_dyn_trait().unwrap(); - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).unwrap(); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did); let span = tcx.hir().span(impl_hir_id); let source = tcx.type_of(impl_did); @@ -326,7 +321,7 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI }); // this provider should only get invoked for local def-ids - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.expect_local()).unwrap(); + let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.expect_local()); let source = tcx.type_of(impl_did); let trait_ref = tcx.impl_trait_ref(impl_did).unwrap(); diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index ed6cdcc38b23a..f19cc924d9058 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -269,7 +269,7 @@ impl ItemCtxt<'tcx> { } pub fn hir_id(&self) -> hir::HirId { - self.tcx.hir().as_local_hir_id(self.item_def_id.expect_local()).unwrap() + self.tcx.hir().as_local_hir_id(self.item_def_id.expect_local()) } pub fn node(&self) -> hir::Node<'tcx> { @@ -484,7 +484,7 @@ fn type_param_predicates( // written inline like `` or in a where-clause like // `where T: Foo`. - let param_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let param_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let param_owner = tcx.hir().ty_param_owner(param_id); let param_owner_def_id = tcx.hir().local_def_id(param_owner); let generics = tcx.generics_of(param_owner_def_id); @@ -506,7 +506,7 @@ fn type_param_predicates( .unwrap_or_default(); let mut extend = None; - let item_hir_id = tcx.hir().as_local_hir_id(item_def_id.expect_local()).unwrap(); + let item_hir_id = tcx.hir().as_local_hir_id(item_def_id.expect_local()); let ast_generics = match tcx.hir().get(item_hir_id) { Node::TraitItem(item) => &item.generics, @@ -812,7 +812,7 @@ fn convert_variant( parent_did: LocalDefId, ) -> ty::VariantDef { let mut seen_fields: FxHashMap = Default::default(); - let hir_id = tcx.hir().as_local_hir_id(variant_did.unwrap_or(parent_did)).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(variant_did.unwrap_or(parent_did)); let fields = def .fields() .iter() @@ -863,7 +863,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { use rustc_hir::*; let def_id = def_id.expect_local(); - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); let item = match tcx.hir().get(hir_id) { Node::Item(item) => item, _ => bug!(), @@ -950,7 +950,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { /// the transitive super-predicates are converted. fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredicates<'_> { debug!("super_predicates(trait_def_id={:?})", trait_def_id); - let trait_hir_id = tcx.hir().as_local_hir_id(trait_def_id.expect_local()).unwrap(); + let trait_hir_id = tcx.hir().as_local_hir_id(trait_def_id.expect_local()); let item = match tcx.hir().get(trait_hir_id) { Node::Item(item) => item, @@ -1001,7 +1001,7 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi } fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TraitDef { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(hir_id); let (is_auto, unsafety) = match item.kind { @@ -1157,7 +1157,7 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option, def_id: DefId) -> &ty::Generics { use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); let parent_def_id = match node { @@ -1455,7 +1455,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { use rustc_hir::Node::*; use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let icx = ItemCtxt::new(tcx, def_id); @@ -1553,7 +1553,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { let icx = ItemCtxt::new(tcx, def_id); - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { ref of_trait, .. } => of_trait.as_ref().map(|ast_trait_ref| { let selfty = tcx.type_of(def_id); @@ -1564,7 +1564,7 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { } fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl); let item = tcx.hir().expect_item(hir_id); match &item.kind { @@ -1698,7 +1698,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat } } - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); let mut is_trait = None; @@ -2550,7 +2550,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { if codegen_fn_attrs.flags.intersects(CodegenFnAttrFlags::NO_SANITIZE_ANY) { if codegen_fn_attrs.inline == InlineAttr::Always { if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) { - let hir_id = tcx.hir().as_local_hir_id(id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(id.expect_local()); tcx.struct_span_lint_hir( lint::builtin::INLINE_NO_SANITIZE, hir_id, diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index 9dfe6c9d03ec6..8c556858d4b2c 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -21,7 +21,7 @@ use super::{bad_placeholder_type, is_suggestable_infer_ty}; pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let icx = ItemCtxt::new(tcx, def_id); @@ -513,7 +513,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> { } } - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let hir_id = tcx.hir().as_local_hir_id(def_id); let scope = tcx.hir().get_defining_scope(hir_id); let mut locator = ConstraintLocator { def_id: def_id.to_def_id(), tcx, found: None }; diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index f419f052b811f..c7ef07bde30cf 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -346,7 +346,7 @@ fn check_predicates<'tcx>( if let Some(obligations) = wf::obligations( infcx, tcx.param_env(impl1_def_id), - tcx.hir().as_local_hir_id(impl1_def_id).unwrap(), + tcx.hir().as_local_hir_id(impl1_def_id), ty, span, ) { diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 382664a1b4661..24d60c43cd867 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -153,7 +153,7 @@ fn require_same_types<'tcx>( } fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) { - let main_id = tcx.hir().as_local_hir_id(main_def_id).unwrap(); + let main_id = tcx.hir().as_local_hir_id(main_def_id); let main_span = tcx.def_span(main_def_id); let main_t = tcx.type_of(main_def_id); match main_t.kind { @@ -220,7 +220,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) { } fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) { - let start_id = tcx.hir().as_local_hir_id(start_def_id).unwrap(); + let start_id = tcx.hir().as_local_hir_id(start_def_id); let start_span = tcx.def_span(start_def_id); let start_t = tcx.type_of(start_def_id); match start_t.kind { diff --git a/src/librustc_typeck/outlives/implicit_infer.rs b/src/librustc_typeck/outlives/implicit_infer.rs index 3c579538e64bb..15c72f8704f65 100644 --- a/src/librustc_typeck/outlives/implicit_infer.rs +++ b/src/librustc_typeck/outlives/implicit_infer.rs @@ -57,7 +57,7 @@ impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> { debug!("InferVisitor::visit_item(item={:?})", item_did); - let hir_id = self.tcx.hir().as_local_hir_id(item_did).expect("expected local def-id"); + let hir_id = self.tcx.hir().as_local_hir_id(item_did); let item = match self.tcx.hir().get(hir_id) { Node::Item(item) => item, _ => bug!(), diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs index 71fcd9ddd6bc5..15e0473bbf596 100644 --- a/src/librustc_typeck/outlives/mod.rs +++ b/src/librustc_typeck/outlives/mod.rs @@ -18,7 +18,7 @@ pub fn provide(providers: &mut Providers<'_>) { } fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] { - let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()); match tcx.hir().get(id) { Node::Item(item) => match item.kind { diff --git a/src/librustc_typeck/variance/constraints.rs b/src/librustc_typeck/variance/constraints.rs index 5b0ccc5169ee0..3ec3ef2f30c92 100644 --- a/src/librustc_typeck/variance/constraints.rs +++ b/src/librustc_typeck/variance/constraints.rs @@ -137,7 +137,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { return; } - let id = tcx.hir().as_local_hir_id(def_id).unwrap(); + let id = tcx.hir().as_local_hir_id(def_id); let inferred_start = self.terms_cx.inferred_starts[&id]; let current_item = &CurrentItem { inferred_start }; match tcx.type_of(def_id).kind { @@ -378,7 +378,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { } let (local, remote) = if let Some(id) = - def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id)) { (Some(self.terms_cx.inferred_starts[&id]), None) } else { diff --git a/src/librustc_typeck/variance/mod.rs b/src/librustc_typeck/variance/mod.rs index f9e32c2a6b0cd..2d78ac4b3c58f 100644 --- a/src/librustc_typeck/variance/mod.rs +++ b/src/librustc_typeck/variance/mod.rs @@ -38,7 +38,7 @@ fn crate_variances(tcx: TyCtxt<'_>, crate_num: CrateNum) -> &CrateVariancesMap<' } fn variances_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[ty::Variance] { - let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()).unwrap(); + let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()); let unsupported = || { // Variance not relevant. span_bug!(tcx.hir().span(id), "asked to compute variance for wrong kind of item") diff --git a/src/librustc_typeck/variance/terms.rs b/src/librustc_typeck/variance/terms.rs index c6cdac5d0ccf6..fe585826d2205 100644 --- a/src/librustc_typeck/variance/terms.rs +++ b/src/librustc_typeck/variance/terms.rs @@ -94,9 +94,7 @@ fn lang_items(tcx: TyCtxt<'_>) -> Vec<(hir::HirId, Vec)> { all.into_iter() // iterating over (Option, Variance) .filter(|&(ref d, _)| d.is_some()) .map(|(d, v)| (d.unwrap(), v)) // (DefId, Variance) - .filter_map(|(d, v)| { - d.as_local().map(|d| tcx.hir().as_local_hir_id(d).unwrap()).map(|n| (n, v)) - }) // (HirId, Variance) + .filter_map(|(d, v)| d.as_local().map(|d| tcx.hir().as_local_hir_id(d)).map(|n| (n, v))) // (HirId, Variance) .collect() } diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 0ed9115e77d5e..19dac6b81a67d 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -340,15 +340,14 @@ pub fn build_impl( } } - let for_ = - if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did).unwrap()) { - match tcx.hir().expect_item(hir_id).kind { - hir::ItemKind::Impl { self_ty, .. } => self_ty.clean(cx), - _ => panic!("did given to build_impl was not an impl"), - } - } else { - tcx.type_of(did).clean(cx) - }; + let for_ = if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did)) { + match tcx.hir().expect_item(hir_id).kind { + hir::ItemKind::Impl { self_ty, .. } => self_ty.clean(cx), + _ => panic!("did given to build_impl was not an impl"), + } + } else { + tcx.type_of(did).clean(cx) + }; // Only inline impl if the implementing type is // reachable in rustdoc generated documentation @@ -362,7 +361,7 @@ pub fn build_impl( let predicates = tcx.explicit_predicates_of(did); let (trait_items, generics) = if let Some(hir_id) = - did.as_local().map(|did| tcx.hir().as_local_hir_id(did).unwrap()) + did.as_local().map(|did| tcx.hir().as_local_hir_id(did)) { match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { ref generics, ref items, .. } => ( @@ -489,7 +488,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet) } pub fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String { - if let Some(hir_id) = did.as_local().map(|did| cx.tcx.hir().as_local_hir_id(did).unwrap()) { + if let Some(hir_id) = did.as_local().map(|did| cx.tcx.hir().as_local_hir_id(did)) { rustc_hir_pretty::id_to_string(&cx.tcx.hir(), hir_id) } else { cx.tcx.rendered_const(did) @@ -502,7 +501,7 @@ fn build_const(cx: &DocContext<'_>, did: DefId) -> clean::Constant { expr: print_inlined_const(cx, did), value: clean::utils::print_evaluated_const(cx, did), is_literal: did.as_local().map_or(false, |did| { - clean::utils::is_literal_expr(cx, cx.tcx.hir().as_local_hir_id(did).unwrap()) + clean::utils::is_literal_expr(cx, cx.tcx.hir().as_local_hir_id(did)) }), } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 8e5189a80853f..cc0423fab5006 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1377,9 +1377,8 @@ impl Clean for hir::Ty<'_> { let mut alias = None; if let Res::Def(DefKind::TyAlias, def_id) = path.res { // Substitute private type aliases - if let Some(hir_id) = def_id - .as_local() - .map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + if let Some(hir_id) = + def_id.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) { if !cx.renderinfo.borrow().access_levels.is_exported(def_id) { alias = Some(&cx.tcx.hir().expect_item(hir_id).kind); diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index be468064368ff..bca920491bb5f 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -474,7 +474,7 @@ pub fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String { match n.val { ty::ConstKind::Unevaluated(def_id, _, promoted) => { let mut s = if let Some(hir_id) = - def_id.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) { print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id)) } else { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index b740d8e65176d..e879687089fff 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -144,7 +144,7 @@ impl<'tcx> DocContext<'tcx> { if self.all_fake_def_ids.borrow().contains(&def_id) { None } else { - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id).unwrap()) + def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) } } diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 2f9719774ec47..bf9f874301280 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -335,10 +335,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { fn fold_item(&mut self, mut item: Item) -> Option { let item_hir_id = if item.is_mod() { - if let Some(id) = item - .def_id - .as_local() - .map(|def_id| self.cx.tcx.hir().as_local_hir_id(def_id).unwrap()) + if let Some(id) = + item.def_id.as_local().map(|def_id| self.cx.tcx.hir().as_local_hir_id(def_id)) { Some(id) } else { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index e74a1fe5b8c09..da4915f130c63 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -331,7 +331,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } let res_hir_id = match res_did.as_local() { - Some(n) => tcx.hir().as_local_hir_id(n).unwrap(), + Some(n) => tcx.hir().as_local_hir_id(n), None => return false, }; From da9c86722177604e313221eb9759100e8b7a4fcc Mon Sep 17 00:00:00 2001 From: marmeladema Date: Thu, 16 Apr 2020 22:00:36 +0100 Subject: [PATCH 5/7] Address comments from review --- src/librustc_codegen_llvm/consts.rs | 5 ++--- .../nice_region_error/find_anon_type.rs | 5 ++--- .../nice_region_error/outlives_closure.rs | 5 ++--- src/librustc_lint/builtin.rs | 11 ++++------- src/librustc_middle/hir/map/mod.rs | 13 ++++--------- src/librustc_middle/mir/mod.rs | 12 +++++------- src/librustc_middle/ty/print/pretty.rs | 12 +++++------- .../borrow_check/diagnostics/conflict_errors.rs | 7 ++----- src/librustc_mir/monomorphize/collector.rs | 3 ++- src/librustc_passes/dead.rs | 5 ++--- src/librustc_privacy/lib.rs | 16 ++++++---------- src/librustc_resolve/late/lifetimes.rs | 17 ++++++----------- src/librustc_trait_selection/opaque_types.rs | 7 +++---- src/librustc_typeck/check/compare_method.rs | 15 ++++++--------- src/librustc_typeck/check/method/suggest.rs | 5 ++--- src/librustc_typeck/variance/constraints.rs | 5 ++--- src/librustdoc/clean/inline.rs | 11 ++++++----- src/librustdoc/clean/mod.rs | 7 +++---- src/librustdoc/clean/utils.rs | 5 ++--- .../passes/collect_intra_doc_links.rs | 6 ++---- 20 files changed, 68 insertions(+), 104 deletions(-) diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index c97b547946ebb..43ced8ee5b13c 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -209,9 +209,8 @@ impl CodegenCx<'ll, 'tcx> { debug!("get_static: sym={} instance={:?}", sym, instance); - let g = if let Some(id) = - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) - { + let g = if let Some(def_id) = def_id.as_local() { + let id = self.tcx.hir().as_local_hir_id(def_id); let llty = self.layout_of(ty).llvm_type(self); let (g, attrs) = match self.tcx.hir().get(id) { Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => { diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs index 1181668e4b951..50755d3d42e95 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs @@ -29,9 +29,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { ) -> Option<(&hir::Ty<'_>, &hir::FnDecl<'_>)> { if let Some(anon_reg) = self.tcx().is_suitable_region(region) { let def_id = anon_reg.def_id; - if let Some(hir_id) = - def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let hir_id = self.tcx().hir().as_local_hir_id(def_id); let fndecl = match self.tcx().hir().get(hir_id) { Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. }) | Node::TraitItem(&hir::TraitItem { diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs index ca051cecbb8c4..fc858a497597e 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs @@ -46,9 +46,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { ) = (&sub_origin, sup_region) { let hir = &self.tcx().hir(); - if let Some(hir_id) = - free_region.scope.as_local().map(|def_id| hir.as_local_hir_id(def_id)) - { + if let Some(def_id) = free_region.scope.as_local() { + let hir_id = hir.as_local_hir_id(def_id); if let Node::Expr(Expr { kind: Closure(_, _, _, closure_span, None), .. }) = hir.get(hir_id) { diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 5b25204f21b1d..cad6a312521e4 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -436,9 +436,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { // If the trait is private, add the impl items to `private_traits` so they don't get // reported for missing docs. let real_trait = trait_ref.path.res.def_id(); - if let Some(hir_id) = - real_trait.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = real_trait.as_local() { + let hir_id = cx.tcx.hir().as_local_hir_id(def_id); if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) { if let hir::VisibilityKind::Inherited = item.vis.node { for impl_item_ref in items { @@ -611,10 +610,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations { let mut impls = HirIdSet::default(); cx.tcx.for_each_impl(debug, |d| { if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() { - if let Some(hir_id) = - ty_def.did.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) - { - impls.insert(hir_id); + if let Some(def_id) = ty_def.did.as_local() { + impls.insert(cx.tcx.hir().as_local_hir_id(def_id)); } } }); diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index d7c4e6fc4ed4b..85a14558162cb 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -482,7 +482,7 @@ impl<'hir> Map<'hir> { } pub fn get_if_local(&self, id: DefId) -> Option> { - if let Some(id) = id.as_local() { Some(self.get(self.as_local_hir_id(id))) } else { None } + id.as_local().map(|id| self.get(self.as_local_hir_id(id))) } pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> { @@ -885,7 +885,7 @@ impl<'hir> Map<'hir> { } pub fn span_if_local(&self, id: DefId) -> Option { - if let Some(id) = id.as_local() { Some(self.span(self.as_local_hir_id(id))) } else { None } + id.as_local().map(|id| self.span(self.as_local_hir_id(id))) } pub fn res_span(&self, res: Res) -> Option { @@ -1084,11 +1084,6 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String { } pub fn provide(providers: &mut Providers<'_>) { - providers.def_kind = |tcx, def_id| { - if let Some(def_id) = def_id.as_local() { - tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id)) - } else { - bug!("calling local def_kind query provider for upstream DefId: {:?}", def_id); - } - }; + providers.def_kind = + |tcx, def_id| tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id.expect_local())); } diff --git a/src/librustc_middle/mir/mod.rs b/src/librustc_middle/mir/mod.rs index ee6b8a1b661c6..565867de7383b 100644 --- a/src/librustc_middle/mir/mod.rs +++ b/src/librustc_middle/mir/mod.rs @@ -2284,14 +2284,13 @@ impl<'tcx> Debug for Rvalue<'tcx> { } AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| { - if let Some(hir_id) = - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let hir_id = tcx.hir().as_local_hir_id(def_id); let name = if tcx.sess.opts.debugging_opts.span_free_formats { let substs = tcx.lift(&substs).unwrap(); format!( "[closure@{}]", - tcx.def_path_str_with_substs(def_id, substs), + tcx.def_path_str_with_substs(def_id.to_def_id(), substs), ) } else { format!("[closure@{:?}]", tcx.hir().span(hir_id)) @@ -2312,9 +2311,8 @@ impl<'tcx> Debug for Rvalue<'tcx> { }), AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| { - if let Some(hir_id) = - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let hir_id = tcx.hir().as_local_hir_id(def_id); let name = format!("[generator@{:?}]", tcx.hir().span(hir_id)); let mut struct_fmt = fmt.debug_struct(&name); diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 0727533223f66..70a3093e684b7 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -620,9 +620,8 @@ pub trait PrettyPrinter<'tcx>: } // FIXME(eddyb) should use `def_span`. - if let Some(hir_id) = - did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did)) - { + if let Some(did) = did.as_local() { + let hir_id = self.tcx().hir().as_local_hir_id(did); p!(write("@{:?}", self.tcx().hir().span(hir_id))); if substs.as_generator().is_valid() { @@ -668,11 +667,10 @@ pub trait PrettyPrinter<'tcx>: p!(write("[closure")); // FIXME(eddyb) should use `def_span`. - if let Some(hir_id) = - did.as_local().map(|did| self.tcx().hir().as_local_hir_id(did)) - { + if let Some(did) = did.as_local() { + let hir_id = self.tcx().hir().as_local_hir_id(did); if self.tcx().sess.opts.debugging_opts.span_free_formats { - p!(write("@"), print_def_path(did, substs)); + p!(write("@"), print_def_path(did.to_def_id(), substs)); } else { p!(write("@{:?}", self.tcx().hir().span(hir_id))); } diff --git a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs index ce0929d0a9191..f11e9d5ba0d35 100644 --- a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs @@ -862,11 +862,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { format!("`{}` would have to be valid for `{}`...", name, region_name), ); - if let Some(fn_hir_id) = self - .mir_def_id - .as_local() - .map(|def_id| self.infcx.tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = self.mir_def_id.as_local() { + let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id); err.span_label( drop_span, format!( diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index dfffcfa9cf2f9..ceac3a317da42 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -429,7 +429,8 @@ fn check_recursion_limit<'tcx>( // infinite expansion. if adjusted_recursion_depth > *tcx.sess.recursion_limit.get() { let error = format!("reached the recursion limit while instantiating `{}`", instance); - if let Some(hir_id) = def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { + if let Some(def_id) = def_id.as_local() { + let hir_id = tcx.hir().as_local_hir_id(def_id); tcx.sess.span_fatal(tcx.hir().span(hir_id), &error); } else { tcx.sess.fatal(&error); diff --git a/src/librustc_passes/dead.rs b/src/librustc_passes/dead.rs index 26b92bbc72627..260c7485b9e74 100644 --- a/src/librustc_passes/dead.rs +++ b/src/librustc_passes/dead.rs @@ -535,9 +535,8 @@ impl DeadVisitor<'tcx> { let inherent_impls = self.tcx.inherent_impls(def_id); for &impl_did in inherent_impls.iter() { for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] { - if let Some(item_hir_id) = - item_did.as_local().map(|did| self.tcx.hir().as_local_hir_id(did)) - { + if let Some(did) = item_did.as_local() { + let item_hir_id = self.tcx.hir().as_local_hir_id(did); if self.live_symbols.contains(&item_hir_id) { return true; } diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 7e1a53d4c3f13..b1fbba7e1a7b3 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -445,9 +445,8 @@ impl VisibilityLike for Option { const SHALLOW: bool = true; fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self { cmp::min( - if let Some(hir_id) = - def_id.as_local().map(|def_id| find.tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let hir_id = find.tcx.hir().as_local_hir_id(def_id); find.access_levels.map.get(&hir_id).cloned() } else { Self::MAX @@ -549,9 +548,8 @@ impl EmbargoVisitor<'tcx> { if export.vis.is_accessible_from(defining_mod, self.tcx) { if let Res::Def(def_kind, def_id) = export.res { let vis = def_id_visibility(self.tcx, def_id).0; - if let Some(hir_id) = - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let hir_id = self.tcx.hir().as_local_hir_id(def_id); self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod); } } @@ -914,10 +912,8 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> { for export in exports.iter() { if export.vis == ty::Visibility::Public { if let Some(def_id) = export.res.opt_def_id() { - if let Some(hir_id) = def_id - .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let hir_id = self.tcx.hir().as_local_hir_id(def_id); self.update(hir_id, Some(AccessLevel::Exported)); } } diff --git a/src/librustc_resolve/late/lifetimes.rs b/src/librustc_resolve/late/lifetimes.rs index 3b9331c394461..ae224992d2ce2 100644 --- a/src/librustc_resolve/late/lifetimes.rs +++ b/src/librustc_resolve/late/lifetimes.rs @@ -596,10 +596,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { // In the future, this should be fixed and this error should be removed. let def = self.map.defs.get(&lifetime.hir_id).cloned(); if let Some(Region::LateBound(_, def_id, _)) = def { - if let Some(hir_id) = def_id - .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let hir_id = self.tcx.hir().as_local_hir_id(def_id); // Ensure that the parent of the def is an item, not HRTB let parent_id = self.tcx.hir().get_parent_node(hir_id); let parent_impl_id = hir::ImplItemId { hir_id: parent_id }; @@ -1559,10 +1557,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } if let Some(parent_def_id) = self.tcx.parent(def_id) { - if let Some(parent_hir_id) = parent_def_id - .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = parent_def_id.as_local() { + let parent_hir_id = self.tcx.hir().as_local_hir_id(def_id); // lifetimes in `derive` expansions don't count (Issue #53738) if self .tcx @@ -1954,9 +1950,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { }; let map = &self.map; - let unsubst = if let Some(id) = - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) - { + let unsubst = if let Some(def_id) = def_id.as_local() { + let id = self.tcx.hir().as_local_hir_id(def_id); &map.object_lifetime_defaults[&id] } else { let tcx = self.tcx; diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index 9bc2f2829cf9a..39a3ee1839897 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1036,9 +1036,8 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { // let x = || foo(); // returns the Opaque assoc with `foo` // } // ``` - if let Some(opaque_hir_id) = - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = def_id.as_local() { + let opaque_hir_id = tcx.hir().as_local_hir_id(def_id); let parent_def_id = self.parent_def_id; let def_scope_default = || { let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id); @@ -1085,7 +1084,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { ), }; if in_definition_scope { - return self.fold_opaque_ty(ty, def_id, substs, origin); + return self.fold_opaque_ty(ty, def_id.to_def_id(), substs, origin); } debug!( diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index f834f74d5df18..590726ce8ed37 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -409,9 +409,8 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( match *terr { TypeError::Mutability => { - if let Some(trait_m_hir_id) = - trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = trait_m.def_id.as_local() { + let trait_m_hir_id = tcx.hir().as_local_hir_id(def_id); let trait_m_iter = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => trait_m_sig.decl.inputs.iter(), _ => bug!("{:?} is not a TraitItemKind::Fn", trait_m), @@ -438,9 +437,8 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( } } TypeError::Sorts(ExpectedFound { .. }) => { - if let Some(trait_m_hir_id) = - trait_m.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) - { + if let Some(def_id) = trait_m.def_id.as_local() { + let trait_m_hir_id = tcx.hir().as_local_hir_id(def_id); let (trait_m_output, trait_m_iter) = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => { @@ -591,9 +589,8 @@ fn compare_number_of_generics<'tcx>( if impl_count != trait_count { err_occurred = true; - let (trait_spans, impl_trait_spans) = if let Some(trait_hir_id) = - trait_.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) - { + let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() { + let trait_hir_id = tcx.hir().as_local_hir_id(def_id); let trait_item = tcx.hir().expect_trait_item(trait_hir_id); if trait_item.generics.params.is_empty() { (Some(vec![trait_item.generics.span]), vec![]) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 5ae66e8a9adb5..90d81618bfdbb 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -1052,9 +1052,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let generics = self.tcx.generics_of(table_owner.to_def_id()); let type_param = generics.type_param(param, self.tcx); let hir = &self.tcx.hir(); - if let Some(id) = - type_param.def_id.as_local().map(|def_id| hir.as_local_hir_id(def_id)) - { + if let Some(def_id) = type_param.def_id.as_local() { + let id = hir.as_local_hir_id(def_id); // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: FooBar`, // instead we suggest `T: Foo + Bar` in that case. diff --git a/src/librustc_typeck/variance/constraints.rs b/src/librustc_typeck/variance/constraints.rs index 3ec3ef2f30c92..01d077d47f039 100644 --- a/src/librustc_typeck/variance/constraints.rs +++ b/src/librustc_typeck/variance/constraints.rs @@ -377,9 +377,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { return; } - let (local, remote) = if let Some(id) = - def_id.as_local().map(|def_id| self.tcx().hir().as_local_hir_id(def_id)) - { + let (local, remote) = if let Some(def_id) = def_id.as_local() { + let id = self.tcx().hir().as_local_hir_id(def_id); (Some(self.terms_cx.inferred_starts[&id]), None) } else { (None, Some(self.tcx().variances_of(def_id))) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 19dac6b81a67d..1d5a7983e7d81 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -340,7 +340,8 @@ pub fn build_impl( } } - let for_ = if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did)) { + let for_ = if let Some(did) = did.as_local() { + let hir_id = tcx.hir().as_local_hir_id(did); match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { self_ty, .. } => self_ty.clean(cx), _ => panic!("did given to build_impl was not an impl"), @@ -360,9 +361,8 @@ pub fn build_impl( } let predicates = tcx.explicit_predicates_of(did); - let (trait_items, generics) = if let Some(hir_id) = - did.as_local().map(|did| tcx.hir().as_local_hir_id(did)) - { + let (trait_items, generics) = if let Some(did) = did.as_local() { + let hir_id = tcx.hir().as_local_hir_id(did); match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { ref generics, ref items, .. } => ( items.iter().map(|item| tcx.hir().impl_item(item.id).clean(cx)).collect::>(), @@ -488,7 +488,8 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet) } pub fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String { - if let Some(hir_id) = did.as_local().map(|did| cx.tcx.hir().as_local_hir_id(did)) { + if let Some(did) = did.as_local() { + let hir_id = cx.tcx.hir().as_local_hir_id(did); rustc_hir_pretty::id_to_string(&cx.tcx.hir(), hir_id) } else { cx.tcx.rendered_const(did) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index cc0423fab5006..d398061ef6266 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1377,10 +1377,9 @@ impl Clean for hir::Ty<'_> { let mut alias = None; if let Res::Def(DefKind::TyAlias, def_id) = path.res { // Substitute private type aliases - if let Some(hir_id) = - def_id.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) - { - if !cx.renderinfo.borrow().access_levels.is_exported(def_id) { + if let Some(def_id) = def_id.as_local() { + let hir_id = cx.tcx.hir().as_local_hir_id(def_id); + if !cx.renderinfo.borrow().access_levels.is_exported(def_id.to_def_id()) { alias = Some(&cx.tcx.hir().expect_item(hir_id).kind); } } diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index bca920491bb5f..6840e27979aa5 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -473,9 +473,8 @@ pub fn name_from_pat(p: &hir::Pat) -> String { pub fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String { match n.val { ty::ConstKind::Unevaluated(def_id, _, promoted) => { - let mut s = if let Some(hir_id) = - def_id.as_local().map(|def_id| cx.tcx.hir().as_local_hir_id(def_id)) - { + let mut s = if let Some(def_id) = def_id.as_local() { + let hir_id = cx.tcx.hir().as_local_hir_id(def_id); print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id)) } else { inline::print_inlined_const(cx, def_id) diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index bf9f874301280..1d0874d47f694 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -335,10 +335,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { fn fold_item(&mut self, mut item: Item) -> Option { let item_hir_id = if item.is_mod() { - if let Some(id) = - item.def_id.as_local().map(|def_id| self.cx.tcx.hir().as_local_hir_id(def_id)) - { - Some(id) + if let Some(def_id) = item.def_id.as_local() { + Some(self.cx.tcx.hir().as_local_hir_id(def_id)) } else { debug!("attempting to fold on a non-local item: {:?}", item); return self.fold_item_recur(item); From af442d964de8cb9814f0e2374db34e87fb21ca73 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 17 Apr 2020 18:36:16 +0900 Subject: [PATCH 6/7] Rename `asm` test directory in favor of `llvm_asm` --- src/test/ui/{asm => llvm-asm}/issue-51431.rs | 0 .../ui/{asm => llvm-asm}/issue-51431.stderr | 0 src/test/ui/{asm => llvm-asm}/issue-54067.rs | 0 src/test/ui/{asm => llvm-asm}/issue-62046.rs | 0 .../ui/{asm => llvm-asm}/issue-62046.stderr | 0 src/test/ui/{asm => llvm-asm}/issue-69092.rs | 0 .../ui/{asm => llvm-asm}/issue-69092.stderr | 0 .../llvm-asm-bad-clobber.rs} | 0 .../llvm-asm-bad-clobber.stderr} | 2 +- .../llvm-asm-in-bad-modifier.rs} | 0 .../llvm-asm-in-bad-modifier.stderr} | 4 ++-- .../llvm-asm-literal-escaping.rs} | 0 .../llvm-asm-misplaced-option.rs} | 0 .../llvm-asm-misplaced-option.stderr} | 4 ++-- .../llvm-asm-out-assign-imm.rs} | 0 .../llvm-asm-out-assign-imm.stderr} | 2 +- .../llvm-asm-out-no-modifier.rs} | 0 .../llvm-asm-out-no-modifier.stderr} | 2 +- .../llvm-asm-out-read-uninit.rs} | 0 .../llvm-asm-out-read-uninit.stderr} | 2 +- .../llvm-asm-parse-errors.rs} | 0 .../llvm-asm-parse-errors.stderr} | 22 +++++++++---------- 22 files changed, 19 insertions(+), 19 deletions(-) rename src/test/ui/{asm => llvm-asm}/issue-51431.rs (100%) rename src/test/ui/{asm => llvm-asm}/issue-51431.stderr (100%) rename src/test/ui/{asm => llvm-asm}/issue-54067.rs (100%) rename src/test/ui/{asm => llvm-asm}/issue-62046.rs (100%) rename src/test/ui/{asm => llvm-asm}/issue-62046.stderr (100%) rename src/test/ui/{asm => llvm-asm}/issue-69092.rs (100%) rename src/test/ui/{asm => llvm-asm}/issue-69092.stderr (100%) rename src/test/ui/{asm/asm-bad-clobber.rs => llvm-asm/llvm-asm-bad-clobber.rs} (100%) rename src/test/ui/{asm/asm-bad-clobber.stderr => llvm-asm/llvm-asm-bad-clobber.stderr} (87%) rename src/test/ui/{asm/asm-in-bad-modifier.rs => llvm-asm/llvm-asm-in-bad-modifier.rs} (100%) rename src/test/ui/{asm/asm-in-bad-modifier.stderr => llvm-asm/llvm-asm-in-bad-modifier.stderr} (84%) rename src/test/ui/{asm/asm-literal-escaping.rs => llvm-asm/llvm-asm-literal-escaping.rs} (100%) rename src/test/ui/{asm/asm-misplaced-option.rs => llvm-asm/llvm-asm-misplaced-option.rs} (100%) rename src/test/ui/{asm/asm-misplaced-option.stderr => llvm-asm/llvm-asm-misplaced-option.stderr} (83%) rename src/test/ui/{asm/asm-out-assign-imm.rs => llvm-asm/llvm-asm-out-assign-imm.rs} (100%) rename src/test/ui/{asm/asm-out-assign-imm.stderr => llvm-asm/llvm-asm-out-assign-imm.stderr} (91%) rename src/test/ui/{asm/asm-out-no-modifier.rs => llvm-asm/llvm-asm-out-no-modifier.rs} (100%) rename src/test/ui/{asm/asm-out-no-modifier.stderr => llvm-asm/llvm-asm-out-no-modifier.stderr} (85%) rename src/test/ui/{asm/asm-out-read-uninit.rs => llvm-asm/llvm-asm-out-read-uninit.rs} (100%) rename src/test/ui/{asm/asm-out-read-uninit.stderr => llvm-asm/llvm-asm-out-read-uninit.stderr} (87%) rename src/test/ui/{asm/asm-parse-errors.rs => llvm-asm/llvm-asm-parse-errors.rs} (100%) rename src/test/ui/{asm/asm-parse-errors.stderr => llvm-asm/llvm-asm-parse-errors.stderr} (80%) diff --git a/src/test/ui/asm/issue-51431.rs b/src/test/ui/llvm-asm/issue-51431.rs similarity index 100% rename from src/test/ui/asm/issue-51431.rs rename to src/test/ui/llvm-asm/issue-51431.rs diff --git a/src/test/ui/asm/issue-51431.stderr b/src/test/ui/llvm-asm/issue-51431.stderr similarity index 100% rename from src/test/ui/asm/issue-51431.stderr rename to src/test/ui/llvm-asm/issue-51431.stderr diff --git a/src/test/ui/asm/issue-54067.rs b/src/test/ui/llvm-asm/issue-54067.rs similarity index 100% rename from src/test/ui/asm/issue-54067.rs rename to src/test/ui/llvm-asm/issue-54067.rs diff --git a/src/test/ui/asm/issue-62046.rs b/src/test/ui/llvm-asm/issue-62046.rs similarity index 100% rename from src/test/ui/asm/issue-62046.rs rename to src/test/ui/llvm-asm/issue-62046.rs diff --git a/src/test/ui/asm/issue-62046.stderr b/src/test/ui/llvm-asm/issue-62046.stderr similarity index 100% rename from src/test/ui/asm/issue-62046.stderr rename to src/test/ui/llvm-asm/issue-62046.stderr diff --git a/src/test/ui/asm/issue-69092.rs b/src/test/ui/llvm-asm/issue-69092.rs similarity index 100% rename from src/test/ui/asm/issue-69092.rs rename to src/test/ui/llvm-asm/issue-69092.rs diff --git a/src/test/ui/asm/issue-69092.stderr b/src/test/ui/llvm-asm/issue-69092.stderr similarity index 100% rename from src/test/ui/asm/issue-69092.stderr rename to src/test/ui/llvm-asm/issue-69092.stderr diff --git a/src/test/ui/asm/asm-bad-clobber.rs b/src/test/ui/llvm-asm/llvm-asm-bad-clobber.rs similarity index 100% rename from src/test/ui/asm/asm-bad-clobber.rs rename to src/test/ui/llvm-asm/llvm-asm-bad-clobber.rs diff --git a/src/test/ui/asm/asm-bad-clobber.stderr b/src/test/ui/llvm-asm/llvm-asm-bad-clobber.stderr similarity index 87% rename from src/test/ui/asm/asm-bad-clobber.stderr rename to src/test/ui/llvm-asm/llvm-asm-bad-clobber.stderr index 8c5d04694c497..9ecd12caa0e2a 100644 --- a/src/test/ui/asm/asm-bad-clobber.stderr +++ b/src/test/ui/llvm-asm/llvm-asm-bad-clobber.stderr @@ -1,5 +1,5 @@ error[E0664]: clobber should not be surrounded by braces - --> $DIR/asm-bad-clobber.rs:22:42 + --> $DIR/llvm-asm-bad-clobber.rs:22:42 | LL | llvm_asm!("xor %eax, %eax" : : : "{eax}"); | ^^^^^^^ diff --git a/src/test/ui/asm/asm-in-bad-modifier.rs b/src/test/ui/llvm-asm/llvm-asm-in-bad-modifier.rs similarity index 100% rename from src/test/ui/asm/asm-in-bad-modifier.rs rename to src/test/ui/llvm-asm/llvm-asm-in-bad-modifier.rs diff --git a/src/test/ui/asm/asm-in-bad-modifier.stderr b/src/test/ui/llvm-asm/llvm-asm-in-bad-modifier.stderr similarity index 84% rename from src/test/ui/asm/asm-in-bad-modifier.stderr rename to src/test/ui/llvm-asm/llvm-asm-in-bad-modifier.stderr index f1624f74a70ac..e94ac94f59f9a 100644 --- a/src/test/ui/asm/asm-in-bad-modifier.stderr +++ b/src/test/ui/llvm-asm/llvm-asm-in-bad-modifier.stderr @@ -1,11 +1,11 @@ error[E0662]: input operand constraint contains '=' - --> $DIR/asm-in-bad-modifier.rs:23:44 + --> $DIR/llvm-asm-in-bad-modifier.rs:23:44 | LL | llvm_asm!("mov $1, $0" : "=r"(x) : "=r"(5)); | ^^^^ error[E0663]: input operand constraint contains '+' - --> $DIR/asm-in-bad-modifier.rs:24:44 + --> $DIR/llvm-asm-in-bad-modifier.rs:24:44 | LL | llvm_asm!("mov $1, $0" : "=r"(y) : "+r"(5)); | ^^^^ diff --git a/src/test/ui/asm/asm-literal-escaping.rs b/src/test/ui/llvm-asm/llvm-asm-literal-escaping.rs similarity index 100% rename from src/test/ui/asm/asm-literal-escaping.rs rename to src/test/ui/llvm-asm/llvm-asm-literal-escaping.rs diff --git a/src/test/ui/asm/asm-misplaced-option.rs b/src/test/ui/llvm-asm/llvm-asm-misplaced-option.rs similarity index 100% rename from src/test/ui/asm/asm-misplaced-option.rs rename to src/test/ui/llvm-asm/llvm-asm-misplaced-option.rs diff --git a/src/test/ui/asm/asm-misplaced-option.stderr b/src/test/ui/llvm-asm/llvm-asm-misplaced-option.stderr similarity index 83% rename from src/test/ui/asm/asm-misplaced-option.stderr rename to src/test/ui/llvm-asm/llvm-asm-misplaced-option.stderr index ea155b91c5d61..21fd27825a185 100644 --- a/src/test/ui/asm/asm-misplaced-option.stderr +++ b/src/test/ui/llvm-asm/llvm-asm-misplaced-option.stderr @@ -1,11 +1,11 @@ warning: unrecognized option - --> $DIR/asm-misplaced-option.rs:24:69 + --> $DIR/llvm-asm-misplaced-option.rs:24:69 | LL | llvm_asm!("mov $1, $0" : "=r"(x) : "r"(5_usize), "0"(x) : : "cc"); | ^^^^ warning: expected a clobber, found an option - --> $DIR/asm-misplaced-option.rs:31:85 + --> $DIR/llvm-asm-misplaced-option.rs:31:85 | LL | llvm_asm!("add $2, $1; mov $1, $0" : "=r"(x) : "r"(x), "r"(8_usize) : "cc", "volatile"); | ^^^^^^^^^^ diff --git a/src/test/ui/asm/asm-out-assign-imm.rs b/src/test/ui/llvm-asm/llvm-asm-out-assign-imm.rs similarity index 100% rename from src/test/ui/asm/asm-out-assign-imm.rs rename to src/test/ui/llvm-asm/llvm-asm-out-assign-imm.rs diff --git a/src/test/ui/asm/asm-out-assign-imm.stderr b/src/test/ui/llvm-asm/llvm-asm-out-assign-imm.stderr similarity index 91% rename from src/test/ui/asm/asm-out-assign-imm.stderr rename to src/test/ui/llvm-asm/llvm-asm-out-assign-imm.stderr index feec61b4fc6ef..e110aec220936 100644 --- a/src/test/ui/asm/asm-out-assign-imm.stderr +++ b/src/test/ui/llvm-asm/llvm-asm-out-assign-imm.stderr @@ -1,5 +1,5 @@ error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/asm-out-assign-imm.rs:24:39 + --> $DIR/llvm-asm-out-assign-imm.rs:24:39 | LL | let x: isize; | - help: make this binding mutable: `mut x` diff --git a/src/test/ui/asm/asm-out-no-modifier.rs b/src/test/ui/llvm-asm/llvm-asm-out-no-modifier.rs similarity index 100% rename from src/test/ui/asm/asm-out-no-modifier.rs rename to src/test/ui/llvm-asm/llvm-asm-out-no-modifier.rs diff --git a/src/test/ui/asm/asm-out-no-modifier.stderr b/src/test/ui/llvm-asm/llvm-asm-out-no-modifier.stderr similarity index 85% rename from src/test/ui/asm/asm-out-no-modifier.stderr rename to src/test/ui/llvm-asm/llvm-asm-out-no-modifier.stderr index 1c9e108f910c6..1f2b272792435 100644 --- a/src/test/ui/asm/asm-out-no-modifier.stderr +++ b/src/test/ui/llvm-asm/llvm-asm-out-no-modifier.stderr @@ -1,5 +1,5 @@ error[E0661]: output operand constraint lacks '=' or '+' - --> $DIR/asm-out-no-modifier.rs:22:34 + --> $DIR/llvm-asm-out-no-modifier.rs:22:34 | LL | llvm_asm!("mov $1, $0" : "r"(x) : "r"(5)); | ^^^ diff --git a/src/test/ui/asm/asm-out-read-uninit.rs b/src/test/ui/llvm-asm/llvm-asm-out-read-uninit.rs similarity index 100% rename from src/test/ui/asm/asm-out-read-uninit.rs rename to src/test/ui/llvm-asm/llvm-asm-out-read-uninit.rs diff --git a/src/test/ui/asm/asm-out-read-uninit.stderr b/src/test/ui/llvm-asm/llvm-asm-out-read-uninit.stderr similarity index 87% rename from src/test/ui/asm/asm-out-read-uninit.stderr rename to src/test/ui/llvm-asm/llvm-asm-out-read-uninit.stderr index 3c3f3a6febb5a..a22ebe4e4d9db 100644 --- a/src/test/ui/asm/asm-out-read-uninit.stderr +++ b/src/test/ui/llvm-asm/llvm-asm-out-read-uninit.stderr @@ -1,5 +1,5 @@ error[E0381]: use of possibly-uninitialized variable: `x` - --> $DIR/asm-out-read-uninit.rs:22:48 + --> $DIR/llvm-asm-out-read-uninit.rs:22:48 | LL | llvm_asm!("mov $1, $0" : "=r"(x) : "r"(x)); | ^ use of possibly-uninitialized `x` diff --git a/src/test/ui/asm/asm-parse-errors.rs b/src/test/ui/llvm-asm/llvm-asm-parse-errors.rs similarity index 100% rename from src/test/ui/asm/asm-parse-errors.rs rename to src/test/ui/llvm-asm/llvm-asm-parse-errors.rs diff --git a/src/test/ui/asm/asm-parse-errors.stderr b/src/test/ui/llvm-asm/llvm-asm-parse-errors.stderr similarity index 80% rename from src/test/ui/asm/asm-parse-errors.stderr rename to src/test/ui/llvm-asm/llvm-asm-parse-errors.stderr index 64f295c3b3698..1fd46809f3eed 100644 --- a/src/test/ui/asm/asm-parse-errors.stderr +++ b/src/test/ui/llvm-asm/llvm-asm-parse-errors.stderr @@ -1,65 +1,65 @@ error: macro requires a string literal as an argument - --> $DIR/asm-parse-errors.rs:4:5 + --> $DIR/llvm-asm-parse-errors.rs:4:5 | LL | llvm_asm!(); | ^^^^^^^^^^^^ string literal required error: expected string literal - --> $DIR/asm-parse-errors.rs:5:23 + --> $DIR/llvm-asm-parse-errors.rs:5:23 | LL | llvm_asm!("nop" : struct); | ^^^^^^ not a string literal error: expected string literal - --> $DIR/asm-parse-errors.rs:6:35 + --> $DIR/llvm-asm-parse-errors.rs:6:35 | LL | llvm_asm!("mov %eax, $$0x2" : struct); | ^^^^^^ not a string literal error: expected `(`, found keyword `struct` - --> $DIR/asm-parse-errors.rs:7:44 + --> $DIR/llvm-asm-parse-errors.rs:7:44 | LL | llvm_asm!("mov %eax, $$0x2" : "={eax}" struct); | ^^^^^^ expected `(` error: expected expression, found keyword `struct` - --> $DIR/asm-parse-errors.rs:8:44 + --> $DIR/llvm-asm-parse-errors.rs:8:44 | LL | llvm_asm!("mov %eax, $$0x2" : "={eax}"(struct)); | ^^^^^^ expected expression error: expected string literal - --> $DIR/asm-parse-errors.rs:9:49 + --> $DIR/llvm-asm-parse-errors.rs:9:49 | LL | llvm_asm!("in %dx, %al" : "={al}"(result) : struct); | ^^^^^^ not a string literal error: expected `(`, found keyword `struct` - --> $DIR/asm-parse-errors.rs:10:56 + --> $DIR/llvm-asm-parse-errors.rs:10:56 | LL | llvm_asm!("in %dx, %al" : "={al}"(result) : "{dx}" struct); | ^^^^^^ expected `(` error: expected expression, found keyword `struct` - --> $DIR/asm-parse-errors.rs:11:56 + --> $DIR/llvm-asm-parse-errors.rs:11:56 | LL | llvm_asm!("in %dx, %al" : "={al}"(result) : "{dx}"(struct)); | ^^^^^^ expected expression error: expected string literal - --> $DIR/asm-parse-errors.rs:12:41 + --> $DIR/llvm-asm-parse-errors.rs:12:41 | LL | llvm_asm!("mov $$0x200, %eax" : : : struct); | ^^^^^^ not a string literal error: expected string literal - --> $DIR/asm-parse-errors.rs:13:50 + --> $DIR/llvm-asm-parse-errors.rs:13:50 | LL | llvm_asm!("mov eax, 2" : "={eax}"(foo) : : : struct); | ^^^^^^ not a string literal error: inline assembly must be a string literal - --> $DIR/asm-parse-errors.rs:14:15 + --> $DIR/llvm-asm-parse-errors.rs:14:15 | LL | llvm_asm!(123); | ^^^ From 1922d203551b4522ad1e7142600f793543aadfe2 Mon Sep 17 00:00:00 2001 From: XAMPPRocky <4464295+XAMPPRocky@users.noreply.github.com> Date: Fri, 17 Apr 2020 13:33:33 +0200 Subject: [PATCH 7/7] Format Mailmap To Work With GitHub --- .mailmap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.mailmap b/.mailmap index 78c3c3019af50..aed3a4ca5b002 100644 --- a/.mailmap +++ b/.mailmap @@ -133,7 +133,7 @@ João Oliveira joaoxsouls Johann Hofmann Johann John Clements John Hodge John Hodge -John Kåre Alsaker +John Kåre Alsaker John Talling Jonathan Bailey Jonathan S Jonathan S @@ -153,7 +153,7 @@ Laurențiu Nicola Lee Jeffery Lee Jeffery Lee Wondong Lennart Kudling -Léo Testard +Léo Testard Lindsey Kuper Lindsey Kuper Luke Metz