1
Fork 0

drive-by: use is_const and is_const_if_const

This commit is contained in:
Michael Goulet 2022-01-26 19:24:01 -08:00
parent 1ab97dbc52
commit c6de4d55aa
7 changed files with 16 additions and 15 deletions

View file

@ -7,7 +7,6 @@ use crate::interpret::{
}; };
use rustc_errors::ErrorReported; use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_middle::mir; use rustc_middle::mir;
use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::mir::interpret::ErrorHandled;
@ -216,7 +215,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>, key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> { ) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
assert!(key.param_env.constness() == hir::Constness::Const); assert!(key.param_env.is_const());
// see comment in eval_to_allocation_raw_provider for what we're doing here // see comment in eval_to_allocation_raw_provider for what we're doing here
if key.param_env.reveal() == Reveal::All { if key.param_env.reveal() == Reveal::All {
let mut key = key; let mut key = key;
@ -251,7 +250,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>, key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> { ) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
assert!(key.param_env.constness() == hir::Constness::Const); assert!(key.param_env.is_const());
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of // Because the constant is computed twice (once per value of `Reveal`), we are at risk of
// reporting the same error twice here. To resolve this, we check whether we can evaluate the // reporting the same error twice here. To resolve this, we check whether we can evaluate the
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was // constant in the more restrictive `Reveal::UserFacing`, which most likely already was

View file

@ -86,7 +86,6 @@ declare_lint_pass!(
impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints { impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
use rustc_middle::ty;
use rustc_middle::ty::PredicateKind::*; use rustc_middle::ty::PredicateKind::*;
let predicates = cx.tcx.explicit_predicates_of(item.def_id); let predicates = cx.tcx.explicit_predicates_of(item.def_id);
@ -94,7 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
let Trait(trait_predicate) = predicate.kind().skip_binder() else { let Trait(trait_predicate) = predicate.kind().skip_binder() else {
continue continue
}; };
if trait_predicate.constness == ty::BoundConstness::ConstIfConst { if trait_predicate.is_const_if_const() {
// `~const Drop` definitely have meanings so avoid linting here. // `~const Drop` definitely have meanings so avoid linting here.
continue; continue;
} }

View file

@ -784,6 +784,11 @@ impl<'tcx> TraitPredicate<'tcx> {
pub fn self_ty(self) -> Ty<'tcx> { pub fn self_ty(self) -> Ty<'tcx> {
self.trait_ref.self_ty() self.trait_ref.self_ty()
} }
#[inline]
pub fn is_const_if_const(self) -> bool {
self.constness == BoundConstness::ConstIfConst
}
} }
impl<'tcx> PolyTraitPredicate<'tcx> { impl<'tcx> PolyTraitPredicate<'tcx> {
@ -804,8 +809,9 @@ impl<'tcx> PolyTraitPredicate<'tcx> {
}); });
} }
pub fn is_const(self) -> bool { #[inline]
self.skip_binder().constness == BoundConstness::ConstIfConst pub fn is_const_if_const(self) -> bool {
self.skip_binder().is_const_if_const()
} }
} }
@ -1392,6 +1398,7 @@ impl<'tcx> ParamEnv<'tcx> {
self.packed.tag().constness self.packed.tag().constness
} }
#[inline]
pub fn is_const(self) -> bool { pub fn is_const(self) -> bool {
self.packed.tag().constness == hir::Constness::Const self.packed.tag().constness == hir::Constness::Const
} }

View file

@ -440,7 +440,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
err.span_label(span, explanation); err.span_label(span, explanation);
} }
if trait_predicate.is_const() && obligation.param_env.is_const() { if trait_predicate.is_const_if_const() && obligation.param_env.is_const() {
let non_const_predicate = trait_ref.without_const(); let non_const_predicate = trait_ref.without_const();
let non_const_obligation = Obligation { let non_const_obligation = Obligation {
cause: obligation.cause.clone(), cause: obligation.cause.clone(),

View file

@ -305,7 +305,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
} else if lang_items.unsize_trait() == Some(def_id) { } else if lang_items.unsize_trait() == Some(def_id) {
self.assemble_candidates_for_unsizing(obligation, &mut candidates); self.assemble_candidates_for_unsizing(obligation, &mut candidates);
} else if lang_items.drop_trait() == Some(def_id) } else if lang_items.drop_trait() == Some(def_id)
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::ConstIfConst && obligation.predicate.is_const_if_const()
{ {
self.assemble_const_drop_candidates(obligation, &mut candidates); self.assemble_const_drop_candidates(obligation, &mut candidates);
} else { } else {

View file

@ -72,9 +72,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// CheckPredicate(&A: Super) // CheckPredicate(&A: Super)
// CheckPredicate(A: ~const Super) // <- still const env, failure // CheckPredicate(A: ~const Super) // <- still const env, failure
// ``` // ```
if obligation.param_env.constness() == Constness::Const if obligation.param_env.is_const() && !obligation.predicate.is_const_if_const() {
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::NotConst
{
new_obligation = TraitObligation { new_obligation = TraitObligation {
cause: obligation.cause.clone(), cause: obligation.cause.clone(),
param_env: obligation.param_env.without_const(), param_env: obligation.param_env.without_const(),

View file

@ -1173,9 +1173,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
ImplCandidate(def_id) ImplCandidate(def_id)
if tcx.impl_constness(def_id) == hir::Constness::Const => {} if tcx.impl_constness(def_id) == hir::Constness::Const => {}
// const param // const param
ParamCandidate(trait_pred) ParamCandidate(trait_pred) if trait_pred.is_const_if_const() => {}
if trait_pred.skip_binder().constness
== ty::BoundConstness::ConstIfConst => {}
// auto trait impl // auto trait impl
AutoImplCandidate(..) => {} AutoImplCandidate(..) => {}
// generator, this will raise error in other places // generator, this will raise error in other places