Skip to content

internal: add_explicit_type respects coercions #9552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ 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> {
self.imp.type_of_expr_with_coercion(expr)
}

pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
self.imp.type_of_pat(pat)
}
Expand Down Expand Up @@ -560,6 +564,10 @@ 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> {
self.analyze(expr.syntax()).type_of_expr_with_coercion(self.db, expr)
}

fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
self.analyze(pat.syntax()).type_of_pat(self.db, pat)
}
Expand Down
15 changes: 15 additions & 0 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions crates/hir_ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ impl<'a> InferenceContext<'a> {
for (_, subst) in result.method_resolutions.values_mut() {
*subst = self.table.resolve_completely(subst.clone());
}
for adjustment in result.expr_adjustments.values_mut().flatten() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid the clones here and above using mem::replace, do you think it's worth it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, cloning Chalk types is cheap.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I suppose we'd still have to intern (or at least clonel the error type.

adjustment.target = self.table.resolve_completely(adjustment.target.clone());
}
for adjustment in result.pat_adjustments.values_mut().flatten() {
adjustment.target = self.table.resolve_completely(adjustment.target.clone());
}
result
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hir_ty/src/tests/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn test2() {

#[test]
fn let_stmt_coerce() {
check_no_mismatches(
check(
r"
//- minicore: coerce_unsized
fn test() {
Expand Down
20 changes: 19 additions & 1 deletion crates/ide_assists/src/handlers/add_explicit_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
}

// Infer type
let ty = ctx.sema.type_of_expr(&expr)?;
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;
Expand Down Expand Up @@ -258,6 +258,24 @@ fn main() {
fn main() {
let test @ (): () = ();
}
"#,
);
}

#[test]
fn add_explicit_type_inserts_coercions() {
check_assist(
add_explicit_type,
r#"
//- minicore: coerce_unsized
fn f() {
let $0x: *const [_] = &[3];
}
"#,
r#"
fn f() {
let x: *const [i32] = &[3];
}
"#,
);
}
Expand Down