1
Fork 0

Reduce use of local_def_id_to_hir_id.

This commit is contained in:
Camille GILLOT 2021-10-20 20:59:15 +02:00
parent ebcc847369
commit 67727aa7c3
39 changed files with 182 additions and 237 deletions

View file

@ -361,8 +361,7 @@ impl<'hir> Map<'hir> {
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
}
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
let id = id.as_local()?;
pub fn get_generics(&self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
let node = self.tcx.hir_owner(id)?;
match node.node {
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),

View file

@ -2449,7 +2449,6 @@ impl<'tcx> Debug for Rvalue<'tcx> {
AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
if let Some(def_id) = def_id.as_local() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
let substs = tcx.lift(substs).unwrap();
format!(
@ -2457,7 +2456,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
)
} else {
let span = tcx.hir().span(hir_id);
let span = tcx.def_span(def_id);
format!(
"[closure@{}]",
tcx.sess.source_map().span_to_diagnostic_string(span)
@ -2481,8 +2480,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
if let Some(def_id) = def_id.as_local() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
let name = format!("[generator@{:?}]", tcx.def_span(def_id));
let mut struct_fmt = fmt.debug_struct(&name);
// FIXME(project-rfc-2229#48): This should be a list of capture names/places

View file

@ -179,15 +179,11 @@ impl<'tcx> MonoItem<'tcx> {
pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> {
match *self {
MonoItem::Fn(Instance { def, .. }) => {
def.def_id().as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
MonoItem::Static(def_id) => {
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
MonoItem::Fn(Instance { def, .. }) => def.def_id().as_local(),
MonoItem::Static(def_id) => def_id.as_local(),
MonoItem::GlobalAsm(item_id) => Some(item_id.def_id),
}
.map(|hir_id| tcx.hir().span(hir_id))
.map(|def_id| tcx.def_span(def_id))
}
// Only used by rustc_codegen_cranelift

View file

@ -665,9 +665,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span {
}
fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
let hir_id =
tcx.hir().local_def_id_to_hir_id(def_id.as_local().expect("expected DefId is local"));
let fn_decl_span = tcx.hir().span(hir_id);
let fn_decl_span = tcx.def_span(def_id);
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
} else {

View file

@ -42,9 +42,7 @@ impl<'tcx> Const<'tcx> {
) -> &'tcx Self {
debug!("Const::from_anon_const(def={:?})", def);
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
let body_id = match tcx.hir().get(hir_id) {
let body_id = match tcx.hir().get_by_def_id(def.did) {
hir::Node::AnonConst(ac) => ac.body,
_ => span_bug!(
tcx.def_span(def.did.to_def_id()),
@ -260,8 +258,7 @@ impl<'tcx> Const<'tcx> {
}
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let default_def_id = match tcx.hir().get(hir_id) {
let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) {
hir::Node::GenericParam(hir::GenericParam {
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
..

View file

@ -1461,8 +1461,7 @@ impl<'tcx> TyCtxt<'tcx> {
_ => return None, // not a free region
};
let hir_id = self.hir().local_def_id_to_hir_id(suitable_region_binding_scope);
let is_impl_item = match self.hir().find(hir_id) {
let is_impl_item = match self.hir().find_by_def_id(suitable_region_binding_scope) {
Some(Node::Item(..) | Node::TraitItem(..)) => false,
Some(Node::ImplItem(..)) => {
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
@ -1495,8 +1494,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
match self.hir().get(hir_id) {
match self.hir().get_by_def_id(scope_def_id) {
Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {}
Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {}
Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {}
@ -1510,6 +1508,7 @@ impl<'tcx> TyCtxt<'tcx> {
let sig = ret_ty.fn_sig(self);
let output = self.erase_late_bound_regions(sig.output());
if output.is_impl_trait() {
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap();
Some((output, fn_decl.output.span()))
} else {

View file

@ -2082,8 +2082,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// with the name of the crate containing the impl.
pub fn span_of_impl(self, impl_did: DefId) -> Result<Span, Symbol> {
if let Some(impl_did) = impl_did.as_local() {
let hir_id = self.hir().local_def_id_to_hir_id(impl_did);
Ok(self.hir().span(hir_id))
Ok(self.def_span(impl_did))
} else {
Err(self.crate_name(impl_did.krate))
}
@ -2130,7 +2129,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// Yields the parent function's `LocalDefId` if `def_id` is an `impl Trait` definition.
pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<LocalDefId> {
let def_id = def_id.as_local()?;
if let Node::Item(item) = tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) {
if let Node::Item(item) = tcx.hir().get_by_def_id(def_id) {
if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind {
return match opaque_ty.origin {
hir::OpaqueTyOrigin::FnReturn(parent) | hir::OpaqueTyOrigin::AsyncFn(parent) => {

View file

@ -671,8 +671,7 @@ pub trait PrettyPrinter<'tcx>:
p!("generator");
// FIXME(eddyb) should use `def_span`.
if let Some(did) = did.as_local() {
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did);
let span = self.tcx().hir().span(hir_id);
let span = self.tcx().def_span(did);
p!(write(
"@{}",
// This may end up in stderr diagnostics but it may also be emitted
@ -708,11 +707,10 @@ pub trait PrettyPrinter<'tcx>:
p!(write("closure"));
// FIXME(eddyb) should use `def_span`.
if let Some(did) = did.as_local() {
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did);
if self.tcx().sess.opts.debugging_opts.span_free_formats {
p!("@", print_def_path(did.to_def_id(), substs));
} else {
let span = self.tcx().hir().span(hir_id);
let span = self.tcx().def_span(did);
p!(write(
"@{}",
// This may end up in stderr diagnostics but it may also be emitted