Expand description
Lowers the AST to the HIR.
Since the AST and HIR are fairly similar, this is mostly a simple procedure, much like a fold. Where lowering involves a bit more work things get more interesting and there are some invariants you should know about. These mostly concern spans and IDs.
Spans are assigned to AST nodes during parsing and then are modified during expansion to indicate the origin of a node and the process it went through being expanded. IDs are assigned to AST nodes just before lowering.
For the simpler lowering steps, IDs and spans should be preserved. Unlike
expansion we do not preserve the process of lowering in the spans, so spans
should not be modified here. When creating a new node (as opposed to
“folding” an existing one), create a new ID using next_id().
You must ensure that IDs are unique. That means that you should only use the ID from an AST node in a single HIR node (you can assume that AST node-IDs are unique). Every new node must have a unique ID. Avoid cloning HIR nodes. If you do, you must then set the new node’s ID to a fresh one.
Spans are used for error messages and for tools to map semantics back to source code. It is therefore not as important with spans as IDs to be strict about use (you can’t break the compiler by screwing up a span). Obviously, a HIR node can only have a single span. But multiple nodes can have the same span and spans don’t need to be kept in order, etc. Where code is preserved by lowering, it should have the same span as in the AST. Where HIR nodes are new it is probably best to give a span for the whole AST node being lowered. All nodes should have real spans; don’t use dummy spans. Tools are likely to get confused if the spans from leaf AST nodes occur in multiple places in the HIR, especially for multiple identifiers.
Re-exports§
pub(crate) use rustc_ast as ast;🔒pub(crate) use std::mem;🔒pub(crate) use Arc;🔒pub(crate) use LazyLock;🔒pub(crate) use self;🔒pub(crate) use MutVisitor;🔒pub(crate) use rustc_ast::node_id::NodeMap;🔒pub(crate) use self;🔒pub(crate) use Visitor;🔒pub(crate) use self as ast;🔒pub(crate) use rustc_attr_ir::find_attr;🔒pub(crate) use rustc_attr_ir::lang_items::LangItem;🔒pub(crate) use AstTarget;🔒pub(crate) use Target;🔒pub(crate) use AttributeParser;🔒pub(crate) use Recovery;🔒pub(crate) use ShouldEmit;🔒pub(crate) use rustc_data_structures::fx::FxIndexMap;🔒pub(crate) use rustc_data_structures::sorted_map::SortedMap;🔒pub(crate) use StableHash;🔒pub(crate) use StableHasher;🔒pub(crate) use rustc_data_structures::steal::Steal;🔒pub(crate) use rustc_data_structures::tagged_ptr::TaggedRef;🔒pub(crate) use DiagArgFromDisplay;🔒pub(crate) use DiagCtxtHandle;🔒pub(crate) use ErrorGuaranteed;🔒pub(crate) use CtorKind;🔒pub(crate) use DefKind;🔒pub(crate) use Namespace;🔒pub(crate) use PerNS;🔒pub(crate) use Res;🔒pub(crate) use DefId;🔒pub(crate) use LOCAL_CRATE;🔒pub(crate) use LocalDefId;🔒pub(crate) use LocalDefIdMap;🔒pub(crate) use rustc_hir::definitions::PerParentDisambiguatorState;🔒pub(crate) use rustc_hir::lints::DelayedLint;🔒pub(crate) use self as hir;🔒pub(crate) use AngleBrackets;🔒pub(crate) use CRATE_OWNER_ID;🔒pub(crate) use ConstArg;🔒pub(crate) use GenericArg;🔒pub(crate) use HirId;🔒pub(crate) use ItemLocalMap;🔒pub(crate) use LifetimeSource;🔒pub(crate) use LifetimeSyntax;🔒pub(crate) use MissingLifetimeKind;🔒pub(crate) use ParamName;🔒pub(crate) use TraitCandidate;🔒pub(crate) use Idx;🔒pub(crate) use IndexSlice;🔒pub(crate) use IndexVec;🔒pub(crate) use rustc_macros::extension;🔒pub(crate) use AstOwner;🔒pub(crate) use LifetimeRes;🔒pub(crate) use PartialRes;🔒pub(crate) use PerOwnerResolverData;🔒pub(crate) use ResolverAstLowering;🔒pub(crate) use rustc_middle::queries::Providers;🔒pub(crate) use rustc_middle::ty::TyCtxt;🔒pub(crate) use rustc_session::diagnostics::add_feature_diagnostics;🔒pub(crate) use Ident;🔒pub(crate) use Symbol;🔒pub(crate) use kw;🔒pub(crate) use sym;🔒pub(crate) use DUMMY_SP;🔒pub(crate) use DesugaringKind;🔒pub(crate) use Span;🔒pub(crate) use span_bug;🔒pub(crate) use SmallVec;🔒pub(crate) use smallvec;🔒pub(crate) use thin_vec::ThinVec;🔒pub(crate) use debug;🔒pub(crate) use instrument;🔒pub(crate) use instrument;🔒pub(crate) use trace;🔒pub(crate) use rustc_ast::*;🔒pub(crate) use rustc_errors::codes::*;🔒
Modules§
- asm 🔒
- block 🔒
- contract 🔒
- delegation 🔒
- This module implements expansion of delegation items with early resolved paths. It includes a delegation to a free functions:
- diagnostics 🔒
- expr 🔒
- format 🔒
- index 🔒
- item 🔒
- pat 🔒
- path 🔒
- re_
lowering 🔒 - stability
Macros§
Structs§
- Assoc
TyParentheses 🔒 - Direct
Const 🔒ArgContext - Generic
Args 🔒Ctor - Helper struct for the delayed construction of
hir::GenericArgs. - Lowering
Context 🔒 - Misplaced
Impl 🔒Trait - PerOwner
Lowering 🔒State - Span
Lowerer 🔒 - Unrepresentable
Const 🔒ArgError
Enums§
- Allow
Return 🔒Type Notation - Assoc
TyParentheses 🔒Sub - Discard
Params 🔒 - FnDecl
Kind 🔒 - Generic
Args 🔒Mode - Impl
Trait 🔒Context - Context of
impl Traitin code, which determines whether it is allowed in an HIR subtree, and if so, what meaning it has. - Impl
Trait 🔒Position - Position in which
impl Traitis disallowed. - Param
Mode 🔒 - Relaxed
Bound 🔒Forbidden Reason - Relaxed
Bound 🔒Policy - How relaxed bounds
?Traitshould be treated. - TryBlock
Scope 🔒
Statics§
- ALLOW_
ASYNC_ 🔒FN_ TRAITS - ALLOW_
ASYNC_ 🔒GEN - ALLOW_
ASYNC_ 🔒ITERATOR - ALLOW_
CONTRACTS 🔒 - ALLOW_
FOR_ 🔒AWAIT - ALLOW_
GEN_ 🔒FUTURE - ALLOW_
GEN_ 🔒FUTURE_ WITH_ ASYNC_ FN_ TRACK_ CALLER - ALLOW_
PATTERN_ 🔒TYPE - ALLOW_
TRY_ 🔒TRAIT