Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow using const fn arguments as "args_required_const" #61536

Merged
merged 2 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
}
}

if self.mode == Mode::Fn {
// No need to do anything in constants and statics, as everything is "constant" anyway
// so promotion would be useless.
if self.mode != Mode::Static && self.mode != Mode::Const {
let constant_args = callee_def_id.and_then(|id| {
args_required_const(self.tcx, id)
}).unwrap_or_default();
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/consts/const_arg_promotable2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This test is a regression test for a bug where we only checked function calls in no-const
// functions for `rustc_args_required_const` arguments. This meant that even though `bar` needs its
// argument to be const, inside a const fn (callable at runtime), the value for it may come from a
// non-constant (namely an argument to the const fn).

#![feature(rustc_attrs)]
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
const fn foo(a: i32) {
bar(a); //~ ERROR argument 1 is required to be a constant
}

#[rustc_args_required_const(0)]
const fn bar(_: i32) {}

fn main() {
// this function call will pass a runtime-value (number of program arguments) to `foo`, which
// will in turn forward it to `bar`, which expects a compile-time argument
foo(std::env::args().count() as i32);
}
8 changes: 8 additions & 0 deletions src/test/ui/consts/const_arg_promotable2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: argument 1 is required to be a constant
--> $DIR/const_arg_promotable2.rs:8:5
|
LL | bar(a);
| ^^^^^^

error: aborting due to previous error