1 //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
4 use std::{mem, sync::Arc};
9 name::{name, AsName, Name},
10 ExpandError, HirFileId,
14 use rustc_hash::FxHashMap;
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, UnresolvedProcMacro},
30 dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label,
31 LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField,
34 item_scope::BuiltinShadowMode,
36 path::{GenericArgs, Path},
37 type_ref::{Mutability, Rawness, TypeRef},
38 AdtId, BlockLoc, ModuleDefId,
41 use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
43 pub(crate) struct LowerCtx {
48 pub(crate) fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self {
49 LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) }
51 pub(crate) fn with_hygiene(hygiene: &Hygiene) -> Self {
52 LowerCtx { hygiene: hygiene.clone() }
55 pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
56 Path::from_src(ast, &self.hygiene)
63 params: Option<ast::ParamList>,
64 body: Option<ast::Expr>,
65 ) -> (Body, BodySourceMap) {
66 let item_tree = db.item_tree(expander.current_file_id);
69 source_map: BodySourceMap::default(),
71 exprs: Arena::default(),
72 pats: Arena::default(),
73 labels: Arena::default(),
75 body_expr: dummy_expr_id(),
76 block_scopes: Vec::new(),
80 let mut map = FxHashMap::default();
81 map.insert(expander.current_file_id, item_tree);
86 .collect(params, body)
89 struct ExprCollector<'a> {
90 db: &'a dyn DefDatabase,
93 source_map: BodySourceMap,
95 item_trees: FxHashMap<HirFileId, Arc<ItemTree>>,
98 impl ExprCollector<'_> {
101 param_list: Option<ast::ParamList>,
102 body: Option<ast::Expr>,
103 ) -> (Body, BodySourceMap) {
104 if let Some(param_list) = param_list {
105 if let Some(self_param) = param_list.self_param() {
106 let ptr = AstPtr::new(&self_param);
107 let param_pat = self.alloc_pat(
110 mode: BindingAnnotation::new(
111 self_param.mut_token().is_some() && self_param.amp_token().is_none(),
118 self.body.params.push(param_pat);
121 for param in param_list.params() {
122 let pat = match param.pat() {
126 let param_pat = self.collect_pat(pat);
127 self.body.params.push(param_pat);
131 self.body.body_expr = self.collect_expr_opt(body);
132 (self.body, self.source_map)
135 fn ctx(&self) -> LowerCtx {
136 LowerCtx::new(self.db, self.expander.current_file_id)
139 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
140 let src = self.expander.to_source(ptr);
141 let id = self.make_expr(expr, Ok(src.clone()));
142 self.source_map.expr_map.insert(src, id);
145 // desugared exprs don't have ptr, that's wrong and should be fixed
147 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
148 self.make_expr(expr, Err(SyntheticSyntax))
150 fn unit(&mut self) -> ExprId {
151 self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
153 fn missing_expr(&mut self) -> ExprId {
154 self.alloc_expr_desugared(Expr::Missing)
156 fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId {
157 let id = self.body.exprs.alloc(expr);
158 self.source_map.expr_map_back.insert(id, src);
162 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
163 let src = self.expander.to_source(ptr);
164 let id = self.make_pat(pat, Ok(src.clone()));
165 self.source_map.pat_map.insert(src, id);
168 fn missing_pat(&mut self) -> PatId {
169 self.make_pat(Pat::Missing, Err(SyntheticSyntax))
171 fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId {
172 let id = self.body.pats.alloc(pat);
173 self.source_map.pat_map_back.insert(id, src);
177 fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
178 let src = self.expander.to_source(ptr);
179 let id = self.make_label(label, src.clone());
180 self.source_map.label_map.insert(src, id);
183 fn make_label(&mut self, label: Label, src: LabelSource) -> LabelId {
184 let id = self.body.labels.alloc(label);
185 self.source_map.label_map_back.insert(id, src);
189 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
190 let syntax_ptr = AstPtr::new(&expr);
191 if self.check_cfg(&expr).is_none() {
192 return self.missing_expr();
196 ast::Expr::IfExpr(e) => {
197 let then_branch = self.collect_block_opt(e.then_branch());
199 let else_branch = e.else_branch().map(|b| match b {
200 ast::ElseBranch::Block(it) => self.collect_block(it),
201 ast::ElseBranch::IfExpr(elif) => {
202 let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
203 self.collect_expr(expr)
207 let condition = match e.condition() {
208 None => self.missing_expr(),
209 Some(condition) => match condition.pat() {
210 None => self.collect_expr_opt(condition.expr()),
211 // if let -- desugar to match
213 let pat = self.collect_pat(pat);
214 let match_expr = self.collect_expr_opt(condition.expr());
215 let placeholder_pat = self.missing_pat();
217 MatchArm { pat, expr: then_branch, guard: None },
219 pat: placeholder_pat,
220 expr: else_branch.unwrap_or_else(|| self.unit()),
225 .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr);
230 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
232 ast::Expr::EffectExpr(e) => match e.effect() {
233 ast::Effect::Try(_) => {
234 let body = self.collect_block_opt(e.block_expr());
235 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
237 ast::Effect::Unsafe(_) => {
238 let body = self.collect_block_opt(e.block_expr());
239 self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
241 // FIXME: we need to record these effects somewhere...
242 ast::Effect::Label(label) => {
243 let label = self.collect_label(label);
244 match e.block_expr() {
246 let res = self.collect_block(block);
247 match &mut self.body.exprs[res] {
248 Expr::Block { label: block_label, .. } => {
249 *block_label = Some(label);
255 None => self.missing_expr(),
258 // FIXME: we need to record these effects somewhere...
259 ast::Effect::Async(_) => {
260 let body = self.collect_block_opt(e.block_expr());
261 self.alloc_expr(Expr::Async { body }, syntax_ptr)
263 ast::Effect::Const(_) => {
264 let body = self.collect_block_opt(e.block_expr());
265 self.alloc_expr(Expr::Const { body }, syntax_ptr)
268 ast::Expr::BlockExpr(e) => self.collect_block(e),
269 ast::Expr::LoopExpr(e) => {
270 let label = e.label().map(|label| self.collect_label(label));
271 let body = self.collect_block_opt(e.loop_body());
272 self.alloc_expr(Expr::Loop { body, label }, syntax_ptr)
274 ast::Expr::WhileExpr(e) => {
275 let label = e.label().map(|label| self.collect_label(label));
276 let body = self.collect_block_opt(e.loop_body());
278 let condition = match e.condition() {
279 None => self.missing_expr(),
280 Some(condition) => match condition.pat() {
281 None => self.collect_expr_opt(condition.expr()),
282 // if let -- desugar to match
284 cov_mark::hit!(infer_resolve_while_let);
285 let pat = self.collect_pat(pat);
286 let match_expr = self.collect_expr_opt(condition.expr());
287 let placeholder_pat = self.missing_pat();
289 self.alloc_expr_desugared(Expr::Break { expr: None, label: None });
291 MatchArm { pat, expr: body, guard: None },
292 MatchArm { pat: placeholder_pat, expr: break_, guard: None },
295 self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
297 .alloc_expr(Expr::Loop { body: match_expr, label }, syntax_ptr);
302 self.alloc_expr(Expr::While { condition, body, label }, syntax_ptr)
304 ast::Expr::ForExpr(e) => {
305 let label = e.label().map(|label| self.collect_label(label));
306 let iterable = self.collect_expr_opt(e.iterable());
307 let pat = self.collect_pat_opt(e.pat());
308 let body = self.collect_block_opt(e.loop_body());
309 self.alloc_expr(Expr::For { iterable, pat, body, label }, syntax_ptr)
311 ast::Expr::CallExpr(e) => {
312 let callee = self.collect_expr_opt(e.expr());
313 let args = if let Some(arg_list) = e.arg_list() {
314 arg_list.args().map(|e| self.collect_expr(e)).collect()
318 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
320 ast::Expr::MethodCallExpr(e) => {
321 let receiver = self.collect_expr_opt(e.receiver());
322 let args = if let Some(arg_list) = e.arg_list() {
323 arg_list.args().map(|e| self.collect_expr(e)).collect()
327 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
329 e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it));
331 Expr::MethodCall { receiver, method_name, args, generic_args },
335 ast::Expr::MatchExpr(e) => {
336 let expr = self.collect_expr_opt(e.expr());
337 let arms = if let Some(match_arm_list) = e.match_arm_list() {
341 self.check_cfg(&arm).map(|()| MatchArm {
342 pat: self.collect_pat_opt(arm.pat()),
343 expr: self.collect_expr_opt(arm.expr()),
346 .and_then(|guard| guard.expr())
347 .map(|e| self.collect_expr(e)),
354 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
356 ast::Expr::PathExpr(e) => {
359 .and_then(|path| self.expander.parse_path(path))
361 .unwrap_or(Expr::Missing);
362 self.alloc_expr(path, syntax_ptr)
364 ast::Expr::ContinueExpr(e) => self.alloc_expr(
365 Expr::Continue { label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
368 ast::Expr::BreakExpr(e) => {
369 let expr = e.expr().map(|e| self.collect_expr(e));
371 Expr::Break { expr, label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
375 ast::Expr::ParenExpr(e) => {
376 let inner = self.collect_expr_opt(e.expr());
377 // make the paren expr point to the inner expression as well
378 let src = self.expander.to_source(syntax_ptr);
379 self.source_map.expr_map.insert(src, inner);
382 ast::Expr::ReturnExpr(e) => {
383 let expr = e.expr().map(|e| self.collect_expr(e));
384 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
386 ast::Expr::YieldExpr(e) => {
387 let expr = e.expr().map(|e| self.collect_expr(e));
388 self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
390 ast::Expr::RecordExpr(e) => {
391 let path = e.path().and_then(|path| self.expander.parse_path(path));
392 let mut field_ptrs = Vec::new();
393 let record_lit = if let Some(nfl) = e.record_expr_field_list() {
396 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
397 .filter_map(|field| {
398 self.check_cfg(&field)?;
400 let name = field.field_name()?.as_name();
402 Some(RecordLitField {
404 expr: match field.expr() {
405 Some(e) => self.collect_expr(e),
406 None => self.missing_expr(),
411 let spread = nfl.spread().map(|s| self.collect_expr(s));
412 Expr::RecordLit { path, fields, spread }
414 Expr::RecordLit { path, fields: Vec::new(), spread: None }
417 let res = self.alloc_expr(record_lit, syntax_ptr);
418 for (i, ptr) in field_ptrs.into_iter().enumerate() {
419 let src = self.expander.to_source(ptr);
420 self.source_map.field_map.insert((res, i), src);
424 ast::Expr::FieldExpr(e) => {
425 let expr = self.collect_expr_opt(e.expr());
426 let name = match e.field_access() {
427 Some(kind) => kind.as_name(),
428 _ => Name::missing(),
430 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
432 ast::Expr::AwaitExpr(e) => {
433 let expr = self.collect_expr_opt(e.expr());
434 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
436 ast::Expr::TryExpr(e) => {
437 let expr = self.collect_expr_opt(e.expr());
438 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
440 ast::Expr::CastExpr(e) => {
441 let expr = self.collect_expr_opt(e.expr());
442 let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty());
443 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
445 ast::Expr::RefExpr(e) => {
446 let expr = self.collect_expr_opt(e.expr());
447 let raw_tok = e.raw_token().is_some();
448 let mutability = if raw_tok {
449 if e.mut_token().is_some() {
451 } else if e.const_token().is_some() {
454 unreachable!("parser only remaps to raw_token() if matching mutability token follows")
457 Mutability::from_mutable(e.mut_token().is_some())
459 let rawness = Rawness::from_raw(raw_tok);
460 self.alloc_expr(Expr::Ref { expr, rawness, mutability }, syntax_ptr)
462 ast::Expr::PrefixExpr(e) => {
463 let expr = self.collect_expr_opt(e.expr());
464 if let Some(op) = e.op_kind() {
465 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
467 self.alloc_expr(Expr::Missing, syntax_ptr)
470 ast::Expr::ClosureExpr(e) => {
471 let mut args = Vec::new();
472 let mut arg_types = Vec::new();
473 if let Some(pl) = e.param_list() {
474 for param in pl.params() {
475 let pat = self.collect_pat_opt(param.pat());
476 let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
478 arg_types.push(type_ref);
482 e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it));
483 let body = self.collect_expr_opt(e.body());
484 self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
486 ast::Expr::BinExpr(e) => {
487 let lhs = self.collect_expr_opt(e.lhs());
488 let rhs = self.collect_expr_opt(e.rhs());
489 let op = e.op_kind().map(BinaryOp::from);
490 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
492 ast::Expr::TupleExpr(e) => {
493 let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
494 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
496 ast::Expr::BoxExpr(e) => {
497 let expr = self.collect_expr_opt(e.expr());
498 self.alloc_expr(Expr::Box { expr }, syntax_ptr)
501 ast::Expr::ArrayExpr(e) => {
505 ArrayExprKind::ElementList(e) => {
506 let exprs = e.map(|expr| self.collect_expr(expr)).collect();
507 self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
509 ArrayExprKind::Repeat { initializer, repeat } => {
510 let initializer = self.collect_expr_opt(initializer);
511 let repeat = self.collect_expr_opt(repeat);
513 Expr::Array(Array::Repeat { initializer, repeat }),
520 ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
521 ast::Expr::IndexExpr(e) => {
522 let base = self.collect_expr_opt(e.base());
523 let index = self.collect_expr_opt(e.index());
524 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
526 ast::Expr::RangeExpr(e) => {
527 let lhs = e.start().map(|lhs| self.collect_expr(lhs));
528 let rhs = e.end().map(|rhs| self.collect_expr(rhs));
530 Some(range_type) => {
531 self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
533 None => self.alloc_expr(Expr::Missing, syntax_ptr),
536 ast::Expr::MacroCall(e) => {
537 let mut ids = vec![];
538 self.collect_macro_call(e, syntax_ptr.clone(), |this, expansion| {
539 ids.push(match expansion {
540 Some(it) => this.collect_expr(it),
541 None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
549 fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>(
552 syntax_ptr: AstPtr<ast::Expr>,
555 // File containing the macro call. Expansion errors will be attached here.
556 let outer_file = self.expander.current_file_id;
558 let macro_call = self.expander.to_source(AstPtr::new(&e));
559 let res = self.expander.enter_expand(self.db, e);
562 Some(ExpandError::UnresolvedProcMacro) => {
563 self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro(
564 UnresolvedProcMacro {
566 node: syntax_ptr.into(),
567 precise_location: None,
573 self.source_map.diagnostics.push(BodyDiagnostic::MacroError(MacroError {
575 node: syntax_ptr.into(),
576 message: err.to_string(),
583 Some((mark, expansion)) => {
584 // FIXME: Statements are too complicated to recover from error for now.
585 // It is because we don't have any hygiene for local variable expansion right now.
586 if T::can_cast(syntax::SyntaxKind::MACRO_STMTS) && res.err.is_some() {
587 self.expander.exit(self.db, mark);
588 collector(self, None);
590 self.source_map.expansions.insert(macro_call, self.expander.current_file_id);
592 let item_tree = self.db.item_tree(self.expander.current_file_id);
593 self.item_trees.insert(self.expander.current_file_id, item_tree);
595 let id = collector(self, Some(expansion));
596 self.expander.exit(self.db, mark);
600 None => collector(self, None),
604 fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
605 if let Some(expr) = expr {
606 self.collect_expr(expr)
612 fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Vec<Statement>> {
615 ast::Stmt::LetStmt(stmt) => {
616 self.check_cfg(&stmt)?;
618 let pat = self.collect_pat_opt(stmt.pat());
619 let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
620 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
621 vec![Statement::Let { pat, type_ref, initializer }]
623 ast::Stmt::ExprStmt(stmt) => {
624 self.check_cfg(&stmt)?;
626 // Note that macro could be expended to multiple statements
627 if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
628 let syntax_ptr = AstPtr::new(&stmt.expr().unwrap());
629 let mut stmts = vec![];
631 self.collect_macro_call(m, syntax_ptr.clone(), |this, expansion| {
634 let statements: ast::MacroStmts = expansion;
636 statements.statements().for_each(|stmt| {
637 if let Some(mut r) = this.collect_stmt(stmt) {
638 stmts.append(&mut r);
641 if let Some(expr) = statements.expr() {
642 stmts.push(Statement::Expr(this.collect_expr(expr)));
646 stmts.push(Statement::Expr(
647 this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
654 vec![Statement::Expr(self.collect_expr_opt(stmt.expr()))]
657 ast::Stmt::Item(item) => {
658 self.check_cfg(&item)?;
667 fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
668 let ast_id = self.expander.ast_id(&block);
670 BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
671 let block_id = self.db.intern_block(block_loc);
672 self.body.block_scopes.push(block_id);
674 let opt_def_map = self.db.block_def_map(block_id);
675 let has_def_map = opt_def_map.is_some();
676 let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone());
677 let module = if has_def_map { def_map.root() } else { self.expander.module };
678 let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
679 let prev_local_module = mem::replace(&mut self.expander.module, module);
682 block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
683 let tail = block.tail_expr().map(|e| self.collect_expr(e));
684 let syntax_node_ptr = AstPtr::new(&block.into());
685 let expr_id = self.alloc_expr(
686 Expr::Block { id: block_id, statements, tail, label: None },
690 self.expander.def_map = prev_def_map;
691 self.expander.module = prev_local_module;
695 fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
696 if let Some(block) = expr {
697 self.collect_block(block)
703 fn collect_label(&mut self, ast_label: ast::Label) -> LabelId {
705 name: ast_label.lifetime().as_ref().map_or_else(Name::missing, Name::new_lifetime),
707 self.alloc_label(label, AstPtr::new(&ast_label))
710 fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
711 let pattern = match &pat {
712 ast::Pat::IdentPat(bp) => {
713 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
715 BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
716 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
717 if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
718 // This could also be a single-segment path pattern. To
719 // decide that, we need to try resolving the name.
720 let (resolved, _) = self.expander.def_map.resolve_path(
722 self.expander.module,
723 &name.clone().into(),
724 BuiltinShadowMode::Other,
726 match resolved.take_values() {
727 Some(ModuleDefId::ConstId(_)) => Pat::Path(name.into()),
728 Some(ModuleDefId::EnumVariantId(_)) => {
729 // this is only really valid for unit variants, but
730 // shadowing other enum variants with a pattern is
732 Pat::Path(name.into())
734 Some(ModuleDefId::AdtId(AdtId::StructId(s)))
735 if self.db.struct_data(s).variant_data.kind() != StructKind::Record =>
737 // Funnily enough, record structs *can* be shadowed
738 // by pattern bindings (but unit or tuple structs
740 Pat::Path(name.into())
742 // shadowing statics is an error as well, so we just ignore that case here
743 _ => Pat::Bind { name, mode: annotation, subpat },
746 Pat::Bind { name, mode: annotation, subpat }
749 ast::Pat::TupleStructPat(p) => {
750 let path = p.path().and_then(|path| self.expander.parse_path(path));
751 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
752 Pat::TupleStruct { path, args, ellipsis }
754 ast::Pat::RefPat(p) => {
755 let pat = self.collect_pat_opt(p.pat());
756 let mutability = Mutability::from_mutable(p.mut_token().is_some());
757 Pat::Ref { pat, mutability }
759 ast::Pat::PathPat(p) => {
760 let path = p.path().and_then(|path| self.expander.parse_path(path));
761 path.map(Pat::Path).unwrap_or(Pat::Missing)
763 ast::Pat::OrPat(p) => {
764 let pats = p.pats().map(|p| self.collect_pat(p)).collect();
767 ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()),
768 ast::Pat::TuplePat(p) => {
769 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
770 Pat::Tuple { args, ellipsis }
772 ast::Pat::WildcardPat(_) => Pat::Wild,
773 ast::Pat::RecordPat(p) => {
774 let path = p.path().and_then(|path| self.expander.parse_path(path));
776 .record_pat_field_list()
777 .expect("every struct should have a field list")
780 let ast_pat = f.pat()?;
781 let pat = self.collect_pat(ast_pat);
782 let name = f.field_name()?.as_name();
783 Some(RecordFieldPat { name, pat })
788 .record_pat_field_list()
789 .expect("every struct should have a field list")
793 Pat::Record { path, args, ellipsis }
795 ast::Pat::SlicePat(p) => {
796 let SlicePatComponents { prefix, slice, suffix } = p.components();
798 // FIXME properly handle `RestPat`
800 prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(),
801 slice: slice.map(|p| self.collect_pat(p)),
802 suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(),
805 ast::Pat::LiteralPat(lit) => {
806 if let Some(ast_lit) = lit.literal() {
807 let expr = Expr::Literal(ast_lit.kind().into());
808 let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
809 let expr_id = self.alloc_expr(expr, expr_ptr);
815 ast::Pat::RestPat(_) => {
816 // `RestPat` requires special handling and should not be mapped
817 // to a Pat. Here we are using `Pat::Missing` as a fallback for
818 // when `RestPat` is mapped to `Pat`, which can easily happen
819 // when the source code being analyzed has a malformed pattern
820 // which includes `..` in a place where it isn't valid.
824 ast::Pat::BoxPat(boxpat) => {
825 let inner = self.collect_pat_opt(boxpat.pat());
828 ast::Pat::ConstBlockPat(const_block_pat) => {
829 if let Some(expr) = const_block_pat.block_expr() {
830 let expr_id = self.collect_block(expr);
831 Pat::ConstBlock(expr_id)
837 ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
839 let ptr = AstPtr::new(&pat);
840 self.alloc_pat(pattern, Either::Left(ptr))
843 fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
844 if let Some(pat) = pat {
845 self.collect_pat(pat)
851 fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) {
852 // Find the location of the `..`, if there is one. Note that we do not
853 // consider the possibility of there being multiple `..` here.
854 let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
855 // We want to skip the `..` pattern here, since we account for it above.
857 .filter(|p| !matches!(p, ast::Pat::RestPat(_)))
858 .map(|p| self.collect_pat(p))
864 /// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
866 fn check_cfg(&mut self, owner: &dyn ast::AttrsOwner) -> Option<()> {
867 match self.expander.parse_attrs(self.db, owner).cfg() {
869 if self.expander.cfg_options().check(&cfg) != Some(false) {
873 self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode(InactiveCode {
874 file: self.expander.current_file_id,
875 node: SyntaxNodePtr::new(owner.syntax()),
877 opts: self.expander.cfg_options().clone(),
887 impl From<ast::BinOp> for BinaryOp {
888 fn from(ast_op: ast::BinOp) -> Self {
890 ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
891 ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
892 ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
893 ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
894 ast::BinOp::LesserEqualTest => {
895 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
897 ast::BinOp::GreaterEqualTest => {
898 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
900 ast::BinOp::LesserTest => {
901 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
903 ast::BinOp::GreaterTest => {
904 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
906 ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
907 ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
908 ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
909 ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
910 ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
911 ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
912 ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
913 ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
914 ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
915 ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
916 ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
917 ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
918 ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
919 ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
920 ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
921 ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
922 ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
923 ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
924 ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
925 ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
926 ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
931 impl From<ast::LiteralKind> for Literal {
932 fn from(ast_lit_kind: ast::LiteralKind) -> Self {
934 LiteralKind::IntNumber(lit) => {
935 if let builtin @ Some(_) = lit.suffix().and_then(BuiltinFloat::from_suffix) {
936 return Literal::Float(Default::default(), builtin);
937 } else if let builtin @ Some(_) =
938 lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it))
940 Literal::Int(Default::default(), builtin)
942 let builtin = lit.suffix().and_then(|it| BuiltinUint::from_suffix(&it));
943 Literal::Uint(Default::default(), builtin)
946 LiteralKind::FloatNumber(lit) => {
947 let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it));
948 Literal::Float(Default::default(), ty)
950 LiteralKind::ByteString(_) => Literal::ByteString(Default::default()),
951 LiteralKind::String(_) => Literal::String(Default::default()),
952 LiteralKind::Byte => Literal::Uint(Default::default(), Some(BuiltinUint::U8)),
953 LiteralKind::Bool(val) => Literal::Bool(val),
954 LiteralKind::Char => Literal::Char(Default::default()),