A global definition identifier consisting of a crate number and a definition index.
- let def_id = crate_num.as_def_id();
- let def_id = local_def_id.to_def_id();
- let def_id = owner_id.to_def_id();
- let def_id = mir_source.def_id();
- let def_id = tcx.hir_opt_delegation_sig_id(local_def_id)?;
- let crate_num = def_id.krate;
- let local_def_id = def_id.as_local()?;
- let local_def_id = def_id.expect_local();
- let crate_num = def_id.as_crate_root()?;
/// A `DefId` identifies a particular *definition*, by combining a crate
/// index and a def index.
///
/// You can create a `DefId` from a `LocalDefId` using `local_def_id.to_def_id()`.
#[derive(Clone, PartialEq, Eq, Copy)]
#[cfg_attr(not(target_pointer_width = "64"), derive(Hash))]
#[repr(C)]
#[rustc_pass_by_value]
pub struct DefId {
#[cfg(not(all(target_pointer_width = "64", target_endian = "big")))]
pub index: DefIndex,
pub krate: CrateNum,
#[cfg(all(target_pointer_width = "64", target_endian = "big"))]
pub index: DefIndex,
}A local-only definition identifier for items in the current crate.
- let local_def_id = def_id.as_local()?;
- let local_def_id = def_id.expect_local();
- let local_def_id = tcx.hir_enclosing_body_owner(hir_id);
- let local_def_id = tcx.hir_body_owner_def_id(body_id);
- let local_def_id = tcx.hir_body_owners().next()?;
- let local_def_id = tcx.hir_ty_param_owner(local_def_id_other);
- let local_def_id = tcx.hir_opt_const_param_default_param_def_id(hir_id)?;
- let def_id = local_def_id.to_def_id();
- let local_def_id = owner_id.def_id;
/// A `LocalDefId` is equivalent to a `DefId` with `krate == LOCAL_CRATE`. Since
/// we encode this information in the type, we can ensure at compile time that
/// no `DefId`s from upstream crates get thrown into the mix. There are quite a
/// few cases where we know that only `DefId`s from the local crate are expected;
/// a `DefId` from a different crate would signify a bug somewhere. This
/// is when `LocalDefId` comes in handy.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct LocalDefId {
pub local_def_index: DefIndex,
}A stable hash of a definition path across sessions and crate boundaries.
- let defpathhash = tcx.hir_def_path_hash(local_def_id);
- let defpathhash = DefPathHash.new(stable_crate_id, local_hash);
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub struct DefPathHash(pub Fingerprint);A HIR owner identifier wrapping the local definition that owns a HIR subtree.
- let owner_id = hir_id.expect_owner();
- let owner_id = hir_id.as_owner()?;
- let owner_id = tcx.hir_get_parent_item(hir_id);
- let def_id = owner_id.to_def_id();
- let owner_id = hir_id.owner;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
pub struct OwnerId {
pub def_id: LocalDefId,
}A stable two-part identifier for a HIR node in the current crate.
- let hir_id = HirId.make_owner(local_def_id);
- let hir_id = tcx.hir_body_owner(body_id);
- let hir_id = tcx.hir_parent_id_iter(hir_id_other).next()?;
- let hir_id = tcx.hir_get_fn_id_for_return_block(hir_id_other)?;
- let hir_id = tcx.hir_get_enclosing_scope(hir_id_other)?;
- let hir_id = tcx.hir_get_defining_scope(hir_id_other);
- let owner_id = hir_id.expect_owner();
- let owner_id = hir_id.as_owner()?;
- let owner_id = hir_id.owner;
- let item_local_id = hir_id.local_id;
- let hir_id = body_id.hir_id;
/// Uniquely identifies a node in the HIR of the current crate. It is
/// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
/// and the `local_id` which is unique within the given owner.
///
/// This two-level structure makes for more stable values: One can move an item
/// around within the source code, or add or remove stuff before it, without
/// the `local_id` part of the `HirId` changing, which is a very useful property in
/// incremental compilation where we have to persist things through changes to
/// the code base.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
#[rustc_pass_by_value]
pub struct HirId {
pub owner: OwnerId,
pub local_id: ItemLocalId,
}A typed wrapper identifying a free HIR item owner.
- let item_id = tcx.hir_free_items().next()?;
- let item_id = tcx.hir_module_free_items(local_mod_def_id).next()?;
- let hir_id = item_id.hir_id();
- let owner_id = item_id.owner_id;
// The bodies for items are stored "out of line", in a separate
// hashmap in the `Crate`. Here we just record the hir-id of the item
// so it can fetched later.
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, Hash, HashStable_Generic)]
pub struct ItemId {
pub owner_id: OwnerId,
}A session-local numeric identifier for a crate.
- let cratenum = def_id.as_crate_root()?;
- let def_id = crate_num.as_def_id();
- let crate_num = def_id.krate;
rustc_index::newtype_index! {
#[orderable]
#[debug_format = "crate{}"]
pub struct CrateNum {}
}A stable hash-based crate identifier that survives across compilation sessions.
- let stable_crate_id = defpathhash.stable_crate_id();
- let stable_crate_id = expn_hash.stable_crate_id();
- let stable_crate_id = StableCrateId.new(crate_name, is_exe, metadata, cfg_version);
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(Hash, HashStable_Generic, Encodable, BlobDecodable)]
pub struct StableCrateId(pub(crate) Hash64);A global macro-expansion identifier consisting of a crate and local expansion index.
- let expn_id = local_expn_id.to_expn_id();
- let expn_id = ExpnId.root();
- let expn_id = ExpnId.from_hash(expn_hash)?;
- let expn_id = syntax_context.remove_mark();
- let expn_id = syntax_context.adjust(expn_id_other)?;
- let expn_id = syntax_context.normalize_to_macros_2_0_and_adjust(expn_id_other)?;
- let expn_id = syntax_context.outer_expn();
- let expn_id = register_local_expn_id(expn_data, expn_hash);
- let expn_id = register_expn_id(crate_num, expn_index, expn_data, expn_hash);
- let expn_id = decode_expn_id(crate_num, index, decode_data);
- let crate_num = expn_id.krate;
- let local_expn_id = expn_id.as_local()?;
- let local_expn_id = expn_id.expect_local();
/// A unique ID associated with a macro invocation and expansion.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct ExpnId {
pub krate: CrateNum,
pub local_id: ExpnIndex,
}A local-only macro-expansion identifier for the current crate.
- let local_expn_id = LocalExpnId.fresh_empty();
- let local_expn_id = LocalExpnId.fresh(expn_data, hcx);
- let local_expn_id = expn_id.as_local()?;
- let local_expn_id = expn_id.expect_local();
- let expn_id = local_expn_id.to_expn_id();
rustc_index::newtype_index! {
/// A unique ID associated with a macro invocation and expansion.
#[debug_format = "expn{}"]
pub struct LocalExpnId {}
}A hygiene context represented as a chain of expansion marks and transparencies.
- let syntax_context = SyntaxContext.root();
- let syntax_context = syntax_context_other.apply_mark(expn_id, transparency);
- let syntax_context = syntax_context_other.normalize_to_macros_2_0();
- let syntax_context = syntax_context_other.normalize_to_macro_rules();
- let expn_id = syntax_context.remove_mark();
- let expn_id = syntax_context.adjust(expn_id_other)?;
- let expn_id = syntax_context.normalize_to_macros_2_0_and_adjust(expn_id_other)?;
- let expn_id = syntax_context.outer_expn();
/// A `SyntaxContext` represents a chain of pairs `(ExpnId, Transparency)` named "marks".
///
/// See <https://rustc-dev-guide.rust-lang.org/macro-expansion.html> for more explanation.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SyntaxContext(u32);A MIR local index naming the return place, an argument, or a local temporary.
- let local = body.mut_vars_iter().next()?;
- let local = body.mut_vars_and_args_iter().next()?;
- let local = body.args_iter().next()?;
- let local = body.vars_and_temps_iter().next()?;
rustc_index::newtype_index! {
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "_{}"]
pub struct Local {
const RETURN_PLACE = 0;
}
}A MIR control-flow-graph node indexing a basic block in a body.
- let basic_block = body.basic_blocks_mut().indices().next()?;
- let location = basic_block.start_location();
- let basic_block = location.block;
rustc_index::newtype_index! {
/// A node in the MIR [control-flow graph][CFG].
///
/// There are no branches (e.g., `if`s, function calls, etc.) within a basic block, which makes
/// it easier to do [data-flow analyses] and optimizations. Instead, branches are represented
/// as an edge in a graph between basic blocks.
///
/// Basic blocks consist of a series of [statements][Statement], ending with a
/// [terminator][Terminator]. Basic blocks can have multiple predecessors and successors,
/// however there is a MIR pass ([`CriticalCallEdges`]) that removes *critical edges*, which
/// are edges that go from a multi-successor node to a multi-predecessor node. This pass is
/// needed because some analyses require that there are no critical edges in the CFG.
///
/// Note that this type is just an index into [`Body.basic_blocks`](Body::basic_blocks);
/// the actual data that a basic block holds is in [`BasicBlockData`].
///
/// Read more about basic blocks in the [rustc-dev-guide][guide-mir].
///
/// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg
/// [data-flow analyses]:
/// https://rustc-dev-guide.rust-lang.org/appendix/background.html#what-is-a-dataflow-analysis
/// [`CriticalCallEdges`]: ../../rustc_mir_transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "bb{}"]
pub struct BasicBlock {
const START_BLOCK = 0;
}
}A MIR source-scope index used for debuginfo and lint-root tracking.
- let hir_id = source_scope.lint_root(source_scopes)?;
- let source_scope = source_info.scope;
rustc_index::newtype_index! {
#[stable_hash]
#[encodable]
#[debug_format = "scope[{}]"]
pub struct SourceScope {
const OUTERMOST_SOURCE_SCOPE = 0;
}
}