Skip to content

Commit

Permalink
Auto merge of #14316 - HKalbasi:master, r=HKalbasi
Browse files Browse the repository at this point in the history
Fix stack overflow when derefrencing `&!`

fix #14310
  • Loading branch information
bors committed Mar 10, 2023
2 parents 070f8f8 + a980b56 commit 9fca0a4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
16 changes: 16 additions & 0 deletions crates/hir-ty/src/consteval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ fn reference_autoderef() {
"#,
3,
);
check_number(
r#"
struct Foo<T> { x: T }
impl<T> Foo<T> {
fn foo(&mut self) -> T { self.x }
}
fn f(i: &mut &mut Foo<Foo<i32>>) -> i32 {
((**i).x).foo()
}
fn g(i: Foo<Foo<i32>>) -> i32 {
i.x.foo()
}
const GOAL: i32 = f(&mut &mut Foo { x: Foo { x: 3 } }) + g(Foo { x: Foo { x: 5 } });
"#,
8,
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl MirLowerCtx<'_> {
}
Expr::Box { .. } => not_supported!("box expression"),
Expr::Field { .. } | Expr::Index { .. } | Expr::UnaryOp { op: hir_def::expr::UnaryOp::Deref, .. } => {
let Some((p, current)) = self.lower_expr_as_place(current, expr_id, true)? else {
let Some((p, current)) = self.lower_expr_as_place_without_adjust(current, expr_id, true)? else {
return Ok(None);
};
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/lower/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl MirLowerCtx<'_> {
}
}

fn lower_expr_as_place_without_adjust(
pub(super) fn lower_expr_as_place_without_adjust(
&mut self,
current: BasicBlockId,
expr_id: ExprId,
Expand Down
12 changes: 12 additions & 0 deletions crates/ide-diagnostics/src/handlers/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,18 @@ fn main() {
);
}

#[test]
fn regression_14310() {
check_diagnostics(
r#"
fn clone(mut i: &!) -> ! {
//^^^^^ 💡 weak: variable does not need to be mutable
*i
}
"#,
);
}

#[test]
fn match_bindings() {
check_diagnostics(
Expand Down

0 comments on commit 9fca0a4

Please sign in to comment.