Skip to content

Commit efdd9e8

Browse files
committed
Auto merge of rust-lang#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 rust-lang#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 rust-lang#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`
2 parents 41cbe3e + 01ff36a commit efdd9e8

38 files changed

+121
-574
lines changed

compiler/rustc_const_eval/messages.ftl

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const_eval_extern_type_field = `extern type` field does not have a known offset
110110
const_eval_fn_ptr_call =
111111
function pointers need an RFC before allowed to be called in {const_eval_const_context}s
112112
const_eval_for_loop_into_iter_non_const =
113-
cannot convert `{$ty}` into an iterator in {const_eval_const_context}s
113+
cannot use `for` loop on `{$ty}` in {const_eval_const_context}s
114114
115115
const_eval_frame_note = {$times ->
116116
[0] {const_eval_frame_note_inner}
@@ -324,11 +324,11 @@ const_eval_ptr_as_bytes_1 =
324324
this code performed an operation that depends on the underlying bytes representing a pointer
325325
const_eval_ptr_as_bytes_2 =
326326
the absolute address of a pointer is not known at compile-time, so such operations are not supported
327-
const_eval_question_branch_non_const =
328-
`?` cannot determine the branch of `{$ty}` in {const_eval_const_context}s
329327
328+
const_eval_question_branch_non_const =
329+
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
330330
const_eval_question_from_residual_non_const =
331-
`?` cannot convert from residual of `{$ty}` in {const_eval_const_context}s
331+
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
332332
333333
const_eval_range = in the range {$lo}..={$hi}
334334
const_eval_range_lower = greater or equal to {$lo}

compiler/rustc_const_eval/src/check_consts/ops.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
180180
};
181181
}
182182

183-
let mut err = match kind {
184-
CallDesugaringKind::ForLoopIntoIter => {
183+
// Don't point at the trait if this is a desugaring...
184+
// FIXME(const_trait_impl): we could perhaps do this for `Iterator`.
185+
match kind {
186+
CallDesugaringKind::ForLoopIntoIter | CallDesugaringKind::ForLoopNext => {
185187
error!(NonConstForLoopIntoIter)
186188
}
187189
CallDesugaringKind::QuestionBranch => {
@@ -196,10 +198,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
196198
CallDesugaringKind::Await => {
197199
error!(NonConstAwait)
198200
}
199-
};
200-
201-
diag_trait(&mut err, self_ty, kind.trait_def_id(tcx));
202-
err
201+
}
203202
}
204203
CallKind::FnCall { fn_trait_id, self_ty } => {
205204
let note = match self_ty.kind() {

compiler/rustc_interface/src/passes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
830830
tcx.ensure().check_mod_attrs(module);
831831
tcx.ensure().check_mod_naked_functions(module);
832832
tcx.ensure().check_mod_unstable_api_usage(module);
833-
tcx.ensure().check_mod_const_bodies(module);
834833
});
835834
},
836835
{

compiler/rustc_middle/src/query/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,6 @@ rustc_queries! {
958958
desc { |tcx| "checking for unstable API usage in {}", describe_as_module(key, tcx) }
959959
}
960960

961-
/// Checks the const bodies in the module for illegal operations (e.g. `if` or `loop`).
962-
query check_mod_const_bodies(key: LocalModDefId) {
963-
desc { |tcx| "checking consts in {}", describe_as_module(key, tcx) }
964-
}
965-
966961
/// Checks the loops in the module.
967962
query check_mod_loops(key: LocalModDefId) {
968963
desc { |tcx| "checking loops in {}", describe_as_module(key, tcx) }

compiler/rustc_middle/src/util/call_kind.rs

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::ty::{AssocItemContainer, GenericArgsRef, Instance, Ty, TyCtxt, Typing
1414
pub enum CallDesugaringKind {
1515
/// for _ in x {} calls x.into_iter()
1616
ForLoopIntoIter,
17+
/// for _ in x {} calls iter.next()
18+
ForLoopNext,
1719
/// x? calls x.branch()
1820
QuestionBranch,
1921
/// x? calls type_of(x)::from_residual()
@@ -28,6 +30,7 @@ impl CallDesugaringKind {
2830
pub fn trait_def_id(self, tcx: TyCtxt<'_>) -> DefId {
2931
match self {
3032
Self::ForLoopIntoIter => tcx.get_diagnostic_item(sym::IntoIterator).unwrap(),
33+
Self::ForLoopNext => tcx.require_lang_item(LangItem::Iterator, None),
3134
Self::QuestionBranch | Self::TryBlockFromOutput => {
3235
tcx.require_lang_item(LangItem::Try, None)
3336
}
@@ -121,6 +124,10 @@ pub fn call_kind<'tcx>(
121124
&& fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop)
122125
{
123126
Some((CallDesugaringKind::ForLoopIntoIter, method_args.type_at(0)))
127+
} else if tcx.is_lang_item(method_did, LangItem::IteratorNext)
128+
&& fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop)
129+
{
130+
Some((CallDesugaringKind::ForLoopNext, method_args.type_at(0)))
124131
} else if fn_call_span.desugaring_kind() == Some(DesugaringKind::QuestionMark) {
125132
if tcx.is_lang_item(method_did, LangItem::TryTraitBranch) {
126133
Some((CallDesugaringKind::QuestionBranch, method_args.type_at(0)))

compiler/rustc_passes/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,6 @@ passes_should_be_applied_to_trait =
709709
attribute should be applied to a trait
710710
.label = not a trait
711711
712-
passes_skipping_const_checks = skipping const checks
713-
714712
passes_stability_promotable =
715713
attribute cannot be applied to an expression
716714

compiler/rustc_passes/src/check_const.rs

-236
This file was deleted.

compiler/rustc_passes/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1670,13 +1670,6 @@ pub(crate) struct ProcMacroBadSig {
16701670
pub kind: ProcMacroKind,
16711671
}
16721672

1673-
#[derive(Diagnostic)]
1674-
#[diag(passes_skipping_const_checks)]
1675-
pub(crate) struct SkippingConstChecks {
1676-
#[primary_span]
1677-
pub span: Span,
1678-
}
1679-
16801673
#[derive(LintDiagnostic)]
16811674
#[diag(passes_unreachable_due_to_uninhabited)]
16821675
pub(crate) struct UnreachableDueToUninhabited<'desc, 'tcx> {

compiler/rustc_passes/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_middle::query::Providers;
1919

2020
pub mod abi_test;
2121
mod check_attr;
22-
mod check_const;
2322
pub mod dead;
2423
mod debugger_visualizer;
2524
mod diagnostic_items;
@@ -43,7 +42,6 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
4342

4443
pub fn provide(providers: &mut Providers) {
4544
check_attr::provide(providers);
46-
check_const::provide(providers);
4745
dead::provide(providers);
4846
debugger_visualizer::provide(providers);
4947
diagnostic_items::provide(providers);

0 commit comments

Comments
 (0)