Skip to content

Commit d9915da

Browse files
committed
Suppress redundant listing of unstable library features used on nightly
On nightly, there's a suggestion/help containing the `#![feature(..)]` attribute required to enable any required unstable features, so the note listing the features is unnecessary. This only applies to const-unstable and default-body-unstable errors. Normal "use of unstable library feature" errors list the features in the error message, so it doesn't feel redundant.
1 parent ac1f9f7 commit d9915da

15 files changed

+49
-28
lines changed

compiler/rustc_const_eval/src/check_consts/ops.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
328328
ccx.dcx().create_err(errors::UnstableConstFn {
329329
span,
330330
def_path: ccx.tcx.def_path_str(self.def_id),
331-
features: stability::unstable_message(&self.features, self.reason.to_opt_reason()),
331+
features: stability::unstable_message(&self.features, self.reason.to_opt_reason())
332+
.hide_features_on_nightly(&ccx.tcx.sess),
332333
issues: stability::unstable_issues(&self.features),
333334
nightly_subdiags: stability::unstable_nightly_subdiags(
334335
&ccx.tcx.sess,
@@ -379,7 +380,8 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
379380
ccx.dcx().create_err(errors::UnstableIntrinsic {
380381
span,
381382
name: self.name,
382-
features: stability::unstable_message(&self.features, self.reason.to_opt_reason()),
383+
features: stability::unstable_message(&self.features, self.reason.to_opt_reason())
384+
.hide_features_on_nightly(&ccx.tcx.sess),
383385
issues: stability::unstable_issues(&self.features),
384386
nightly_subdiags: stability::unstable_nightly_subdiags(
385387
&ccx.tcx.sess,

compiler/rustc_const_eval/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub(crate) struct UnstableConstFn {
120120
pub span: Span,
121121
pub def_path: String,
122122
#[subdiagnostic]
123-
pub features: rustc_middle::error::UnstableLibraryFeatureNote,
123+
pub features: Option<rustc_middle::error::UnstableLibraryFeatureNote>,
124124
#[subdiagnostic]
125125
pub issues: Vec<rustc_middle::error::UnstableLibraryFeatureIssue>,
126126
#[subdiagnostic]
@@ -134,7 +134,7 @@ pub(crate) struct UnstableIntrinsic {
134134
pub span: Span,
135135
pub name: Symbol,
136136
#[subdiagnostic]
137-
pub features: rustc_middle::error::UnstableLibraryFeatureNote,
137+
pub features: Option<rustc_middle::error::UnstableLibraryFeatureNote>,
138138
#[subdiagnostic]
139139
pub issues: Vec<rustc_middle::error::UnstableLibraryFeatureIssue>,
140140
#[subdiagnostic]

compiler/rustc_hir_analysis/src/check/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,10 @@ fn default_body_is_unstable(
300300
let inject_span = item_did
301301
.as_local()
302302
.and_then(|id| tcx.crate_level_attribute_injection_span(tcx.local_def_id_to_hir_id(id)));
303-
304303
tcx.dcx().emit_err(errors::MissingTraitItemUnstable {
305304
span: impl_span,
306305
missing_item_name,
307-
features: stability::unstable_message(denials, reason),
306+
features: stability::unstable_message(denials, reason).hide_features_on_nightly(&tcx.sess),
308307
issues: stability::unstable_issues(denials),
309308
nightly_subdiags: stability::unstable_nightly_subdiags(&tcx.sess, denials, inject_span),
310309
});

compiler/rustc_hir_analysis/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ pub(crate) struct MissingTraitItemUnstable {
979979
pub span: Span,
980980
pub missing_item_name: Symbol,
981981
#[subdiagnostic]
982-
pub features: rustc_middle::error::UnstableLibraryFeatureNote,
982+
pub features: Option<rustc_middle::error::UnstableLibraryFeatureNote>,
983983
#[subdiagnostic]
984984
pub issues: Vec<rustc_middle::error::UnstableLibraryFeatureIssue>,
985985
#[subdiagnostic]

compiler/rustc_middle/messages.ftl

+13
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ middle_unstable_library_feature_issue =
117117
*[false] {""}
118118
}
119119
120+
middle_unstable_library_feature_note =
121+
{$only_show_reason ->
122+
[true] reason for unstability: {$reason}
123+
*[false] -> use of unstable library {$count ->
124+
[one] feature
125+
*[other] features
126+
} {$features}{STREQ($reason, "") ->
127+
[true] {""}
128+
*[false] : {$reason}
129+
}
130+
}
131+
132+
120133
middle_unstable_library_feature_suggestion_for_allocator_api =
121134
consider wrapping the inner types in tuple
122135

compiler/rustc_middle/src/error.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,28 @@ pub struct SoftUnstableLibraryFeature {
199199

200200
/// A note for other diagnostics which report unstable library features
201201
#[derive(Subdiagnostic)]
202-
#[note(middle_unstable_library_feature)]
202+
#[note(middle_unstable_library_feature_note)]
203203
pub struct UnstableLibraryFeatureNote {
204204
pub features: DiagSymbolList,
205205
pub count: usize,
206206
pub reason: String,
207+
pub only_show_reason: bool,
208+
}
209+
210+
impl UnstableLibraryFeatureNote {
211+
/// On nightly, in errors for const-unstable and default-body-unstable items, suppress the
212+
/// feature list, since it's redundant with the `#![feature(...)]` suggestion.
213+
pub fn hide_features_on_nightly(self, sess: &rustc_session::Session) -> Option<Self> {
214+
if sess.is_nightly_build() {
215+
if self.reason.is_empty() {
216+
None
217+
} else {
218+
Some(UnstableLibraryFeatureNote { only_show_reason: true, ..self })
219+
}
220+
} else {
221+
Some(self)
222+
}
223+
}
207224
}
208225

209226
#[derive(Subdiagnostic)]

compiler/rustc_middle/src/middle/stability.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub fn unstable_message(
118118
features: denials.iter().map(|d| d.feature).collect(),
119119
count: denials.len(),
120120
reason: reason.map(|r| r.to_string()).unwrap_or_default(),
121+
only_show_reason: false,
121122
}
122123
}
123124

@@ -164,7 +165,8 @@ pub fn report_unstable(
164165
suggestions: Vec<UnstableLibraryFeatureSugg>,
165166
span: Span,
166167
) {
167-
let UnstableLibraryFeatureNote { features, count, reason } = unstable_message(denials, reason);
168+
let UnstableLibraryFeatureNote { features, count, reason, .. } =
169+
unstable_message(denials, reason);
168170
let issues = unstable_issues(denials);
169171
let nightly_subdiags = unstable_nightly_subdiags(sess, denials, None);
170172

@@ -186,7 +188,8 @@ pub fn soft_unstable(
186188
reason: Option<Symbol>,
187189
suggestions: Vec<UnstableLibraryFeatureSugg>,
188190
) -> SoftUnstableLibraryFeature {
189-
let UnstableLibraryFeatureNote { features, count, reason } = unstable_message(denials, reason);
191+
let UnstableLibraryFeatureNote { features, count, reason, .. } =
192+
unstable_message(denials, reason);
190193
let mut issues = unstable_issues(denials);
191194
let nightly_subdiags = unstable_nightly_subdiags(sess, denials, None);
192195

tests/ui/consts/const-unstable-intrinsic.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ error: `min_align_of_val` is not yet stable as a const intrinsic
3232
LL | unstable_intrinsic::min_align_of_val(&x);
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3434
|
35-
= note: use of unstable library feature `unstable`
3635
= note: see issue #42 <https://github.com/rust-lang/rust/issues/42> for more information
3736
= help: add `#![feature(unstable)]` to the crate attributes to enable
3837
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

tests/ui/stability-attribute/const-stability-attribute-implies-no-feature.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error: `foobar` is not yet stable as a const fn
44
LL | foobar();
55
| ^^^^^^^^
66
|
7-
= note: use of unstable library feature `const_foobar`
87
= note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
98
= help: add `#![feature(const_foobar)]` to the crate attributes to enable
109
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

tests/ui/stability-attribute/default-body-stability-err.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ LL | impl JustTrait for Type {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: default implementation of `CONSTANT` is unstable
8-
= note: use of unstable library feature `constant_default_body`
98
= help: add `#![feature(constant_default_body)]` to the crate attributes to enable
109
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1110

@@ -16,7 +15,6 @@ LL | impl JustTrait for Type {}
1615
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1716
|
1817
= note: default implementation of `fun` is unstable
19-
= note: use of unstable library feature `fun_default_body`
2018
= help: add `#![feature(fun_default_body)]` to the crate attributes to enable
2119
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2220

@@ -27,7 +25,7 @@ LL | impl JustTrait for Type {}
2725
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2826
|
2927
= note: default implementation of `fun2` is unstable
30-
= note: use of unstable library feature `fun_default_body`: reason
28+
= note: reason for unstability: reason
3129
= help: add `#![feature(fun_default_body)]` to the crate attributes to enable
3230
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3331

@@ -43,7 +41,6 @@ LL | | }
4341
| |_^
4442
|
4543
= note: default implementation of `eq` is unstable
46-
= note: use of unstable library feature `eq_default_body`
4744
= help: add `#![feature(eq_default_body)]` to the crate attributes to enable
4845
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4946

tests/ui/stability-attribute/mixed-levels.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ error: `const_unstable_fn` is not yet stable as a const fn
1313
LL | const USE_UNSTABLE: () = mixed_levels::const_unstable_fn();
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
16-
= note: use of unstable library feature `unstable_c`
1716
= help: add `#![feature(unstable_c)]` to the crate attributes to enable
1817
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1918

tests/ui/stability-attribute/two-unstables.none.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LL | impl two_unstables::Trait for Wrapper {}
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2828
|
2929
= note: default implementation of `method` is unstable
30-
= note: use of unstable library features `e` and `f`: reason
30+
= note: reason for unstability: reason
3131
= note: see issue #5 <https://github.com/rust-lang/rust/issues/5> for more information about `e`
3232
= note: see issue #6 <https://github.com/rust-lang/rust/issues/6> for more information about `f`
3333
= help: add `#![feature(e, f)]` to the crate attributes to enable
@@ -39,7 +39,7 @@ error: `nothing` is not yet stable as a const fn
3939
LL | const USE_NOTHING: () = two_unstables::nothing();
4040
| ^^^^^^^^^^^^^^^^^^^^^^^^
4141
|
42-
= note: use of unstable library features `c` and `d`: reason
42+
= note: reason for unstability: reason
4343
= note: see issue #3 <https://github.com/rust-lang/rust/issues/3> for more information about `c`
4444
= note: see issue #4 <https://github.com/rust-lang/rust/issues/4> for more information about `d`
4545
= help: add `#![feature(c, d)]` to the crate attributes to enable

tests/ui/stability-attribute/two-unstables.some.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | impl two_unstables::Trait for Wrapper {}
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626
|
2727
= note: default implementation of `method` is unstable
28-
= note: use of unstable library feature `f`: reason
28+
= note: reason for unstability: reason
2929
= note: see issue #6 <https://github.com/rust-lang/rust/issues/6> for more information
3030
= help: add `#![feature(f)]` to the crate attributes to enable
3131
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
@@ -36,7 +36,7 @@ error: `nothing` is not yet stable as a const fn
3636
LL | const USE_NOTHING: () = two_unstables::nothing();
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^
3838
|
39-
= note: use of unstable library feature `c`: reason
39+
= note: reason for unstability: reason
4040
= note: see issue #3 <https://github.com/rust-lang/rust/issues/3> for more information
4141
= help: add `#![feature(c)]` to the crate attributes to enable
4242
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

tests/ui/traits/const-traits/staged-api.stable.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable a
2727
LL | Unstable::func();
2828
| ^^^^^^^^^^^^^^^^
2929
|
30-
= note: use of unstable library feature `unstable`
3130
= help: add `#![feature(unstable)]` to the crate attributes to enable
3231
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3332

@@ -45,7 +44,6 @@ error: `<staged_api::Unstable2 as staged_api::MyTrait>::func` is not yet stable
4544
LL | Unstable2::func();
4645
| ^^^^^^^^^^^^^^^^^
4746
|
48-
= note: use of unstable library feature `unstable2`
4947
= help: add `#![feature(unstable2)]` to the crate attributes to enable
5048
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5149

@@ -55,7 +53,6 @@ error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable a
5553
LL | Unstable::func();
5654
| ^^^^^^^^^^^^^^^^
5755
|
58-
= note: use of unstable library feature `unstable`
5956
= help: add `#![feature(unstable)]` to the crate attributes to enable
6057
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
6158

@@ -73,7 +70,6 @@ error: `<staged_api::Unstable2 as staged_api::MyTrait>::func` is not yet stable
7370
LL | Unstable2::func();
7471
| ^^^^^^^^^^^^^^^^^
7572
|
76-
= note: use of unstable library feature `unstable2`
7773
= help: add `#![feature(unstable2)]` to the crate attributes to enable
7874
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
7975

@@ -83,7 +79,6 @@ error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable a
8379
LL | Unstable::func();
8480
| ^^^^^^^^^^^^^^^^
8581
|
86-
= note: use of unstable library feature `unstable`
8782
= help: add `#![feature(unstable)]` to the crate attributes to enable
8883
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
8984

tests/ui/traits/const-traits/staged-api.unstable.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ error: `<staged_api::Unstable2 as staged_api::MyTrait>::func` is not yet stable
4040
LL | Unstable2::func();
4141
| ^^^^^^^^^^^^^^^^^
4242
|
43-
= note: use of unstable library feature `unstable2`
4443
= help: add `#![feature(unstable2)]` to the crate attributes to enable
4544
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4645

@@ -50,7 +49,6 @@ error: `<staged_api::Unstable2 as staged_api::MyTrait>::func` is not yet stable
5049
LL | Unstable2::func();
5150
| ^^^^^^^^^^^^^^^^^
5251
|
53-
= note: use of unstable library feature `unstable2`
5452
= help: add `#![feature(unstable2)]` to the crate attributes to enable
5553
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5654

0 commit comments

Comments
 (0)