Skip to content

Commit ed7bdbb

Browse files
Store do_not_recommend-ness in impl header
1 parent 68d2e8a commit ed7bdbb

File tree

5 files changed

+13
-20
lines changed

5 files changed

+13
-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
}

0 commit comments

Comments
 (0)