Skip to content

Commit 9ac8b36

Browse files
committed
don't point at const usage site for resolution-time errors
also share the code that emits the actual error
1 parent 89ac57d commit 9ac8b36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+133
-171
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use rustc_ast::InlineAsmOptions;
44
use rustc_index::IndexVec;
5-
use rustc_middle::mir::interpret::ErrorHandled;
65
use rustc_middle::ty::adjustment::PointerCoercion;
76
use rustc_middle::ty::layout::FnAbiOf;
87
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -251,21 +250,15 @@ pub(crate) fn verify_func(
251250
}
252251

253252
fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
254-
match fx.mir.post_mono_checks(fx.tcx, ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c))) {
255-
Ok(()) => {}
256-
Err(ErrorHandled::TooGeneric(span)) => {
257-
span_bug!(span, "codegen encountered polymorphic constant");
258-
}
259-
Err(ErrorHandled::Reported(info, span)) => {
260-
if !info.is_tainted_by_errors() {
261-
fx.tcx.sess.span_err(span, "erroneous constant encountered");
262-
}
263-
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
264-
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
265-
// compilation should have been aborted
266-
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
267-
return;
268-
}
253+
if let Err(err) =
254+
fx.mir.post_mono_checks(fx.tcx, ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c)))
255+
{
256+
err.emit_err(fx.tcx);
257+
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
258+
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
259+
// compilation should have been aborted
260+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
261+
return;
269262
}
270263

271264
let arg_uninhabited = fx

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$e
1919
2020
codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error}
2121
22-
codegen_ssa_erroneous_constant = erroneous constant encountered
23-
2422
codegen_ssa_error_creating_remark_dir = failed to create remark directory: {$error}
2523
2624
codegen_ssa_expected_coverage_symbol = expected `coverage(off)` or `coverage(on)`
@@ -174,8 +172,6 @@ codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error}
174172
175173
codegen_ssa_option_gcc_only = option `-Z gcc-ld` is used even though linker flavor is not gcc
176174
177-
codegen_ssa_polymorphic_constant_too_generic = codegen encountered polymorphic constant: TooGeneric
178-
179175
codegen_ssa_processing_dymutil_failed = processing debug info with `dsymutil` failed: {$status}
180176
.note = {$output}
181177

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -595,20 +595,6 @@ pub struct InvalidWindowsSubsystem {
595595
pub subsystem: Symbol,
596596
}
597597

598-
#[derive(Diagnostic)]
599-
#[diag(codegen_ssa_erroneous_constant)]
600-
pub struct ErroneousConstant {
601-
#[primary_span]
602-
pub span: Span,
603-
}
604-
605-
#[derive(Diagnostic)]
606-
#[diag(codegen_ssa_polymorphic_constant_too_generic)]
607-
pub struct PolymorphicConstantTooGeneric {
608-
#[primary_span]
609-
pub span: Span,
610-
}
611-
612598
#[derive(Diagnostic)]
613599
#[diag(codegen_ssa_shuffle_indices_evaluation)]
614600
pub struct ShuffleIndicesEvaluation {

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use crate::base;
2-
use crate::errors;
32
use crate::traits::*;
43
use rustc_index::bit_set::BitSet;
54
use rustc_index::IndexVec;
65
use rustc_middle::mir;
7-
use rustc_middle::mir::interpret::ErrorHandled;
86
use rustc_middle::mir::traversal;
97
use rustc_middle::mir::UnwindTerminateReason;
108
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout};
@@ -214,20 +212,14 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
214212
fx.per_local_var_debug_info = fx.compute_per_local_var_debug_info(&mut start_bx);
215213

216214
// Rust post-monomorphization checks; we later rely on them.
217-
match mir.post_mono_checks(cx.tcx(), ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c))) {
218-
Ok(()) => {}
219-
Err(ErrorHandled::TooGeneric(span)) => {
220-
cx.tcx().sess.diagnostic().emit_bug(errors::PolymorphicConstantTooGeneric { span });
221-
}
222-
Err(ErrorHandled::Reported(info, span)) => {
223-
if !info.is_tainted_by_errors() {
224-
cx.tcx().sess.emit_err(errors::ErroneousConstant { span });
225-
}
226-
// This IR shouldn't ever be emitted, but let's try to guard against any of this code
227-
// ever running.
228-
start_bx.abort();
229-
return;
230-
}
215+
if let Err(err) =
216+
mir.post_mono_checks(cx.tcx(), ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c)))
217+
{
218+
err.emit_err(cx.tcx());
219+
// This IR shouldn't ever be emitted, but let's try to guard against any of this code
220+
// ever running.
221+
start_bx.abort();
222+
return;
231223
}
232224

233225
let memory_locals = analyze::non_ssa_locals(&fx);

compiler/rustc_const_eval/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ const_eval_dyn_call_vtable_mismatch =
8383
const_eval_dyn_star_call_vtable_mismatch =
8484
`dyn*` call on a pointer whose vtable does not match its type
8585
86-
const_eval_erroneous_constant =
87-
erroneous constant used
88-
8986
const_eval_error = {$error_kind ->
9087
[static] could not evaluate static initializer
9188
[const] evaluation of constant value failed

compiler/rustc_const_eval/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,6 @@ pub struct LongRunningWarn {
239239
pub item_span: Span,
240240
}
241241

242-
#[derive(Diagnostic)]
243-
#[diag(const_eval_erroneous_constant)]
244-
pub(crate) struct ErroneousConstUsed {
245-
#[primary_span]
246-
pub span: Span,
247-
}
248-
249242
#[derive(Subdiagnostic)]
250243
#[note(const_eval_non_const_impl)]
251244
pub(crate) struct NonConstImplNote {

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use super::{
2424
MemPlaceMeta, Memory, MemoryKind, OpTy, Operand, Place, PlaceTy, Pointer, PointerArithmetic,
2525
Projectable, Provenance, Scalar, StackPopJump,
2626
};
27-
use crate::errors::{self, ErroneousConstUsed};
27+
use crate::errors;
2828
use crate::util;
2929
use crate::{fluent_generated as fluent, ReportErrorExt};
3030

@@ -1063,17 +1063,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10631063
) -> Result<T, ErrorHandled> {
10641064
// Use a precise span for better cycle errors.
10651065
query(self.tcx.at(span.unwrap_or_else(|| self.cur_span()))).map_err(|err| {
1066-
match err {
1067-
ErrorHandled::Reported(err, reported_span) => {
1068-
// We trust the provided span more than the one that came out of the query.
1069-
let span = span.unwrap_or(reported_span);
1070-
if !err.is_tainted_by_errors() {
1071-
// To make it easier to figure out where this error comes from, also add a note at the current location.
1072-
self.tcx.sess.emit_note(ErroneousConstUsed { span });
1073-
}
1074-
}
1075-
ErrorHandled::TooGeneric(_) => {}
1076-
}
1066+
err.emit_note(*self.tcx);
10771067
err
10781068
})
10791069
}

compiler/rustc_middle/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ middle_drop_check_overflow =
5252
overflow while adding drop-check rules for {$ty}
5353
.note = overflowed on {$overflow_ty}
5454
55+
middle_erroneous_constant = erroneous constant encountered
56+
5557
middle_layout_references_error =
5658
the type has an unknown layout
5759

compiler/rustc_middle/src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,12 @@ pub struct UnsupportedFnAbi {
144144
pub abi: &'static str,
145145
}
146146

147+
#[derive(Diagnostic)]
148+
#[diag(middle_erroneous_constant)]
149+
pub struct ErroneousConstant {
150+
#[primary_span]
151+
pub span: Span,
152+
}
153+
147154
/// Used by `rustc_const_eval`
148155
pub use crate::fluent_generated::middle_adjust_for_foreign_abi_error;

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use super::{AllocId, AllocRange, ConstAlloc, Pointer, Scalar};
22

3+
use crate::error;
34
use crate::mir::interpret::ConstValue;
45
use crate::query::TyCtxtAt;
5-
use crate::ty::{layout, tls, Ty, ValTree};
6+
use crate::ty::{layout, tls, Ty, TyCtxt, ValTree};
67

78
use rustc_data_structures::sync::Lock;
89
use rustc_errors::{
@@ -41,6 +42,32 @@ impl ErrorHandled {
4142
ErrorHandled::TooGeneric(_span) => ErrorHandled::TooGeneric(span),
4243
}
4344
}
45+
46+
pub fn emit_err(&self, tcx: TyCtxt<'_>) -> ErrorGuaranteed {
47+
match self {
48+
&ErrorHandled::Reported(err, span) => {
49+
if !err.is_tainted_by_errors && !span.is_dummy() {
50+
tcx.sess.emit_err(error::ErroneousConstant { span });
51+
}
52+
err.error
53+
}
54+
&ErrorHandled::TooGeneric(span) => tcx.sess.delay_span_bug(
55+
span,
56+
"encountered TooGeneric error when monomorphic data was expected",
57+
),
58+
}
59+
}
60+
61+
pub fn emit_note(&self, tcx: TyCtxt<'_>) {
62+
match self {
63+
&ErrorHandled::Reported(err, span) => {
64+
if !err.is_tainted_by_errors && !span.is_dummy() {
65+
tcx.sess.emit_note(error::ErroneousConstant { span });
66+
}
67+
}
68+
&ErrorHandled::TooGeneric(_) => {}
69+
}
70+
}
4471
}
4572

4673
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
@@ -54,12 +81,6 @@ impl ReportedErrorInfo {
5481
pub fn tainted_by_errors(error: ErrorGuaranteed) -> ReportedErrorInfo {
5582
ReportedErrorInfo { is_tainted_by_errors: true, error }
5683
}
57-
58-
/// Returns true if evaluation failed because MIR was tainted by errors.
59-
#[inline]
60-
pub fn is_tainted_by_errors(self) -> bool {
61-
self.is_tainted_by_errors
62-
}
6384
}
6485

6586
impl From<ErrorGuaranteed> for ReportedErrorInfo {

0 commit comments

Comments
 (0)