Improve diagnostic for const ctors in array repeat expressions

This commit is contained in:
clubby789
2023-07-21 15:59:28 +00:00
parent c06b2b9117
commit 86b112204a
7 changed files with 126 additions and 28 deletions

View File

@@ -301,9 +301,14 @@ pub enum ObligationCauseCode<'tcx> {
InlineAsmSized,
/// `[expr; N]` requires `type_of(expr): Copy`.
RepeatElementCopy {
/// If element is a `const fn` we display a help message suggesting to move the
/// function call to a new `const` item while saying that `T` doesn't implement `Copy`.
is_const_fn: bool,
/// If element is a `const fn` or const ctor we display a help message suggesting
/// to move it to a new `const` item while saying that `T` doesn't implement `Copy`.
is_constable: IsConstable,
elt_type: Ty<'tcx>,
elt_span: Span,
/// Span of the statement/item in which the repeat expression occurs. We can use this to
/// place a `const` declaration before it
elt_stmt_span: Span,
},
/// Types of fields (other than the last, except for packed structs) in a struct must be sized.
@@ -448,6 +453,21 @@ pub enum ObligationCauseCode<'tcx> {
TypeAlias(InternedObligationCauseCode<'tcx>, Span, DefId),
}
/// Whether a value can be extracted into a const.
/// Used for diagnostics around array repeat expressions.
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
pub enum IsConstable {
No,
/// Call to a const fn
Fn,
/// Use of a const ctor
Ctor,
}
crate::TrivialTypeTraversalAndLiftImpls! {
IsConstable,
}
/// The 'location' at which we try to perform HIR-based wf checking.
/// This information is used to obtain an `hir::Ty`, which
/// we can walk in order to obtain precise spans for any