Skip to content

Commit dc77672

Browse files
authored
Rollup merge of #152698 - Zalathar:zforce, r=jieyouxu
Suppress unstable-trait notes under `-Zforce-unstable-if-unmarked` - Fixes #152692. --- #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.)
2 parents 7ab22cd + 125e69e commit dc77672

5 files changed

Lines changed: 125 additions & 8 deletions

File tree

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5608,15 +5608,17 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
56085608
None => String::new(),
56095609
};
56105610
if let ty::PredicatePolarity::Positive = trait_predicate.polarity() {
5611+
// If the trait in question is unstable, mention that fact in the diagnostic.
5612+
// But if we're building with `-Zforce-unstable-if-unmarked` then _any_ trait
5613+
// not explicitly marked stable is considered unstable, so the extra text is
5614+
// unhelpful noise. See <https://github.com/rust-lang/rust/issues/152692>.
5615+
let mention_unstable = !tcx.sess.opts.unstable_opts.force_unstable_if_unmarked
5616+
&& try { tcx.lookup_stability(trait_predicate.def_id())?.level.is_stable() }
5617+
== Some(false);
5618+
let unstable = if mention_unstable { "nightly-only, unstable " } else { "" };
5619+
56115620
format!(
5612-
"{pre_message}the {}trait `{}` is not implemented for{desc} `{}`",
5613-
if tcx.lookup_stability(trait_predicate.def_id()).map(|s| s.level.is_stable())
5614-
== Some(false)
5615-
{
5616-
"nightly-only, unstable "
5617-
} else {
5618-
""
5619-
},
5621+
"{pre_message}the {unstable}trait `{}` is not implemented for{desc} `{}`",
56205622
trait_predicate.print_modifiers_and_trait_path(),
56215623
tcx.short_string(trait_predicate.self_ty().skip_binder(), long_ty_path),
56225624
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ edition: 2024
2+
//@ compile-flags: -Zforce-unstable-if-unmarked
3+
4+
// Auxiliary crate that uses `-Zforce-unstable-if-unmarked` to export an
5+
// "unstable" trait.
6+
7+
pub trait ForeignTrait {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0277]: the trait bound `(): LocalTrait` is not satisfied
2+
--> $DIR/nightly-only-unstable.rs:25:21
3+
|
4+
LL | use_local_trait(());
5+
| --------------- ^^ the trait `LocalTrait` is not implemented for `()`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
help: this trait has no implementations, consider adding one
10+
--> $DIR/nightly-only-unstable.rs:14:1
11+
|
12+
LL | trait LocalTrait {}
13+
| ^^^^^^^^^^^^^^^^
14+
note: required by a bound in `use_local_trait`
15+
--> $DIR/nightly-only-unstable.rs:16:28
16+
|
17+
LL | fn use_local_trait(_: impl LocalTrait) {}
18+
| ^^^^^^^^^^ required by this bound in `use_local_trait`
19+
20+
error[E0277]: the trait bound `(): ForeignTrait` is not satisfied
21+
--> $DIR/nightly-only-unstable.rs:31:23
22+
|
23+
LL | use_foreign_trait(());
24+
| ----------------- ^^ the trait `ForeignTrait` is not implemented for `()`
25+
| |
26+
| required by a bound introduced by this call
27+
|
28+
note: required by a bound in `use_foreign_trait`
29+
--> $DIR/nightly-only-unstable.rs:20:30
30+
|
31+
LL | fn use_foreign_trait(_: impl force_unstable::ForeignTrait) {}
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `use_foreign_trait`
33+
34+
error: aborting due to 2 previous errors
35+
36+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0277]: the trait bound `(): LocalTrait` is not satisfied
2+
--> $DIR/nightly-only-unstable.rs:25:21
3+
|
4+
LL | use_local_trait(());
5+
| --------------- ^^ the trait `LocalTrait` is not implemented for `()`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
help: this trait has no implementations, consider adding one
10+
--> $DIR/nightly-only-unstable.rs:14:1
11+
|
12+
LL | trait LocalTrait {}
13+
| ^^^^^^^^^^^^^^^^
14+
note: required by a bound in `use_local_trait`
15+
--> $DIR/nightly-only-unstable.rs:16:28
16+
|
17+
LL | fn use_local_trait(_: impl LocalTrait) {}
18+
| ^^^^^^^^^^ required by this bound in `use_local_trait`
19+
20+
error[E0277]: the trait bound `(): ForeignTrait` is not satisfied
21+
--> $DIR/nightly-only-unstable.rs:31:23
22+
|
23+
LL | use_foreign_trait(());
24+
| ----------------- ^^ the nightly-only, unstable trait `ForeignTrait` is not implemented for `()`
25+
| |
26+
| required by a bound introduced by this call
27+
|
28+
note: required by a bound in `use_foreign_trait`
29+
--> $DIR/nightly-only-unstable.rs:20:30
30+
|
31+
LL | fn use_foreign_trait(_: impl force_unstable::ForeignTrait) {}
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `use_foreign_trait`
33+
34+
error: aborting due to 2 previous errors
35+
36+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ revisions: normal force
2+
//@ edition: 2024
3+
//@ aux-crate: force_unstable=force_unstable.rs
4+
//@[force] compile-flags: -Zforce-unstable-if-unmarked
5+
6+
#![feature(rustc_private)]
7+
8+
// Regression test for <https://github.com/rust-lang/rust/issues/152692>.
9+
//
10+
// When building a crate with `-Zforce-unstable-if-unmarked` (e.g. the compiler or stdlib),
11+
// it's unhelpful to mention that a not-implemented trait is unstable, because that will
12+
// be true of every local and foreign trait that isn't explicitly marked stable.
13+
14+
trait LocalTrait {}
15+
16+
fn use_local_trait(_: impl LocalTrait) {}
17+
//~^ NOTE required by a bound in `use_local_trait`
18+
//~| NOTE required by this bound in `use_local_trait`
19+
20+
fn use_foreign_trait(_: impl force_unstable::ForeignTrait) {}
21+
//~^ NOTE required by a bound in `use_foreign_trait`
22+
//~| NOTE required by this bound in `use_foreign_trait`
23+
24+
fn main() {
25+
use_local_trait(());
26+
//~^ ERROR the trait bound `(): LocalTrait` is not satisfied
27+
//[normal]~| NOTE the trait `LocalTrait` is not implemented for `()`
28+
//[force]~| NOTE the trait `LocalTrait` is not implemented for `()`
29+
//~| NOTE required by a bound introduced by this call
30+
31+
use_foreign_trait(());
32+
//~^ ERROR the trait bound `(): ForeignTrait` is not satisfied
33+
//[normal]~| NOTE the nightly-only, unstable trait `ForeignTrait` is not implemented for `()`
34+
//[force]~| NOTE the trait `ForeignTrait` is not implemented for `()`
35+
//~| NOTE required by a bound introduced by this call
36+
}

0 commit comments

Comments
 (0)