From 1e1d6fe84dd086a9075fcfe5fc63d81738c02f12 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 20 Jun 2022 16:07:14 +0400 Subject: [PATCH] Improve error message for unstable default body --- Cargo.lock | 1 + compiler/rustc_typeck/Cargo.toml | 1 + compiler/rustc_typeck/src/check/check.rs | 19 ++++++------- compiler/rustc_typeck/src/check/mod.rs | 28 +++++++++++++------ .../default-body-stability-err.rs | 6 ++-- .../default-body-stability-err.stderr | 14 +++++++--- 6 files changed, 43 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed3e30342f2..bb7598bbf3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4616,6 +4616,7 @@ dependencies = [ "rustc_attr", "rustc_data_structures", "rustc_errors", + "rustc_feature", "rustc_graphviz", "rustc_hir", "rustc_hir_pretty", diff --git a/compiler/rustc_typeck/Cargo.toml b/compiler/rustc_typeck/Cargo.toml index faf52e2695a..cae29c1d3c5 100644 --- a/compiler/rustc_typeck/Cargo.toml +++ b/compiler/rustc_typeck/Cargo.toml @@ -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" } diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index 6254825d96d..0293bf7803a 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -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 => {} diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index acd7e76fe25..b5201c737df 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -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, issue: Option, - 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. diff --git a/src/test/ui/stability-attribute/default-body-stability-err.rs b/src/test/ui/stability-attribute/default-body-stability-err.rs index 8f970d0c9f6..ecb281bccf6 100644 --- a/src/test/ui/stability-attribute/default-body-stability-err.rs +++ b/src/test/ui/stability-attribute/default-body-stability-err.rs @@ -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 } diff --git a/src/test/ui/stability-attribute/default-body-stability-err.stderr b/src/test/ui/stability-attribute/default-body-stability-err.stderr index 6abf68bbcae..ef666f30fc2 100644 --- a/src/test/ui/stability-attribute/default-body-stability-err.stderr +++ b/src/test/ui/stability-attribute/default-body-stability-err.stderr @@ -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`.