Skip to content

Commit f30fc0a

Browse files
committed
Auto merge of rust-lang#104844 - cjgillot:mention-eval-place, r=jackh726,RalfJung
Evaluate place expression in `PlaceMention` rust-lang#102256 introduces a `PlaceMention(place)` MIR statement which keep trace of `let _ = place` statements from surface rust, but without semantics. This PR proposes to change the behaviour of `let _ =` patterns with respect to the borrow-checker to verify that the bound place is live. Specifically, consider this code: ```rust let _ = { let a = 5; &a }; ``` This passes borrowck without error on stable. Meanwhile, replacing `_` by `_: _` or `_p` errors with "error[E0597]: `a` does not live long enough", [see playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c448d25a7c205dc95a0967fe96bccce8). This PR *does not* change how `_` patterns behave with respect to initializedness: it remains ok to bind a moved-from place to `_`. The relevant test is `tests/ui/borrowck/let_underscore_temporary.rs`. Crater check found no regression. For consistency, this PR changes miri to evaluate the place found in `PlaceMention`, and report eventual dangling pointers found within it. r? `@RalfJung`
2 parents 86d8f12 + cb3e0fb commit f30fc0a

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

tests/ui/option_if_let_else.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
2525
fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
2626
let _ = string.map_or(0, |s| s.len());
2727
let _ = num.as_ref().map_or(&0, |s| s);
28-
let _ = num.as_mut().map_or(&mut 0, |s| {
28+
let _ = num.as_mut().map_or(&0, |s| {
2929
*s += 1;
3030
s
3131
});
@@ -34,7 +34,7 @@ fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
3434
s += 1;
3535
s
3636
});
37-
let _ = num.as_mut().map_or(&mut 0, |s| {
37+
let _ = num.as_mut().map_or(&0, |s| {
3838
*s += 1;
3939
s
4040
});

tests/ui/option_if_let_else.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
3333
*s += 1;
3434
s
3535
} else {
36-
&mut 0
36+
&0
3737
};
3838
let _ = if let Some(ref s) = num { s } else { &0 };
3939
let _ = if let Some(mut s) = num {
@@ -46,7 +46,7 @@ fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
4646
*s += 1;
4747
s
4848
} else {
49-
&mut 0
49+
&0
5050
};
5151
}
5252

tests/ui/option_if_let_else.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ LL | let _ = if let Some(s) = &mut num {
3030
LL | | *s += 1;
3131
LL | | s
3232
LL | | } else {
33-
LL | | &mut 0
33+
LL | | &0
3434
LL | | };
3535
| |_____^
3636
|
3737
help: try
3838
|
39-
LL ~ let _ = num.as_mut().map_or(&mut 0, |s| {
39+
LL ~ let _ = num.as_mut().map_or(&0, |s| {
4040
LL + *s += 1;
4141
LL + s
4242
LL ~ });
@@ -76,13 +76,13 @@ LL | let _ = if let Some(ref mut s) = num {
7676
LL | | *s += 1;
7777
LL | | s
7878
LL | | } else {
79-
LL | | &mut 0
79+
LL | | &0
8080
LL | | };
8181
| |_____^
8282
|
8383
help: try
8484
|
85-
LL ~ let _ = num.as_mut().map_or(&mut 0, |s| {
85+
LL ~ let _ = num.as_mut().map_or(&0, |s| {
8686
LL + *s += 1;
8787
LL + s
8888
LL ~ });

0 commit comments

Comments
 (0)