Improve error message for unstable default body

This commit is contained in:
Maybe Waffle 2022-06-20 16:07:14 +04:00
parent 1984437115
commit 1e1d6fe84d
6 changed files with 43 additions and 26 deletions

View File

@ -4616,6 +4616,7 @@ dependencies = [
"rustc_attr",
"rustc_data_structures",
"rustc_errors",
"rustc_feature",
"rustc_graphviz",
"rustc_hir",
"rustc_hir_pretty",

View File

@ -30,3 +30,4 @@ rustc_ty_utils = { path = "../rustc_ty_utils" }
rustc_lint = { path = "../rustc_lint" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_type_ir = { path = "../rustc_type_ir" }
rustc_feature = { path = "../rustc_feature" }

View File

@ -1112,17 +1112,14 @@ fn check_impl_items_against_trait<'tcx>(
if !is_implemented_here {
match tcx.eval_default_body_stability(trait_item_id, full_impl_span) {
EvalResult::Deny { feature, reason, issue, is_soft, .. } => {
default_body_is_unstable(
tcx,
full_impl_span,
trait_item_id,
feature,
reason,
issue,
is_soft,
)
}
EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable(
tcx,
full_impl_span,
trait_item_id,
feature,
reason,
issue,
),
// Unmarked default bodies are considered stable (at least for now).
EvalResult::Allow | EvalResult::Unmarked => {}

View File

@ -99,7 +99,6 @@ pub use expectation::Expectation;
pub use fn_ctxt::*;
use hir::def::CtorOf;
pub use inherited::{Inherited, InheritedBuilder};
use rustc_middle::middle::stability::report_unstable;
use crate::astconv::AstConv;
use crate::check::gather_locals::GatherLocalsVisitor;
@ -667,19 +666,32 @@ fn missing_items_must_implement_one_of_err(
fn default_body_is_unstable(
tcx: TyCtxt<'_>,
impl_span: Span,
_item_did: DefId,
item_did: DefId,
feature: Symbol,
reason: Option<Symbol>,
issue: Option<NonZeroU32>,
is_soft: bool,
) {
let soft_handler = |lint, span, msg: &_| {
tcx.struct_span_lint_hir(lint, hir::CRATE_HIR_ID, span, |lint| {
lint.build(msg).emit();
})
let missing_item_name = &tcx.associated_item(item_did).name;
let use_of_unstable_library_feature_note = match reason {
Some(r) => format!("use of unstable library feature '{feature}': {r}"),
None => format!("use of unstable library feature '{feature}'"),
};
report_unstable(tcx.sess, feature, reason, issue, None, is_soft, impl_span, soft_handler)
let mut err = struct_span_err!(
tcx.sess,
impl_span,
E0046,
"not all trait items implemented, missing: `{missing_item_name}`",
);
err.note(format!("default implementation of `{missing_item_name}` is unstable"));
err.note(use_of_unstable_library_feature_note);
rustc_session::parse::add_feature_diagnostics_for_issue(
&mut err,
&tcx.sess.parse_sess,
feature,
rustc_feature::GateIssue::Library(issue),
);
err.emit();
}
/// Re-sugar `ty::GenericPredicates` in a way suitable to be used in structured suggestions.

View File

@ -8,11 +8,11 @@ use default_body::{Equal, JustTrait};
struct Type;
impl JustTrait for Type {}
//~^ ERROR use of unstable library feature 'fun_default_body'
//~| ERROR use of unstable library feature 'constant_default_body'
//~^ ERROR not all trait items implemented, missing: `CONSTANT` [E0046]
//~| ERROR not all trait items implemented, missing: `fun` [E0046]
impl Equal for Type {
//~^ ERROR use of unstable library feature 'eq_default_body'
//~^ ERROR not all trait items implemented, missing: `eq` [E0046]
fn neq(&self, other: &Self) -> bool {
false
}

View File

@ -1,20 +1,24 @@
error[E0658]: use of unstable library feature 'constant_default_body'
error[E0046]: not all trait items implemented, missing: `CONSTANT`
--> $DIR/default-body-stability-err.rs:10:1
|
LL | impl JustTrait for Type {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: default implementation of `CONSTANT` is unstable
= note: use of unstable library feature 'constant_default_body'
= help: add `#![feature(constant_default_body)]` to the crate attributes to enable
error[E0658]: use of unstable library feature 'fun_default_body'
error[E0046]: not all trait items implemented, missing: `fun`
--> $DIR/default-body-stability-err.rs:10:1
|
LL | impl JustTrait for Type {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: default implementation of `fun` is unstable
= note: use of unstable library feature 'fun_default_body'
= help: add `#![feature(fun_default_body)]` to the crate attributes to enable
error[E0658]: use of unstable library feature 'eq_default_body'
error[E0046]: not all trait items implemented, missing: `eq`
--> $DIR/default-body-stability-err.rs:14:1
|
LL | / impl Equal for Type {
@ -25,8 +29,10 @@ LL | | }
LL | | }
| |_^
|
= note: default implementation of `eq` is unstable
= note: use of unstable library feature 'eq_default_body'
= help: add `#![feature(eq_default_body)]` to the crate attributes to enable
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0046`.