Skip to content

Commit

Permalink
Improve error message for unstable default body
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Jul 26, 2022
1 parent 1984437 commit 1e1d6fe
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4616,6 +4616,7 @@ dependencies = [
"rustc_attr",
"rustc_data_structures",
"rustc_errors",
"rustc_feature",
"rustc_graphviz",
"rustc_hir",
"rustc_hir_pretty",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
19 changes: 8 additions & 11 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}
Expand Down
28 changes: 20 additions & 8 deletions compiler/rustc_typeck/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/stability-attribute/default-body-stability-err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions src/test/ui/stability-attribute/default-body-stability-err.stderr
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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`.

0 comments on commit 1e1d6fe

Please sign in to comment.