Add profiling to mir lower and borrowck query

This commit is contained in:
hkalbasi 2023-03-07 12:42:52 +03:30
parent 2cce9dc3e9
commit bcd7ecb242
4 changed files with 33 additions and 21 deletions

View file

@ -33,6 +33,7 @@ pub fn borrowck_query(
db: &dyn HirDatabase, db: &dyn HirDatabase,
def: DefWithBodyId, def: DefWithBodyId,
) -> Result<Arc<BorrowckResult>, MirLowerError> { ) -> Result<Arc<BorrowckResult>, MirLowerError> {
let _p = profile::span("borrowck_query");
let body = db.mir_body(def)?; let body = db.mir_body(def)?;
let r = BorrowckResult { mutability_of_locals: mutability_of_locals(&body), mir_body: body }; let r = BorrowckResult { mutability_of_locals: mutability_of_locals(&body), mir_body: body };
Ok(Arc::new(r)) Ok(Arc::new(r))

View file

@ -15,7 +15,7 @@ use hir_def::{
resolver::{resolver_for_expr, ResolveValueResult, ValueNs}, resolver::{resolver_for_expr, ResolveValueResult, ValueNs},
DefWithBodyId, EnumVariantId, HasModule, DefWithBodyId, EnumVariantId, HasModule,
}; };
use hir_expand::name; use hir_expand::name::Name;
use la_arena::ArenaMap; use la_arena::ArenaMap;
use crate::{ use crate::{
@ -1453,6 +1453,16 @@ fn cast_kind(source_ty: &Ty, target_ty: &Ty) -> Result<CastKind> {
} }
pub fn mir_body_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Result<Arc<MirBody>> { pub fn mir_body_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Result<Arc<MirBody>> {
let _p = profile::span("mir_body_query").detail(|| match def {
DefWithBodyId::FunctionId(it) => db.function_data(it).name.to_string(),
DefWithBodyId::StaticId(it) => db.static_data(it).name.clone().to_string(),
DefWithBodyId::ConstId(it) => {
db.const_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
}
DefWithBodyId::VariantId(it) => {
db.enum_data(it.parent).variants[it.local_id].name.to_string()
}
});
let body = db.body(def); let body = db.body(def);
let infer = db.infer(def); let infer = db.infer(def);
let result = lower_to_mir(db, def, &body, &infer, body.body_expr)?; let result = lower_to_mir(db, def, &body, &infer, body.body_expr)?;

View file

@ -1,6 +1,7 @@
//! MIR lowering for places //! MIR lowering for places
use super::*; use super::*;
use hir_expand::name;
macro_rules! not_supported { macro_rules! not_supported {
($x: expr) => { ($x: expr) => {

View file

@ -22,8 +22,8 @@ pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Diagno
} }
let edit = edit_builder.finish(); let edit = edit_builder.finish();
Some(vec![fix( Some(vec![fix(
"remove_mut", "add_mut",
"Remove unnecessary `mut`", "Change it to be mutable",
SourceChange::from_text_edit(file_id, edit), SourceChange::from_text_edit(file_id, edit),
use_range, use_range,
)]) )])
@ -66,7 +66,7 @@ pub(crate) fn unused_mut(ctx: &DiagnosticsContext<'_>, d: &hir::UnusedMut) -> Di
let ast = d.local.primary_source(ctx.sema.db).syntax_ptr(); let ast = d.local.primary_source(ctx.sema.db).syntax_ptr();
Diagnostic::new( Diagnostic::new(
"unused-mut", "unused-mut",
"remove this `mut`", "variable does not need to be mutable",
ctx.sema.diagnostics_display_range(ast).range, ctx.sema.diagnostics_display_range(ast).range,
) )
.severity(Severity::WeakWarning) .severity(Severity::WeakWarning)
@ -89,7 +89,7 @@ mod tests {
fn f(_: i32) {} fn f(_: i32) {}
fn main() { fn main() {
let mut x = 2; let mut x = 2;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
f(x); f(x);
} }
"#, "#,
@ -264,7 +264,7 @@ fn main() {
fn f(_: i32) {} fn f(_: i32) {}
fn main() { fn main() {
let mut x = (2, 7); let mut x = (2, 7);
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
f(x.1); f(x.1);
} }
"#, "#,
@ -298,7 +298,7 @@ fn main() {
r#" r#"
fn main() { fn main() {
let mut x = &mut 2; let mut x = &mut 2;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
*x = 5; *x = 5;
} }
"#, "#,
@ -343,7 +343,7 @@ fn main() {
fn main() { fn main() {
match (2, 3) { match (2, 3) {
(x, mut y) => { (x, mut y) => {
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
x = 7; x = 7;
//^^^^^ 💡 error: cannot mutate immutable variable `x` //^^^^^ 💡 error: cannot mutate immutable variable `x`
} }
@ -364,7 +364,7 @@ fn main() {
fn main() { fn main() {
return; return;
let mut x = 2; let mut x = 2;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
&mut x; &mut x;
} }
"#, "#,
@ -374,7 +374,7 @@ fn main() {
fn main() { fn main() {
loop {} loop {}
let mut x = 2; let mut x = 2;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
&mut x; &mut x;
} }
"#, "#,
@ -395,7 +395,7 @@ fn main(b: bool) {
g(); g();
} }
let mut x = 2; let mut x = 2;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
&mut x; &mut x;
} }
"#, "#,
@ -409,7 +409,7 @@ fn main(b: bool) {
return; return;
} }
let mut x = 2; let mut x = 2;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
&mut x; &mut x;
} }
"#, "#,
@ -423,7 +423,7 @@ fn main(b: bool) {
fn f(_: i32) {} fn f(_: i32) {}
fn main() { fn main() {
let mut x; let mut x;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
x = 5; x = 5;
f(x); f(x);
} }
@ -434,7 +434,7 @@ fn main() {
fn f(_: i32) {} fn f(_: i32) {}
fn main(b: bool) { fn main(b: bool) {
let mut x; let mut x;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
if b { if b {
x = 1; x = 1;
} else { } else {
@ -477,15 +477,15 @@ fn f(_: i32) {}
fn main() { fn main() {
loop { loop {
let mut x = 1; let mut x = 1;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
f(x); f(x);
if let mut y = 2 { if let mut y = 2 {
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
f(y); f(y);
} }
match 3 { match 3 {
mut z => f(z), mut z => f(z),
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
} }
} }
} }
@ -498,7 +498,7 @@ fn main() {
check_diagnostics( check_diagnostics(
r#" r#"
fn f(mut x: i32) { fn f(mut x: i32) {
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
} }
"#, "#,
); );
@ -519,7 +519,7 @@ fn f(x: i32) {
//- minicore: iterators //- minicore: iterators
fn f(x: [(i32, u8); 10]) { fn f(x: [(i32, u8); 10]) {
for (a, mut b) in x { for (a, mut b) in x {
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
a = 2; a = 2;
//^^^^^ 💡 error: cannot mutate immutable variable `a` //^^^^^ 💡 error: cannot mutate immutable variable `a`
} }
@ -567,7 +567,7 @@ fn f() {
fn f(_: i32) {} fn f(_: i32) {}
fn main() { fn main() {
let ((Some(mut x), None) | (_, Some(mut x))) = (None, Some(7)); let ((Some(mut x), None) | (_, Some(mut x))) = (None, Some(7));
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
f(x); f(x);
} }
"#, "#,
@ -583,7 +583,7 @@ fn f(_: i32) {}
fn main() { fn main() {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut x = 2; let mut x = 2;
//^^^^^ 💡 weak: remove this `mut` //^^^^^ 💡 weak: variable does not need to be mutable
f(x); f(x);
} }
"#, "#,