Skip to content

Expose more information with DefId in smir #115534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ impl<'tcx> Context for Tables<'tcx> {
})
}

fn name_of_def_id(&self, def_id: stable_mir::DefId) -> String {
self.tcx.def_path_str(self[def_id])
}

fn all_local_items(&mut self) -> stable_mir::CrateItems {
self.tcx.mir_keys(()).iter().map(|item| self.crate_item(item.to_def_id())).collect()
}

fn entry_fn(&mut self) -> Option<stable_mir::CrateItem> {
Some(self.crate_item(self.tcx.entry_fn(())?.0))
}
Expand Down
16 changes: 15 additions & 1 deletion compiler/rustc_smir/src/stable_mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//! If you need an internal construct, consider using `rustc_internal` or `rustc_smir`.

use std::cell::Cell;
use std::fmt;
use std::fmt::Debug;

use self::ty::{
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
Expand All @@ -29,9 +31,18 @@ pub type Symbol = String;
pub type CrateNum = usize;

/// A unique identification number for each item accessible for the current compilation unit.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct DefId(pub(crate) usize);

impl Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DefId:")
.field("id", &self.0)
.field("name", &with(|cx| cx.name_of_def_id(*self)))
.finish()
}
}

/// A unique identification number for each provenance
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AllocId(pub(crate) usize);
Expand Down Expand Up @@ -127,6 +138,9 @@ pub trait Context {
/// Find a crate with the given name.
fn find_crate(&self, name: &str) -> Option<Crate>;

/// Prints the name of given `DefId`
fn name_of_def_id(&self, def_id: DefId) -> String;

/// Obtain the representation of a type.
fn ty_kind(&mut self, ty: Ty) -> TyKind;

Expand Down