From dcf49b5f680a74b402a516e5bd1c855ac18259f5 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 19 Sep 2015 09:29:04 +0530 Subject: [PATCH] Don't recommend const fns on a stable build without a note about nightlies Fixes #28490 --- src/librustc/diagnostics.rs | 12 ++++++++++-- src/librustc/middle/check_const.rs | 20 ++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 19c880905aeb8..93c8b2ab0f134 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -298,10 +298,18 @@ const FOO: i32 = { 0 }; // but brackets are useless here ``` "##, +// FIXME(#24111) Change the language here when const fn stabilizes E0015: r##" The only functions that can be called in static or constant expressions are -`const` functions. Rust currently does not support more general compile-time -function execution. +`const` functions, and struct/enum constructors. `const` functions are only +available on a nightly compiler. Rust currently does not support more general +compile-time function execution. + +``` +const FOO: Option = Some(1); // enum constructor +struct Bar {x: u8} +const BAR: Bar = Bar {x: 1}; // struct constructor +``` See [RFC 911] for more details on the design of `const fn`s. diff --git a/src/librustc/middle/check_const.rs b/src/librustc/middle/check_const.rs index 7da2c8f1492df..f7bfb6e8a4018 100644 --- a/src/librustc/middle/check_const.rs +++ b/src/librustc/middle/check_const.rs @@ -39,6 +39,7 @@ use util::nodemap::NodeMap; use rustc_front::hir; use syntax::ast; use syntax::codemap::Span; +use syntax::feature_gate::UnstableFeatures; use rustc_front::visit::{self, FnKind, Visitor}; use std::collections::hash_map::Entry; @@ -709,10 +710,21 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, if !is_const { v.add_qualif(ConstQualif::NOT_CONST); if v.mode != Mode::Var { - span_err!(v.tcx.sess, e.span, E0015, - "function calls in {}s are limited to \ - constant functions, \ - struct and enum constructors", v.msg()); + // FIXME(#24111) Remove this check when const fn stabilizes + if let UnstableFeatures::Disallow = v.tcx.sess.opts.unstable_features { + span_err!(v.tcx.sess, e.span, E0015, + "function calls in {}s are limited to \ + struct and enum constructors", v.msg()); + v.tcx.sess.span_note(e.span, + "a limited form of compile-time function \ + evaluation is available on a nightly \ + compiler via `const fn`"); + } else { + span_err!(v.tcx.sess, e.span, E0015, + "function calls in {}s are limited to \ + constant functions, \ + struct and enum constructors", v.msg()); + } } } }