Skip to content

Commit c6de4d5

Browse files
drive-by: use is_const and is_const_if_const
1 parent 1ab97db commit c6de4d5

File tree

7 files changed

+16
-15
lines changed

7 files changed

+16
-15
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::interpret::{
77
};
88

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

compiler/rustc_lint/src/traits.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,14 @@ declare_lint_pass!(
8686

8787
impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
8888
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
89-
use rustc_middle::ty;
9089
use rustc_middle::ty::PredicateKind::*;
9190

9291
let predicates = cx.tcx.explicit_predicates_of(item.def_id);
9392
for &(predicate, span) in predicates.predicates {
9493
let Trait(trait_predicate) = predicate.kind().skip_binder() else {
9594
continue
9695
};
97-
if trait_predicate.constness == ty::BoundConstness::ConstIfConst {
96+
if trait_predicate.is_const_if_const() {
9897
// `~const Drop` definitely have meanings so avoid linting here.
9998
continue;
10099
}

compiler/rustc_middle/src/ty/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,11 @@ impl<'tcx> TraitPredicate<'tcx> {
784784
pub fn self_ty(self) -> Ty<'tcx> {
785785
self.trait_ref.self_ty()
786786
}
787+
788+
#[inline]
789+
pub fn is_const_if_const(self) -> bool {
790+
self.constness == BoundConstness::ConstIfConst
791+
}
787792
}
788793

789794
impl<'tcx> PolyTraitPredicate<'tcx> {
@@ -804,8 +809,9 @@ impl<'tcx> PolyTraitPredicate<'tcx> {
804809
});
805810
}
806811

807-
pub fn is_const(self) -> bool {
808-
self.skip_binder().constness == BoundConstness::ConstIfConst
812+
#[inline]
813+
pub fn is_const_if_const(self) -> bool {
814+
self.skip_binder().is_const_if_const()
809815
}
810816
}
811817

@@ -1392,6 +1398,7 @@ impl<'tcx> ParamEnv<'tcx> {
13921398
self.packed.tag().constness
13931399
}
13941400

1401+
#[inline]
13951402
pub fn is_const(self) -> bool {
13961403
self.packed.tag().constness == hir::Constness::Const
13971404
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
440440
err.span_label(span, explanation);
441441
}
442442

443-
if trait_predicate.is_const() && obligation.param_env.is_const() {
443+
if trait_predicate.is_const_if_const() && obligation.param_env.is_const() {
444444
let non_const_predicate = trait_ref.without_const();
445445
let non_const_obligation = Obligation {
446446
cause: obligation.cause.clone(),

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
305305
} else if lang_items.unsize_trait() == Some(def_id) {
306306
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
307307
} else if lang_items.drop_trait() == Some(def_id)
308-
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::ConstIfConst
308+
&& obligation.predicate.is_const_if_const()
309309
{
310310
self.assemble_const_drop_candidates(obligation, &mut candidates);
311311
} else {

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
7272
// CheckPredicate(&A: Super)
7373
// CheckPredicate(A: ~const Super) // <- still const env, failure
7474
// ```
75-
if obligation.param_env.constness() == Constness::Const
76-
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::NotConst
77-
{
75+
if obligation.param_env.is_const() && !obligation.predicate.is_const_if_const() {
7876
new_obligation = TraitObligation {
7977
cause: obligation.cause.clone(),
8078
param_env: obligation.param_env.without_const(),

compiler/rustc_trait_selection/src/traits/select/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1173,9 +1173,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11731173
ImplCandidate(def_id)
11741174
if tcx.impl_constness(def_id) == hir::Constness::Const => {}
11751175
// const param
1176-
ParamCandidate(trait_pred)
1177-
if trait_pred.skip_binder().constness
1178-
== ty::BoundConstness::ConstIfConst => {}
1176+
ParamCandidate(trait_pred) if trait_pred.is_const_if_const() => {}
11791177
// auto trait impl
11801178
AutoImplCandidate(..) => {}
11811179
// generator, this will raise error in other places

0 commit comments

Comments
 (0)