9570: minor: Simplify Semantics::type_of_expr_with_coercion r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-07-11 12:44:31 +00:00 committed by GitHub
commit a70d9313af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 18 deletions

View file

@ -216,7 +216,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.type_of_expr(expr)
}
pub fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, Option<Type>)> {
/// Returns true in case a coercion happened.
pub fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, bool)> {
self.imp.type_of_expr_with_coercion(expr)
}
@ -224,7 +225,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.type_of_pat(pat)
}
pub fn type_of_pat_with_coercion(&self, expr: &ast::Pat) -> Option<(Type, Option<Type>)> {
pub fn type_of_pat_with_coercion(&self, expr: &ast::Pat) -> Option<Type> {
self.imp.type_of_pat_with_coercion(expr)
}
@ -568,7 +569,7 @@ impl<'db> SemanticsImpl<'db> {
self.analyze(expr.syntax()).type_of_expr(self.db, expr)
}
fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, Option<Type>)> {
fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, bool)> {
self.analyze(expr.syntax()).type_of_expr_with_coercion(self.db, expr)
}
@ -576,7 +577,7 @@ impl<'db> SemanticsImpl<'db> {
self.analyze(pat.syntax()).type_of_pat(self.db, pat)
}
fn type_of_pat_with_coercion(&self, pat: &ast::Pat) -> Option<(Type, Option<Type>)> {
fn type_of_pat_with_coercion(&self, pat: &ast::Pat) -> Option<Type> {
self.analyze(pat.syntax()).type_of_pat_with_coercion(self.db, pat)
}

View file

@ -21,7 +21,7 @@ use hir_def::{
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
use hir_ty::{
diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
InferenceResult, Interner, Substitution, Ty, TyExt, TyLoweringContext,
InferenceResult, Interner, Substitution, TyExt, TyLoweringContext,
};
use syntax::{
ast::{self, AstNode},
@ -126,15 +126,15 @@ impl SourceAnalyzer {
&self,
db: &dyn HirDatabase,
expr: &ast::Expr,
) -> Option<(Type, Option<Type>)> {
) -> Option<(Type, bool)> {
let expr_id = self.expr_id(db, expr)?;
let infer = self.infer.as_ref()?;
let coerced = infer
let (ty, coerced) = infer
.expr_adjustments
.get(&expr_id)
.and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target));
let mk_ty = |ty: &Ty| Type::new_with_resolver(db, &self.resolver, ty.clone());
mk_ty(&infer[expr_id]).map(|ty| (ty, coerced.and_then(mk_ty)))
.and_then(|adjusts| adjusts.last().map(|adjust| (&adjust.target, true)))
.unwrap_or_else(|| (&infer[expr_id], false));
Type::new_with_resolver(db, &self.resolver, ty.clone()).zip(Some(coerced))
}
pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option<Type> {
@ -147,15 +147,15 @@ impl SourceAnalyzer {
&self,
db: &dyn HirDatabase,
pat: &ast::Pat,
) -> Option<(Type, Option<Type>)> {
) -> Option<Type> {
let pat_id = self.pat_id(pat)?;
let infer = self.infer.as_ref()?;
let coerced = infer
let ty = infer
.pat_adjustments
.get(&pat_id)
.and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target));
let mk_ty = |ty: &Ty| Type::new_with_resolver(db, &self.resolver, ty.clone());
mk_ty(&infer[pat_id]).map(|ty| (ty, coerced.and_then(mk_ty)))
.and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target))
.unwrap_or_else(|| &infer[pat_id]);
Type::new_with_resolver(db, &self.resolver, ty.clone())
}
pub(crate) fn type_of_self(

View file

@ -55,8 +55,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
}
// Infer type
let (ty, coerced) = ctx.sema.type_of_expr_with_coercion(&expr)?;
let ty = coerced.unwrap_or(ty);
let (ty, _) = ctx.sema.type_of_expr_with_coercion(&expr)?;
if ty.contains_unknown() || ty.is_closure() {
cov_mark::hit!(add_explicit_type_not_applicable_if_ty_not_inferred);
return None;

View file

@ -190,7 +190,7 @@ pub(crate) fn inline_(
let ty = ctx
.sema
.type_of_expr_with_coercion(&expr)
.map_or(false, |(_, coerced)| coerced.is_some())
.map_or(false, |(_, coerced)| coerced)
.then(|| param_ty)
.flatten();
body.push_front(