Add feature-gate to calling const fn

This commit is contained in:
Niko Matsakis 2015-05-28 11:22:00 -04:00
parent 3a433b968b
commit 710270d9c0
3 changed files with 30 additions and 7 deletions

View File

@ -199,8 +199,23 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
}
/// Returns true if the call is to a const fn or method.
fn handle_const_fn_call(&mut self, def_id: ast::DefId, ret_ty: Ty<'tcx>) -> bool {
fn handle_const_fn_call(&mut self,
expr: &ast::Expr,
def_id: ast::DefId,
ret_ty: Ty<'tcx>)
-> bool {
if let Some(fn_like) = const_eval::lookup_const_fn_by_id(self.tcx, def_id) {
if self.mode != Mode::Var && !self.tcx.sess.features.borrow().const_fn {
self.tcx.sess.span_err(
expr.span,
&format!("const fns are an unstable feature"));
fileline_help!(
self.tcx.sess,
expr.span,
"in Nightly builds, add `#![feature(const_fn)]` to the crate \
attributes to enable");
}
let qualif = self.fn_like(fn_like.kind(),
fn_like.decl(),
fn_like.body(),
@ -657,7 +672,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
}
Some(def::DefMethod(did, def::FromImpl(_))) |
Some(def::DefFn(did, _)) => {
v.handle_const_fn_call(did, node_ty)
v.handle_const_fn_call(e, did, node_ty)
}
_ => false
};
@ -677,7 +692,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
_ => None
};
let is_const = match method_did {
Some(did) => v.handle_const_fn_call(did, node_ty),
Some(did) => v.handle_const_fn_call(e, did, node_ty),
None => false
};
if !is_const {

View File

@ -332,7 +332,8 @@ pub struct Features {
/// spans of #![feature] attrs for stable language features. for error reporting
pub declared_stable_lang_features: Vec<Span>,
/// #![feature] attrs for non-language (library) features
pub declared_lib_features: Vec<(InternedString, Span)>
pub declared_lib_features: Vec<(InternedString, Span)>,
pub const_fn: bool,
}
impl Features {
@ -352,7 +353,8 @@ impl Features {
unmarked_api: false,
negate_unsigned: false,
declared_stable_lang_features: Vec::new(),
declared_lib_features: Vec::new()
declared_lib_features: Vec::new(),
const_fn: false,
}
}
}
@ -802,7 +804,8 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
unmarked_api: cx.has_feature("unmarked_api"),
negate_unsigned: cx.has_feature("negate_unsigned"),
declared_stable_lang_features: accepted_features,
declared_lib_features: unknown_features
declared_lib_features: unknown_features,
const_fn: cx.has_feature("const_fn"),
}
}

View File

@ -25,4 +25,9 @@ impl Foo for u32 {
const fn foo() -> u32 { 0 } //~ ERROR const fn is unstable
}
fn main() { }
static FOO: usize = foo();
const BAR: usize = foo();
fn main() {
let x: [usize; foo()] = [];
}