Skip to content

Commit a70d931

Browse files
bors[bot]Veykril
andauthored
Merge #9570
9570: minor: Simplify Semantics::type_of_expr_with_coercion r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 0fbeacc + c6b6f18 commit a70d931

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

crates/hir/src/semantics.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,16 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
216216
self.imp.type_of_expr(expr)
217217
}
218218

219-
pub fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, Option<Type>)> {
219+
/// Returns true in case a coercion happened.
220+
pub fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, bool)> {
220221
self.imp.type_of_expr_with_coercion(expr)
221222
}
222223

223224
pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
224225
self.imp.type_of_pat(pat)
225226
}
226227

227-
pub fn type_of_pat_with_coercion(&self, expr: &ast::Pat) -> Option<(Type, Option<Type>)> {
228+
pub fn type_of_pat_with_coercion(&self, expr: &ast::Pat) -> Option<Type> {
228229
self.imp.type_of_pat_with_coercion(expr)
229230
}
230231

@@ -568,15 +569,15 @@ impl<'db> SemanticsImpl<'db> {
568569
self.analyze(expr.syntax()).type_of_expr(self.db, expr)
569570
}
570571

571-
fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, Option<Type>)> {
572+
fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, bool)> {
572573
self.analyze(expr.syntax()).type_of_expr_with_coercion(self.db, expr)
573574
}
574575

575576
fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
576577
self.analyze(pat.syntax()).type_of_pat(self.db, pat)
577578
}
578579

579-
fn type_of_pat_with_coercion(&self, pat: &ast::Pat) -> Option<(Type, Option<Type>)> {
580+
fn type_of_pat_with_coercion(&self, pat: &ast::Pat) -> Option<Type> {
580581
self.analyze(pat.syntax()).type_of_pat_with_coercion(self.db, pat)
581582
}
582583

crates/hir/src/source_analyzer.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use hir_def::{
2121
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
2222
use hir_ty::{
2323
diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
24-
InferenceResult, Interner, Substitution, Ty, TyExt, TyLoweringContext,
24+
InferenceResult, Interner, Substitution, TyExt, TyLoweringContext,
2525
};
2626
use syntax::{
2727
ast::{self, AstNode},
@@ -126,15 +126,15 @@ impl SourceAnalyzer {
126126
&self,
127127
db: &dyn HirDatabase,
128128
expr: &ast::Expr,
129-
) -> Option<(Type, Option<Type>)> {
129+
) -> Option<(Type, bool)> {
130130
let expr_id = self.expr_id(db, expr)?;
131131
let infer = self.infer.as_ref()?;
132-
let coerced = infer
132+
let (ty, coerced) = infer
133133
.expr_adjustments
134134
.get(&expr_id)
135-
.and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target));
136-
let mk_ty = |ty: &Ty| Type::new_with_resolver(db, &self.resolver, ty.clone());
137-
mk_ty(&infer[expr_id]).map(|ty| (ty, coerced.and_then(mk_ty)))
135+
.and_then(|adjusts| adjusts.last().map(|adjust| (&adjust.target, true)))
136+
.unwrap_or_else(|| (&infer[expr_id], false));
137+
Type::new_with_resolver(db, &self.resolver, ty.clone()).zip(Some(coerced))
138138
}
139139

140140
pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option<Type> {
@@ -147,15 +147,15 @@ impl SourceAnalyzer {
147147
&self,
148148
db: &dyn HirDatabase,
149149
pat: &ast::Pat,
150-
) -> Option<(Type, Option<Type>)> {
150+
) -> Option<Type> {
151151
let pat_id = self.pat_id(pat)?;
152152
let infer = self.infer.as_ref()?;
153-
let coerced = infer
153+
let ty = infer
154154
.pat_adjustments
155155
.get(&pat_id)
156-
.and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target));
157-
let mk_ty = |ty: &Ty| Type::new_with_resolver(db, &self.resolver, ty.clone());
158-
mk_ty(&infer[pat_id]).map(|ty| (ty, coerced.and_then(mk_ty)))
156+
.and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target))
157+
.unwrap_or_else(|| &infer[pat_id]);
158+
Type::new_with_resolver(db, &self.resolver, ty.clone())
159159
}
160160

161161
pub(crate) fn type_of_self(

crates/ide_assists/src/handlers/add_explicit_type.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
5555
}
5656

5757
// Infer type
58-
let (ty, coerced) = ctx.sema.type_of_expr_with_coercion(&expr)?;
59-
let ty = coerced.unwrap_or(ty);
58+
let (ty, _) = ctx.sema.type_of_expr_with_coercion(&expr)?;
6059
if ty.contains_unknown() || ty.is_closure() {
6160
cov_mark::hit!(add_explicit_type_not_applicable_if_ty_not_inferred);
6261
return None;

crates/ide_assists/src/handlers/inline_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub(crate) fn inline_(
190190
let ty = ctx
191191
.sema
192192
.type_of_expr_with_coercion(&expr)
193-
.map_or(false, |(_, coerced)| coerced.is_some())
193+
.map_or(false, |(_, coerced)| coerced)
194194
.then(|| param_ty)
195195
.flatten();
196196
body.push_front(

0 commit comments

Comments
 (0)