Skip to content

Commit d51068c

Browse files
oli-obkfee1-dead
authored andcommitted
Use the constness from the param env instead of having a separate dimension for it
This breaks a ~const test that will be fixed in a follow up commit of this PR
1 parent 19f2101 commit d51068c

File tree

12 files changed

+25
-142
lines changed

12 files changed

+25
-142
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
817817
);
818818

819819
let implsrc = tcx.infer_ctxt().enter(|infcx| {
820-
let mut selcx =
821-
SelectionContext::with_constness(&infcx, hir::Constness::Const);
820+
let mut selcx = SelectionContext::new(&infcx);
822821
selcx.select(&obligation)
823822
});
824823

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! See the `Qualif` trait for more info.
44
55
use rustc_errors::ErrorReported;
6-
use rustc_hir as hir;
76
use rustc_infer::infer::TyCtxtInferExt;
87
use rustc_middle::mir::*;
98
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty};
@@ -167,7 +166,7 @@ impl Qualif for NeedsNonConstDrop {
167166
);
168167

169168
let implsrc = cx.tcx.infer_ctxt().enter(|infcx| {
170-
let mut selcx = SelectionContext::with_constness(&infcx, hir::Constness::Const);
169+
let mut selcx = SelectionContext::new(&infcx);
171170
selcx.select(&obligation)
172171
});
173172
!matches!(

compiler/rustc_infer/src/traits/engine.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::infer::InferCtxt;
22
use crate::traits::Obligation;
33
use rustc_data_structures::fx::FxHashMap;
4-
use rustc_hir as hir;
54
use rustc_hir::def_id::DefId;
65
use rustc_middle::ty::{self, ToPredicate, Ty, WithConstness};
76

@@ -48,25 +47,10 @@ pub trait TraitEngine<'tcx>: 'tcx {
4847

4948
fn select_all_or_error(&mut self, infcx: &InferCtxt<'_, 'tcx>) -> Vec<FulfillmentError<'tcx>>;
5049

51-
fn select_all_with_constness_or_error(
50+
fn select_where_possible(
5251
&mut self,
5352
infcx: &InferCtxt<'_, 'tcx>,
54-
_constness: hir::Constness,
55-
) -> Vec<FulfillmentError<'tcx>> {
56-
self.select_all_or_error(infcx)
57-
}
58-
59-
fn select_where_possible(&mut self, infcx: &InferCtxt<'_, 'tcx>)
60-
-> Vec<FulfillmentError<'tcx>>;
61-
62-
// FIXME(fee1-dead) this should not provide a default body for chalk as chalk should be updated
63-
fn select_with_constness_where_possible(
64-
&mut self,
65-
infcx: &InferCtxt<'_, 'tcx>,
66-
_constness: hir::Constness,
67-
) -> Vec<FulfillmentError<'tcx>> {
68-
self.select_where_possible(infcx)
69-
}
53+
) -> Vec<FulfillmentError<'tcx>>;
7054

7155
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
7256

compiler/rustc_infer/src/traits/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ impl PredicateObligation<'tcx> {
6969
}
7070
}
7171

72+
impl TraitObligation<'tcx> {
73+
/// Returns `true` if the trait predicate is considered `const` in its ParamEnv.
74+
pub fn is_const(&self) -> bool {
75+
match (self.predicate.skip_binder().constness, self.param_env.constness()) {
76+
(ty::BoundConstness::ConstIfConst, hir::Constness::Const) => true,
77+
_ => false,
78+
}
79+
}
80+
}
81+
7282
// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
7383
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
7484
static_assert_size!(PredicateObligation<'_>, 32);

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_data_structures::obligation_forest::ProcessResult;
44
use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome};
55
use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
66
use rustc_errors::ErrorReported;
7-
use rustc_hir as hir;
87
use rustc_infer::traits::{SelectionError, TraitEngine, TraitEngineExt as _, TraitObligation};
98
use rustc_middle::mir::interpret::ErrorHandled;
109
use rustc_middle::thir::abstract_const::NotConstEvaluatable;
@@ -231,21 +230,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
231230
self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
232231
}
233232

234-
fn select_all_with_constness_or_error(
235-
&mut self,
236-
infcx: &InferCtxt<'_, 'tcx>,
237-
constness: rustc_hir::Constness,
238-
) -> Vec<FulfillmentError<'tcx>> {
239-
{
240-
let errors = self.select_with_constness_where_possible(infcx, constness);
241-
if !errors.is_empty() {
242-
return errors;
243-
}
244-
}
245-
246-
self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
247-
}
248-
249233
fn select_where_possible(
250234
&mut self,
251235
infcx: &InferCtxt<'_, 'tcx>,
@@ -254,15 +238,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
254238
self.select(&mut selcx)
255239
}
256240

257-
fn select_with_constness_where_possible(
258-
&mut self,
259-
infcx: &InferCtxt<'_, 'tcx>,
260-
constness: hir::Constness,
261-
) -> Vec<FulfillmentError<'tcx>> {
262-
let mut selcx = SelectionContext::with_constness(infcx, constness);
263-
self.select(&mut selcx)
264-
}
265-
266241
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
267242
self.predicates.map_pending_obligations(|o| o.obligation.clone())
268243
}
@@ -679,12 +654,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
679654
if obligation.predicate.is_known_global() {
680655
// no type variables present, can use evaluation for better caching.
681656
// FIXME: consider caching errors too.
682-
//
683-
// If the predicate is considered const, then we cannot use this because
684-
// it will cause false negatives in the ui tests.
685-
if !self.selcx.is_predicate_const(obligation.predicate)
686-
&& infcx.predicate_must_hold_considering_regions(obligation)
687-
{
657+
if infcx.predicate_must_hold_considering_regions(obligation) {
688658
debug!(
689659
"selecting trait at depth {} evaluated to holds",
690660
obligation.recursion_depth
@@ -738,12 +708,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
738708
if obligation.predicate.is_global(tcx) {
739709
// no type variables present, can use evaluation for better caching.
740710
// FIXME: consider caching errors too.
741-
//
742-
// If the predicate is considered const, then we cannot use this because
743-
// it will cause false negatives in the ui tests.
744-
if !self.selcx.is_predicate_const(obligation.predicate)
745-
&& self.selcx.infcx().predicate_must_hold_considering_regions(obligation)
746-
{
711+
if self.selcx.infcx().predicate_must_hold_considering_regions(obligation) {
747712
return ProcessResult::Changed(vec![]);
748713
} else {
749714
tracing::debug!("Does NOT hold: {:?}", obligation);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
303303
} else if lang_items.drop_trait() == Some(def_id)
304304
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::ConstIfConst
305305
{
306-
if self.is_in_const_context {
306+
if obligation.param_env.constness() == hir::Constness::Const {
307307
self.assemble_const_drop_candidates(obligation, stack, &mut candidates)?;
308308
} else {
309309
debug!("passing ~const Drop bound; in non-const context");

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ pub struct SelectionContext<'cx, 'tcx> {
128128
/// and a negative impl
129129
allow_negative_impls: bool,
130130

131-
/// Are we in a const context that needs `~const` bounds to be const?
132-
is_in_const_context: bool,
133-
134131
/// The mode that trait queries run in, which informs our error handling
135132
/// policy. In essence, canonicalized queries need their errors propagated
136133
/// rather than immediately reported because we do not have accurate spans.
@@ -222,7 +219,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
222219
intercrate: false,
223220
intercrate_ambiguity_causes: None,
224221
allow_negative_impls: false,
225-
is_in_const_context: false,
226222
query_mode: TraitQueryMode::Standard,
227223
}
228224
}
@@ -234,7 +230,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
234230
intercrate: true,
235231
intercrate_ambiguity_causes: None,
236232
allow_negative_impls: false,
237-
is_in_const_context: false,
238233
query_mode: TraitQueryMode::Standard,
239234
}
240235
}
@@ -250,7 +245,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
250245
intercrate: false,
251246
intercrate_ambiguity_causes: None,
252247
allow_negative_impls,
253-
is_in_const_context: false,
254248
query_mode: TraitQueryMode::Standard,
255249
}
256250
}
@@ -266,26 +260,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
266260
intercrate: false,
267261
intercrate_ambiguity_causes: None,
268262
allow_negative_impls: false,
269-
is_in_const_context: false,
270263
query_mode,
271264
}
272265
}
273266

274-
pub fn with_constness(
275-
infcx: &'cx InferCtxt<'cx, 'tcx>,
276-
constness: hir::Constness,
277-
) -> SelectionContext<'cx, 'tcx> {
278-
SelectionContext {
279-
infcx,
280-
freshener: infcx.freshener_keep_static(),
281-
intercrate: false,
282-
intercrate_ambiguity_causes: None,
283-
allow_negative_impls: false,
284-
is_in_const_context: matches!(constness, hir::Constness::Const),
285-
query_mode: TraitQueryMode::Standard,
286-
}
287-
}
288-
289267
/// Enables tracking of intercrate ambiguity causes. These are
290268
/// used in coherence to give improved diagnostics. We don't do
291269
/// this until we detect a coherence error because it can lead to
@@ -318,20 +296,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
318296
self.intercrate
319297
}
320298

321-
/// Returns `true` if the trait predicate is considerd `const` to this selection context.
322-
pub fn is_trait_predicate_const(&self, pred: ty::TraitPredicate<'_>) -> bool {
323-
matches!(pred.constness, ty::BoundConstness::ConstIfConst) && self.is_in_const_context
324-
}
325-
326-
/// Returns `true` if the predicate is considered `const` to
327-
/// this selection context.
328-
pub fn is_predicate_const(&self, pred: ty::Predicate<'_>) -> bool {
329-
match pred.kind().skip_binder() {
330-
ty::PredicateKind::Trait(pred) => self.is_trait_predicate_const(pred),
331-
_ => false,
332-
}
333-
}
334-
335299
///////////////////////////////////////////////////////////////////////////
336300
// Selection
337301
//
@@ -1138,7 +1102,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11381102

11391103
for candidate in candidates {
11401104
// Respect const trait obligations
1141-
if self.is_trait_predicate_const(obligation.predicate.skip_binder()) {
1105+
if obligation.is_const() {
11421106
match candidate {
11431107
// const impl
11441108
ImplCandidate(def_id)

compiler/rustc_typeck/src/check/compare_method.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,13 +1388,7 @@ pub fn check_type_bounds<'tcx>(
13881388
impl_ty_substs.rebase_onto(tcx, impl_ty.container.id(), impl_trait_ref.substs);
13891389

13901390
tcx.infer_ctxt().enter(move |infcx| {
1391-
let constness = impl_ty
1392-
.container
1393-
.impl_def_id()
1394-
.map(|did| tcx.impl_constness(did))
1395-
.unwrap_or(hir::Constness::NotConst);
1396-
1397-
let inh = Inherited::with_constness(infcx, impl_ty.def_id.expect_local(), constness);
1391+
let inh = Inherited::new(infcx, impl_ty.def_id.expect_local());
13981392
let infcx = &inh.infcx;
13991393
let mut selcx = traits::SelectionContext::new(&infcx);
14001394

@@ -1439,7 +1433,7 @@ pub fn check_type_bounds<'tcx>(
14391433
// Check that all obligations are satisfied by the implementation's
14401434
// version.
14411435
let errors =
1442-
inh.fulfillment_cx.borrow_mut().select_all_with_constness_or_error(&infcx, constness);
1436+
inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx);
14431437
if !errors.is_empty() {
14441438
infcx.report_fulfillment_errors(&errors, None, false);
14451439
return Err(ErrorReported);

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
613613
let errors = self
614614
.fulfillment_cx
615615
.borrow_mut()
616-
.select_all_with_constness_or_error(&self, self.inh.constness);
616+
.select_all_or_error(&self);
617617

618618
if !errors.is_empty() {
619619
self.report_fulfillment_errors(&errors, self.inh.body_id, false);
@@ -629,7 +629,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
629629
let mut result = self
630630
.fulfillment_cx
631631
.borrow_mut()
632-
.select_with_constness_where_possible(self, self.inh.constness);
632+
.select_where_possible(self);
633633
if !result.is_empty() {
634634
mutate_fulfillment_errors(&mut result);
635635
self.report_fulfillment_errors(&result, self.inh.body_id, fallback_has_occurred);

compiler/rustc_typeck/src/check/inherited.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ pub struct Inherited<'a, 'tcx> {
5353
pub(super) deferred_generator_interiors:
5454
RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
5555

56-
/// Reports whether this is in a const context.
57-
pub(super) constness: hir::Constness,
58-
5956
pub(super) body_id: Option<hir::BodyId>,
6057

6158
/// Whenever we introduce an adjustment from `!` into a type variable,
@@ -102,16 +99,6 @@ impl<'tcx> InheritedBuilder<'tcx> {
10299

103100
impl Inherited<'a, 'tcx> {
104101
pub(super) fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
105-
let tcx = infcx.tcx;
106-
let item_id = tcx.hir().local_def_id_to_hir_id(def_id);
107-
Self::with_constness(infcx, def_id, tcx.hir().get(item_id).constness_for_typeck())
108-
}
109-
110-
pub(super) fn with_constness(
111-
infcx: InferCtxt<'a, 'tcx>,
112-
def_id: LocalDefId,
113-
constness: hir::Constness,
114-
) -> Self {
115102
let tcx = infcx.tcx;
116103
let item_id = tcx.hir().local_def_id_to_hir_id(def_id);
117104
let body_id = tcx.hir().maybe_body_owned_by(item_id);
@@ -128,7 +115,6 @@ impl Inherited<'a, 'tcx> {
128115
deferred_cast_checks: RefCell::new(Vec::new()),
129116
deferred_generator_interiors: RefCell::new(Vec::new()),
130117
diverging_type_vars: RefCell::new(Default::default()),
131-
constness,
132118
body_id,
133119
}
134120
}

0 commit comments

Comments
 (0)