Skip to content

Commit 0db3168

Browse files
committed
Auto merge of rust-lang#128912 - compiler-errors:do-not-recommend-impl, r=<try>
Store `do_not_recommend`-ness in impl header Alternative to rust-lang#128674 It's less flexible, but also less invasive. Hopefully it's also performant. I'd recommend we think separately about the design for how to gate arbitrary diagnostic attributes moving forward.
2 parents 68d2e8a + 20a16bb commit 0db3168

File tree

7 files changed

+55
-20
lines changed

7 files changed

+55
-20
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,8 @@ fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::ImplTrai
16991699
trait_ref: ty::EarlyBinder::bind(trait_ref),
17001700
safety: impl_.safety,
17011701
polarity: polarity_of_impl(tcx, def_id, impl_, item.span),
1702+
do_not_recommend: tcx.features().do_not_recommend
1703+
&& tcx.has_attrs_with_path(def_id, &[sym::diagnostic, sym::do_not_recommend]),
17021704
}
17031705
})
17041706
}

compiler/rustc_middle/src/ty/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3176,6 +3176,12 @@ impl<'tcx> TyCtxt<'tcx> {
31763176
pub fn impl_polarity(self, def_id: impl IntoQueryParam<DefId>) -> ty::ImplPolarity {
31773177
self.impl_trait_header(def_id).map_or(ty::ImplPolarity::Positive, |h| h.polarity)
31783178
}
3179+
3180+
/// Whether this is a trait implementation that has `#[diagnostic::do_not_recommend]`
3181+
pub fn do_not_recommend_impl(self, def_id: DefId) -> bool {
3182+
matches!(self.def_kind(def_id), DefKind::Impl { of_trait: true })
3183+
&& self.impl_trait_header(def_id).is_some_and(|header| header.do_not_recommend)
3184+
}
31793185
}
31803186

31813187
/// Parameter attributes that can only be determined by examining the body of a function instead

compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ pub struct ImplTraitHeader<'tcx> {
262262
pub trait_ref: ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>>,
263263
pub polarity: ImplPolarity,
264264
pub safety: hir::Safety,
265+
pub do_not_recommend: bool,
265266
}
266267

267268
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)]

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

+3-15
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
687687
let mut applied_do_not_recommend = false;
688688
loop {
689689
if let ObligationCauseCode::ImplDerived(ref c) = base_cause {
690-
if self.tcx.has_attrs_with_path(
691-
c.impl_or_alias_def_id,
692-
&[sym::diagnostic, sym::do_not_recommend],
693-
) {
690+
if self.tcx.do_not_recommend_impl(c.impl_or_alias_def_id) {
694691
let code = (*c.derived.parent_code).clone();
695692
obligation.cause.map_code(|_| code);
696693
obligation.predicate = c.derived.parent_trait_pred.upcast(self.tcx);
@@ -1630,11 +1627,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16301627
.tcx
16311628
.all_impls(def_id)
16321629
// ignore `do_not_recommend` items
1633-
.filter(|def_id| {
1634-
!self
1635-
.tcx
1636-
.has_attrs_with_path(*def_id, &[sym::diagnostic, sym::do_not_recommend])
1637-
})
1630+
.filter(|def_id| !self.tcx.do_not_recommend_impl(*def_id))
16381631
// Ignore automatically derived impls and `!Trait` impls.
16391632
.filter_map(|def_id| self.tcx.impl_trait_header(def_id))
16401633
.filter_map(|header| {
@@ -1904,12 +1897,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19041897
let impl_candidates = impl_candidates
19051898
.into_iter()
19061899
.cloned()
1907-
.filter(|cand| {
1908-
!self.tcx.has_attrs_with_path(
1909-
cand.impl_def_id,
1910-
&[sym::diagnostic, sym::do_not_recommend],
1911-
)
1912-
})
1900+
.filter(|cand| !self.tcx.do_not_recommend_impl(cand.impl_def_id))
19131901
.collect::<Vec<_>>();
19141902

19151903
let def_id = trait_ref.def_id();

compiler/rustc_trait_selection/src/solve/fulfill.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_middle::bug;
1313
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1414
use rustc_middle::ty::{self, TyCtxt};
1515
use rustc_next_trait_solver::solve::{GenerateProofTree, SolverDelegateEvalExt as _};
16-
use rustc_span::symbol::sym;
1716

1817
use super::delegate::SolverDelegate;
1918
use super::inspect::{self, ProofTreeInferCtxtExt, ProofTreeVisitor};
@@ -440,10 +439,7 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
440439
source: CandidateSource::Impl(impl_def_id),
441440
result: _,
442441
} = candidate.kind()
443-
&& goal
444-
.infcx()
445-
.tcx
446-
.has_attrs_with_path(impl_def_id, &[sym::diagnostic, sym::do_not_recommend])
442+
&& goal.infcx().tcx.do_not_recommend_impl(impl_def_id)
447443
{
448444
return ControlFlow::Break(self.obligation.clone());
449445
}
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)