Fix inference with conditionally compiled tails

Fixes #8378
This commit is contained in:
Daniel McNab 2021-04-07 12:45:17 +01:00
parent eb741e895f
commit ebbcf9f458
5 changed files with 21 additions and 11 deletions

View File

@ -203,7 +203,7 @@ impl ExprCollector<'_> {
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> {
let syntax_ptr = AstPtr::new(&expr);
self.check_cfg(&expr)?;
@ -665,7 +665,7 @@ impl ExprCollector<'_> {
if self.check_cfg(&stmt).is_none() {
return;
}
let has_semi = stmt.semicolon_token().is_some();
// Note that macro could be expended to multiple statements
if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
let macro_ptr = AstPtr::new(&m);
@ -682,18 +682,19 @@ impl ExprCollector<'_> {
statements.statements().for_each(|stmt| this.collect_stmt(stmt));
if let Some(expr) = statements.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 => {
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 {
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) => {
@ -722,8 +723,17 @@ impl ExprCollector<'_> {
let prev_statements = std::mem::take(&mut self.statements_in_scope);
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 syntax_node_ptr = AstPtr::new(&block.into());
let expr_id = self.alloc_expr(

View File

@ -157,7 +157,7 @@ fn compute_block_scopes(
scope = scopes.new_scope(scope);
scopes.add_bindings(body, scope, *pat);
}
Statement::Expr(expr) => {
Statement::Expr { expr, .. } => {
scopes.set_scope(*expr, scope);
compute_expr_scopes(*expr, body, scopes, scope);
}

View File

@ -242,7 +242,7 @@ pub struct RecordLitField {
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Statement {
Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> },
Expr(ExprId),
Expr { expr: ExprId, has_semi: bool },
}
impl Expr {
@ -265,7 +265,7 @@ impl Expr {
f(*expr);
}
}
Statement::Expr(e) => f(*e),
Statement::Expr { expr: expression, .. } => f(*expression),
}
}
if let Some(expr) = tail {

View File

@ -83,7 +83,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
if let Expr::Block { statements, tail, .. } = body_expr {
if let Some(t) = tail {
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);
}
}

View File

@ -809,7 +809,7 @@ impl<'a> InferenceContext<'a> {
let ty = self.resolve_ty_as_possible(ty);
self.infer_pat(*pat, &ty, BindingMode::default());
}
Statement::Expr(expr) => {
Statement::Expr { expr, .. } => {
self.infer_expr(*expr, &Expectation::none());
}
}