Skip to content

Commit d9d9663

Browse files
authored
Rollup merge of rust-lang#54702 - RalfJung:fn-ptr-promotion, r=oli-obk
do not promote comparing function pointers This *could* break existing code that relied on fn ptr comparison getting promoted to `'static` lifetime. Fixes rust-lang#54696
2 parents 958f1c5 + 4cbfc93 commit d9d9663

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

src/librustc_mir/transform/qualify_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
750750
}
751751

752752
Rvalue::BinaryOp(op, ref lhs, _) => {
753-
if let ty::RawPtr(_) = lhs.ty(self.mir, self.tcx).sty {
753+
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(self.mir, self.tcx).sty {
754754
assert!(op == BinOp::Eq || op == BinOp::Ne ||
755755
op == BinOp::Le || op == BinOp::Lt ||
756756
op == BinOp::Ge || op == BinOp::Gt ||

src/librustc_passes/rvalue_promotion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ fn check_expr_kind<'a, 'tcx>(
348348
return NotPromotable;
349349
}
350350
match v.tables.node_id_to_type(lhs.hir_id).sty {
351-
ty::RawPtr(_) => {
351+
ty::RawPtr(_) | ty::FnPtr(..) => {
352352
assert!(op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne ||
353353
op.node == hir::BinOpKind::Le || op.node == hir::BinOpKind::Lt ||
354354
op.node == hir::BinOpKind::Ge || op.node == hir::BinOpKind::Gt);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-pass
2+
3+
fn main() {
4+
// We shouldn't promote this
5+
&(main as fn() == main as fn());
6+
// Also check nested case
7+
&(&(main as fn()) == &(main as fn()));
8+
}

src/test/ui/consts/const-eval/promoted_raw_ptr_ops.nll.stderr

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0716]: temporary value dropped while borrowed
1414
|
1515
LL | let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
17-
LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
17+
...
1818
LL | }
1919
| - temporary value is freed at the end of this statement
2020
|
@@ -25,11 +25,22 @@ error[E0716]: temporary value dropped while borrowed
2525
|
2626
LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
28+
LL | let a: &'static bool = &(main as fn() == main as fn()); //~ ERROR does not live long enough
29+
LL | }
30+
| - temporary value is freed at the end of this statement
31+
|
32+
= note: borrowed value must be valid for the static lifetime...
33+
34+
error[E0716]: temporary value dropped while borrowed
35+
--> $DIR/promoted_raw_ptr_ops.rs:18:29
36+
|
37+
LL | let a: &'static bool = &(main as fn() == main as fn()); //~ ERROR does not live long enough
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
2839
LL | }
2940
| - temporary value is freed at the end of this statement
3041
|
3142
= note: borrowed value must be valid for the static lifetime...
3243

33-
error: aborting due to 3 previous errors
44+
error: aborting due to 4 previous errors
3445

3546
For more information about this error, try `rustc --explain E0716`.

src/test/ui/consts/const-eval/promoted_raw_ptr_ops.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ fn main() {
1515
//~^ ERROR does not live long enough
1616
let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough
1717
let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
18+
let a: &'static bool = &(main as fn() == main as fn()); //~ ERROR does not live long enough
1819
}

src/test/ui/consts/const-eval/promoted_raw_ptr_ops.stderr

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0597]: borrowed value does not live long enough
1414
|
1515
LL | let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
17-
LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
17+
...
1818
LL | }
1919
| - temporary value only lives until here
2020
|
@@ -25,11 +25,22 @@ error[E0597]: borrowed value does not live long enough
2525
|
2626
LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
28+
LL | let a: &'static bool = &(main as fn() == main as fn()); //~ ERROR does not live long enough
29+
LL | }
30+
| - temporary value only lives until here
31+
|
32+
= note: borrowed value must be valid for the static lifetime...
33+
34+
error[E0597]: borrowed value does not live long enough
35+
--> $DIR/promoted_raw_ptr_ops.rs:18:29
36+
|
37+
LL | let a: &'static bool = &(main as fn() == main as fn()); //~ ERROR does not live long enough
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
2839
LL | }
2940
| - temporary value only lives until here
3041
|
3142
= note: borrowed value must be valid for the static lifetime...
3243

33-
error: aborting due to 3 previous errors
44+
error: aborting due to 4 previous errors
3445

3546
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)