minor: Remove frequent Arc<Body> clones in type checking

This commit is contained in:
Lukas Wirth 2022-04-15 21:44:47 +02:00
parent f82d230081
commit e5bf60fee2
3 changed files with 29 additions and 25 deletions

View file

@ -59,7 +59,8 @@ mod closure;
pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
let _p = profile::span("infer_query");
let resolver = def.resolver(db.upcast());
let mut ctx = InferenceContext::new(db, def, resolver);
let body = db.body(def);
let mut ctx = InferenceContext::new(db, def, &body, resolver);
match def {
DefWithBodyId::ConstId(c) => ctx.collect_const(&db.const_data(c)),
@ -360,7 +361,7 @@ impl Index<PatId> for InferenceResult {
pub(crate) struct InferenceContext<'a> {
pub(crate) db: &'a dyn HirDatabase,
pub(crate) owner: DefWithBodyId,
pub(crate) body: Arc<Body>,
pub(crate) body: &'a Body,
pub(crate) resolver: Resolver,
table: unify::InferenceTable<'a>,
trait_env: Arc<TraitEnvironment>,
@ -394,7 +395,12 @@ fn find_breakable<'c>(
}
impl<'a> InferenceContext<'a> {
fn new(db: &'a dyn HirDatabase, owner: DefWithBodyId, resolver: Resolver) -> Self {
fn new(
db: &'a dyn HirDatabase,
owner: DefWithBodyId,
body: &'a Body,
resolver: Resolver,
) -> Self {
let krate = owner.module(db.upcast()).krate();
let trait_env = owner
.as_generic_def_id()
@ -406,7 +412,7 @@ impl<'a> InferenceContext<'a> {
return_ty: TyKind::Error.intern(Interner), // set in collect_fn_signature
db,
owner,
body: db.body(owner),
body,
resolver,
diverges: Diverges::Maybe,
breakables: Vec::new(),
@ -452,12 +458,11 @@ impl<'a> InferenceContext<'a> {
}
fn collect_fn(&mut self, data: &FunctionData) {
let body = Arc::clone(&self.body); // avoid borrow checker problem
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
.with_impl_trait_mode(ImplTraitLoweringMode::Param);
let param_tys =
data.params.iter().map(|(_, type_ref)| ctx.lower_ty(type_ref)).collect::<Vec<_>>();
for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) {
for (ty, pat) in param_tys.into_iter().zip(self.body.params.iter()) {
let ty = self.insert_type_vars(ty);
let ty = self.normalize_associated_types_in(ty);

View file

@ -4,7 +4,6 @@ use std::{
collections::hash_map::Entry,
iter::{repeat, repeat_with},
mem,
sync::Arc,
};
use chalk_ir::{
@ -80,8 +79,7 @@ impl<'a> InferenceContext<'a> {
fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
self.db.unwind_if_cancelled();
let body = Arc::clone(&self.body); // avoid borrow checker problem
let ty = match &body[tgt_expr] {
let ty = match &self.body[tgt_expr] {
Expr::Missing => self.err_ty(),
&Expr::If { condition, then_branch, else_branch } => {
self.infer_expr(
@ -560,17 +558,7 @@ impl<'a> InferenceContext<'a> {
}
.intern(Interner)
}
Expr::Box { expr } => {
let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
if let Some(box_) = self.resolve_boxed_box() {
TyBuilder::adt(self.db, box_)
.push(inner_ty)
.fill_with_defaults(self.db, || self.table.new_type_var())
.build()
} else {
self.err_ty()
}
}
&Expr::Box { expr } => self.infer_expr_box(expr),
Expr::UnaryOp { expr, op } => {
let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
let inner_ty = self.resolve_ty_shallow(&inner_ty);
@ -798,6 +786,18 @@ impl<'a> InferenceContext<'a> {
ty
}
fn infer_expr_box(&mut self, inner_expr: ExprId) -> chalk_ir::Ty<Interner> {
let inner_ty = self.infer_expr_inner(inner_expr, &Expectation::none());
if let Some(box_) = self.resolve_boxed_box() {
TyBuilder::adt(self.db, box_)
.push(inner_ty)
.fill_with_defaults(self.db, || self.table.new_type_var())
.build()
} else {
self.err_ty()
}
}
fn infer_overloadable_binop(
&mut self,
lhs: ExprId,

View file

@ -1,6 +1,6 @@
//! Type inference for patterns.
use std::{iter::repeat, sync::Arc};
use std::iter::repeat;
use chalk_ir::Mutability;
use hir_def::{
@ -100,10 +100,9 @@ impl<'a> InferenceContext<'a> {
expected: &Ty,
mut default_bm: BindingMode,
) -> Ty {
let body = Arc::clone(&self.body); // avoid borrow checker problem
let mut expected = self.resolve_ty_shallow(expected);
if is_non_ref_pat(&body, pat) {
if is_non_ref_pat(&self.body, pat) {
let mut pat_adjustments = Vec::new();
while let Some((inner, _lifetime, mutability)) = expected.as_reference() {
pat_adjustments.push(Adjustment {
@ -122,7 +121,7 @@ impl<'a> InferenceContext<'a> {
pat_adjustments.shrink_to_fit();
self.result.pat_adjustments.insert(pat, pat_adjustments);
}
} else if let Pat::Ref { .. } = &body[pat] {
} else if let Pat::Ref { .. } = &self.body[pat] {
cov_mark::hit!(match_ergonomics_ref);
// When you encounter a `&pat` pattern, reset to Move.
// This is so that `w` is by value: `let (_, &w) = &(1, &2);`
@ -133,7 +132,7 @@ impl<'a> InferenceContext<'a> {
let default_bm = default_bm;
let expected = expected;
let ty = match &body[pat] {
let ty = match &self.body[pat] {
Pat::Tuple { args, ellipsis } => {
let expectations = match expected.as_tuple() {
Some(parameters) => &*parameters.as_slice(Interner),