1 //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
9 name::{name, AsName, Name},
10 ExpandError, HirFileId,
16 self, ArgListOwner, ArrayExprKind, AstChildren, LiteralKind, LoopBodyOwner, NameOwner,
19 AstNode, AstPtr, SyntaxNodePtr,
24 body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax},
25 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
27 diagnostics::{InactiveCode, MacroError, UnresolvedMacroCall, UnresolvedProcMacro},
29 dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label,
30 LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField,
33 item_scope::BuiltinShadowMode,
34 path::{GenericArgs, Path},
35 type_ref::{Mutability, Rawness, TypeRef},
36 AdtId, BlockLoc, ModuleDefId, UnresolvedMacro,
39 use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
41 pub(crate) struct LowerCtx {
46 pub(crate) fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self {
47 LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) }
49 pub(crate) fn with_hygiene(hygiene: &Hygiene) -> Self {
50 LowerCtx { hygiene: hygiene.clone() }
53 pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
54 Path::from_src(ast, &self.hygiene)
61 params: Option<ast::ParamList>,
62 body: Option<ast::Expr>,
63 ) -> (Body, BodySourceMap) {
66 source_map: BodySourceMap::default(),
68 exprs: Arena::default(),
69 pats: Arena::default(),
70 labels: Arena::default(),
72 body_expr: dummy_expr_id(),
73 block_scopes: Vec::new(),
77 statements_in_scope: Vec::new(),
79 .collect(params, body)
82 struct ExprCollector<'a> {
83 db: &'a dyn DefDatabase,
86 source_map: BodySourceMap,
87 statements_in_scope: Vec<Statement>,
90 impl ExprCollector<'_> {
93 param_list: Option<ast::ParamList>,
94 body: Option<ast::Expr>,
95 ) -> (Body, BodySourceMap) {
96 if let Some(param_list) = param_list {
97 if let Some(self_param) = param_list.self_param() {
98 let ptr = AstPtr::new(&self_param);
99 let param_pat = self.alloc_pat(
102 mode: BindingAnnotation::new(
103 self_param.mut_token().is_some() && self_param.amp_token().is_none(),
110 self.body.params.push(param_pat);
113 for param in param_list.params() {
114 let pat = match param.pat() {
118 let param_pat = self.collect_pat(pat);
119 self.body.params.push(param_pat);
123 self.body.body_expr = self.collect_expr_opt(body);
124 (self.body, self.source_map)
127 fn ctx(&self) -> LowerCtx {
128 LowerCtx::new(self.db, self.expander.current_file_id)
131 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
132 let src = self.expander.to_source(ptr);
133 let id = self.make_expr(expr, Ok(src.clone()));
134 self.source_map.expr_map.insert(src, id);
137 // desugared exprs don't have ptr, that's wrong and should be fixed
139 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
140 self.make_expr(expr, Err(SyntheticSyntax))
142 fn unit(&mut self) -> ExprId {
143 self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
145 fn missing_expr(&mut self) -> ExprId {
146 self.alloc_expr_desugared(Expr::Missing)
148 fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId {
149 let id = self.body.exprs.alloc(expr);
150 self.source_map.expr_map_back.insert(id, src);
154 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
155 let src = self.expander.to_source(ptr);
156 let id = self.make_pat(pat, Ok(src.clone()));
157 self.source_map.pat_map.insert(src, id);
160 fn missing_pat(&mut self) -> PatId {
161 self.make_pat(Pat::Missing, Err(SyntheticSyntax))
163 fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId {
164 let id = self.body.pats.alloc(pat);
165 self.source_map.pat_map_back.insert(id, src);
169 fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
170 let src = self.expander.to_source(ptr);
171 let id = self.make_label(label, src.clone());
172 self.source_map.label_map.insert(src, id);
175 fn make_label(&mut self, label: Label, src: LabelSource) -> LabelId {
176 let id = self.body.labels.alloc(label);
177 self.source_map.label_map_back.insert(id, src);
181 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
182 self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr())
185 /// Returns `None` if the expression is `#[cfg]`d out.
186 fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option<ExprId> {
187 let syntax_ptr = AstPtr::new(&expr);
188 self.check_cfg(&expr)?;
191 ast::Expr::IfExpr(e) => {
192 let then_branch = self.collect_block_opt(e.then_branch());
194 let else_branch = e.else_branch().map(|b| match b {
195 ast::ElseBranch::Block(it) => self.collect_block(it),
196 ast::ElseBranch::IfExpr(elif) => {
197 let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
198 self.collect_expr(expr)
202 let condition = match e.condition() {
203 None => self.missing_expr(),
204 Some(condition) => match condition.pat() {
205 None => self.collect_expr_opt(condition.expr()),
206 // if let -- desugar to match
208 let pat = self.collect_pat(pat);
209 let match_expr = self.collect_expr_opt(condition.expr());
210 let placeholder_pat = self.missing_pat();
212 MatchArm { pat, expr: then_branch, guard: None },
214 pat: placeholder_pat,
215 expr: else_branch.unwrap_or_else(|| self.unit()),
220 self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr),
226 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
228 ast::Expr::EffectExpr(e) => match e.effect() {
229 ast::Effect::Try(_) => {
230 let body = self.collect_block_opt(e.block_expr());
231 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
233 ast::Effect::Unsafe(_) => {
234 let body = self.collect_block_opt(e.block_expr());
235 self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
237 // FIXME: we need to record these effects somewhere...
238 ast::Effect::Label(label) => {
239 let label = self.collect_label(label);
240 match e.block_expr() {
242 let res = self.collect_block(block);
243 match &mut self.body.exprs[res] {
244 Expr::Block { label: block_label, .. } => {
245 *block_label = Some(label);
251 None => self.missing_expr(),
254 // FIXME: we need to record these effects somewhere...
255 ast::Effect::Async(_) => {
256 let body = self.collect_block_opt(e.block_expr());
257 self.alloc_expr(Expr::Async { body }, syntax_ptr)
259 ast::Effect::Const(_) => {
260 let body = self.collect_block_opt(e.block_expr());
261 self.alloc_expr(Expr::Const { body }, syntax_ptr)
264 ast::Expr::BlockExpr(e) => self.collect_block(e),
265 ast::Expr::LoopExpr(e) => {
266 let label = e.label().map(|label| self.collect_label(label));
267 let body = self.collect_block_opt(e.loop_body());
268 self.alloc_expr(Expr::Loop { body, label }, syntax_ptr)
270 ast::Expr::WhileExpr(e) => {
271 let label = e.label().map(|label| self.collect_label(label));
272 let body = self.collect_block_opt(e.loop_body());
274 let condition = match e.condition() {
275 None => self.missing_expr(),
276 Some(condition) => match condition.pat() {
277 None => self.collect_expr_opt(condition.expr()),
278 // if let -- desugar to match
280 cov_mark::hit!(infer_resolve_while_let);
281 let pat = self.collect_pat(pat);
282 let match_expr = self.collect_expr_opt(condition.expr());
283 let placeholder_pat = self.missing_pat();
285 self.alloc_expr_desugared(Expr::Break { expr: None, label: None });
287 MatchArm { pat, expr: body, guard: None },
288 MatchArm { pat: placeholder_pat, expr: break_, guard: None },
291 self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
293 self.alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr),
299 self.alloc_expr(Expr::While { condition, body, label }, syntax_ptr)
301 ast::Expr::ForExpr(e) => {
302 let label = e.label().map(|label| self.collect_label(label));
303 let iterable = self.collect_expr_opt(e.iterable());
304 let pat = self.collect_pat_opt(e.pat());
305 let body = self.collect_block_opt(e.loop_body());
306 self.alloc_expr(Expr::For { iterable, pat, body, label }, syntax_ptr)
308 ast::Expr::CallExpr(e) => {
309 let callee = self.collect_expr_opt(e.expr());
310 let args = if let Some(arg_list) = e.arg_list() {
311 arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
315 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
317 ast::Expr::MethodCallExpr(e) => {
318 let receiver = self.collect_expr_opt(e.receiver());
319 let args = if let Some(arg_list) = e.arg_list() {
320 arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
324 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
327 .and_then(|it| GenericArgs::from_ast(&self.ctx(), it))
330 Expr::MethodCall { receiver, method_name, args, generic_args },
334 ast::Expr::MatchExpr(e) => {
335 let expr = self.collect_expr_opt(e.expr());
336 let arms = if let Some(match_arm_list) = e.match_arm_list() {
340 self.check_cfg(&arm).map(|()| MatchArm {
341 pat: self.collect_pat_opt(arm.pat()),
342 expr: self.collect_expr_opt(arm.expr()),
345 .and_then(|guard| guard.expr())
346 .map(|e| self.collect_expr(e)),
353 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
355 ast::Expr::PathExpr(e) => {
358 .and_then(|path| self.expander.parse_path(path))
360 .unwrap_or(Expr::Missing);
361 self.alloc_expr(path, syntax_ptr)
363 ast::Expr::ContinueExpr(e) => self.alloc_expr(
364 Expr::Continue { label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
367 ast::Expr::BreakExpr(e) => {
368 let expr = e.expr().map(|e| self.collect_expr(e));
370 Expr::Break { expr, label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
374 ast::Expr::ParenExpr(e) => {
375 let inner = self.collect_expr_opt(e.expr());
376 // make the paren expr point to the inner expression as well
377 let src = self.expander.to_source(syntax_ptr);
378 self.source_map.expr_map.insert(src, inner);
381 ast::Expr::ReturnExpr(e) => {
382 let expr = e.expr().map(|e| self.collect_expr(e));
383 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
385 ast::Expr::YieldExpr(e) => {
386 let expr = e.expr().map(|e| self.collect_expr(e));
387 self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
389 ast::Expr::RecordExpr(e) => {
390 let path = e.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
391 let record_lit = if let Some(nfl) = e.record_expr_field_list() {
394 .filter_map(|field| {
395 self.check_cfg(&field)?;
397 let name = field.field_name()?.as_name();
399 let expr = match field.expr() {
400 Some(e) => self.collect_expr(e),
401 None => self.missing_expr(),
403 let src = self.expander.to_source(AstPtr::new(&field));
404 self.source_map.field_map.insert(src.clone(), expr);
405 self.source_map.field_map_back.insert(expr, src);
406 Some(RecordLitField { name, expr })
409 let spread = nfl.spread().map(|s| self.collect_expr(s));
410 Expr::RecordLit { path, fields, spread }
412 Expr::RecordLit { path, fields: Vec::new(), spread: None }
415 self.alloc_expr(record_lit, syntax_ptr)
417 ast::Expr::FieldExpr(e) => {
418 let expr = self.collect_expr_opt(e.expr());
419 let name = match e.field_access() {
420 Some(kind) => kind.as_name(),
421 _ => Name::missing(),
423 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
425 ast::Expr::AwaitExpr(e) => {
426 let expr = self.collect_expr_opt(e.expr());
427 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
429 ast::Expr::TryExpr(e) => {
430 let expr = self.collect_expr_opt(e.expr());
431 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
433 ast::Expr::CastExpr(e) => {
434 let expr = self.collect_expr_opt(e.expr());
435 let type_ref = Box::new(TypeRef::from_ast_opt(&self.ctx(), e.ty()));
436 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
438 ast::Expr::RefExpr(e) => {
439 let expr = self.collect_expr_opt(e.expr());
440 let raw_tok = e.raw_token().is_some();
441 let mutability = if raw_tok {
442 if e.mut_token().is_some() {
444 } else if e.const_token().is_some() {
447 unreachable!("parser only remaps to raw_token() if matching mutability token follows")
450 Mutability::from_mutable(e.mut_token().is_some())
452 let rawness = Rawness::from_raw(raw_tok);
453 self.alloc_expr(Expr::Ref { expr, rawness, mutability }, syntax_ptr)
455 ast::Expr::PrefixExpr(e) => {
456 let expr = self.collect_expr_opt(e.expr());
457 if let Some(op) = e.op_kind() {
458 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
460 self.alloc_expr(Expr::Missing, syntax_ptr)
463 ast::Expr::ClosureExpr(e) => {
464 let mut args = Vec::new();
465 let mut arg_types = Vec::new();
466 if let Some(pl) = e.param_list() {
467 for param in pl.params() {
468 let pat = self.collect_pat_opt(param.pat());
469 let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
471 arg_types.push(type_ref);
476 .and_then(|r| r.ty())
477 .map(|it| Box::new(TypeRef::from_ast(&self.ctx(), it)));
478 let body = self.collect_expr_opt(e.body());
479 self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
481 ast::Expr::BinExpr(e) => {
482 let lhs = self.collect_expr_opt(e.lhs());
483 let rhs = self.collect_expr_opt(e.rhs());
484 let op = e.op_kind().map(BinaryOp::from);
485 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
487 ast::Expr::TupleExpr(e) => {
488 let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
489 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
491 ast::Expr::BoxExpr(e) => {
492 let expr = self.collect_expr_opt(e.expr());
493 self.alloc_expr(Expr::Box { expr }, syntax_ptr)
496 ast::Expr::ArrayExpr(e) => {
500 ArrayExprKind::ElementList(e) => {
501 let exprs = e.map(|expr| self.collect_expr(expr)).collect();
502 self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
504 ArrayExprKind::Repeat { initializer, repeat } => {
505 let initializer = self.collect_expr_opt(initializer);
506 let repeat = self.collect_expr_opt(repeat);
508 Expr::Array(Array::Repeat { initializer, repeat }),
515 ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
516 ast::Expr::IndexExpr(e) => {
517 let base = self.collect_expr_opt(e.base());
518 let index = self.collect_expr_opt(e.index());
519 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
521 ast::Expr::RangeExpr(e) => {
522 let lhs = e.start().map(|lhs| self.collect_expr(lhs));
523 let rhs = e.end().map(|rhs| self.collect_expr(rhs));
525 Some(range_type) => {
526 self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
528 None => self.alloc_expr(Expr::Missing, syntax_ptr),
531 ast::Expr::MacroCall(e) => {
532 let mut ids = vec![];
533 self.collect_macro_call(e, syntax_ptr.clone(), true, |this, expansion| {
534 ids.push(match expansion {
535 Some(it) => this.collect_expr(it),
536 None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
541 ast::Expr::MacroStmts(e) => {
542 e.statements().for_each(|s| self.collect_stmt(s));
545 .map(|e| self.collect_expr(e))
546 .unwrap_or_else(|| self.alloc_expr(Expr::Missing, syntax_ptr.clone()));
548 self.alloc_expr(Expr::MacroStmts { tail }, syntax_ptr)
553 fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>(
556 syntax_ptr: AstPtr<ast::Expr>,
557 is_error_recoverable: bool,
560 // File containing the macro call. Expansion errors will be attached here.
561 let outer_file = self.expander.current_file_id;
563 let macro_call = self.expander.to_source(AstPtr::new(&e));
564 let res = self.expander.enter_expand(self.db, e);
566 let res = match res {
568 Err(UnresolvedMacro) => {
569 self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall(
570 UnresolvedMacroCall { file: outer_file, node: syntax_ptr.cast().unwrap() },
572 collector(self, None);
578 Some(ExpandError::UnresolvedProcMacro) => {
579 self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro(
580 UnresolvedProcMacro {
582 node: syntax_ptr.into(),
583 precise_location: None,
589 self.source_map.diagnostics.push(BodyDiagnostic::MacroError(MacroError {
591 node: syntax_ptr.into(),
592 message: err.to_string(),
599 Some((mark, expansion)) => {
600 // FIXME: Statements are too complicated to recover from error for now.
601 // It is because we don't have any hygiene for local variable expansion right now.
602 if !is_error_recoverable && res.err.is_some() {
603 self.expander.exit(self.db, mark);
604 collector(self, None);
606 self.source_map.expansions.insert(macro_call, self.expander.current_file_id);
608 let id = collector(self, Some(expansion));
609 self.expander.exit(self.db, mark);
613 None => collector(self, None),
617 fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
618 if let Some(expr) = expr {
619 self.collect_expr(expr)
625 fn collect_stmt(&mut self, s: ast::Stmt) {
627 ast::Stmt::LetStmt(stmt) => {
628 if self.check_cfg(&stmt).is_none() {
631 let pat = self.collect_pat_opt(stmt.pat());
632 let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
633 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
634 self.statements_in_scope.push(Statement::Let { pat, type_ref, initializer });
636 ast::Stmt::ExprStmt(stmt) => {
637 if self.check_cfg(&stmt).is_none() {
641 // Note that macro could be expended to multiple statements
642 if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
643 let syntax_ptr = AstPtr::new(&stmt.expr().unwrap());
645 self.collect_macro_call(m, syntax_ptr.clone(), false, |this, expansion| {
648 let statements: ast::MacroStmts = expansion;
650 statements.statements().for_each(|stmt| this.collect_stmt(stmt));
651 if let Some(expr) = statements.expr() {
652 let expr = this.collect_expr(expr);
653 this.statements_in_scope.push(Statement::Expr(expr));
657 let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone());
658 this.statements_in_scope.push(Statement::Expr(expr));
663 let expr = self.collect_expr_opt(stmt.expr());
664 self.statements_in_scope.push(Statement::Expr(expr));
667 ast::Stmt::Item(item) => {
668 if self.check_cfg(&item).is_none() {
675 fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
676 let ast_id = self.expander.ast_id(&block);
678 BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
679 let block_id = self.db.intern_block(block_loc);
681 let (module, def_map) = match self.db.block_def_map(block_id) {
683 self.body.block_scopes.push(block_id);
684 (def_map.root(), def_map)
686 None => (self.expander.module, self.expander.def_map.clone()),
688 let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
689 let prev_local_module = mem::replace(&mut self.expander.module, module);
690 let prev_statements = std::mem::take(&mut self.statements_in_scope);
692 block.statements().for_each(|s| self.collect_stmt(s));
694 let tail = block.tail_expr().map(|e| self.collect_expr(e));
695 let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements);
696 let syntax_node_ptr = AstPtr::new(&block.into());
697 let expr_id = self.alloc_expr(
698 Expr::Block { id: block_id, statements, tail, label: None },
702 self.expander.def_map = prev_def_map;
703 self.expander.module = prev_local_module;
707 fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
708 if let Some(block) = expr {
709 self.collect_block(block)
715 fn collect_label(&mut self, ast_label: ast::Label) -> LabelId {
717 name: ast_label.lifetime().as_ref().map_or_else(Name::missing, Name::new_lifetime),
719 self.alloc_label(label, AstPtr::new(&ast_label))
722 fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
723 let pattern = match &pat {
724 ast::Pat::IdentPat(bp) => {
725 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
727 BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
728 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
729 if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
730 // This could also be a single-segment path pattern. To
731 // decide that, we need to try resolving the name.
732 let (resolved, _) = self.expander.def_map.resolve_path(
734 self.expander.module,
735 &name.clone().into(),
736 BuiltinShadowMode::Other,
738 match resolved.take_values() {
739 Some(ModuleDefId::ConstId(_)) => Pat::Path(name.into()),
740 Some(ModuleDefId::EnumVariantId(_)) => {
741 // this is only really valid for unit variants, but
742 // shadowing other enum variants with a pattern is
744 Pat::Path(name.into())
746 Some(ModuleDefId::AdtId(AdtId::StructId(s)))
747 if self.db.struct_data(s).variant_data.kind() != StructKind::Record =>
749 // Funnily enough, record structs *can* be shadowed
750 // by pattern bindings (but unit or tuple structs
752 Pat::Path(name.into())
754 // shadowing statics is an error as well, so we just ignore that case here
755 _ => Pat::Bind { name, mode: annotation, subpat },
758 Pat::Bind { name, mode: annotation, subpat }
761 ast::Pat::TupleStructPat(p) => {
762 let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
763 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
764 Pat::TupleStruct { path, args, ellipsis }
766 ast::Pat::RefPat(p) => {
767 let pat = self.collect_pat_opt(p.pat());
768 let mutability = Mutability::from_mutable(p.mut_token().is_some());
769 Pat::Ref { pat, mutability }
771 ast::Pat::PathPat(p) => {
772 let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
773 path.map(Pat::Path).unwrap_or(Pat::Missing)
775 ast::Pat::OrPat(p) => {
776 let pats = p.pats().map(|p| self.collect_pat(p)).collect();
779 ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()),
780 ast::Pat::TuplePat(p) => {
781 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
782 Pat::Tuple { args, ellipsis }
784 ast::Pat::WildcardPat(_) => Pat::Wild,
785 ast::Pat::RecordPat(p) => {
786 let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
788 .record_pat_field_list()
789 .expect("every struct should have a field list")
792 let ast_pat = f.pat()?;
793 let pat = self.collect_pat(ast_pat);
794 let name = f.field_name()?.as_name();
795 Some(RecordFieldPat { name, pat })
800 .record_pat_field_list()
801 .expect("every struct should have a field list")
805 Pat::Record { path, args, ellipsis }
807 ast::Pat::SlicePat(p) => {
808 let SlicePatComponents { prefix, slice, suffix } = p.components();
810 // FIXME properly handle `RestPat`
812 prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(),
813 slice: slice.map(|p| self.collect_pat(p)),
814 suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(),
817 ast::Pat::LiteralPat(lit) => {
818 if let Some(ast_lit) = lit.literal() {
819 let expr = Expr::Literal(ast_lit.kind().into());
820 let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
821 let expr_id = self.alloc_expr(expr, expr_ptr);
827 ast::Pat::RestPat(_) => {
828 // `RestPat` requires special handling and should not be mapped
829 // to a Pat. Here we are using `Pat::Missing` as a fallback for
830 // when `RestPat` is mapped to `Pat`, which can easily happen
831 // when the source code being analyzed has a malformed pattern
832 // which includes `..` in a place where it isn't valid.
836 ast::Pat::BoxPat(boxpat) => {
837 let inner = self.collect_pat_opt(boxpat.pat());
840 ast::Pat::ConstBlockPat(const_block_pat) => {
841 if let Some(expr) = const_block_pat.block_expr() {
842 let expr_id = self.collect_block(expr);
843 Pat::ConstBlock(expr_id)
849 ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
851 let ptr = AstPtr::new(&pat);
852 self.alloc_pat(pattern, Either::Left(ptr))
855 fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
856 if let Some(pat) = pat {
857 self.collect_pat(pat)
863 fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) {
864 // Find the location of the `..`, if there is one. Note that we do not
865 // consider the possibility of there being multiple `..` here.
866 let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
867 // We want to skip the `..` pattern here, since we account for it above.
869 .filter(|p| !matches!(p, ast::Pat::RestPat(_)))
870 .map(|p| self.collect_pat(p))
876 /// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
878 fn check_cfg(&mut self, owner: &dyn ast::AttrsOwner) -> Option<()> {
879 match self.expander.parse_attrs(self.db, owner).cfg() {
881 if self.expander.cfg_options().check(&cfg) != Some(false) {
885 self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode(InactiveCode {
886 file: self.expander.current_file_id,
887 node: SyntaxNodePtr::new(owner.syntax()),
889 opts: self.expander.cfg_options().clone(),
899 impl From<ast::BinOp> for BinaryOp {
900 fn from(ast_op: ast::BinOp) -> Self {
902 ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
903 ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
904 ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
905 ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
906 ast::BinOp::LesserEqualTest => {
907 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
909 ast::BinOp::GreaterEqualTest => {
910 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
912 ast::BinOp::LesserTest => {
913 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
915 ast::BinOp::GreaterTest => {
916 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
918 ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
919 ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
920 ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
921 ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
922 ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
923 ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
924 ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
925 ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
926 ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
927 ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
928 ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
929 ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
930 ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
931 ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
932 ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
933 ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
934 ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
935 ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
936 ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
937 ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
938 ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
943 impl From<ast::LiteralKind> for Literal {
944 fn from(ast_lit_kind: ast::LiteralKind) -> Self {
946 LiteralKind::IntNumber(lit) => {
947 if let builtin @ Some(_) = lit.suffix().and_then(BuiltinFloat::from_suffix) {
948 return Literal::Float(Default::default(), builtin);
949 } else if let builtin @ Some(_) =
950 lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it))
952 Literal::Int(Default::default(), builtin)
954 let builtin = lit.suffix().and_then(|it| BuiltinUint::from_suffix(&it));
955 Literal::Uint(Default::default(), builtin)
958 LiteralKind::FloatNumber(lit) => {
959 let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it));
960 Literal::Float(Default::default(), ty)
962 LiteralKind::ByteString(_) => Literal::ByteString(Default::default()),
963 LiteralKind::String(_) => Literal::String(Default::default()),
964 LiteralKind::Byte => Literal::Uint(Default::default(), Some(BuiltinUint::U8)),
965 LiteralKind::Bool(val) => Literal::Bool(val),
966 LiteralKind::Char => Literal::Char(Default::default()),