Store do_not_recommend-ness in impl header

This commit is contained in:
Michael Goulet 2024-08-09 22:02:20 -04:00
parent 68d2e8a66e
commit ed7bdbb17b
5 changed files with 13 additions and 20 deletions

View File

@ -1699,6 +1699,8 @@ fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::ImplTrai
trait_ref: ty::EarlyBinder::bind(trait_ref), trait_ref: ty::EarlyBinder::bind(trait_ref),
safety: impl_.safety, safety: impl_.safety,
polarity: polarity_of_impl(tcx, def_id, impl_, item.span), polarity: polarity_of_impl(tcx, def_id, impl_, item.span),
do_not_recommend: tcx.features().do_not_recommend
&& tcx.has_attrs_with_path(def_id, &[sym::diagnostic, sym::do_not_recommend]),
} }
}) })
} }

View File

@ -3176,6 +3176,12 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn impl_polarity(self, def_id: impl IntoQueryParam<DefId>) -> ty::ImplPolarity { pub fn impl_polarity(self, def_id: impl IntoQueryParam<DefId>) -> ty::ImplPolarity {
self.impl_trait_header(def_id).map_or(ty::ImplPolarity::Positive, |h| h.polarity) self.impl_trait_header(def_id).map_or(ty::ImplPolarity::Positive, |h| h.polarity)
} }
/// Whether this is a trait implementation that has `#[diagnostic::do_not_recommend]`
pub fn do_not_recommend_impl(self, def_id: DefId) -> bool {
matches!(self.def_kind(def_id), DefKind::Impl { of_trait: true })
&& self.impl_trait_header(def_id).is_some_and(|header| header.do_not_recommend)
}
} }
/// Parameter attributes that can only be determined by examining the body of a function instead /// Parameter attributes that can only be determined by examining the body of a function instead

View File

@ -262,6 +262,7 @@ pub struct ImplTraitHeader<'tcx> {
pub trait_ref: ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>>, pub trait_ref: ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>>,
pub polarity: ImplPolarity, pub polarity: ImplPolarity,
pub safety: hir::Safety, pub safety: hir::Safety,
pub do_not_recommend: bool,
} }
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)] #[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)]

View File

@ -687,10 +687,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let mut applied_do_not_recommend = false; let mut applied_do_not_recommend = false;
loop { loop {
if let ObligationCauseCode::ImplDerived(ref c) = base_cause { if let ObligationCauseCode::ImplDerived(ref c) = base_cause {
if self.tcx.has_attrs_with_path( if self.tcx.do_not_recommend_impl(c.impl_or_alias_def_id) {
c.impl_or_alias_def_id,
&[sym::diagnostic, sym::do_not_recommend],
) {
let code = (*c.derived.parent_code).clone(); let code = (*c.derived.parent_code).clone();
obligation.cause.map_code(|_| code); obligation.cause.map_code(|_| code);
obligation.predicate = c.derived.parent_trait_pred.upcast(self.tcx); obligation.predicate = c.derived.parent_trait_pred.upcast(self.tcx);
@ -1630,11 +1627,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
.tcx .tcx
.all_impls(def_id) .all_impls(def_id)
// ignore `do_not_recommend` items // ignore `do_not_recommend` items
.filter(|def_id| { .filter(|def_id| !self.tcx.do_not_recommend_impl(*def_id))
!self
.tcx
.has_attrs_with_path(*def_id, &[sym::diagnostic, sym::do_not_recommend])
})
// Ignore automatically derived impls and `!Trait` impls. // Ignore automatically derived impls and `!Trait` impls.
.filter_map(|def_id| self.tcx.impl_trait_header(def_id)) .filter_map(|def_id| self.tcx.impl_trait_header(def_id))
.filter_map(|header| { .filter_map(|header| {
@ -1904,12 +1897,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let impl_candidates = impl_candidates let impl_candidates = impl_candidates
.into_iter() .into_iter()
.cloned() .cloned()
.filter(|cand| { .filter(|cand| !self.tcx.do_not_recommend_impl(cand.impl_def_id))
!self.tcx.has_attrs_with_path(
cand.impl_def_id,
&[sym::diagnostic, sym::do_not_recommend],
)
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let def_id = trait_ref.def_id(); let def_id = trait_ref.def_id();

View File

@ -13,7 +13,6 @@ use rustc_middle::bug;
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_next_trait_solver::solve::{GenerateProofTree, SolverDelegateEvalExt as _}; use rustc_next_trait_solver::solve::{GenerateProofTree, SolverDelegateEvalExt as _};
use rustc_span::symbol::sym;
use super::delegate::SolverDelegate; use super::delegate::SolverDelegate;
use super::inspect::{self, ProofTreeInferCtxtExt, ProofTreeVisitor}; use super::inspect::{self, ProofTreeInferCtxtExt, ProofTreeVisitor};
@ -440,10 +439,7 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
source: CandidateSource::Impl(impl_def_id), source: CandidateSource::Impl(impl_def_id),
result: _, result: _,
} = candidate.kind() } = candidate.kind()
&& goal && goal.infcx().tcx.do_not_recommend_impl(impl_def_id)
.infcx()
.tcx
.has_attrs_with_path(impl_def_id, &[sym::diagnostic, sym::do_not_recommend])
{ {
return ControlFlow::Break(self.obligation.clone()); return ControlFlow::Break(self.obligation.clone());
} }