Skip to content

Commit 12c433f

Browse files
committed
experiment: never promote fn calls
1 parent 4a9ab29 commit 12c433f

File tree

6 files changed

+23
-123
lines changed

6 files changed

+23
-123
lines changed

compiler/rustc_mir_transform/src/promote_consts.rs

+3-27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! This pass assumes that every use is dominated by an
1212
//! initialization and can otherwise silence errors, if
1313
//! move analysis runs after promotion on broken MIR.
14+
#![allow(unused)]
1415

1516
use either::{Left, Right};
1617
use rustc_data_structures::fx::FxHashSet;
@@ -657,33 +658,8 @@ impl<'tcx> Validator<'_, 'tcx> {
657658
}
658659
}
659660

660-
// Ideally, we'd stop here and reject the rest.
661-
// But for backward compatibility, we have to accept some promotion in const/static
662-
// initializers. Inline consts are explicitly excluded, they are more recent so we have no
663-
// backwards compatibility reason to allow more promotion inside of them.
664-
let promote_all_fn = matches!(
665-
self.const_kind,
666-
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false })
667-
);
668-
if !promote_all_fn {
669-
return Err(Unpromotable);
670-
}
671-
// Make sure the callee is a `const fn`.
672-
let is_const_fn = match *fn_ty.kind() {
673-
ty::FnDef(def_id, _) => self.tcx.is_const_fn_raw(def_id),
674-
_ => false,
675-
};
676-
if !is_const_fn {
677-
return Err(Unpromotable);
678-
}
679-
// The problem is, this may promote calls to functions that panic.
680-
// We don't want to introduce compilation errors if there's a panic in a call in dead code.
681-
// So we ensure that this is not dead code.
682-
if !self.is_promotion_safe_block(block) {
683-
return Err(Unpromotable);
684-
}
685-
// This passed all checks, so let's accept.
686-
Ok(())
661+
// Ideally, we'd stop here and reject the rest. So let's do that.
662+
return Err(Unpromotable);
687663
}
688664
}
689665

tests/ui/consts/const-eval/raw-bytes.64bit.stderr

+6-24
Original file line numberDiff line numberDiff line change
@@ -337,50 +337,32 @@ LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
337337
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x03, but expected a boolean
338338
|
339339
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
340-
= note: the raw bytes of the constant (size: 8, align: 8) {
341-
╾ALLOC_ID╼ │ ╾──────╼
340+
= note: the raw bytes of the constant (size: 16, align: 8) {
341+
╾ALLOC_ID╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........
342342
}
343343

344-
note: erroneous constant encountered
345-
--> $DIR/raw-bytes.rs:162:40
346-
|
347-
LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
348-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
349-
350344
error[E0080]: it is undefined behavior to use this value
351345
--> $DIR/raw-bytes.rs:168:1
352346
|
353347
LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
354348
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>.0: encountered 0x03, but expected a boolean
355349
|
356350
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
357-
= note: the raw bytes of the constant (size: 8, align: 8) {
358-
╾ALLOC_ID╼ │ ╾──────╼
351+
= note: the raw bytes of the constant (size: 16, align: 8) {
352+
╾ALLOC_ID╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........
359353
}
360354

361-
note: erroneous constant encountered
362-
--> $DIR/raw-bytes.rs:168:42
363-
|
364-
LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
365-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
366-
367355
error[E0080]: it is undefined behavior to use this value
368356
--> $DIR/raw-bytes.rs:172:1
369357
|
370358
LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
371359
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>.1[0]: encountered 0x03, but expected a boolean
372360
|
373361
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
374-
= note: the raw bytes of the constant (size: 8, align: 8) {
375-
╾ALLOC_ID╼ │ ╾──────╼
362+
= note: the raw bytes of the constant (size: 16, align: 8) {
363+
╾ALLOC_ID╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........
376364
}
377365

378-
note: erroneous constant encountered
379-
--> $DIR/raw-bytes.rs:172:42
380-
|
381-
LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
382-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
383-
384366
error[E0080]: it is undefined behavior to use this value
385367
--> $DIR/raw-bytes.rs:177:1
386368
|

tests/ui/consts/const-eval/ub-ref-ptr.stderr

-12
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
6060
= help: this code performed an operation that depends on the underlying bytes representing a pointer
6161
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
6262

63-
note: erroneous constant encountered
64-
--> $DIR/ub-ref-ptr.rs:36:38
65-
|
66-
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
67-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68-
6963
error[E0080]: evaluation of constant value failed
7064
--> $DIR/ub-ref-ptr.rs:39:86
7165
|
@@ -75,12 +69,6 @@ LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[us
7569
= help: this code performed an operation that depends on the underlying bytes representing a pointer
7670
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
7771

78-
note: erroneous constant encountered
79-
--> $DIR/ub-ref-ptr.rs:39:85
80-
|
81-
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
82-
| ^^^^^^^^^^^^^^^^^^^^^
83-
8472
error[E0080]: it is undefined behavior to use this value
8573
--> $DIR/ub-ref-ptr.rs:42:1
8674
|

tests/ui/consts/const-eval/ub-wide-ptr.stderr

-18
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
139139
HEX_DUMP
140140
}
141141

142-
note: erroneous constant encountered
143-
--> $DIR/ub-wide-ptr.rs:86:40
144-
|
145-
LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
146-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147-
148142
error[E0080]: it is undefined behavior to use this value
149143
--> $DIR/ub-wide-ptr.rs:93:1
150144
|
@@ -156,12 +150,6 @@ LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3
156150
HEX_DUMP
157151
}
158152

159-
note: erroneous constant encountered
160-
--> $DIR/ub-wide-ptr.rs:93:42
161-
|
162-
LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
163-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164-
165153
error[E0080]: it is undefined behavior to use this value
166154
--> $DIR/ub-wide-ptr.rs:97:1
167155
|
@@ -173,12 +161,6 @@ LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::tran
173161
HEX_DUMP
174162
}
175163

176-
note: erroneous constant encountered
177-
--> $DIR/ub-wide-ptr.rs:97:42
178-
|
179-
LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
180-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
181-
182164
error[E0080]: evaluation of constant value failed
183165
--> $DIR/ub-wide-ptr.rs:105:1
184166
|
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
error[E0080]: evaluation of constant value failed
2-
--> $SRC_DIR/core/src/hint.rs:LL:COL
3-
|
4-
= note: entering unreachable code
5-
|
6-
note: inside `unreachable_unchecked`
7-
--> $SRC_DIR/core/src/hint.rs:LL:COL
8-
note: inside `ub`
9-
--> $DIR/interpret-in-promoted.rs:6:5
10-
|
11-
LL | std::hint::unreachable_unchecked();
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13-
note: inside `FOO`
1+
error[E0716]: temporary value dropped while borrowed
142
--> $DIR/interpret-in-promoted.rs:12:28
153
|
164
LL | let _x: &'static () = &ub();
17-
| ^^^^
18-
19-
note: erroneous constant encountered
20-
--> $DIR/interpret-in-promoted.rs:12:27
21-
|
22-
LL | let _x: &'static () = &ub();
23-
| ^^^^^
5+
| ----------- ^^^^ creates a temporary value which is freed while still in use
6+
| |
7+
| type annotation requires that borrow lasts for `'static`
8+
LL | };
9+
| - temporary value is freed at the end of this statement
2410

2511
error: aborting due to 1 previous error
2612

27-
For more information about this error, try `rustc --explain E0080`.
13+
For more information about this error, try `rustc --explain E0716`.
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
error[E0080]: evaluation of constant value failed
2-
--> $SRC_DIR/core/src/hint.rs:LL:COL
3-
|
4-
= note: entering unreachable code
5-
|
6-
note: inside `unreachable_unchecked`
7-
--> $SRC_DIR/core/src/hint.rs:LL:COL
8-
note: inside `ub`
9-
--> $DIR/interpret-in-promoted.rs:6:5
10-
|
11-
LL | std::hint::unreachable_unchecked();
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13-
note: inside `FOO`
1+
error[E0716]: temporary value dropped while borrowed
142
--> $DIR/interpret-in-promoted.rs:12:28
153
|
164
LL | let _x: &'static () = &ub();
17-
| ^^^^
18-
19-
note: erroneous constant encountered
20-
--> $DIR/interpret-in-promoted.rs:12:27
21-
|
22-
LL | let _x: &'static () = &ub();
23-
| ^^^^^
5+
| ----------- ^^^^ creates a temporary value which is freed while still in use
6+
| |
7+
| type annotation requires that borrow lasts for `'static`
8+
LL | };
9+
| - temporary value is freed at the end of this statement
2410

2511
error: aborting due to 1 previous error
2612

27-
For more information about this error, try `rustc --explain E0080`.
13+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)