Rollup merge of #152698 - Zalathar:zforce, r=jieyouxu

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

- Fixes https://github.com/rust-lang/rust/issues/152692.
---

https://github.com/rust-lang/rust/pull/151036 adds extra diagnostic text (“the nightly-only, unstable trait”) to note when a not-implemented trait is unstable.

However, that extra text is usually unhelpful when building a crate graph with `-Zforce-unstable-if-unmarked` (such as the compiler or stdlib), because *any* trait not explicitly marked stable will be treated as unstable.

(For typical compiler contributors using the stage0 compiler, this fix won't take effect until the next bootstrap beta bump.)
This commit is contained in:
Stuart Cook 2026-02-17 13:02:24 +11:00 committed by GitHub
commit dc77672e9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 125 additions and 8 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),
)