Skip to content

Commit dcf49b5

Browse files
committedSep 20, 2015
Don't recommend const fns on a stable build without a note about nightlies
Fixes #28490
1 parent 6e5a325 commit dcf49b5

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed
 

‎src/librustc/diagnostics.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,18 @@ const FOO: i32 = { 0 }; // but brackets are useless here
298298
```
299299
"##,
300300

301+
// FIXME(#24111) Change the language here when const fn stabilizes
301302
E0015: r##"
302303
The only functions that can be called in static or constant expressions are
303-
`const` functions. Rust currently does not support more general compile-time
304-
function execution.
304+
`const` functions, and struct/enum constructors. `const` functions are only
305+
available on a nightly compiler. Rust currently does not support more general
306+
compile-time function execution.
307+
308+
```
309+
const FOO: Option<u8> = Some(1); // enum constructor
310+
struct Bar {x: u8}
311+
const BAR: Bar = Bar {x: 1}; // struct constructor
312+
```
305313
306314
See [RFC 911] for more details on the design of `const fn`s.
307315

‎src/librustc/middle/check_const.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use util::nodemap::NodeMap;
3939
use rustc_front::hir;
4040
use syntax::ast;
4141
use syntax::codemap::Span;
42+
use syntax::feature_gate::UnstableFeatures;
4243
use rustc_front::visit::{self, FnKind, Visitor};
4344

4445
use std::collections::hash_map::Entry;
@@ -709,10 +710,21 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
709710
if !is_const {
710711
v.add_qualif(ConstQualif::NOT_CONST);
711712
if v.mode != Mode::Var {
712-
span_err!(v.tcx.sess, e.span, E0015,
713-
"function calls in {}s are limited to \
714-
constant functions, \
715-
struct and enum constructors", v.msg());
713+
// FIXME(#24111) Remove this check when const fn stabilizes
714+
if let UnstableFeatures::Disallow = v.tcx.sess.opts.unstable_features {
715+
span_err!(v.tcx.sess, e.span, E0015,
716+
"function calls in {}s are limited to \
717+
struct and enum constructors", v.msg());
718+
v.tcx.sess.span_note(e.span,
719+
"a limited form of compile-time function \
720+
evaluation is available on a nightly \
721+
compiler via `const fn`");
722+
} else {
723+
span_err!(v.tcx.sess, e.span, E0015,
724+
"function calls in {}s are limited to \
725+
constant functions, \
726+
struct and enum constructors", v.msg());
727+
}
716728
}
717729
}
718730
}

0 commit comments

Comments
 (0)