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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 27 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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;
Lines changed: 21 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)