Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify conditional-const error reporting with non-const error reporting #134732

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the non-const part swappable in the diagnostic
  • Loading branch information
compiler-errors committed Jan 9, 2025
commit 5a9f0be0bd7ae663a3084da90eb89dcc5721ec15
59 changes: 32 additions & 27 deletions compiler/rustc_const_eval/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const_eval_already_reported =
const_eval_assume_false =
`assume` called with `false`

const_eval_await_non_const =
cannot convert `{$ty}` into a future in {const_eval_const_context}s
const_eval_bounds_check_failed =
indexing out of bounds: the len is {$len} but the index is {$index}
const_eval_call_nonzero_intrinsic =
Expand All @@ -23,9 +21,6 @@ const_eval_closure_call =
closures need an RFC before allowed to be called in {const_eval_const_context}s
const_eval_closure_fndef_not_const =
function defined here, but it is not `const`
const_eval_closure_non_const =
cannot call non-const closure in {const_eval_const_context}s

const_eval_conditionally_const_call =
cannot call conditionally-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s

Expand Down Expand Up @@ -62,10 +57,6 @@ const_eval_dealloc_incorrect_layout =
const_eval_dealloc_kind_mismatch =
deallocating {$alloc}, which is {$alloc_kind} memory, using {$kind} deallocation operation

const_eval_deref_coercion_non_const =
cannot perform deref coercion on `{$ty}` in {const_eval_const_context}s
.note = attempting to deref into `{$target_ty}`
.target_note = deref defined here
const_eval_deref_function_pointer =
accessing {$allocation} which contains a function
const_eval_deref_vtable_pointer =
Expand Down Expand Up @@ -109,9 +100,6 @@ 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 use `for` loop on `{$ty}` in {const_eval_const_context}s

const_eval_frame_note = {$times ->
[0] {const_eval_frame_note_inner}
*[other] [... {$times} additional calls {const_eval_frame_note_inner} ...]
Expand Down Expand Up @@ -216,9 +204,6 @@ const_eval_long_running =
.label = the const evaluator is currently interpreting this expression
.help = the constant being evaluated

const_eval_match_eq_non_const = cannot match on `{$ty}` in {const_eval_const_context}s
.note = `{$ty}` cannot be compared in compile-time, and therefore cannot be used in `match`es

const_eval_max_num_nodes_in_const = maximum number of nodes exceeded in constant {$global_const_id}

const_eval_memory_access_test = memory access failed
Expand Down Expand Up @@ -249,17 +234,46 @@ const_eval_mutable_ref_escaping =
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.

const_eval_nested_static_in_thread_local = #[thread_local] does not support implicit nested statics, please create explicit static items and refer to them instead

const_eval_non_const_await =
cannot convert `{$ty}` into a future in {const_eval_const_context}s

const_eval_non_const_closure =
cannot call {$non}-const closure in {const_eval_const_context}s

const_eval_non_const_deref_coercion =
cannot perform {$non}-const deref coercion on `{$ty}` in {const_eval_const_context}s
.note = attempting to deref into `{$target_ty}`
.target_note = deref defined here

const_eval_non_const_fmt_macro_call =
cannot call non-const formatting macro in {const_eval_const_context}s
cannot call {$non}-const formatting macro in {const_eval_const_context}s

const_eval_non_const_fn_call =
cannot call non-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s
cannot call {$non}-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s

const_eval_non_const_for_loop_into_iter =
cannot use `for` loop on `{$ty}` in {const_eval_const_context}s

const_eval_non_const_impl =
impl defined here, but it is not `const`

const_eval_non_const_intrinsic =
cannot call non-const intrinsic `{$name}` in {const_eval_const_context}s
cannot call {$non}-const intrinsic `{$name}` in {const_eval_const_context}s

const_eval_non_const_match_eq = cannot match on `{$ty}` in {const_eval_const_context}s
.note = `{$ty}` cannot be compared in compile-time, and therefore cannot be used in `match`es

const_eval_non_const_operator =
cannot call {$non}-const operator in {const_eval_const_context}s

const_eval_non_const_question_branch =
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
const_eval_non_const_question_from_residual =
`?` is not allowed on `{$ty}` in {const_eval_const_context}s

const_eval_non_const_try_block_from_output =
`try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s

const_eval_not_enough_caller_args =
calling a function with fewer arguments than it requires
Expand All @@ -281,8 +295,6 @@ const_eval_offset_from_unsigned_overflow =
*[false] offset
} than second: {$a_offset} < {$b_offset}

const_eval_operator_non_const =
cannot call non-const operator in {const_eval_const_context}s
const_eval_overflow_arith =
arithmetic overflow in `{$intrinsic}`
const_eval_overflow_shift =
Expand Down Expand Up @@ -325,11 +337,6 @@ const_eval_ptr_as_bytes_1 =
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 =
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
const_eval_question_from_residual_non_const =
`?` 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}
const_eval_range_singular = equal to {$lo}
Expand Down Expand Up @@ -379,8 +386,6 @@ const_eval_too_generic =
const_eval_too_many_caller_args =
calling a function with more arguments than it expected

const_eval_try_block_from_output_non_const =
`try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s
const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in {const_eval_const_context}s

const_eval_unallowed_heap_allocations =
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
let ConstCx { tcx, typing_env, .. } = *ccx;
let caller = ccx.def_id();

let diag_trait = |err, self_ty: Ty<'_>, trait_id| {
let note_trait_if_possible = |err, self_ty: Ty<'_>, trait_id| {
let trait_ref = TraitRef::from_method(tcx, trait_id, args);

match self_ty.kind() {
Expand Down Expand Up @@ -183,6 +183,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
ty: self_ty,
kind: ccx.const_kind(),
non: "non",
})
};
}
Expand Down Expand Up @@ -226,9 +227,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
kind: ccx.const_kind(),
note,
non: "non",
});

diag_trait(&mut err, self_ty, fn_trait_id);
note_trait_if_possible(&mut err, self_ty, fn_trait_id);
err
}
CallKind::Operator { trait_id, self_ty, .. } => {
Expand All @@ -237,6 +239,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
kind: ccx.const_kind(),
ty: self_ty,
non: "non",
})
} else {
let mut sugg = None;
Expand Down Expand Up @@ -282,10 +285,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
kind: ccx.const_kind(),
sugg,
non: "non",
})
};

diag_trait(&mut err, self_ty, trait_id);
note_trait_if_possible(&mut err, self_ty, trait_id);
err
}
CallKind::DerefCoercion { deref_target, deref_target_ty, self_ty } => {
Expand All @@ -302,19 +306,21 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
kind: ccx.const_kind(),
target_ty: deref_target_ty,
deref_target: target,
non: "non",
});

diag_trait(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span)));
note_trait_if_possible(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span)));
err
}
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentMethods) => {
ccx.dcx().create_err(errors::NonConstFmtMacroCall { span, kind: ccx.const_kind() })
ccx.dcx().create_err(errors::NonConstFmtMacroCall { span, kind: ccx.const_kind(), non: "non" })
}
_ => ccx.dcx().create_err(errors::NonConstFnCall {
span,
def_descr: ccx.tcx.def_descr(callee),
def_path_str: ccx.tcx.def_path_str_with_args(callee, args),
kind: ccx.const_kind(),
non: "non",
}),
};

Expand Down
29 changes: 20 additions & 9 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub(crate) struct NonConstFmtMacroCall {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
Expand All @@ -194,6 +195,7 @@ pub(crate) struct NonConstFnCall {
pub def_path_str: String,
pub def_descr: &'static str,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
Expand Down Expand Up @@ -293,68 +295,75 @@ pub struct RawBytesNote {
// FIXME(fee1-dead) do not use stringly typed `ConstContext`

#[derive(Diagnostic)]
#[diag(const_eval_match_eq_non_const, code = E0015)]
#[diag(const_eval_non_const_match_eq, code = E0015)]
#[note]
pub struct NonConstMatchEq<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
#[diag(const_eval_for_loop_into_iter_non_const, code = E0015)]
#[diag(const_eval_non_const_for_loop_into_iter, code = E0015)]
pub struct NonConstForLoopIntoIter<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
#[diag(const_eval_question_branch_non_const, code = E0015)]
#[diag(const_eval_non_const_question_branch, code = E0015)]
pub struct NonConstQuestionBranch<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
#[diag(const_eval_question_from_residual_non_const, code = E0015)]
#[diag(const_eval_non_const_question_from_residual, code = E0015)]
pub struct NonConstQuestionFromResidual<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
#[diag(const_eval_try_block_from_output_non_const, code = E0015)]
#[diag(const_eval_non_const_try_block_from_output, code = E0015)]
pub struct NonConstTryBlockFromOutput<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
#[diag(const_eval_await_non_const, code = E0015)]
#[diag(const_eval_non_const_await, code = E0015)]
pub struct NonConstAwait<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}

#[derive(Diagnostic)]
#[diag(const_eval_closure_non_const, code = E0015)]
#[diag(const_eval_non_const_closure, code = E0015)]
pub struct NonConstClosure {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
#[subdiagnostic]
pub note: Option<NonConstClosureNote>,
pub non: &'static str,
}

#[derive(Subdiagnostic)]
Expand All @@ -381,17 +390,18 @@ pub struct ConsiderDereferencing {
}

#[derive(Diagnostic)]
#[diag(const_eval_operator_non_const, code = E0015)]
#[diag(const_eval_non_const_operator, code = E0015)]
pub struct NonConstOperator {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
#[subdiagnostic]
pub sugg: Option<ConsiderDereferencing>,
pub non: &'static str,
}

#[derive(Diagnostic)]
#[diag(const_eval_deref_coercion_non_const, code = E0015)]
#[diag(const_eval_non_const_deref_coercion, code = E0015)]
#[note]
pub struct NonConstDerefCoercion<'tcx> {
#[primary_span]
Expand All @@ -401,6 +411,7 @@ pub struct NonConstDerefCoercion<'tcx> {
pub target_ty: Ty<'tcx>,
#[note(const_eval_target_note)]
pub deref_target: Option<Span>,
pub non: &'static str,
}

#[derive(Diagnostic)]
Expand Down