1 //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
4 use std::{mem, sync::Arc};
8 ast_id_map::{AstIdMap, FileAstId},
10 name::{name, AsName, Name},
11 ExpandError, HirFileId,
17 self, ArgListOwner, ArrayExprKind, AstChildren, LiteralKind, LoopBodyOwner, NameOwner,
20 AstNode, AstPtr, SyntaxNodePtr,
25 body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax},
26 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
28 diagnostics::{InactiveCode, MacroError, UnresolvedMacroCall, UnresolvedProcMacro},
30 dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label,
31 LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField,
35 item_scope::BuiltinShadowMode,
36 path::{GenericArgs, Path},
37 type_ref::{Mutability, Rawness, TypeRef},
38 AdtId, BlockLoc, ModuleDefId, UnresolvedMacro,
41 use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
43 pub struct LowerCtx<'a> {
44 pub db: &'a dyn DefDatabase,
46 file_id: Option<HirFileId>,
47 source_ast_id_map: Option<Arc<AstIdMap>>,
50 impl<'a> LowerCtx<'a> {
51 pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
54 hygiene: Hygiene::new(db.upcast(), file_id),
55 file_id: Some(file_id),
56 source_ast_id_map: Some(db.ast_id_map(file_id)),
60 pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: &Hygiene) -> Self {
61 LowerCtx { db, hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None }
64 pub(crate) fn hygiene(&self) -> &Hygiene {
68 pub(crate) fn file_id(&self) -> HirFileId {
72 pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
73 Path::from_src(ast, self)
76 pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> Option<FileAstId<N>> {
77 self.source_ast_id_map.as_ref().map(|ast_id_map| ast_id_map.ast_id(item))
84 params: Option<ast::ParamList>,
85 body: Option<ast::Expr>,
86 ) -> (Body, BodySourceMap) {
89 source_map: BodySourceMap::default(),
91 exprs: Arena::default(),
92 pats: Arena::default(),
93 labels: Arena::default(),
95 body_expr: dummy_expr_id(),
96 block_scopes: Vec::new(),
100 statements_in_scope: Vec::new(),
102 .collect(params, body)
105 struct ExprCollector<'a> {
106 db: &'a dyn DefDatabase,
109 source_map: BodySourceMap,
110 statements_in_scope: Vec<Statement>,
113 impl ExprCollector<'_> {
116 param_list: Option<ast::ParamList>,
117 body: Option<ast::Expr>,
118 ) -> (Body, BodySourceMap) {
119 if let Some(param_list) = param_list {
120 if let Some(self_param) = param_list.self_param() {
121 let ptr = AstPtr::new(&self_param);
122 let param_pat = self.alloc_pat(
125 mode: BindingAnnotation::new(
126 self_param.mut_token().is_some() && self_param.amp_token().is_none(),
133 self.body.params.push(param_pat);
136 for param in param_list.params() {
137 let pat = match param.pat() {
141 let param_pat = self.collect_pat(pat);
142 self.body.params.push(param_pat);
146 self.body.body_expr = self.collect_expr_opt(body);
147 (self.body, self.source_map)
150 fn ctx(&self) -> LowerCtx<'_> {
151 LowerCtx::new(self.db, self.expander.current_file_id)
154 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
155 let src = self.expander.to_source(ptr);
156 let id = self.make_expr(expr, Ok(src.clone()));
157 self.source_map.expr_map.insert(src, id);
160 // desugared exprs don't have ptr, that's wrong and should be fixed
162 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
163 self.make_expr(expr, Err(SyntheticSyntax))
165 fn unit(&mut self) -> ExprId {
166 self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
168 fn missing_expr(&mut self) -> ExprId {
169 self.alloc_expr_desugared(Expr::Missing)
171 fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId {
172 let id = self.body.exprs.alloc(expr);
173 self.source_map.expr_map_back.insert(id, src);
177 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
178 let src = self.expander.to_source(ptr);
179 let id = self.make_pat(pat, Ok(src.clone()));
180 self.source_map.pat_map.insert(src, id);
183 fn missing_pat(&mut self) -> PatId {
184 self.make_pat(Pat::Missing, Err(SyntheticSyntax))
186 fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId {
187 let id = self.body.pats.alloc(pat);
188 self.source_map.pat_map_back.insert(id, src);
192 fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
193 let src = self.expander.to_source(ptr);
194 let id = self.make_label(label, src.clone());
195 self.source_map.label_map.insert(src, id);
198 fn make_label(&mut self, label: Label, src: LabelSource) -> LabelId {
199 let id = self.body.labels.alloc(label);
200 self.source_map.label_map_back.insert(id, src);
204 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
205 self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr())
208 /// Returns `None` if and only if the expression is `#[cfg]`d out.
209 fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option<ExprId> {
210 let syntax_ptr = AstPtr::new(&expr);
211 self.check_cfg(&expr)?;
214 ast::Expr::IfExpr(e) => {
215 let then_branch = self.collect_block_opt(e.then_branch());
217 let else_branch = e.else_branch().map(|b| match b {
218 ast::ElseBranch::Block(it) => self.collect_block(it),
219 ast::ElseBranch::IfExpr(elif) => {
220 let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
221 self.collect_expr(expr)
225 let condition = match e.condition() {
226 None => self.missing_expr(),
227 Some(condition) => match condition.pat() {
228 None => self.collect_expr_opt(condition.expr()),
229 // if let -- desugar to match
231 let pat = self.collect_pat(pat);
232 let match_expr = self.collect_expr_opt(condition.expr());
233 let placeholder_pat = self.missing_pat();
235 MatchArm { pat, expr: then_branch, guard: None },
237 pat: placeholder_pat,
238 expr: else_branch.unwrap_or_else(|| self.unit()),
243 self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr),
249 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
251 ast::Expr::EffectExpr(e) => match e.effect() {
252 ast::Effect::Try(_) => {
253 let body = self.collect_block_opt(e.block_expr());
254 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
256 ast::Effect::Unsafe(_) => {
257 let body = self.collect_block_opt(e.block_expr());
258 self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
260 // FIXME: we need to record these effects somewhere...
261 ast::Effect::Label(label) => {
262 let label = self.collect_label(label);
263 match e.block_expr() {
265 let res = self.collect_block(block);
266 match &mut self.body.exprs[res] {
267 Expr::Block { label: block_label, .. } => {
268 *block_label = Some(label);
274 None => self.missing_expr(),
277 // FIXME: we need to record these effects somewhere...
278 ast::Effect::Async(_) => {
279 let body = self.collect_block_opt(e.block_expr());
280 self.alloc_expr(Expr::Async { body }, syntax_ptr)
282 ast::Effect::Const(_) => {
283 let body = self.collect_block_opt(e.block_expr());
284 self.alloc_expr(Expr::Const { body }, syntax_ptr)
287 ast::Expr::BlockExpr(e) => self.collect_block(e),
288 ast::Expr::LoopExpr(e) => {
289 let label = e.label().map(|label| self.collect_label(label));
290 let body = self.collect_block_opt(e.loop_body());
291 self.alloc_expr(Expr::Loop { body, label }, syntax_ptr)
293 ast::Expr::WhileExpr(e) => {
294 let label = e.label().map(|label| self.collect_label(label));
295 let body = self.collect_block_opt(e.loop_body());
297 let condition = match e.condition() {
298 None => self.missing_expr(),
299 Some(condition) => match condition.pat() {
300 None => self.collect_expr_opt(condition.expr()),
301 // if let -- desugar to match
303 cov_mark::hit!(infer_resolve_while_let);
304 let pat = self.collect_pat(pat);
305 let match_expr = self.collect_expr_opt(condition.expr());
306 let placeholder_pat = self.missing_pat();
308 self.alloc_expr_desugared(Expr::Break { expr: None, label: None });
310 MatchArm { pat, expr: body, guard: None },
311 MatchArm { pat: placeholder_pat, expr: break_, guard: None },
314 self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
316 self.alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr),
322 self.alloc_expr(Expr::While { condition, body, label }, syntax_ptr)
324 ast::Expr::ForExpr(e) => {
325 let label = e.label().map(|label| self.collect_label(label));
326 let iterable = self.collect_expr_opt(e.iterable());
327 let pat = self.collect_pat_opt(e.pat());
328 let body = self.collect_block_opt(e.loop_body());
329 self.alloc_expr(Expr::For { iterable, pat, body, label }, syntax_ptr)
331 ast::Expr::CallExpr(e) => {
332 let callee = self.collect_expr_opt(e.expr());
333 let args = if let Some(arg_list) = e.arg_list() {
334 arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
338 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
340 ast::Expr::MethodCallExpr(e) => {
341 let receiver = self.collect_expr_opt(e.receiver());
342 let args = if let Some(arg_list) = e.arg_list() {
343 arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
347 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
350 .and_then(|it| GenericArgs::from_ast(&self.ctx(), it))
353 Expr::MethodCall { receiver, method_name, args, generic_args },
357 ast::Expr::MatchExpr(e) => {
358 let expr = self.collect_expr_opt(e.expr());
359 let arms = if let Some(match_arm_list) = e.match_arm_list() {
363 self.check_cfg(&arm).map(|()| MatchArm {
364 pat: self.collect_pat_opt(arm.pat()),
365 expr: self.collect_expr_opt(arm.expr()),
368 .and_then(|guard| guard.expr())
369 .map(|e| self.collect_expr(e)),
376 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
378 ast::Expr::PathExpr(e) => {
381 .and_then(|path| self.expander.parse_path(self.db, path))
383 .unwrap_or(Expr::Missing);
384 self.alloc_expr(path, syntax_ptr)
386 ast::Expr::ContinueExpr(e) => self.alloc_expr(
387 Expr::Continue { label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
390 ast::Expr::BreakExpr(e) => {
391 let expr = e.expr().map(|e| self.collect_expr(e));
393 Expr::Break { expr, label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
397 ast::Expr::ParenExpr(e) => {
398 let inner = self.collect_expr_opt(e.expr());
399 // make the paren expr point to the inner expression as well
400 let src = self.expander.to_source(syntax_ptr);
401 self.source_map.expr_map.insert(src, inner);
404 ast::Expr::ReturnExpr(e) => {
405 let expr = e.expr().map(|e| self.collect_expr(e));
406 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
408 ast::Expr::YieldExpr(e) => {
409 let expr = e.expr().map(|e| self.collect_expr(e));
410 self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
412 ast::Expr::RecordExpr(e) => {
414 e.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
415 let record_lit = if let Some(nfl) = e.record_expr_field_list() {
418 .filter_map(|field| {
419 self.check_cfg(&field)?;
421 let name = field.field_name()?.as_name();
423 let expr = match field.expr() {
424 Some(e) => self.collect_expr(e),
425 None => self.missing_expr(),
427 let src = self.expander.to_source(AstPtr::new(&field));
428 self.source_map.field_map.insert(src.clone(), expr);
429 self.source_map.field_map_back.insert(expr, src);
430 Some(RecordLitField { name, expr })
433 let spread = nfl.spread().map(|s| self.collect_expr(s));
434 Expr::RecordLit { path, fields, spread }
436 Expr::RecordLit { path, fields: Vec::new(), spread: None }
439 self.alloc_expr(record_lit, syntax_ptr)
441 ast::Expr::FieldExpr(e) => {
442 let expr = self.collect_expr_opt(e.expr());
443 let name = match e.field_access() {
444 Some(kind) => kind.as_name(),
445 _ => Name::missing(),
447 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
449 ast::Expr::AwaitExpr(e) => {
450 let expr = self.collect_expr_opt(e.expr());
451 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
453 ast::Expr::TryExpr(e) => {
454 let expr = self.collect_expr_opt(e.expr());
455 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
457 ast::Expr::CastExpr(e) => {
458 let expr = self.collect_expr_opt(e.expr());
459 let type_ref = Interned::new(TypeRef::from_ast_opt(&self.ctx(), e.ty()));
460 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
462 ast::Expr::RefExpr(e) => {
463 let expr = self.collect_expr_opt(e.expr());
464 let raw_tok = e.raw_token().is_some();
465 let mutability = if raw_tok {
466 if e.mut_token().is_some() {
468 } else if e.const_token().is_some() {
471 unreachable!("parser only remaps to raw_token() if matching mutability token follows")
474 Mutability::from_mutable(e.mut_token().is_some())
476 let rawness = Rawness::from_raw(raw_tok);
477 self.alloc_expr(Expr::Ref { expr, rawness, mutability }, syntax_ptr)
479 ast::Expr::PrefixExpr(e) => {
480 let expr = self.collect_expr_opt(e.expr());
481 if let Some(op) = e.op_kind() {
482 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
484 self.alloc_expr(Expr::Missing, syntax_ptr)
487 ast::Expr::ClosureExpr(e) => {
488 let mut args = Vec::new();
489 let mut arg_types = Vec::new();
490 if let Some(pl) = e.param_list() {
491 for param in pl.params() {
492 let pat = self.collect_pat_opt(param.pat());
494 param.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
496 arg_types.push(type_ref);
501 .and_then(|r| r.ty())
502 .map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
503 let body = self.collect_expr_opt(e.body());
504 self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
506 ast::Expr::BinExpr(e) => {
507 let lhs = self.collect_expr_opt(e.lhs());
508 let rhs = self.collect_expr_opt(e.rhs());
509 let op = e.op_kind().map(BinaryOp::from);
510 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
512 ast::Expr::TupleExpr(e) => {
513 let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
514 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
516 ast::Expr::BoxExpr(e) => {
517 let expr = self.collect_expr_opt(e.expr());
518 self.alloc_expr(Expr::Box { expr }, syntax_ptr)
521 ast::Expr::ArrayExpr(e) => {
525 ArrayExprKind::ElementList(e) => {
526 let exprs = e.map(|expr| self.collect_expr(expr)).collect();
527 self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
529 ArrayExprKind::Repeat { initializer, repeat } => {
530 let initializer = self.collect_expr_opt(initializer);
531 let repeat = self.collect_expr_opt(repeat);
533 Expr::Array(Array::Repeat { initializer, repeat }),
540 ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
541 ast::Expr::IndexExpr(e) => {
542 let base = self.collect_expr_opt(e.base());
543 let index = self.collect_expr_opt(e.index());
544 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
546 ast::Expr::RangeExpr(e) => {
547 let lhs = e.start().map(|lhs| self.collect_expr(lhs));
548 let rhs = e.end().map(|rhs| self.collect_expr(rhs));
550 Some(range_type) => {
551 self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
553 None => self.alloc_expr(Expr::Missing, syntax_ptr),
556 ast::Expr::MacroCall(e) => {
557 let macro_ptr = AstPtr::new(&e);
558 let mut ids = vec![];
559 self.collect_macro_call(e, macro_ptr, true, |this, expansion| {
560 ids.push(match expansion {
561 Some(it) => this.collect_expr(it),
562 None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
567 ast::Expr::MacroStmts(e) => {
568 e.statements().for_each(|s| self.collect_stmt(s));
571 .map(|e| self.collect_expr(e))
572 .unwrap_or_else(|| self.alloc_expr(Expr::Missing, syntax_ptr.clone()));
574 self.alloc_expr(Expr::MacroStmts { tail }, syntax_ptr)
579 fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>(
582 syntax_ptr: AstPtr<ast::MacroCall>,
583 is_error_recoverable: bool,
586 // File containing the macro call. Expansion errors will be attached here.
587 let outer_file = self.expander.current_file_id;
589 let macro_call = self.expander.to_source(AstPtr::new(&e));
590 let res = self.expander.enter_expand(self.db, e);
592 let res = match res {
594 Err(UnresolvedMacro { path }) => {
595 self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall(
596 UnresolvedMacroCall {
598 node: syntax_ptr.cast().unwrap(),
602 collector(self, None);
608 Some(ExpandError::UnresolvedProcMacro) => {
609 self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro(
610 UnresolvedProcMacro {
612 node: syntax_ptr.into(),
613 precise_location: None,
619 self.source_map.diagnostics.push(BodyDiagnostic::MacroError(MacroError {
621 node: syntax_ptr.into(),
622 message: err.to_string(),
629 Some((mark, expansion)) => {
630 // FIXME: Statements are too complicated to recover from error for now.
631 // It is because we don't have any hygiene for local variable expansion right now.
632 if !is_error_recoverable && res.err.is_some() {
633 self.expander.exit(self.db, mark);
634 collector(self, None);
636 self.source_map.expansions.insert(macro_call, self.expander.current_file_id);
638 let id = collector(self, Some(expansion));
639 self.expander.exit(self.db, mark);
643 None => collector(self, None),
647 fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
648 if let Some(expr) = expr {
649 self.collect_expr(expr)
655 fn collect_stmt(&mut self, s: ast::Stmt) {
657 ast::Stmt::LetStmt(stmt) => {
658 if self.check_cfg(&stmt).is_none() {
661 let pat = self.collect_pat_opt(stmt.pat());
663 stmt.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
664 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
665 self.statements_in_scope.push(Statement::Let { pat, type_ref, initializer });
667 ast::Stmt::ExprStmt(stmt) => {
668 if self.check_cfg(&stmt).is_none() {
671 let has_semi = stmt.semicolon_token().is_some();
672 // Note that macro could be expended to multiple statements
673 if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
674 let macro_ptr = AstPtr::new(&m);
675 let syntax_ptr = AstPtr::new(&stmt.expr().unwrap());
677 self.collect_macro_call(
681 |this, expansion| match expansion {
683 let statements: ast::MacroStmts = expansion;
685 statements.statements().for_each(|stmt| this.collect_stmt(stmt));
686 if let Some(expr) = statements.expr() {
687 let expr = this.collect_expr(expr);
688 this.statements_in_scope
689 .push(Statement::Expr { expr, has_semi });
693 let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone());
694 this.statements_in_scope.push(Statement::Expr { expr, has_semi });
699 let expr = self.collect_expr_opt(stmt.expr());
700 self.statements_in_scope.push(Statement::Expr { expr, has_semi });
703 ast::Stmt::Item(item) => {
704 if self.check_cfg(&item).is_none() {
711 fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
712 let ast_id = self.expander.ast_id(&block);
714 BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
715 let block_id = self.db.intern_block(block_loc);
717 let (module, def_map) = match self.db.block_def_map(block_id) {
719 self.body.block_scopes.push(block_id);
720 (def_map.root(), def_map)
722 None => (self.expander.module, self.expander.def_map.clone()),
724 let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
725 let prev_local_module = mem::replace(&mut self.expander.module, module);
726 let prev_statements = std::mem::take(&mut self.statements_in_scope);
728 block.statements().for_each(|s| self.collect_stmt(s));
729 block.tail_expr().and_then(|e| {
730 let expr = self.maybe_collect_expr(e)?;
731 Some(self.statements_in_scope.push(Statement::Expr { expr, has_semi: false }))
735 if let Some(Statement::Expr { expr, has_semi: false }) = self.statements_in_scope.last() {
737 self.statements_in_scope.pop();
740 let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements);
741 let syntax_node_ptr = AstPtr::new(&block.into());
742 let expr_id = self.alloc_expr(
743 Expr::Block { id: block_id, statements, tail, label: None },
747 self.expander.def_map = prev_def_map;
748 self.expander.module = prev_local_module;
752 fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
753 if let Some(block) = expr {
754 self.collect_block(block)
760 fn collect_label(&mut self, ast_label: ast::Label) -> LabelId {
762 name: ast_label.lifetime().as_ref().map_or_else(Name::missing, Name::new_lifetime),
764 self.alloc_label(label, AstPtr::new(&ast_label))
767 fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
768 let pattern = match &pat {
769 ast::Pat::IdentPat(bp) => {
770 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
772 BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
773 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
774 if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
775 // This could also be a single-segment path pattern. To
776 // decide that, we need to try resolving the name.
777 let (resolved, _) = self.expander.def_map.resolve_path(
779 self.expander.module,
780 &name.clone().into(),
781 BuiltinShadowMode::Other,
783 match resolved.take_values() {
784 Some(ModuleDefId::ConstId(_)) => Pat::Path(name.into()),
785 Some(ModuleDefId::EnumVariantId(_)) => {
786 // this is only really valid for unit variants, but
787 // shadowing other enum variants with a pattern is
789 Pat::Path(name.into())
791 Some(ModuleDefId::AdtId(AdtId::StructId(s)))
792 if self.db.struct_data(s).variant_data.kind() != StructKind::Record =>
794 // Funnily enough, record structs *can* be shadowed
795 // by pattern bindings (but unit or tuple structs
797 Pat::Path(name.into())
799 // shadowing statics is an error as well, so we just ignore that case here
800 _ => Pat::Bind { name, mode: annotation, subpat },
803 Pat::Bind { name, mode: annotation, subpat }
806 ast::Pat::TupleStructPat(p) => {
808 p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
809 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
810 Pat::TupleStruct { path, args, ellipsis }
812 ast::Pat::RefPat(p) => {
813 let pat = self.collect_pat_opt(p.pat());
814 let mutability = Mutability::from_mutable(p.mut_token().is_some());
815 Pat::Ref { pat, mutability }
817 ast::Pat::PathPat(p) => {
819 p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
820 path.map(Pat::Path).unwrap_or(Pat::Missing)
822 ast::Pat::OrPat(p) => {
823 let pats = p.pats().map(|p| self.collect_pat(p)).collect();
826 ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()),
827 ast::Pat::TuplePat(p) => {
828 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
829 Pat::Tuple { args, ellipsis }
831 ast::Pat::WildcardPat(_) => Pat::Wild,
832 ast::Pat::RecordPat(p) => {
834 p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
836 .record_pat_field_list()
837 .expect("every struct should have a field list")
840 let ast_pat = f.pat()?;
841 let pat = self.collect_pat(ast_pat);
842 let name = f.field_name()?.as_name();
843 Some(RecordFieldPat { name, pat })
848 .record_pat_field_list()
849 .expect("every struct should have a field list")
853 Pat::Record { path, args, ellipsis }
855 ast::Pat::SlicePat(p) => {
856 let SlicePatComponents { prefix, slice, suffix } = p.components();
858 // FIXME properly handle `RestPat`
860 prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(),
861 slice: slice.map(|p| self.collect_pat(p)),
862 suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(),
865 ast::Pat::LiteralPat(lit) => {
866 if let Some(ast_lit) = lit.literal() {
867 let expr = Expr::Literal(ast_lit.kind().into());
868 let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
869 let expr_id = self.alloc_expr(expr, expr_ptr);
875 ast::Pat::RestPat(_) => {
876 // `RestPat` requires special handling and should not be mapped
877 // to a Pat. Here we are using `Pat::Missing` as a fallback for
878 // when `RestPat` is mapped to `Pat`, which can easily happen
879 // when the source code being analyzed has a malformed pattern
880 // which includes `..` in a place where it isn't valid.
884 ast::Pat::BoxPat(boxpat) => {
885 let inner = self.collect_pat_opt(boxpat.pat());
888 ast::Pat::ConstBlockPat(const_block_pat) => {
889 if let Some(expr) = const_block_pat.block_expr() {
890 let expr_id = self.collect_block(expr);
891 Pat::ConstBlock(expr_id)
896 ast::Pat::MacroPat(mac) => match mac.macro_call() {
898 let macro_ptr = AstPtr::new(&call);
900 self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
901 pat = Some(this.collect_pat_opt(expanded_pat));
905 Some(pat) => return pat,
906 None => Pat::Missing,
909 None => Pat::Missing,
912 ast::Pat::RangePat(_) => Pat::Missing,
914 let ptr = AstPtr::new(&pat);
915 self.alloc_pat(pattern, Either::Left(ptr))
918 fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
919 if let Some(pat) = pat {
920 self.collect_pat(pat)
926 fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) {
927 // Find the location of the `..`, if there is one. Note that we do not
928 // consider the possibility of there being multiple `..` here.
929 let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
930 // We want to skip the `..` pattern here, since we account for it above.
932 .filter(|p| !matches!(p, ast::Pat::RestPat(_)))
933 .map(|p| self.collect_pat(p))
939 /// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
941 fn check_cfg(&mut self, owner: &dyn ast::AttrsOwner) -> Option<()> {
942 match self.expander.parse_attrs(self.db, owner).cfg() {
944 if self.expander.cfg_options().check(&cfg) != Some(false) {
948 self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode(InactiveCode {
949 file: self.expander.current_file_id,
950 node: SyntaxNodePtr::new(owner.syntax()),
952 opts: self.expander.cfg_options().clone(),
962 impl From<ast::BinOp> for BinaryOp {
963 fn from(ast_op: ast::BinOp) -> Self {
965 ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
966 ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
967 ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
968 ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
969 ast::BinOp::LesserEqualTest => {
970 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
972 ast::BinOp::GreaterEqualTest => {
973 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
975 ast::BinOp::LesserTest => {
976 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
978 ast::BinOp::GreaterTest => {
979 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
981 ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
982 ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
983 ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
984 ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
985 ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
986 ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
987 ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
988 ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
989 ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
990 ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
991 ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
992 ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
993 ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
994 ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
995 ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
996 ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
997 ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
998 ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
999 ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
1000 ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
1001 ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
1006 impl From<ast::LiteralKind> for Literal {
1007 fn from(ast_lit_kind: ast::LiteralKind) -> Self {
1008 match ast_lit_kind {
1009 // FIXME: these should have actual values filled in, but unsure on perf impact
1010 LiteralKind::IntNumber(lit) => {
1011 if let builtin @ Some(_) = lit.suffix().and_then(BuiltinFloat::from_suffix) {
1012 return Literal::Float(Default::default(), builtin);
1013 } else if let builtin @ Some(_) =
1014 lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it))
1016 Literal::Int(lit.value().unwrap_or(0) as i128, builtin)
1018 let builtin = lit.suffix().and_then(|it| BuiltinUint::from_suffix(&it));
1019 Literal::Uint(lit.value().unwrap_or(0), builtin)
1022 LiteralKind::FloatNumber(lit) => {
1023 let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it));
1024 Literal::Float(Default::default(), ty)
1026 LiteralKind::ByteString(bs) => {
1027 let text = bs.value().map(Vec::from).unwrap_or_else(Default::default);
1028 Literal::ByteString(text)
1030 LiteralKind::String(_) => Literal::String(Default::default()),
1031 LiteralKind::Byte => Literal::Uint(Default::default(), Some(BuiltinUint::U8)),
1032 LiteralKind::Bool(val) => Literal::Bool(val),
1033 LiteralKind::Char => Literal::Char(Default::default()),