Skip to content

Commit 8425494

Browse files
committed
Auto merge of #25880 - nikomatsakis:const-fn-feature-gate-calls, r=alexcrichton
The previous feature gate assumed we would not define any (stable) const fns. But then @eddyb went and cleaned up the code. So this now extends the feature-gate to prohibit calls; but calls inside of macros are considered ok. r? @alexcrichton
2 parents 2d447e4 + 808b411 commit 8425494

35 files changed

+232
-7
lines changed

src/libcollectionstest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(collections)]
1515
#![feature(collections_drain)]
1616
#![feature(core)]
17+
#![feature(const_fn)]
1718
#![feature(hash)]
1819
#![feature(rand)]
1920
#![feature(rustc_private)]

src/libcoretest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(box_syntax)]
1515
#![feature(unboxed_closures)]
1616
#![feature(core)]
17+
#![feature(const_fn)]
1718
#![feature(test)]
1819
#![feature(rand)]
1920
#![feature(unicode)]

src/liblog/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
#![feature(staged_api)]
174174
#![feature(box_syntax)]
175175
#![feature(core)]
176+
#![feature(const_fn)]
176177
#![feature(std_misc)]
177178

178179
use std::boxed;

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(box_patterns)]
3030
#![feature(box_syntax)]
3131
#![feature(collections)]
32+
#![feature(const_fn)]
3233
#![feature(core)]
3334
#![feature(duration)]
3435
#![feature(duration_span)]

src/librustc/middle/check_const.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,32 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
199199
}
200200

201201
/// Returns true if the call is to a const fn or method.
202-
fn handle_const_fn_call(&mut self, def_id: ast::DefId, ret_ty: Ty<'tcx>) -> bool {
202+
fn handle_const_fn_call(&mut self,
203+
expr: &ast::Expr,
204+
def_id: ast::DefId,
205+
ret_ty: Ty<'tcx>)
206+
-> bool {
203207
if let Some(fn_like) = const_eval::lookup_const_fn_by_id(self.tcx, def_id) {
208+
if
209+
// we are in a static/const initializer
210+
self.mode != Mode::Var &&
211+
212+
// feature-gate is not enabled
213+
!self.tcx.sess.features.borrow().const_fn &&
214+
215+
// this doesn't come from a macro that has #[allow_internal_unstable]
216+
!self.tcx.sess.codemap().span_allows_unstable(expr.span)
217+
{
218+
self.tcx.sess.span_err(
219+
expr.span,
220+
&format!("const fns are an unstable feature"));
221+
fileline_help!(
222+
self.tcx.sess,
223+
expr.span,
224+
"in Nightly builds, add `#![feature(const_fn)]` to the crate \
225+
attributes to enable");
226+
}
227+
204228
let qualif = self.fn_like(fn_like.kind(),
205229
fn_like.decl(),
206230
fn_like.body(),
@@ -657,7 +681,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
657681
}
658682
Some(def::DefMethod(did, def::FromImpl(_))) |
659683
Some(def::DefFn(did, _)) => {
660-
v.handle_const_fn_call(did, node_ty)
684+
v.handle_const_fn_call(e, did, node_ty)
661685
}
662686
_ => false
663687
};
@@ -677,7 +701,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
677701
_ => None
678702
};
679703
let is_const = match method_did {
680-
Some(did) => v.handle_const_fn_call(did, node_ty),
704+
Some(did) => v.handle_const_fn_call(e, did, node_ty),
681705
None => false
682706
};
683707
if !is_const {

src/librustc_trans/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(box_syntax)]
3131
#![feature(collections)]
3232
#![feature(core)]
33+
#![feature(const_fn)]
3334
#![feature(libc)]
3435
#![feature(quote)]
3536
#![feature(rustc_diagnostic_macros)]

src/libsyntax/feature_gate.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ pub struct Features {
332332
/// spans of #![feature] attrs for stable language features. for error reporting
333333
pub declared_stable_lang_features: Vec<Span>,
334334
/// #![feature] attrs for non-language (library) features
335-
pub declared_lib_features: Vec<(InternedString, Span)>
335+
pub declared_lib_features: Vec<(InternedString, Span)>,
336+
pub const_fn: bool,
336337
}
337338

338339
impl Features {
@@ -352,7 +353,8 @@ impl Features {
352353
unmarked_api: false,
353354
negate_unsigned: false,
354355
declared_stable_lang_features: Vec::new(),
355-
declared_lib_features: Vec::new()
356+
declared_lib_features: Vec::new(),
357+
const_fn: false,
356358
}
357359
}
358360
}
@@ -802,7 +804,8 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
802804
unmarked_api: cx.has_feature("unmarked_api"),
803805
negate_unsigned: cx.has_feature("negate_unsigned"),
804806
declared_stable_lang_features: accepted_features,
805-
declared_lib_features: unknown_features
807+
declared_lib_features: unknown_features,
808+
const_fn: cx.has_feature("const_fn"),
806809
}
807810
}
808811

src/test/auxiliary/const_fn_lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Crate that exports a const fn. Used for testing cross-crate.
12+
13+
#![crate_type="rlib"]
14+
#![feature(const_fn)]
15+
16+
pub const fn foo() -> usize { 22 } //~ ERROR const fn is unstable

src/test/auxiliary/issue-17718.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(const_fn)]
12+
1113
use std::sync::atomic;
1214

1315
pub const C1: usize = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test use of const fn from another crate without a feature gate.
12+
13+
// aux-build:const_fn_lib.rs
14+
15+
extern crate const_fn_lib;
16+
17+
use const_fn_lib::foo;
18+
19+
fn main() {
20+
let x: [usize; foo()] = []; //~ ERROR unsupported constant expr
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test use of const fn from another crate without a feature gate.
12+
13+
#![feature(rustc_attrs)]
14+
#![allow(unused_variables)]
15+
16+
// aux-build:const_fn_lib.rs
17+
18+
extern crate const_fn_lib;
19+
20+
use const_fn_lib::foo;
21+
22+
#[rustc_error]
23+
fn main() { //~ ERROR compilation successful
24+
let x = foo(); // use outside a constant is ok
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test use of const fn from another crate without a feature gate.
12+
13+
// aux-build:const_fn_lib.rs
14+
15+
extern crate const_fn_lib;
16+
17+
use const_fn_lib::foo;
18+
19+
static FOO: usize = foo(); //~ ERROR const fns are an unstable feature
20+
const BAR: usize = foo(); //~ ERROR const fns are an unstable feature
21+
22+
macro_rules! constant {
23+
($n:ident: $t:ty = $v:expr) => {
24+
const $n: $t = $v;
25+
}
26+
}
27+
28+
constant! {
29+
BAZ: usize = foo() //~ ERROR const fns are an unstable feature
30+
}
31+
32+
fn main() {
33+
// let x: [usize; foo()] = [];
34+
}

src/test/compile-fail/const-fn-stability.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,19 @@ impl Foo for u32 {
2525
const fn foo() -> u32 { 0 } //~ ERROR const fn is unstable
2626
}
2727

28-
fn main() { }
28+
static FOO: usize = foo();
29+
const BAR: usize = foo();
30+
31+
macro_rules! constant {
32+
($n:ident: $t:ty = $v:expr) => {
33+
const $n: $t = $v;
34+
}
35+
}
36+
37+
constant! {
38+
BAZ: usize = foo()
39+
}
40+
41+
fn main() {
42+
let x: [usize; foo()] = [];
43+
}

src/test/compile-fail/dropck_arr_cycle_checked.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
//
1414
// (Compare against compile-fail/dropck_vec_cycle_checked.rs)
1515

16+
#![feature(const_fn)]
17+
1618
use std::cell::Cell;
1719
use id::Id;
1820

src/test/compile-fail/dropck_tarena_cycle_checked.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// for the error message we see here.)
1818

1919
#![allow(unstable)]
20+
#![feature(const_fn)]
2021

2122
extern crate arena;
2223

src/test/compile-fail/dropck_trait_cycle_checked.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
//
1414
// (Compare against compile-fail/dropck_vec_cycle_checked.rs)
1515

16+
#![feature(const_fn)]
17+
1618
use std::cell::Cell;
1719
use id::Id;
1820

src/test/compile-fail/dropck_vec_cycle_checked.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
// (Compare against compile-fail/dropck_arr_cycle_checked.rs)
1414

15+
#![feature(const_fn)]
16+
1517
use std::cell::Cell;
1618
use id::Id;
1719

src/test/compile-fail/functional-struct-update-respects-privacy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// RFC 736 (and Issue 21407): functional struct update should respect privacy.
1212

13+
#![feature(const_fn)]
14+
1315
// The `foo` module attempts to maintains an invariant that each `S`
1416
// has a unique `u64` id.
1517
use self::foo::S;

src/test/compile-fail/issue-17718-const-borrow.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(const_fn)]
12+
1113
use std::cell::UnsafeCell;
1214

1315
const A: UnsafeCell<usize> = UnsafeCell::new(1);

src/test/compile-fail/issue-7364.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#![feature(box_syntax)]
12+
#![feature(const_fn)]
1213

1314
use std::cell::RefCell;
1415

src/test/compile-fail/vec-must-not-hide-type-from-dropck.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// conditions above to be satisfied, meaning that if the dropck is
2424
// sound, it should reject this code.
2525

26+
#![feature(const_fn)]
27+
2628
use std::cell::Cell;
2729
use id::Id;
2830

src/test/debuginfo/constant-debug-locs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![allow(dead_code, unused_variables)]
1616
#![omit_gdb_pretty_printer_section]
1717
#![feature(std_misc, core)]
18+
#![feature(const_fn)]
1819

1920
// This test makes sure that the compiler doesn't crash when trying to assign
2021
// debug locations to const-expressions.

src/test/run-pass-valgrind/cast-enum-with-dtor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#![allow(dead_code)]
12+
#![feature(const_fn)]
1213

1314
// check dtor calling order when casting enums.
1415

src/test/run-pass/associated-types-project-from-type-param-via-bound-in-where-clause.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// `Item` originates in a where-clause, not the declaration of
1313
// `T`. Issue #20300.
1414

15+
#![feature(const_fn)]
16+
1517
use std::marker::{PhantomData};
1618
use std::sync::atomic::{AtomicUsize};
1719
use std::sync::atomic::Ordering::SeqCst;

src/test/run-pass/box-of-array-of-drop-1.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// Test that we cleanup a fixed size Box<[D; k]> properly when D has a
1212
// destructor.
1313

14+
#![feature(const_fn)]
15+
1416
use std::thread;
1517
use std::sync::atomic::{AtomicUsize, Ordering};
1618

src/test/run-pass/box-of-array-of-drop-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// Test that we cleanup dynamic sized Box<[D]> properly when D has a
1212
// destructor.
1313

14+
#![feature(const_fn)]
15+
1416
use std::thread;
1517
use std::sync::atomic::{AtomicUsize, Ordering};
1618

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:const_fn_lib.rs
12+
13+
// A very basic test of const fn functionality.
14+
15+
#![feature(const_fn)]
16+
17+
extern crate const_fn_lib;
18+
19+
use const_fn_lib::foo;
20+
21+
const FOO: usize = foo();
22+
23+
fn main() {
24+
assert_eq!(FOO, 22);
25+
}

0 commit comments

Comments
 (0)