Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/body/lower.rs')
-rw-r--r--crates/hir-def/src/body/lower.rs45
1 files changed, 29 insertions, 16 deletions
diff --git a/crates/hir-def/src/body/lower.rs b/crates/hir-def/src/body/lower.rs
index abf7895829..9c547574ec 100644
--- a/crates/hir-def/src/body/lower.rs
+++ b/crates/hir-def/src/body/lower.rs
@@ -1,6 +1,8 @@
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
//! representation.
+mod asm;
+
use std::mem;
use base_db::CrateId;
@@ -35,8 +37,8 @@ use crate::{
FormatPlaceholder, FormatSign, FormatTrait,
},
Array, Binding, BindingAnnotation, BindingId, BindingProblems, CaptureBy, ClosureKind,
- Expr, ExprId, InlineAsm, Label, LabelId, Literal, LiteralOrConst, MatchArm, Movability,
- OffsetOf, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
+ Expr, ExprId, Label, LabelId, Literal, LiteralOrConst, MatchArm, Movability, OffsetOf, Pat,
+ PatId, RecordFieldPat, RecordLitField, Statement,
},
item_scope::BuiltinShadowMode,
lang_item::LangItem,
@@ -693,10 +695,7 @@ impl ExprCollector<'_> {
}
}
ast::Expr::UnderscoreExpr(_) => self.alloc_expr(Expr::Underscore, syntax_ptr),
- ast::Expr::AsmExpr(e) => {
- let e = self.collect_expr_opt(e.expr());
- self.alloc_expr(Expr::InlineAsm(InlineAsm { e }), syntax_ptr)
- }
+ ast::Expr::AsmExpr(e) => self.lower_inline_asm(e, syntax_ptr),
ast::Expr::OffsetOfExpr(e) => {
let container = Interned::new(TypeRef::from_ast_opt(&self.ctx(), e.ty()));
let fields = e.fields().map(|it| it.as_name()).collect();
@@ -737,7 +736,7 @@ impl ExprCollector<'_> {
/// `try { <stmts>; }` into `'<new_label>: { <stmts>; ::std::ops::Try::from_output(()) }`
/// and save the `<new_label>` to use it as a break target for desugaring of the `?` operator.
fn desugar_try_block(&mut self, e: BlockExpr) -> ExprId {
- let Some(try_from_output) = LangItem::TryTraitFromOutput.path(self.db, self.krate) else {
+ let Some(try_from_output) = self.lang_path(LangItem::TryTraitFromOutput) else {
return self.collect_block(e);
};
let label = self
@@ -840,10 +839,10 @@ impl ExprCollector<'_> {
fn collect_for_loop(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::ForExpr) -> ExprId {
let Some((into_iter_fn, iter_next_fn, option_some, option_none)) = (|| {
Some((
- LangItem::IntoIterIntoIter.path(self.db, self.krate)?,
- LangItem::IteratorNext.path(self.db, self.krate)?,
- LangItem::OptionSome.path(self.db, self.krate)?,
- LangItem::OptionNone.path(self.db, self.krate)?,
+ self.lang_path(LangItem::IntoIterIntoIter)?,
+ self.lang_path(LangItem::IteratorNext)?,
+ self.lang_path(LangItem::OptionSome)?,
+ self.lang_path(LangItem::OptionNone)?,
))
})() else {
// Some of the needed lang items are missing, so we can't desugar
@@ -896,6 +895,15 @@ impl ExprCollector<'_> {
Expr::Match { expr: iter_next_expr, arms: Box::new([none_arm, some_arm]) },
syntax_ptr,
);
+ let loop_inner = self.alloc_expr(
+ Expr::Block {
+ id: None,
+ statements: Box::default(),
+ tail: Some(loop_inner),
+ label: None,
+ },
+ syntax_ptr,
+ );
let loop_outer = self.alloc_expr(Expr::Loop { body: loop_inner, label }, syntax_ptr);
let iter_binding = self.alloc_binding(iter_name, BindingAnnotation::Mutable);
let iter_pat = self.alloc_pat_desugared(Pat::Bind { id: iter_binding, subpat: None });
@@ -923,10 +931,10 @@ impl ExprCollector<'_> {
fn collect_try_operator(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::TryExpr) -> ExprId {
let Some((try_branch, cf_continue, cf_break, try_from_residual)) = (|| {
Some((
- LangItem::TryTraitBranch.path(self.db, self.krate)?,
- LangItem::ControlFlowContinue.path(self.db, self.krate)?,
- LangItem::ControlFlowBreak.path(self.db, self.krate)?,
- LangItem::TryTraitFromResidual.path(self.db, self.krate)?,
+ self.lang_path(LangItem::TryTraitBranch)?,
+ self.lang_path(LangItem::ControlFlowContinue)?,
+ self.lang_path(LangItem::ControlFlowBreak)?,
+ self.lang_path(LangItem::TryTraitFromResidual)?,
))
})() else {
// Some of the needed lang items are missing, so we can't desugar
@@ -1839,7 +1847,7 @@ impl ExprCollector<'_> {
},
syntax_ptr,
);
- self.source_map.format_args_template_map.insert(idx, mappings);
+ self.source_map.template_map.get_or_insert_with(Default::default).0.insert(idx, mappings);
idx
}
@@ -2052,7 +2060,12 @@ impl ExprCollector<'_> {
is_assignee_expr: false,
})
}
+
// endregion: format
+
+ fn lang_path(&self, lang: LangItem) -> Option<Path> {
+ lang.path(self.db, self.krate)
+ }
}
fn pat_literal_to_hir(lit: &ast::LiteralPat) -> Option<(Literal, ast::Literal)> {