Skip to content

Commit 850b50a

Browse files
authored
Rollup merge of rust-lang#61536 - oli-obk:args_required_const_in_const_fn, r=eddyb
Don't allow using const fn arguments as "args_required_const" r? @eddyb
2 parents 76f9f6b + 192c1d0 commit 850b50a

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
13041304
}
13051305
}
13061306

1307-
if self.mode == Mode::Fn {
1307+
// No need to do anything in constants and statics, as everything is "constant" anyway
1308+
// so promotion would be useless.
1309+
if self.mode != Mode::Static && self.mode != Mode::Const {
13081310
let constant_args = callee_def_id.and_then(|id| {
13091311
args_required_const(self.tcx, id)
13101312
}).unwrap_or_default();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This test is a regression test for a bug where we only checked function calls in no-const
2+
// functions for `rustc_args_required_const` arguments. This meant that even though `bar` needs its
3+
// argument to be const, inside a const fn (callable at runtime), the value for it may come from a
4+
// non-constant (namely an argument to the const fn).
5+
6+
#![feature(rustc_attrs)]
7+
const fn foo(a: i32) {
8+
bar(a); //~ ERROR argument 1 is required to be a constant
9+
}
10+
11+
#[rustc_args_required_const(0)]
12+
const fn bar(_: i32) {}
13+
14+
fn main() {
15+
// this function call will pass a runtime-value (number of program arguments) to `foo`, which
16+
// will in turn forward it to `bar`, which expects a compile-time argument
17+
foo(std::env::args().count() as i32);
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: argument 1 is required to be a constant
2+
--> $DIR/const_arg_promotable2.rs:8:5
3+
|
4+
LL | bar(a);
5+
| ^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)