2019-09-30 08:58:53 +00:00
|
|
|
//! 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-15 11:53:09 +00:00
|
|
|
use hir_def::path::known;
|
|
|
|
use hir_expand::diagnostics::DiagnosticSink;
|
|
|
|
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-20 18:55:33 +00:00
|
|
|
resolve::HasResolver,
|
2019-11-15 11:53:09 +00:00
|
|
|
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
|
|
|
|
Adt, DefWithBody, Function, HasBody, Name, Path, Resolver,
|
|
|
|
};
|
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-01-23 22:08:41 +00:00
|
|
|
// needs arbitrary_self_types to be a method... or maybe move to the def?
|
2019-04-13 08:02:23 +00:00
|
|
|
pub(crate) fn resolver_for_expr(
|
|
|
|
db: &impl HirDatabase,
|
2019-11-12 13:46:27 +00:00
|
|
|
owner: DefWithBody,
|
2019-04-13 08:02:23 +00:00
|
|
|
expr_id: ExprId,
|
|
|
|
) -> Resolver {
|
2019-11-14 14:37:22 +00:00
|
|
|
let scopes = owner.expr_scopes(db);
|
2019-11-12 13:46:27 +00:00
|
|
|
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
|
2019-01-27 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 08:02:23 +00:00
|
|
|
pub(crate) fn resolver_for_scope(
|
2019-01-27 19:50:57 +00:00
|
|
|
db: &impl HirDatabase,
|
2019-11-12 13:46:27 +00:00
|
|
|
owner: DefWithBody,
|
2019-11-14 08:56:13 +00:00
|
|
|
scope_id: Option<ScopeId>,
|
2019-01-27 16:23:49 +00:00
|
|
|
) -> Resolver {
|
2019-11-12 13:46:27 +00:00
|
|
|
let mut r = owner.resolver(db);
|
2019-11-14 14:37:22 +00:00
|
|
|
let scopes = owner.expr_scopes(db);
|
2019-04-13 07:49:01 +00:00
|
|
|
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
|
2019-01-23 22:08:41 +00:00
|
|
|
for scope in scope_chain.into_iter().rev() {
|
2019-11-15 09:00:36 +00:00
|
|
|
r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);
|
2019-01-23 22:08:41 +00:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
2019-11-15 11:53:09 +00:00
|
|
|
|
|
|
|
pub(crate) struct ExprValidator<'a, 'b: 'a> {
|
|
|
|
func: Function,
|
|
|
|
infer: Arc<InferenceResult>,
|
|
|
|
sink: &'a mut DiagnosticSink<'b>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> ExprValidator<'a, 'b> {
|
|
|
|
pub(crate) fn new(
|
|
|
|
func: Function,
|
|
|
|
infer: Arc<InferenceResult>,
|
|
|
|
sink: &'a mut DiagnosticSink<'b>,
|
|
|
|
) -> ExprValidator<'a, 'b> {
|
|
|
|
ExprValidator { func, infer, sink }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn validate_body(&mut self, db: &impl HirDatabase) {
|
|
|
|
let body = self.func.body(db);
|
|
|
|
|
|
|
|
for e in body.exprs() {
|
|
|
|
if let (id, Expr::RecordLit { path, fields, spread }) = e {
|
|
|
|
self.validate_record_literal(id, path, fields, *spread, db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let body_expr = &body[body.body_expr()];
|
|
|
|
if let Expr::Block { statements: _, tail: Some(t) } = body_expr {
|
|
|
|
self.validate_results_in_tail_expr(body.body_expr(), *t, db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
Some((Adt::Struct(s), _)) => s,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let lit_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
|
|
|
|
let missed_fields: Vec<Name> = struct_def
|
|
|
|
.fields(db)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|f| {
|
|
|
|
let name = f.name(db);
|
|
|
|
if lit_fields.contains(&name) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
if missed_fields.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let source_map = self.func.body_source_map(db);
|
|
|
|
|
|
|
|
if let Some(source_ptr) = source_map.expr_syntax(id) {
|
2019-11-20 06:40:36 +00:00
|
|
|
if let Some(expr) = source_ptr.value.a() {
|
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();
|
|
|
|
|
|
|
|
let resolver = self.func.resolver(db);
|
|
|
|
let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) {
|
|
|
|
Some(it) => it,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum));
|
|
|
|
let params = match &mismatch.expected {
|
|
|
|
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if params.len() == 2 && ¶ms[0] == &mismatch.actual {
|
|
|
|
let source_map = self.func.body_source_map(db);
|
|
|
|
|
|
|
|
if let Some(source_ptr) = source_map.expr_syntax(id) {
|
2019-11-20 06:40:36 +00:00
|
|
|
if let Some(expr) = source_ptr.value.a() {
|
2019-11-15 11:53:09 +00:00
|
|
|
self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|