rust-analyzer/crates/ra_hir_ty/src/expr.rs

152 lines
4.6 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-11-12 15:46:57 +00:00
use std::sync::Arc;
2019-01-05 15:32:07 +00:00
2019-11-27 14:46:02 +00:00
use hir_def::{
path::{known, Path},
resolver::HasResolver,
AdtId, FunctionId,
};
use hir_expand::{diagnostics::DiagnosticSink, name::Name};
2019-11-15 11:53:09 +00:00
use ra_syntax::ast;
2019-11-14 14:37:22 +00:00
use ra_syntax::AstPtr;
2019-11-15 11:53:09 +00:00
use rustc_hash::FxHashSet;
2019-01-05 15:32:07 +00:00
2019-11-15 11:53:09 +00:00
use crate::{
db::HirDatabase,
diagnostics::{MissingFields, MissingOkInTailExpr},
2019-11-27 14:46:02 +00:00
ApplicationTy, InferenceResult, Ty, TypeCtor,
2019-11-15 11:53:09 +00:00
};
2019-01-05 15:32:07 +00:00
2019-11-12 15:46:57 +00:00
pub use hir_def::{
2019-11-14 08:56:13 +00:00
body::{
scope::{ExprScopes, ScopeEntry, ScopeId},
Body, BodySourceMap, ExprPtr, ExprSource, PatPtr, PatSource,
},
2019-11-12 15:46:57 +00:00
expr::{
ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp,
MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp,
},
2019-11-12 12:09:25 +00:00
};
2019-09-03 05:56:36 +00:00
2019-11-27 14:46:02 +00:00
pub struct ExprValidator<'a, 'b: 'a> {
func: FunctionId,
2019-11-15 11:53:09 +00:00
infer: Arc<InferenceResult>,
sink: &'a mut DiagnosticSink<'b>,
}
impl<'a, 'b> ExprValidator<'a, 'b> {
2019-11-27 14:46:02 +00:00
pub fn new(
func: FunctionId,
2019-11-15 11:53:09 +00:00
infer: Arc<InferenceResult>,
sink: &'a mut DiagnosticSink<'b>,
) -> ExprValidator<'a, 'b> {
ExprValidator { func, infer, sink }
}
2019-11-27 14:46:02 +00:00
pub fn validate_body(&mut self, db: &impl HirDatabase) {
let body = db.body(self.func.into());
2019-11-15 11:53:09 +00:00
2019-11-24 15:48:29 +00:00
for e in body.exprs.iter() {
2019-11-15 11:53:09 +00:00
if let (id, Expr::RecordLit { path, fields, spread }) = e {
self.validate_record_literal(id, path, fields, *spread, db);
}
}
2019-11-24 15:48:29 +00:00
let body_expr = &body[body.body_expr];
2019-11-15 11:53:09 +00:00
if let Expr::Block { statements: _, tail: Some(t) } = body_expr {
2019-11-24 15:48:29 +00:00
self.validate_results_in_tail_expr(body.body_expr, *t, db);
2019-11-15 11:53:09 +00:00
}
}
fn validate_record_literal(
&mut self,
id: ExprId,
_path: &Option<Path>,
fields: &[RecordLitField],
spread: Option<ExprId>,
db: &impl HirDatabase,
) {
if spread.is_some() {
return;
}
let struct_def = match self.infer[id].as_adt() {
2019-11-27 14:46:02 +00:00
Some((AdtId::StructId(s), _)) => s,
2019-11-15 11:53:09 +00:00
_ => return,
};
2019-11-27 14:46:02 +00:00
let struct_data = db.struct_data(struct_def);
2019-11-15 11:53:09 +00:00
let lit_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
2019-11-27 14:46:02 +00:00
let missed_fields: Vec<Name> = struct_data
.variant_data
.fields()
2019-11-15 11:53:09 +00:00
.iter()
2019-11-27 14:46:02 +00:00
.filter_map(|(_f, d)| {
let name = d.name.clone();
2019-11-15 11:53:09 +00:00
if lit_fields.contains(&name) {
None
} else {
Some(name)
}
})
.collect();
if missed_fields.is_empty() {
return;
}
2019-11-27 14:46:02 +00:00
let (_, source_map) = db.body_with_source_map(self.func.into());
2019-11-15 11:53:09 +00:00
if let Some(source_ptr) = source_map.expr_syntax(id) {
if let Some(expr) = source_ptr.value.left() {
2019-11-15 11:53:09 +00:00
let root = source_ptr.file_syntax(db);
if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) {
if let Some(field_list) = record_lit.record_field_list() {
self.sink.push(MissingFields {
file: source_ptr.file_id,
field_list: AstPtr::new(&field_list),
missed_fields,
})
}
}
}
}
}
fn validate_results_in_tail_expr(
&mut self,
body_id: ExprId,
id: ExprId,
db: &impl HirDatabase,
) {
// the mismatch will be on the whole block currently
let mismatch = match self.infer.type_mismatch_for_expr(body_id) {
Some(m) => m,
None => return,
};
let std_result_path = known::std_result_result();
2019-11-27 14:46:02 +00:00
let resolver = self.func.resolver(db);
2019-11-15 11:53:09 +00:00
let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) {
Some(it) => it,
_ => return,
};
2019-11-26 11:29:12 +00:00
let std_result_ctor = TypeCtor::Adt(AdtId::EnumId(std_result_enum));
2019-11-15 11:53:09 +00:00
let params = match &mismatch.expected {
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters,
_ => return,
};
if params.len() == 2 && &params[0] == &mismatch.actual {
2019-11-27 14:46:02 +00:00
let (_, source_map) = db.body_with_source_map(self.func.into());
2019-11-15 11:53:09 +00:00
if let Some(source_ptr) = source_map.expr_syntax(id) {
if let Some(expr) = source_ptr.value.left() {
2019-11-15 11:53:09 +00:00
self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr });
}
}
}
}
}