Skip to content

Commit

Permalink
Suppress redundant listing of unstable library features used on nightly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dianne committed Oct 28, 2024
1 parent 5a2346a commit c1a79d8
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 29 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
ccx.dcx().create_err(errors::UnstableConstFn {
span,
def_path: ccx.tcx.def_path_str(self.def_id),
features: stability::unstable_message(&self.features, self.reason.to_opt_reason()),
features: stability::unstable_message(&self.features, self.reason.to_opt_reason())
.hide_features_on_nightly(&ccx.tcx.sess),
issues: stability::unstable_issues(&self.features),
nightly_subdiags: stability::unstable_nightly_subdiags(
&ccx.tcx.sess,
Expand Down Expand Up @@ -380,7 +381,8 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
ccx.dcx().create_err(errors::UnstableIntrinsic {
span,
name: self.name,
features: stability::unstable_message(&self.features, self.reason.to_opt_reason()),
features: stability::unstable_message(&self.features, self.reason.to_opt_reason())
.hide_features_on_nightly(&ccx.tcx.sess),
issues: stability::unstable_issues(&self.features),
nightly_subdiags: stability::unstable_nightly_subdiags(
&ccx.tcx.sess,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub(crate) struct UnstableConstFn {
pub span: Span,
pub def_path: String,
#[subdiagnostic]
pub features: rustc_middle::error::UnstableLibraryFeatureNote,
pub features: Option<rustc_middle::error::UnstableLibraryFeatureNote>,
#[subdiagnostic]
pub issues: Vec<rustc_middle::error::UnstableLibraryFeatureIssue>,
#[subdiagnostic]
Expand All @@ -134,7 +134,7 @@ pub(crate) struct UnstableIntrinsic {
pub span: Span,
pub name: Symbol,
#[subdiagnostic]
pub features: rustc_middle::error::UnstableLibraryFeatureNote,
pub features: Option<rustc_middle::error::UnstableLibraryFeatureNote>,
#[subdiagnostic]
pub issues: Vec<rustc_middle::error::UnstableLibraryFeatureIssue>,
#[subdiagnostic]
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,10 @@ fn default_body_is_unstable(
let inject_span = item_did
.as_local()
.and_then(|id| tcx.crate_level_attribute_injection_span(tcx.local_def_id_to_hir_id(id)));

tcx.dcx().emit_err(errors::MissingTraitItemUnstable {
span: impl_span,
missing_item_name,
features: stability::unstable_message(denials, reason),
features: stability::unstable_message(denials, reason).hide_features_on_nightly(&tcx.sess),
issues: stability::unstable_issues(denials),
nightly_subdiags: stability::unstable_nightly_subdiags(&tcx.sess, denials, inject_span),
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ pub(crate) struct MissingTraitItemUnstable {
pub span: Span,
pub missing_item_name: Symbol,
#[subdiagnostic]
pub features: rustc_middle::error::UnstableLibraryFeatureNote,
pub features: Option<rustc_middle::error::UnstableLibraryFeatureNote>,
#[subdiagnostic]
pub issues: Vec<rustc_middle::error::UnstableLibraryFeatureIssue>,
#[subdiagnostic]
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_middle/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ middle_unstable_library_feature_issue =
*[false] {""}
}
middle_unstable_library_feature_note =
{$only_show_reason ->
[true] reason for unstability: {$reason}
*[false] -> use of unstable library {$count ->
[one] feature
*[other] features
} {$features}{STREQ($reason, "") ->
[true] {""}
*[false] : {$reason}
}
}
middle_unstable_library_feature_suggestion_for_allocator_api =
consider wrapping the inner types in tuple
Expand Down
19 changes: 18 additions & 1 deletion compiler/rustc_middle/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,28 @@ pub struct SoftUnstableLibraryFeature {

/// A note for other diagnostics which report unstable library features
#[derive(Subdiagnostic)]
#[note(middle_unstable_library_feature)]
#[note(middle_unstable_library_feature_note)]
pub struct UnstableLibraryFeatureNote {
pub features: DiagSymbolList,
pub count: usize,
pub reason: String,
pub only_show_reason: bool,
}

impl UnstableLibraryFeatureNote {
/// On nightly, in errors for const-unstable and default-body-unstable items, suppress the
/// feature list, since it's redundant with the `#![feature(...)]` suggestion.
pub fn hide_features_on_nightly(self, sess: &rustc_session::Session) -> Option<Self> {
if sess.is_nightly_build() {
if self.reason.is_empty() {
None
} else {
Some(UnstableLibraryFeatureNote { only_show_reason: true, ..self })
}
} else {
Some(self)
}
}
}

#[derive(Subdiagnostic)]
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub fn unstable_message(
features: denials.iter().map(|d| d.feature).collect(),
count: denials.len(),
reason: reason.map(|r| r.to_string()).unwrap_or_default(),
only_show_reason: false,
}
}

Expand Down Expand Up @@ -164,7 +165,8 @@ pub fn report_unstable(
suggestions: Vec<UnstableLibraryFeatureSugg>,
span: Span,
) {
let UnstableLibraryFeatureNote { features, count, reason } = unstable_message(denials, reason);
let UnstableLibraryFeatureNote { features, count, reason, .. } =
unstable_message(denials, reason);
let issues = unstable_issues(denials);
let nightly_subdiags = unstable_nightly_subdiags(sess, denials, None);

Expand All @@ -186,7 +188,8 @@ pub fn soft_unstable(
reason: Option<Symbol>,
suggestions: Vec<UnstableLibraryFeatureSugg>,
) -> SoftUnstableLibraryFeature {
let UnstableLibraryFeatureNote { features, count, reason } = unstable_message(denials, reason);
let UnstableLibraryFeatureNote { features, count, reason, .. } =
unstable_message(denials, reason);
let mut issues = unstable_issues(denials);
let nightly_subdiags = unstable_nightly_subdiags(sess, denials, None);

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/consts/const-unstable-intrinsic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ error: `min_align_of_val` is not yet stable as a const intrinsic
LL | unstable_intrinsic::old_way::min_align_of_val(&x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: use of unstable library feature `unstable`
= note: see issue #42 <https://github.com/rust-lang/rust/issues/42> for more information
= help: add `#![feature(unstable)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
Expand All @@ -69,7 +68,6 @@ error: `min_align_of_val` is not yet stable as a const intrinsic
LL | unstable_intrinsic::new_way::min_align_of_val(&x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: use of unstable library feature `unstable`
= note: see issue #42 <https://github.com/rust-lang/rust/issues/42> for more information
= help: add `#![feature(unstable)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ error: `foobar` is not yet stable as a const fn
LL | foobar();
| ^^^^^^^^
|
= note: use of unstable library feature `const_foobar`
= note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
= help: add `#![feature(const_foobar)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

Expand All @@ -16,7 +15,6 @@ 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
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

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

Expand All @@ -43,7 +41,6 @@ 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
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

Expand Down
1 change: 0 additions & 1 deletion tests/ui/stability-attribute/mixed-levels.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ error: `const_unstable_fn` is not yet stable as a const fn
LL | const USE_UNSTABLE: () = mixed_levels::const_unstable_fn();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: use of unstable library feature `unstable_c`
= help: add `#![feature(unstable_c)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/stability-attribute/two-unstables.none.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LL | impl two_unstables::Trait for Wrapper {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: default implementation of `method` is unstable
= note: use of unstable library features `e` and `f`: reason
= note: reason for unstability: reason
= note: see issue #5 <https://github.com/rust-lang/rust/issues/5> for more information about `e`
= note: see issue #6 <https://github.com/rust-lang/rust/issues/6> for more information about `f`
= help: add `#![feature(e, f)]` to the crate attributes to enable
Expand All @@ -39,7 +39,7 @@ error: `nothing` is not yet stable as a const fn
LL | const USE_NOTHING: () = two_unstables::nothing();
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: use of unstable library features `c` and `d`: reason
= note: reason for unstability: reason
= note: see issue #3 <https://github.com/rust-lang/rust/issues/3> for more information about `c`
= note: see issue #4 <https://github.com/rust-lang/rust/issues/4> for more information about `d`
= help: add `#![feature(c, d)]` to the crate attributes to enable
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/stability-attribute/two-unstables.some.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LL | impl two_unstables::Trait for Wrapper {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: default implementation of `method` is unstable
= note: use of unstable library feature `f`: reason
= note: reason for unstability: reason
= note: see issue #6 <https://github.com/rust-lang/rust/issues/6> for more information
= help: add `#![feature(f)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
Expand All @@ -36,7 +36,7 @@ error: `nothing` is not yet stable as a const fn
LL | const USE_NOTHING: () = two_unstables::nothing();
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: use of unstable library feature `c`: reason
= note: reason for unstability: reason
= note: see issue #3 <https://github.com/rust-lang/rust/issues/3> for more information
= help: add `#![feature(c)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
Expand Down
5 changes: 0 additions & 5 deletions tests/ui/traits/const-traits/staged-api.stable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable a
LL | Unstable::func();
| ^^^^^^^^^^^^^^^^
|
= note: use of unstable library feature `unstable`
= help: add `#![feature(unstable)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

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

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

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

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

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/traits/const-traits/staged-api.unstable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ error: `<staged_api::Unstable2 as staged_api::MyTrait>::func` is not yet stable
LL | Unstable2::func();
| ^^^^^^^^^^^^^^^^^
|
= note: use of unstable library feature `unstable2`
= help: add `#![feature(unstable2)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

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

Expand Down

0 comments on commit c1a79d8

Please sign in to comment.