Suppress unstable-trait notes under -Zforce-unstable-if-unmarked

This commit is contained in:
Zalathar 2026-02-16 17:05:13 +11:00
parent 0f5c2215ad
commit 125e69e862
3 changed files with 14 additions and 12 deletions

View file

@ -5608,15 +5608,17 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
None => String::new(),
};
if let ty::PredicatePolarity::Positive = trait_predicate.polarity() {
// If the trait in question is unstable, mention that fact in the diagnostic.
// But if we're building with `-Zforce-unstable-if-unmarked` then _any_ trait
// not explicitly marked stable is considered unstable, so the extra text is
// unhelpful noise. See <https://github.com/rust-lang/rust/issues/152692>.
let mention_unstable = !tcx.sess.opts.unstable_opts.force_unstable_if_unmarked
&& try { tcx.lookup_stability(trait_predicate.def_id())?.level.is_stable() }
== Some(false);
let unstable = if mention_unstable { "nightly-only, unstable " } else { "" };
format!(
"{pre_message}the {}trait `{}` is not implemented for{desc} `{}`",
if tcx.lookup_stability(trait_predicate.def_id()).map(|s| s.level.is_stable())
== Some(false)
{
"nightly-only, unstable "
} else {
""
},
"{pre_message}the {unstable}trait `{}` is not implemented for{desc} `{}`",
trait_predicate.print_modifiers_and_trait_path(),
tcx.short_string(trait_predicate.self_ty().skip_binder(), long_ty_path),
)

View file

@ -2,7 +2,7 @@ error[E0277]: the trait bound `(): LocalTrait` is not satisfied
--> $DIR/nightly-only-unstable.rs:25:21
|
LL | use_local_trait(());
| --------------- ^^ the nightly-only, unstable trait `LocalTrait` is not implemented for `()`
| --------------- ^^ the trait `LocalTrait` is not implemented for `()`
| |
| required by a bound introduced by this call
|
@ -21,7 +21,7 @@ error[E0277]: the trait bound `(): ForeignTrait` is not satisfied
--> $DIR/nightly-only-unstable.rs:31:23
|
LL | use_foreign_trait(());
| ----------------- ^^ the nightly-only, unstable trait `ForeignTrait` is not implemented for `()`
| ----------------- ^^ the trait `ForeignTrait` is not implemented for `()`
| |
| required by a bound introduced by this call
|

View file

@ -25,12 +25,12 @@ fn main() {
use_local_trait(());
//~^ ERROR the trait bound `(): LocalTrait` is not satisfied
//[normal]~| NOTE the trait `LocalTrait` is not implemented for `()`
//[force]~| NOTE the nightly-only, unstable trait `LocalTrait` is not implemented for `()`
//[force]~| NOTE the trait `LocalTrait` is not implemented for `()`
//~| NOTE required by a bound introduced by this call
use_foreign_trait(());
//~^ ERROR the trait bound `(): ForeignTrait` is not satisfied
//[normal]~| NOTE the nightly-only, unstable trait `ForeignTrait` is not implemented for `()`
//[force]~| NOTE the nightly-only, unstable trait `ForeignTrait` is not implemented for `()`
//[force]~| NOTE the trait `ForeignTrait` is not implemented for `()`
//~| NOTE required by a bound introduced by this call
}