Skip to content

Commit b36d15e

Browse files
committed
Do not apply #[do_not_recommend] if the feature flag is not set
This commit adds additional checks for the feature flag as apparently it is possible to use this on a beta compiler without feature flags. This PR might be a candidate for backporting. Reported in the bevy issue tracker: bevyengine/bevy#14591 (comment)
1 parent 176e545 commit b36d15e

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
682682
&self,
683683
obligation: &mut PredicateObligation<'tcx>,
684684
) -> bool {
685+
if !self.tcx.features().do_not_recommend {
686+
return false;
687+
}
688+
685689
let mut base_cause = obligation.cause.code().clone();
686690
let mut applied_do_not_recommend = false;
687691
loop {
@@ -1782,10 +1786,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17821786
.into_iter()
17831787
.cloned()
17841788
.filter(|cand| {
1785-
!self.tcx.has_attrs_with_path(
1786-
cand.impl_def_id,
1787-
&[sym::diagnostic, sym::do_not_recommend],
1788-
)
1789+
!self.tcx.features().do_not_recommend
1790+
|| !self.tcx.has_attrs_with_path(
1791+
cand.impl_def_id,
1792+
&[sym::diagnostic, sym::do_not_recommend],
1793+
)
17891794
})
17901795
.collect::<Vec<_>>();
17911796

@@ -1803,9 +1808,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18031808
.all_impls(def_id)
18041809
// ignore `do_not_recommend` items
18051810
.filter(|def_id| {
1806-
!self
1807-
.tcx
1808-
.has_attrs_with_path(*def_id, &[sym::diagnostic, sym::do_not_recommend])
1811+
!self.tcx.features().do_not_recommend
1812+
|| !self
1813+
.tcx
1814+
.has_attrs_with_path(*def_id, &[sym::diagnostic, sym::do_not_recommend])
18091815
})
18101816
// Ignore automatically derived impls and `!Trait` impls.
18111817
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![allow(unknown_or_malformed_diagnostic_attributes)]
2+
3+
trait Foo {}
4+
5+
#[diagnostic::do_not_recommend]
6+
impl<A> Foo for (A,) {}
7+
8+
#[diagnostic::do_not_recommend]
9+
impl<A, B> Foo for (A, B) {}
10+
11+
#[diagnostic::do_not_recommend]
12+
impl<A, B, C> Foo for (A, B, C) {}
13+
14+
impl Foo for i32 {}
15+
16+
fn check(a: impl Foo) {}
17+
18+
fn main() {
19+
check(());
20+
//~^ ERROR the trait bound `(): Foo` is not satisfied
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0277]: the trait bound `(): Foo` is not satisfied
2+
--> $DIR/do_not_apply_attribute_without_feature_flag.rs:19:11
3+
|
4+
LL | check(());
5+
| ----- ^^ the trait `Foo` is not implemented for `()`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the following other types implement trait `Foo`:
10+
(A, B)
11+
(A, B, C)
12+
(A,)
13+
note: required by a bound in `check`
14+
--> $DIR/do_not_apply_attribute_without_feature_flag.rs:16:18
15+
|
16+
LL | fn check(a: impl Foo) {}
17+
| ^^^ required by this bound in `check`
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)