2019-11-07 12:02:01 -06:00
|
|
|
//! This pass checks HIR bodies that may be evaluated at compile-time (e.g., `const`, `static`,
|
|
|
|
//! `const fn`) for structured control flow (e.g. `if`, `while`), which is forbidden in a const
|
|
|
|
//! context.
|
2019-11-06 13:44:56 -06:00
|
|
|
//!
|
|
|
|
//! By the time the MIR const-checker runs, these high-level constructs have been lowered to
|
|
|
|
//! control-flow primitives (e.g., `Goto`, `SwitchInt`), making it tough to properly attribute
|
|
|
|
//! errors. We still look for those primitives in the MIR const-checker to ensure nothing slips
|
|
|
|
//! through, but errors for structured control flow in a `const` should be emitted here.
|
|
|
|
|
2020-05-21 16:23:50 -05:00
|
|
|
use rustc_attr as attr;
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_errors::struct_span_err;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
2020-06-27 06:09:54 -05:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-01-07 11:12:06 -06:00
|
|
|
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::hir::map::Map;
|
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-03-11 06:49:08 -05:00
|
|
|
use rustc_session::parse::feature_err;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::{sym, Span, Symbol};
|
2019-11-06 13:44:56 -06:00
|
|
|
|
2019-11-16 01:08:30 -06:00
|
|
|
/// An expression that is not *always* legal in a const context.
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum NonConstExpr {
|
|
|
|
Loop(hir::LoopSource),
|
|
|
|
Match(hir::MatchSource),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NonConstExpr {
|
2019-12-24 10:43:17 -06:00
|
|
|
fn name(self) -> String {
|
2019-11-16 01:08:30 -06:00
|
|
|
match self {
|
2019-12-24 10:43:17 -06:00
|
|
|
Self::Loop(src) => format!("`{}`", src.name()),
|
|
|
|
Self::Match(src) => format!("`{}`", src.name()),
|
2019-11-16 01:08:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 14:43:15 -06:00
|
|
|
fn required_feature_gates(self) -> Option<&'static [Symbol]> {
|
|
|
|
use hir::LoopSource::*;
|
2019-12-22 16:42:04 -06:00
|
|
|
use hir::MatchSource::*;
|
2019-12-10 14:43:15 -06:00
|
|
|
|
|
|
|
let gates: &[_] = match self {
|
2020-05-21 14:47:09 -05:00
|
|
|
// A `for` loop's desugaring contains a call to `IntoIterator::into_iter`,
|
2020-06-25 19:41:32 -05:00
|
|
|
// so they are not yet allowed.
|
2020-05-21 14:47:09 -05:00
|
|
|
// Likewise, `?` desugars to a call to `Try::into_result`.
|
|
|
|
Self::Loop(ForLoop) | Self::Match(ForLoopDesugar | TryDesugar | AwaitDesugar) => {
|
|
|
|
return None;
|
|
|
|
}
|
2019-12-10 14:43:15 -06:00
|
|
|
|
2021-02-17 04:26:38 -06:00
|
|
|
Self::Match(IfLetGuardDesugar) => bug!("`if let` guard outside a `match` expression"),
|
2020-11-13 11:27:27 -06:00
|
|
|
|
2020-06-25 19:41:32 -05:00
|
|
|
// All other expressions are allowed.
|
|
|
|
Self::Loop(Loop | While | WhileLet)
|
2021-01-01 12:38:11 -06:00
|
|
|
| Self::Match(WhileDesugar | WhileLetDesugar | Normal | IfLetDesugar { .. }) => &[],
|
2019-12-10 14:43:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
Some(gates)
|
2019-11-16 01:08:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 06:09:54 -05:00
|
|
|
fn check_mod_const_bodies(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
2019-11-06 13:44:56 -06:00
|
|
|
let mut vis = CheckConstVisitor::new(tcx);
|
|
|
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut vis.as_deep_visitor());
|
|
|
|
}
|
|
|
|
|
2020-07-05 15:00:14 -05:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2019-12-22 16:42:04 -06:00
|
|
|
*providers = Providers { check_mod_const_bodies, ..*providers };
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct CheckConstVisitor<'tcx> {
|
2019-11-15 16:46:11 -06:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-05-07 12:24:20 -05:00
|
|
|
const_kind: Option<hir::ConstContext>,
|
2020-06-28 12:13:04 -05:00
|
|
|
def_id: Option<LocalDefId>,
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CheckConstVisitor<'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> Self {
|
2020-05-21 16:23:50 -05:00
|
|
|
CheckConstVisitor { tcx, const_kind: None, def_id: None }
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits an error when an unsupported expression is found in a const context.
|
2019-11-16 01:08:30 -06:00
|
|
|
fn const_check_violated(&self, expr: NonConstExpr, span: Span) {
|
2020-05-21 16:23:50 -05:00
|
|
|
let Self { tcx, def_id, const_kind } = *self;
|
|
|
|
|
|
|
|
let features = tcx.features();
|
2019-12-11 12:26:06 -06:00
|
|
|
let required_gates = expr.required_feature_gates();
|
2020-05-21 16:23:50 -05:00
|
|
|
|
|
|
|
let is_feature_allowed = |feature_gate| {
|
|
|
|
// All features require that the corresponding gate be enabled,
|
2020-10-21 13:54:20 -05:00
|
|
|
// even if the function has `#[rustc_allow_const_fn_unstable(the_gate)]`.
|
2020-05-21 16:23:50 -05:00
|
|
|
if !tcx.features().enabled(feature_gate) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If `def_id` is `None`, we don't need to consider stability attributes.
|
|
|
|
let def_id = match def_id {
|
2020-06-28 12:13:04 -05:00
|
|
|
Some(x) => x.to_def_id(),
|
2020-05-21 16:23:50 -05:00
|
|
|
None => return true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// If this crate is not using stability attributes, or this function is not claiming to be a
|
|
|
|
// stable `const fn`, that is all that is required.
|
|
|
|
if !tcx.features().staged_api || tcx.has_attr(def_id, sym::rustc_const_unstable) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// However, we cannot allow stable `const fn`s to use unstable features without an explicit
|
2020-10-21 13:54:20 -05:00
|
|
|
// opt-in via `rustc_allow_const_fn_unstable`.
|
|
|
|
attr::rustc_allow_const_fn_unstable(&tcx.sess, &tcx.get_attrs(def_id))
|
2021-02-21 11:11:27 -06:00
|
|
|
.any(|name| name == feature_gate)
|
2020-05-21 16:23:50 -05:00
|
|
|
};
|
|
|
|
|
2019-12-11 12:26:06 -06:00
|
|
|
match required_gates {
|
2019-11-16 01:08:30 -06:00
|
|
|
// Don't emit an error if the user has enabled the requisite feature gates.
|
2020-05-21 16:23:50 -05:00
|
|
|
Some(gates) if gates.iter().copied().all(is_feature_allowed) => return,
|
2019-11-16 01:08:30 -06:00
|
|
|
|
2019-12-10 14:43:15 -06:00
|
|
|
// `-Zunleash-the-miri-inside-of-you` only works for expressions that don't have a
|
|
|
|
// corresponding feature gate. This encourages nightly users to use feature gates when
|
|
|
|
// possible.
|
2020-05-21 16:23:50 -05:00
|
|
|
None if tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you => {
|
|
|
|
tcx.sess.span_warn(span, "skipping const checks");
|
2019-11-16 01:08:30 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
2019-11-09 11:07:13 -06:00
|
|
|
}
|
|
|
|
|
2020-05-21 16:23:50 -05:00
|
|
|
let const_kind =
|
|
|
|
const_kind.expect("`const_check_violated` may only be called inside a const context");
|
2020-05-07 12:24:20 -05:00
|
|
|
|
|
|
|
let msg = format!("{} is not allowed in a `{}`", expr.name(), const_kind.keyword_name());
|
2019-11-16 01:08:30 -06:00
|
|
|
|
2019-12-11 12:26:06 -06:00
|
|
|
let required_gates = required_gates.unwrap_or(&[]);
|
2019-12-22 16:42:04 -06:00
|
|
|
let missing_gates: Vec<_> =
|
|
|
|
required_gates.iter().copied().filter(|&g| !features.enabled(g)).collect();
|
2019-12-10 14:43:15 -06:00
|
|
|
|
|
|
|
match missing_gates.as_slice() {
|
2020-05-21 16:23:50 -05:00
|
|
|
&[] => struct_span_err!(tcx.sess, span, E0744, "{}", msg).emit(),
|
2019-12-10 14:43:15 -06:00
|
|
|
|
|
|
|
&[missing_primary, ref missing_secondary @ ..] => {
|
2020-05-21 16:23:50 -05:00
|
|
|
let mut err = feature_err(&tcx.sess.parse_sess, missing_primary, span, &msg);
|
2019-12-10 14:43:15 -06:00
|
|
|
|
|
|
|
// If multiple feature gates would be required to enable this expression, include
|
|
|
|
// them as help messages. Don't emit a separate error for each missing feature gate.
|
|
|
|
//
|
|
|
|
// FIXME(ecstaticmorse): Maybe this could be incorporated into `feature_err`? This
|
|
|
|
// is a pretty narrow case, however.
|
2020-10-10 13:27:52 -05:00
|
|
|
if tcx.sess.is_nightly_build() {
|
2019-12-10 14:43:15 -06:00
|
|
|
for gate in missing_secondary {
|
|
|
|
let note = format!(
|
|
|
|
"add `#![feature({})]` to the crate attributes to enable",
|
|
|
|
gate,
|
|
|
|
);
|
|
|
|
err.help(¬e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err.emit();
|
|
|
|
}
|
2019-11-16 01:08:30 -06:00
|
|
|
}
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
2019-11-07 12:02:01 -06:00
|
|
|
/// Saves the parent `const_kind` before calling `f` and restores it afterwards.
|
2020-05-21 16:23:50 -05:00
|
|
|
fn recurse_into(
|
|
|
|
&mut self,
|
|
|
|
kind: Option<hir::ConstContext>,
|
2020-06-28 12:13:04 -05:00
|
|
|
def_id: Option<LocalDefId>,
|
2020-05-21 16:23:50 -05:00
|
|
|
f: impl FnOnce(&mut Self),
|
|
|
|
) {
|
|
|
|
let parent_def_id = self.def_id;
|
2019-11-06 13:44:56 -06:00
|
|
|
let parent_kind = self.const_kind;
|
2020-05-21 16:23:50 -05:00
|
|
|
self.def_id = def_id;
|
2019-11-06 13:44:56 -06:00
|
|
|
self.const_kind = kind;
|
|
|
|
f(self);
|
2020-05-21 16:23:50 -05:00
|
|
|
self.def_id = parent_def_id;
|
2019-11-06 13:44:56 -06:00
|
|
|
self.const_kind = parent_kind;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
|
2020-01-07 10:25:33 -06:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2020-02-09 08:32:00 -06:00
|
|
|
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
|
|
|
|
NestedVisitorMap::OnlyBodies(self.tcx.hir())
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_anon_const(&mut self, anon: &'tcx hir::AnonConst) {
|
2020-05-07 12:24:20 -05:00
|
|
|
let kind = Some(hir::ConstContext::Const);
|
2020-05-21 16:23:50 -05:00
|
|
|
self.recurse_into(kind, None, |this| intravisit::walk_anon_const(this, anon));
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
2019-11-29 04:09:23 -06:00
|
|
|
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
|
2020-05-07 12:24:20 -05:00
|
|
|
let owner = self.tcx.hir().body_owner_def_id(body.id());
|
|
|
|
let kind = self.tcx.hir().body_const_context(owner);
|
2020-06-28 12:13:04 -05:00
|
|
|
self.recurse_into(kind, Some(owner), |this| intravisit::walk_body(this, body));
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
2019-11-30 08:08:22 -06:00
|
|
|
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
|
2019-11-06 13:44:56 -06:00
|
|
|
match &e.kind {
|
2019-11-07 12:02:01 -06:00
|
|
|
// Skip the following checks if we are not currently in a const context.
|
2019-11-06 13:44:56 -06:00
|
|
|
_ if self.const_kind.is_none() => {}
|
|
|
|
|
2021-01-20 19:15:08 -06:00
|
|
|
hir::ExprKind::Loop(_, _, source, _) => {
|
2019-11-16 01:08:30 -06:00
|
|
|
self.const_check_violated(NonConstExpr::Loop(*source), e.span);
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
2019-11-16 01:08:30 -06:00
|
|
|
hir::ExprKind::Match(_, _, source) => {
|
|
|
|
let non_const_expr = match source {
|
2019-11-06 13:44:56 -06:00
|
|
|
// These are handled by `ExprKind::Loop` above.
|
2019-12-22 16:42:04 -06:00
|
|
|
hir::MatchSource::WhileDesugar
|
2019-11-16 01:08:30 -06:00
|
|
|
| hir::MatchSource::WhileLetDesugar
|
2019-12-22 16:42:04 -06:00
|
|
|
| hir::MatchSource::ForLoopDesugar => None,
|
2019-11-16 01:08:30 -06:00
|
|
|
|
|
|
|
_ => Some(NonConstExpr::Match(*source)),
|
2019-11-06 13:44:56 -06:00
|
|
|
};
|
|
|
|
|
2019-11-16 01:08:30 -06:00
|
|
|
if let Some(expr) = non_const_expr {
|
|
|
|
self.const_check_violated(expr, e.span);
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => {}
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
|
2020-01-04 19:37:57 -06:00
|
|
|
intravisit::walk_expr(self, e);
|
2019-11-06 13:44:56 -06:00
|
|
|
}
|
|
|
|
}
|