Tweak wording of non-const traits used as const bounds
Use verbose suggestions and add additional labels/notes. Add more test cases for stable/nightly and feature enabled/disabled.
This commit is contained in:
parent
f6cb952dc1
commit
4007fc9a0f
49 changed files with 1121 additions and 194 deletions
|
@ -96,12 +96,14 @@ hir_analysis_coercion_between_struct_same_note = expected coercion between the s
|
|||
|
||||
hir_analysis_coercion_between_struct_single_note = expected a single field to be coerced, none found
|
||||
|
||||
hir_analysis_const_bound_for_non_const_trait =
|
||||
`{$modifier}` can only be applied to `#[const_trait]` traits
|
||||
hir_analysis_const_bound_for_non_const_trait = `{$modifier}` can only be applied to `#[const_trait]` traits
|
||||
.label = can't be applied to `{$trait_name}`
|
||||
.note = `{$trait_name}` can't be used with `{$modifier}` because it isn't annotated with `#[const_trait]`
|
||||
.suggestion = {$suggestion_pre}mark `{$trait_name}` as `#[const_trait]` to allow it to have `const` implementations
|
||||
|
||||
hir_analysis_const_impl_for_non_const_trait =
|
||||
const `impl` for trait `{$trait_name}` which is not marked with `#[const_trait]`
|
||||
.suggestion = mark `{$trait_name}` as const
|
||||
hir_analysis_const_impl_for_non_const_trait = const `impl` for trait `{$trait_name}` which is not marked with `#[const_trait]`
|
||||
.label = this trait is not `const`
|
||||
.suggestion = {$suggestion_pre}mark `{$trait_name}` as `#[const_trait]` to allow it to have `const` implementations
|
||||
.note = marking a trait with `#[const_trait]` ensures all default method bodies are `const`
|
||||
.adding = adding a non-const method body in the future would be a breaking change
|
||||
|
||||
|
|
|
@ -1637,11 +1637,23 @@ fn check_impl_constness(
|
|||
}
|
||||
|
||||
let trait_name = tcx.item_name(trait_def_id).to_string();
|
||||
let (local_trait_span, suggestion_pre) =
|
||||
match (trait_def_id.is_local(), tcx.sess.is_nightly_build()) {
|
||||
(true, true) => (
|
||||
Some(tcx.def_span(trait_def_id).shrink_to_lo()),
|
||||
if tcx.features().const_trait_impl() {
|
||||
""
|
||||
} else {
|
||||
"enable `#![feature(const_trait_impl)]` in your crate and "
|
||||
},
|
||||
),
|
||||
(false, _) | (_, false) => (None, ""),
|
||||
};
|
||||
Some(tcx.dcx().emit_err(errors::ConstImplForNonConstTrait {
|
||||
trait_ref_span: hir_trait_ref.path.span,
|
||||
trait_name,
|
||||
local_trait_span:
|
||||
trait_def_id.as_local().map(|_| tcx.def_span(trait_def_id).shrink_to_lo()),
|
||||
local_trait_span,
|
||||
suggestion_pre,
|
||||
marking: (),
|
||||
adding: (),
|
||||
}))
|
||||
|
|
|
@ -530,10 +530,16 @@ pub(crate) struct GenericArgsOnOverriddenImpl {
|
|||
#[diag(hir_analysis_const_impl_for_non_const_trait)]
|
||||
pub(crate) struct ConstImplForNonConstTrait {
|
||||
#[primary_span]
|
||||
#[label]
|
||||
pub trait_ref_span: Span,
|
||||
pub trait_name: String,
|
||||
#[suggestion(applicability = "machine-applicable", code = "#[const_trait]")]
|
||||
#[suggestion(
|
||||
applicability = "machine-applicable",
|
||||
code = "#[const_trait] ",
|
||||
style = "verbose"
|
||||
)]
|
||||
pub local_trait_span: Option<Span>,
|
||||
pub suggestion_pre: &'static str,
|
||||
#[note]
|
||||
pub marking: (),
|
||||
#[note(hir_analysis_adding)]
|
||||
|
@ -544,8 +550,19 @@ pub(crate) struct ConstImplForNonConstTrait {
|
|||
#[diag(hir_analysis_const_bound_for_non_const_trait)]
|
||||
pub(crate) struct ConstBoundForNonConstTrait {
|
||||
#[primary_span]
|
||||
#[label]
|
||||
pub span: Span,
|
||||
pub modifier: &'static str,
|
||||
#[note]
|
||||
pub def_span: Option<Span>,
|
||||
pub suggestion_pre: &'static str,
|
||||
#[suggestion(
|
||||
applicability = "machine-applicable",
|
||||
code = "#[const_trait] ",
|
||||
style = "verbose"
|
||||
)]
|
||||
pub suggestion: Option<Span>,
|
||||
pub trait_name: String,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
|
|
|
@ -737,9 +737,26 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
|
||||
&& !self.tcx().is_const_trait(trait_def_id)
|
||||
{
|
||||
let (def_span, suggestion, suggestion_pre) =
|
||||
match (trait_def_id.is_local(), self.tcx().sess.is_nightly_build()) {
|
||||
(true, true) => (
|
||||
None,
|
||||
Some(tcx.def_span(trait_def_id).shrink_to_lo()),
|
||||
if self.tcx().features().const_trait_impl() {
|
||||
""
|
||||
} else {
|
||||
"enable `#![feature(const_trait_impl)]` in your crate and "
|
||||
},
|
||||
),
|
||||
(false, _) | (_, false) => (Some(tcx.def_span(trait_def_id)), None, ""),
|
||||
};
|
||||
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
|
||||
span,
|
||||
modifier: constness.as_str(),
|
||||
def_span,
|
||||
trait_name: self.tcx().def_path_str(trait_def_id),
|
||||
suggestion_pre,
|
||||
suggestion,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue