about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-12-03 04:39:48 +0000
committerbors <bors@rust-lang.org>2024-12-03 04:39:48 +0000
commitefdd9e802053caeb52103945df858e87f837e59a (patch)
treeba16a8a78b5db2b43298849d14d711b20cc444b9 /compiler
parent41cbe3e4d1ef8de310fbbd77043082960def9446 (diff)
parent01ff36a6b9e9134421c4e353ccbc904fdfe53be6 (diff)
downloadrust-efdd9e802053caeb52103945df858e87f837e59a.tar.gz
rust-efdd9e802053caeb52103945df858e87f837e59a.zip
Auto merge of #133321 - compiler-errors:const-checker, r=wesleywiser
Get rid of HIR const checker

As far as I can tell, the HIR const checker was implemented in https://github.com/rust-lang/rust/pull/66170 because we were not able to issue useful const error messages in the MIR const checker.

This seems to have changed in the last 5 years, probably due to work like #90532. I've tweaked the diagnostics slightly and think the error messages have gotten *better* in fact.

Thus I think the HIR const checker has reached the end of its usefulness, and we can retire it.

cc `@RalfJung`
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_const_eval/messages.ftl8
-rw-r--r--compiler/rustc_const_eval/src/check_consts/ops.rs11
-rw-r--r--compiler/rustc_interface/src/passes.rs1
-rw-r--r--compiler/rustc_middle/src/query/mod.rs5
-rw-r--r--compiler/rustc_middle/src/util/call_kind.rs7
-rw-r--r--compiler/rustc_passes/messages.ftl2
-rw-r--r--compiler/rustc_passes/src/check_const.rs236
-rw-r--r--compiler/rustc_passes/src/errors.rs7
-rw-r--r--compiler/rustc_passes/src/lib.rs2
9 files changed, 16 insertions, 263 deletions
diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl
index f93f4d36e45..c31c94495d0 100644
--- a/compiler/rustc_const_eval/messages.ftl
+++ b/compiler/rustc_const_eval/messages.ftl
@@ -110,7 +110,7 @@ const_eval_extern_type_field = `extern type` field does not have a known offset
 const_eval_fn_ptr_call =
     function pointers need an RFC before allowed to be called in {const_eval_const_context}s
 const_eval_for_loop_into_iter_non_const =
-    cannot convert `{$ty}` into an iterator in {const_eval_const_context}s
+    cannot use `for` loop on `{$ty}` in {const_eval_const_context}s
 
 const_eval_frame_note = {$times ->
     [0] {const_eval_frame_note_inner}
@@ -324,11 +324,11 @@ const_eval_ptr_as_bytes_1 =
     this code performed an operation that depends on the underlying bytes representing a pointer
 const_eval_ptr_as_bytes_2 =
     the absolute address of a pointer is not known at compile-time, so such operations are not supported
-const_eval_question_branch_non_const =
-    `?` cannot determine the branch of `{$ty}` in {const_eval_const_context}s
 
+const_eval_question_branch_non_const =
+    `?` is not allowed on `{$ty}` in {const_eval_const_context}s
 const_eval_question_from_residual_non_const =
-    `?` cannot convert from residual of `{$ty}` in {const_eval_const_context}s
+    `?` is not allowed on `{$ty}` in {const_eval_const_context}s
 
 const_eval_range = in the range {$lo}..={$hi}
 const_eval_range_lower = greater or equal to {$lo}
diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs
index ab81e60a33f..489bb54a6f9 100644
--- a/compiler/rustc_const_eval/src/check_consts/ops.rs
+++ b/compiler/rustc_const_eval/src/check_consts/ops.rs
@@ -180,8 +180,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
                     };
                 }
 
-                let mut err = match kind {
-                    CallDesugaringKind::ForLoopIntoIter => {
+                // Don't point at the trait if this is a desugaring...
+                // FIXME(const_trait_impl): we could perhaps do this for `Iterator`.
+                match kind {
+                    CallDesugaringKind::ForLoopIntoIter | CallDesugaringKind::ForLoopNext => {
                         error!(NonConstForLoopIntoIter)
                     }
                     CallDesugaringKind::QuestionBranch => {
@@ -196,10 +198,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
                     CallDesugaringKind::Await => {
                         error!(NonConstAwait)
                     }
-                };
-
-                diag_trait(&mut err, self_ty, kind.trait_def_id(tcx));
-                err
+                }
             }
             CallKind::FnCall { fn_trait_id, self_ty } => {
                 let note = match self_ty.kind() {
diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs
index 2905fe688b5..06e27ed8060 100644
--- a/compiler/rustc_interface/src/passes.rs
+++ b/compiler/rustc_interface/src/passes.rs
@@ -830,7 +830,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
                     tcx.ensure().check_mod_attrs(module);
                     tcx.ensure().check_mod_naked_functions(module);
                     tcx.ensure().check_mod_unstable_api_usage(module);
-                    tcx.ensure().check_mod_const_bodies(module);
                 });
             },
             {
diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs
index a17aa9ecc04..dfa62d875b3 100644
--- a/compiler/rustc_middle/src/query/mod.rs
+++ b/compiler/rustc_middle/src/query/mod.rs
@@ -958,11 +958,6 @@ rustc_queries! {
         desc { |tcx| "checking for unstable API usage in {}", describe_as_module(key, tcx) }
     }
 
-    /// Checks the const bodies in the module for illegal operations (e.g. `if` or `loop`).
-    query check_mod_const_bodies(key: LocalModDefId) {
-        desc { |tcx| "checking consts in {}", describe_as_module(key, tcx) }
-    }
-
     /// Checks the loops in the module.
     query check_mod_loops(key: LocalModDefId) {
         desc { |tcx| "checking loops in {}", describe_as_module(key, tcx) }
diff --git a/compiler/rustc_middle/src/util/call_kind.rs b/compiler/rustc_middle/src/util/call_kind.rs
index acfb78b3f6e..df5b73ac1bd 100644
--- a/compiler/rustc_middle/src/util/call_kind.rs
+++ b/compiler/rustc_middle/src/util/call_kind.rs
@@ -14,6 +14,8 @@ use crate::ty::{AssocItemContainer, GenericArgsRef, Instance, Ty, TyCtxt, Typing
 pub enum CallDesugaringKind {
     /// for _ in x {} calls x.into_iter()
     ForLoopIntoIter,
+    /// for _ in x {} calls iter.next()
+    ForLoopNext,
     /// x? calls x.branch()
     QuestionBranch,
     /// x? calls type_of(x)::from_residual()
@@ -28,6 +30,7 @@ impl CallDesugaringKind {
     pub fn trait_def_id(self, tcx: TyCtxt<'_>) -> DefId {
         match self {
             Self::ForLoopIntoIter => tcx.get_diagnostic_item(sym::IntoIterator).unwrap(),
+            Self::ForLoopNext => tcx.require_lang_item(LangItem::Iterator, None),
             Self::QuestionBranch | Self::TryBlockFromOutput => {
                 tcx.require_lang_item(LangItem::Try, None)
             }
@@ -121,6 +124,10 @@ pub fn call_kind<'tcx>(
             && fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop)
         {
             Some((CallDesugaringKind::ForLoopIntoIter, method_args.type_at(0)))
+        } else if tcx.is_lang_item(method_did, LangItem::IteratorNext)
+            && fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop)
+        {
+            Some((CallDesugaringKind::ForLoopNext, method_args.type_at(0)))
         } else if fn_call_span.desugaring_kind() == Some(DesugaringKind::QuestionMark) {
             if tcx.is_lang_item(method_did, LangItem::TryTraitBranch) {
                 Some((CallDesugaringKind::QuestionBranch, method_args.type_at(0)))
diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl
index 0712af422ad..7a0a518bb51 100644
--- a/compiler/rustc_passes/messages.ftl
+++ b/compiler/rustc_passes/messages.ftl
@@ -709,8 +709,6 @@ passes_should_be_applied_to_trait =
     attribute should be applied to a trait
     .label = not a trait
 
-passes_skipping_const_checks = skipping const checks
-
 passes_stability_promotable =
     attribute cannot be applied to an expression
 
diff --git a/compiler/rustc_passes/src/check_const.rs b/compiler/rustc_passes/src/check_const.rs
deleted file mode 100644
index f5ece513956..00000000000
--- a/compiler/rustc_passes/src/check_const.rs
+++ /dev/null
@@ -1,236 +0,0 @@
-//! 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.
-//!
-//! 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.
-
-use rustc_hir::def_id::{LocalDefId, LocalModDefId};
-use rustc_hir::intravisit::{self, Visitor};
-use rustc_middle::hir::nested_filter;
-use rustc_middle::query::Providers;
-use rustc_middle::span_bug;
-use rustc_middle::ty::TyCtxt;
-use rustc_session::parse::feature_err;
-use rustc_span::{Span, Symbol, sym};
-use {rustc_attr as attr, rustc_hir as hir};
-
-use crate::errors::SkippingConstChecks;
-
-/// An expression that is not *always* legal in a const context.
-#[derive(Clone, Copy)]
-enum NonConstExpr {
-    Loop(hir::LoopSource),
-    Match(hir::MatchSource),
-}
-
-impl NonConstExpr {
-    fn name(self) -> String {
-        match self {
-            Self::Loop(src) => format!("`{}`", src.name()),
-            Self::Match(src) => format!("`{}`", src.name()),
-        }
-    }
-
-    fn required_feature_gates(self) -> Option<&'static [Symbol]> {
-        use hir::LoopSource::*;
-        use hir::MatchSource::*;
-
-        let gates: &[_] = match self {
-            Self::Match(AwaitDesugar) => {
-                return None;
-            }
-
-            Self::Loop(ForLoop) | Self::Match(ForLoopDesugar) => &[sym::const_for],
-
-            Self::Match(TryDesugar(_)) => &[sym::const_try],
-
-            // All other expressions are allowed.
-            Self::Loop(Loop | While) | Self::Match(Normal | Postfix | FormatArgs) => &[],
-        };
-
-        Some(gates)
-    }
-}
-
-fn check_mod_const_bodies(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
-    let mut vis = CheckConstVisitor::new(tcx);
-    tcx.hir().visit_item_likes_in_module(module_def_id, &mut vis);
-}
-
-pub(crate) fn provide(providers: &mut Providers) {
-    *providers = Providers { check_mod_const_bodies, ..*providers };
-}
-
-#[derive(Copy, Clone)]
-struct CheckConstVisitor<'tcx> {
-    tcx: TyCtxt<'tcx>,
-    const_kind: Option<hir::ConstContext>,
-    def_id: Option<LocalDefId>,
-}
-
-impl<'tcx> CheckConstVisitor<'tcx> {
-    fn new(tcx: TyCtxt<'tcx>) -> Self {
-        CheckConstVisitor { tcx, const_kind: None, def_id: None }
-    }
-
-    /// Emits an error when an unsupported expression is found in a const context.
-    #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
-    fn const_check_violated(&self, expr: NonConstExpr, span: Span) {
-        let Self { tcx, def_id, const_kind } = *self;
-
-        let features = tcx.features();
-        let required_gates = expr.required_feature_gates();
-
-        let is_feature_allowed = |feature_gate| {
-            // All features require that the corresponding gate be enabled,
-            // even if the function has `#[rustc_allow_const_fn_unstable(the_gate)]`.
-            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 {
-                Some(x) => x,
-                None => return true,
-            };
-
-            // If the function belongs to a trait, then it must enable the const_trait_impl
-            // feature to use that trait function (with a const default body).
-            if tcx.trait_of_item(def_id.to_def_id()).is_some() {
-                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
-            // opt-in via `rustc_allow_const_fn_unstable`.
-            let attrs = tcx.hir().attrs(tcx.local_def_id_to_hir_id(def_id));
-            attr::rustc_allow_const_fn_unstable(tcx.sess, attrs).any(|name| name == feature_gate)
-        };
-
-        match required_gates {
-            // Don't emit an error if the user has enabled the requisite feature gates.
-            Some(gates) if gates.iter().copied().all(is_feature_allowed) => return,
-
-            // `-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.
-            None if tcx.sess.opts.unstable_opts.unleash_the_miri_inside_of_you => {
-                tcx.dcx().emit_warn(SkippingConstChecks { span });
-                return;
-            }
-
-            _ => {}
-        }
-
-        let const_kind =
-            const_kind.expect("`const_check_violated` may only be called inside a const context");
-
-        let required_gates = required_gates.unwrap_or(&[]);
-        let missing_gates: Vec<_> =
-            required_gates.iter().copied().filter(|&g| !features.enabled(g)).collect();
-
-        match missing_gates.as_slice() {
-            [] => {
-                span_bug!(
-                    span,
-                    "we should not have reached this point, since `.await` is denied earlier"
-                );
-            }
-
-            [missing_primary, ref missing_secondary @ ..] => {
-                let msg =
-                    format!("{} is not allowed in a `{}`", expr.name(), const_kind.keyword_name());
-                let mut err = feature_err(&tcx.sess, *missing_primary, span, msg);
-
-                // 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.
-                tcx.disabled_nightly_features(
-                    &mut err,
-                    def_id.map(|id| tcx.local_def_id_to_hir_id(id)),
-                    missing_secondary.into_iter().map(|gate| (String::new(), *gate)),
-                );
-
-                err.emit();
-            }
-        }
-    }
-
-    /// Saves the parent `const_kind` before calling `f` and restores it afterwards.
-    fn recurse_into(
-        &mut self,
-        kind: Option<hir::ConstContext>,
-        def_id: Option<LocalDefId>,
-        f: impl FnOnce(&mut Self),
-    ) {
-        let parent_def_id = self.def_id;
-        let parent_kind = self.const_kind;
-        self.def_id = def_id;
-        self.const_kind = kind;
-        f(self);
-        self.def_id = parent_def_id;
-        self.const_kind = parent_kind;
-    }
-}
-
-impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
-    type NestedFilter = nested_filter::OnlyBodies;
-
-    fn nested_visit_map(&mut self) -> Self::Map {
-        self.tcx.hir()
-    }
-
-    fn visit_anon_const(&mut self, anon: &'tcx hir::AnonConst) {
-        let kind = Some(hir::ConstContext::Const { inline: false });
-        self.recurse_into(kind, None, |this| intravisit::walk_anon_const(this, anon));
-    }
-
-    fn visit_inline_const(&mut self, block: &'tcx hir::ConstBlock) {
-        let kind = Some(hir::ConstContext::Const { inline: true });
-        self.recurse_into(kind, None, |this| intravisit::walk_inline_const(this, block));
-    }
-
-    fn visit_body(&mut self, body: &hir::Body<'tcx>) {
-        let owner = self.tcx.hir().body_owner_def_id(body.id());
-        let kind = self.tcx.hir().body_const_context(owner);
-        self.recurse_into(kind, Some(owner), |this| intravisit::walk_body(this, body));
-    }
-
-    fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
-        match &e.kind {
-            // Skip the following checks if we are not currently in a const context.
-            _ if self.const_kind.is_none() => {}
-
-            hir::ExprKind::Loop(_, _, source, _) => {
-                self.const_check_violated(NonConstExpr::Loop(*source), e.span);
-            }
-
-            hir::ExprKind::Match(_, _, source) => {
-                let non_const_expr = match source {
-                    // These are handled by `ExprKind::Loop` above.
-                    hir::MatchSource::ForLoopDesugar => None,
-
-                    _ => Some(NonConstExpr::Match(*source)),
-                };
-
-                if let Some(expr) = non_const_expr {
-                    self.const_check_violated(expr, e.span);
-                }
-            }
-
-            _ => {}
-        }
-
-        intravisit::walk_expr(self, e);
-    }
-}
diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs
index fbf25cb476a..fdc7e1bba2f 100644
--- a/compiler/rustc_passes/src/errors.rs
+++ b/compiler/rustc_passes/src/errors.rs
@@ -1670,13 +1670,6 @@ pub(crate) struct ProcMacroBadSig {
     pub kind: ProcMacroKind,
 }
 
-#[derive(Diagnostic)]
-#[diag(passes_skipping_const_checks)]
-pub(crate) struct SkippingConstChecks {
-    #[primary_span]
-    pub span: Span,
-}
-
 #[derive(LintDiagnostic)]
 #[diag(passes_unreachable_due_to_uninhabited)]
 pub(crate) struct UnreachableDueToUninhabited<'desc, 'tcx> {
diff --git a/compiler/rustc_passes/src/lib.rs b/compiler/rustc_passes/src/lib.rs
index 8f53b71319d..1aa077ad2bb 100644
--- a/compiler/rustc_passes/src/lib.rs
+++ b/compiler/rustc_passes/src/lib.rs
@@ -19,7 +19,6 @@ use rustc_middle::query::Providers;
 
 pub mod abi_test;
 mod check_attr;
-mod check_const;
 pub mod dead;
 mod debugger_visualizer;
 mod diagnostic_items;
@@ -43,7 +42,6 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
 
 pub fn provide(providers: &mut Providers) {
     check_attr::provide(providers);
-    check_const::provide(providers);
     dead::provide(providers);
     debugger_visualizer::provide(providers);
     diagnostic_items::provide(providers);