add_explicit_type respects coercions

This commit is contained in:
Lukas Wirth 2021-07-10 18:19:46 +02:00
parent d5a43d1f85
commit 576e3a4e12
4 changed files with 43 additions and 2 deletions

View file

@ -122,6 +122,21 @@ impl SourceAnalyzer {
Type::new_with_resolver(db, &self.resolver, ty)
}
pub(crate) fn type_of_expr_with_coercion(
&self,
db: &dyn HirDatabase,
expr: &ast::Expr,
) -> Option<Type> {
let expr_id = self.expr_id(db, expr)?;
let infer = self.infer.as_ref()?;
let ty = infer
.expr_adjustments
.get(&expr_id)
.and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target))
.unwrap_or_else(|| &infer[expr_id]);
Type::new_with_resolver(db, &self.resolver, ty.clone())
}
pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option<Type> {
let pat_id = self.pat_id(pat)?;
let ty = self.infer.as_ref()?[pat_id].clone();