提交 710270d9 编写于 作者: N Niko Matsakis

Add feature-gate to calling const fn

上级 3a433b96
...@@ -199,8 +199,23 @@ fn add_qualif(&mut self, qualif: ConstQualif) { ...@@ -199,8 +199,23 @@ fn add_qualif(&mut self, qualif: ConstQualif) {
} }
/// Returns true if the call is to a const fn or method. /// 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 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(), let qualif = self.fn_like(fn_like.kind(),
fn_like.decl(), fn_like.decl(),
fn_like.body(), fn_like.body(),
...@@ -657,7 +672,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, ...@@ -657,7 +672,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
} }
Some(def::DefMethod(did, def::FromImpl(_))) | Some(def::DefMethod(did, def::FromImpl(_))) |
Some(def::DefFn(did, _)) => { Some(def::DefFn(did, _)) => {
v.handle_const_fn_call(did, node_ty) v.handle_const_fn_call(e, did, node_ty)
} }
_ => false _ => false
}; };
...@@ -677,7 +692,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, ...@@ -677,7 +692,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
_ => None _ => None
}; };
let is_const = match method_did { 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 None => false
}; };
if !is_const { if !is_const {
......
...@@ -332,7 +332,8 @@ pub struct Features { ...@@ -332,7 +332,8 @@ pub struct Features {
/// spans of #![feature] attrs for stable language features. for error reporting /// spans of #![feature] attrs for stable language features. for error reporting
pub declared_stable_lang_features: Vec<Span>, pub declared_stable_lang_features: Vec<Span>,
/// #![feature] attrs for non-language (library) features /// #![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 { impl Features {
...@@ -352,7 +353,8 @@ pub fn new() -> Features { ...@@ -352,7 +353,8 @@ pub fn new() -> Features {
unmarked_api: false, unmarked_api: false,
negate_unsigned: false, negate_unsigned: false,
declared_stable_lang_features: Vec::new(), 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, ...@@ -802,7 +804,8 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
unmarked_api: cx.has_feature("unmarked_api"), unmarked_api: cx.has_feature("unmarked_api"),
negate_unsigned: cx.has_feature("negate_unsigned"), negate_unsigned: cx.has_feature("negate_unsigned"),
declared_stable_lang_features: accepted_features, declared_stable_lang_features: accepted_features,
declared_lib_features: unknown_features declared_lib_features: unknown_features,
const_fn: cx.has_feature("const_fn"),
} }
} }
......
...@@ -25,4 +25,9 @@ impl Foo for u32 { ...@@ -25,4 +25,9 @@ impl Foo for u32 {
const fn foo() -> u32 { 0 } //~ ERROR const fn is unstable 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()] = [];
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册