Skip to content

Commit

Permalink
Auto merge of #15860 - HKalbasi:fix-capture-raw-pointer, r=HKalbasi
Browse files Browse the repository at this point in the history
Truncate closure capture place for raw pointer

fix #15670 (comment)
  • Loading branch information
bors committed Nov 9, 2023
2 parents ebb9ed9 + 3bcdb7d commit 7663319
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/hir-ty/src/chalk_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub trait TyExt {
fn is_unknown(&self) -> bool;
fn contains_unknown(&self) -> bool;
fn is_ty_var(&self) -> bool;
fn is_union(&self) -> bool;

fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>;
fn as_builtin(&self) -> Option<BuiltinType>;
Expand Down Expand Up @@ -96,6 +97,10 @@ impl TyExt for Ty {
matches!(self.kind(Interner), TyKind::InferenceVar(_, _))
}

fn is_union(&self) -> bool {
matches!(self.adt_id(Interner), Some(AdtId(hir_def::AdtId::UnionId(_))))
}

fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> {
match self.kind(Interner) {
TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),
Expand Down
27 changes: 27 additions & 0 deletions crates/hir-ty/src/infer/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,32 @@ impl InferenceContext<'_> {
self.walk_expr(expr);
}

fn restrict_precision_for_unsafe(&mut self) {
for capture in &mut self.current_captures {
let mut ty = self.table.resolve_completely(self.result[capture.place.local].clone());
if ty.as_raw_ptr().is_some() || ty.is_union() {
capture.kind = CaptureKind::ByRef(BorrowKind::Shared);
capture.place.projections.truncate(0);
continue;
}
for (i, p) in capture.place.projections.iter().enumerate() {
ty = p.projected_ty(
ty,
self.db,
|_, _, _| {
unreachable!("Closure field only happens in MIR");
},
self.owner.module(self.db.upcast()).krate(),
);
if ty.as_raw_ptr().is_some() || ty.is_union() {
capture.kind = CaptureKind::ByRef(BorrowKind::Shared);
capture.place.projections.truncate(i + 1);
break;
}
}
}
}

fn adjust_for_move_closure(&mut self) {
for capture in &mut self.current_captures {
if let Some(first_deref) =
Expand Down Expand Up @@ -924,6 +950,7 @@ impl InferenceContext<'_> {
self.result.mutated_bindings_in_closure.insert(item.place.local);
}
}
self.restrict_precision_for_unsafe();
// closure_kind should be done before adjust_for_move_closure
let closure_kind = self.closure_kind();
match capture_by {
Expand Down
16 changes: 16 additions & 0 deletions crates/ide-diagnostics/src/handlers/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,22 @@ fn foo(mut foo: Foo) {
};
call_me();
}
"#,
);
}

#[test]
fn regression_15670() {
check_diagnostics(
r#"
//- minicore: fn
pub struct A {}
pub unsafe fn foo(a: *mut A) {
let mut b = || -> *mut A { &mut *a };
//^^^^^ 💡 warn: variable does not need to be mutable
let _ = b();
}
"#,
);
}
Expand Down

0 comments on commit 7663319

Please sign in to comment.