Skip to content

Commit 1e1d6fe

Browse files
committed
Improve error message for unstable default body
1 parent 1984437 commit 1e1d6fe

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4616,6 +4616,7 @@ dependencies = [
46164616
"rustc_attr",
46174617
"rustc_data_structures",
46184618
"rustc_errors",
4619+
"rustc_feature",
46194620
"rustc_graphviz",
46204621
"rustc_hir",
46214622
"rustc_hir_pretty",

compiler/rustc_typeck/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ rustc_ty_utils = { path = "../rustc_ty_utils" }
3030
rustc_lint = { path = "../rustc_lint" }
3131
rustc_serialize = { path = "../rustc_serialize" }
3232
rustc_type_ir = { path = "../rustc_type_ir" }
33+
rustc_feature = { path = "../rustc_feature" }

compiler/rustc_typeck/src/check/check.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1112,17 +1112,14 @@ fn check_impl_items_against_trait<'tcx>(
11121112

11131113
if !is_implemented_here {
11141114
match tcx.eval_default_body_stability(trait_item_id, full_impl_span) {
1115-
EvalResult::Deny { feature, reason, issue, is_soft, .. } => {
1116-
default_body_is_unstable(
1117-
tcx,
1118-
full_impl_span,
1119-
trait_item_id,
1120-
feature,
1121-
reason,
1122-
issue,
1123-
is_soft,
1124-
)
1125-
}
1115+
EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable(
1116+
tcx,
1117+
full_impl_span,
1118+
trait_item_id,
1119+
feature,
1120+
reason,
1121+
issue,
1122+
),
11261123

11271124
// Unmarked default bodies are considered stable (at least for now).
11281125
EvalResult::Allow | EvalResult::Unmarked => {}

compiler/rustc_typeck/src/check/mod.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ pub use expectation::Expectation;
9999
pub use fn_ctxt::*;
100100
use hir::def::CtorOf;
101101
pub use inherited::{Inherited, InheritedBuilder};
102-
use rustc_middle::middle::stability::report_unstable;
103102

104103
use crate::astconv::AstConv;
105104
use crate::check::gather_locals::GatherLocalsVisitor;
@@ -667,19 +666,32 @@ fn missing_items_must_implement_one_of_err(
667666
fn default_body_is_unstable(
668667
tcx: TyCtxt<'_>,
669668
impl_span: Span,
670-
_item_did: DefId,
669+
item_did: DefId,
671670
feature: Symbol,
672671
reason: Option<Symbol>,
673672
issue: Option<NonZeroU32>,
674-
is_soft: bool,
675673
) {
676-
let soft_handler = |lint, span, msg: &_| {
677-
tcx.struct_span_lint_hir(lint, hir::CRATE_HIR_ID, span, |lint| {
678-
lint.build(msg).emit();
679-
})
674+
let missing_item_name = &tcx.associated_item(item_did).name;
675+
let use_of_unstable_library_feature_note = match reason {
676+
Some(r) => format!("use of unstable library feature '{feature}': {r}"),
677+
None => format!("use of unstable library feature '{feature}'"),
680678
};
681679

682-
report_unstable(tcx.sess, feature, reason, issue, None, is_soft, impl_span, soft_handler)
680+
let mut err = struct_span_err!(
681+
tcx.sess,
682+
impl_span,
683+
E0046,
684+
"not all trait items implemented, missing: `{missing_item_name}`",
685+
);
686+
err.note(format!("default implementation of `{missing_item_name}` is unstable"));
687+
err.note(use_of_unstable_library_feature_note);
688+
rustc_session::parse::add_feature_diagnostics_for_issue(
689+
&mut err,
690+
&tcx.sess.parse_sess,
691+
feature,
692+
rustc_feature::GateIssue::Library(issue),
693+
);
694+
err.emit();
683695
}
684696

685697
/// Re-sugar `ty::GenericPredicates` in a way suitable to be used in structured suggestions.

src/test/ui/stability-attribute/default-body-stability-err.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use default_body::{Equal, JustTrait};
88
struct Type;
99

1010
impl JustTrait for Type {}
11-
//~^ ERROR use of unstable library feature 'fun_default_body'
12-
//~| ERROR use of unstable library feature 'constant_default_body'
11+
//~^ ERROR not all trait items implemented, missing: `CONSTANT` [E0046]
12+
//~| ERROR not all trait items implemented, missing: `fun` [E0046]
1313

1414
impl Equal for Type {
15-
//~^ ERROR use of unstable library feature 'eq_default_body'
15+
//~^ ERROR not all trait items implemented, missing: `eq` [E0046]
1616
fn neq(&self, other: &Self) -> bool {
1717
false
1818
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
error[E0658]: use of unstable library feature 'constant_default_body'
1+
error[E0046]: not all trait items implemented, missing: `CONSTANT`
22
--> $DIR/default-body-stability-err.rs:10:1
33
|
44
LL | impl JustTrait for Type {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: default implementation of `CONSTANT` is unstable
8+
= note: use of unstable library feature 'constant_default_body'
79
= help: add `#![feature(constant_default_body)]` to the crate attributes to enable
810

9-
error[E0658]: use of unstable library feature 'fun_default_body'
11+
error[E0046]: not all trait items implemented, missing: `fun`
1012
--> $DIR/default-body-stability-err.rs:10:1
1113
|
1214
LL | impl JustTrait for Type {}
1315
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1416
|
17+
= note: default implementation of `fun` is unstable
18+
= note: use of unstable library feature 'fun_default_body'
1519
= help: add `#![feature(fun_default_body)]` to the crate attributes to enable
1620

17-
error[E0658]: use of unstable library feature 'eq_default_body'
21+
error[E0046]: not all trait items implemented, missing: `eq`
1822
--> $DIR/default-body-stability-err.rs:14:1
1923
|
2024
LL | / impl Equal for Type {
@@ -25,8 +29,10 @@ LL | | }
2529
LL | | }
2630
| |_^
2731
|
32+
= note: default implementation of `eq` is unstable
33+
= note: use of unstable library feature 'eq_default_body'
2834
= help: add `#![feature(eq_default_body)]` to the crate attributes to enable
2935

3036
error: aborting due to 3 previous errors
3137

32-
For more information about this error, try `rustc --explain E0658`.
38+
For more information about this error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)