mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
parent
eb741e895f
commit
ebbcf9f458
5 changed files with 21 additions and 11 deletions
|
@ -203,7 +203,7 @@ impl ExprCollector<'_> {
|
||||||
self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr())
|
self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `None` if the expression is `#[cfg]`d out.
|
/// Returns `None` if and only if the expression is `#[cfg]`d out.
|
||||||
fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option<ExprId> {
|
fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option<ExprId> {
|
||||||
let syntax_ptr = AstPtr::new(&expr);
|
let syntax_ptr = AstPtr::new(&expr);
|
||||||
self.check_cfg(&expr)?;
|
self.check_cfg(&expr)?;
|
||||||
|
@ -665,7 +665,7 @@ impl ExprCollector<'_> {
|
||||||
if self.check_cfg(&stmt).is_none() {
|
if self.check_cfg(&stmt).is_none() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let has_semi = stmt.semicolon_token().is_some();
|
||||||
// Note that macro could be expended to multiple statements
|
// Note that macro could be expended to multiple statements
|
||||||
if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
|
if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
|
||||||
let macro_ptr = AstPtr::new(&m);
|
let macro_ptr = AstPtr::new(&m);
|
||||||
|
@ -682,18 +682,19 @@ impl ExprCollector<'_> {
|
||||||
statements.statements().for_each(|stmt| this.collect_stmt(stmt));
|
statements.statements().for_each(|stmt| this.collect_stmt(stmt));
|
||||||
if let Some(expr) = statements.expr() {
|
if let Some(expr) = statements.expr() {
|
||||||
let expr = this.collect_expr(expr);
|
let expr = this.collect_expr(expr);
|
||||||
this.statements_in_scope.push(Statement::Expr(expr));
|
this.statements_in_scope
|
||||||
|
.push(Statement::Expr { expr, has_semi });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone());
|
let expr = this.alloc_expr(Expr::Missing, syntax_ptr.clone());
|
||||||
this.statements_in_scope.push(Statement::Expr(expr));
|
this.statements_in_scope.push(Statement::Expr { expr, has_semi });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let expr = self.collect_expr_opt(stmt.expr());
|
let expr = self.collect_expr_opt(stmt.expr());
|
||||||
self.statements_in_scope.push(Statement::Expr(expr));
|
self.statements_in_scope.push(Statement::Expr { expr, has_semi });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::Stmt::Item(item) => {
|
ast::Stmt::Item(item) => {
|
||||||
|
@ -722,8 +723,17 @@ impl ExprCollector<'_> {
|
||||||
let prev_statements = std::mem::take(&mut self.statements_in_scope);
|
let prev_statements = std::mem::take(&mut self.statements_in_scope);
|
||||||
|
|
||||||
block.statements().for_each(|s| self.collect_stmt(s));
|
block.statements().for_each(|s| self.collect_stmt(s));
|
||||||
|
block.tail_expr().and_then(|e| {
|
||||||
|
let expr = self.maybe_collect_expr(e)?;
|
||||||
|
Some(self.statements_in_scope.push(Statement::Expr { expr, has_semi: false }))
|
||||||
|
});
|
||||||
|
|
||||||
let tail = block.tail_expr().map(|e| self.collect_expr(e));
|
let mut tail = None;
|
||||||
|
if let Some(Statement::Expr { expr, has_semi: false }) = self.statements_in_scope.last() {
|
||||||
|
tail = Some(*expr);
|
||||||
|
self.statements_in_scope.pop();
|
||||||
|
}
|
||||||
|
let tail = tail;
|
||||||
let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements);
|
let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements);
|
||||||
let syntax_node_ptr = AstPtr::new(&block.into());
|
let syntax_node_ptr = AstPtr::new(&block.into());
|
||||||
let expr_id = self.alloc_expr(
|
let expr_id = self.alloc_expr(
|
||||||
|
|
|
@ -157,7 +157,7 @@ fn compute_block_scopes(
|
||||||
scope = scopes.new_scope(scope);
|
scope = scopes.new_scope(scope);
|
||||||
scopes.add_bindings(body, scope, *pat);
|
scopes.add_bindings(body, scope, *pat);
|
||||||
}
|
}
|
||||||
Statement::Expr(expr) => {
|
Statement::Expr { expr, .. } => {
|
||||||
scopes.set_scope(*expr, scope);
|
scopes.set_scope(*expr, scope);
|
||||||
compute_expr_scopes(*expr, body, scopes, scope);
|
compute_expr_scopes(*expr, body, scopes, scope);
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,7 +242,7 @@ pub struct RecordLitField {
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> },
|
Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> },
|
||||||
Expr(ExprId),
|
Expr { expr: ExprId, has_semi: bool },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Expr {
|
impl Expr {
|
||||||
|
@ -265,7 +265,7 @@ impl Expr {
|
||||||
f(*expr);
|
f(*expr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Statement::Expr(e) => f(*e),
|
Statement::Expr { expr: expression, .. } => f(*expression),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(expr) = tail {
|
if let Some(expr) = tail {
|
||||||
|
|
|
@ -83,7 +83,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||||
if let Expr::Block { statements, tail, .. } = body_expr {
|
if let Expr::Block { statements, tail, .. } = body_expr {
|
||||||
if let Some(t) = tail {
|
if let Some(t) = tail {
|
||||||
self.validate_results_in_tail_expr(body.body_expr, *t, db);
|
self.validate_results_in_tail_expr(body.body_expr, *t, db);
|
||||||
} else if let Some(Statement::Expr(id)) = statements.last() {
|
} else if let Some(Statement::Expr { expr: id, .. }) = statements.last() {
|
||||||
self.validate_missing_tail_expr(body.body_expr, *id, db);
|
self.validate_missing_tail_expr(body.body_expr, *id, db);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -809,7 +809,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
let ty = self.resolve_ty_as_possible(ty);
|
let ty = self.resolve_ty_as_possible(ty);
|
||||||
self.infer_pat(*pat, &ty, BindingMode::default());
|
self.infer_pat(*pat, &ty, BindingMode::default());
|
||||||
}
|
}
|
||||||
Statement::Expr(expr) => {
|
Statement::Expr { expr, .. } => {
|
||||||
self.infer_expr(*expr, &Expectation::none());
|
self.infer_expr(*expr, &Expectation::none());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue