Skip to content

Commit 3193cf7

Browse files
committed
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 fce93cb commit 3193cf7

File tree

12 files changed

+23
-155
lines changed

12 files changed

+23
-155
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
@@ -859,8 +859,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
859859
);
860860

861861
let implsrc = tcx.infer_ctxt().enter(|infcx| {
862-
let mut selcx =
863-
SelectionContext::with_constness(&infcx, hir::Constness::Const);
862+
let mut selcx = SelectionContext::new(&infcx);
864863
selcx.select(&obligation)
865864
});
866865

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
match implsrc {

compiler/rustc_infer/src/traits/engine.rs

Lines changed: 0 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

@@ -51,28 +50,11 @@ pub trait TraitEngine<'tcx>: 'tcx {
5150
infcx: &InferCtxt<'_, 'tcx>,
5251
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
5352

54-
fn select_all_with_constness_or_error(
55-
&mut self,
56-
infcx: &InferCtxt<'_, 'tcx>,
57-
_constness: hir::Constness,
58-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
59-
self.select_all_or_error(infcx)
60-
}
61-
6253
fn select_where_possible(
6354
&mut self,
6455
infcx: &InferCtxt<'_, 'tcx>,
6556
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
6657

67-
// FIXME(fee1-dead) this should not provide a default body for chalk as chalk should be updated
68-
fn select_with_constness_where_possible(
69-
&mut self,
70-
infcx: &InferCtxt<'_, 'tcx>,
71-
_constness: hir::Constness,
72-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
73-
self.select_where_possible(infcx)
74-
}
75-
7658
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
7759

7860
fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships>;

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 & 38 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;
@@ -238,22 +237,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
238237
if errors.is_empty() { Ok(()) } else { Err(errors) }
239238
}
240239

241-
fn select_all_with_constness_or_error(
242-
&mut self,
243-
infcx: &InferCtxt<'_, 'tcx>,
244-
constness: rustc_hir::Constness,
245-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
246-
self.select_with_constness_where_possible(infcx, constness)?;
247-
248-
let errors: Vec<_> = self
249-
.predicates
250-
.to_errors(CodeAmbiguity)
251-
.into_iter()
252-
.map(to_fulfillment_error)
253-
.collect();
254-
if errors.is_empty() { Ok(()) } else { Err(errors) }
255-
}
256-
257240
fn select_where_possible(
258241
&mut self,
259242
infcx: &InferCtxt<'_, 'tcx>,
@@ -262,15 +245,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
262245
self.select(&mut selcx)
263246
}
264247

265-
fn select_with_constness_where_possible(
266-
&mut self,
267-
infcx: &InferCtxt<'_, 'tcx>,
268-
constness: hir::Constness,
269-
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
270-
let mut selcx = SelectionContext::with_constness(infcx, constness);
271-
self.select(&mut selcx)
272-
}
273-
274248
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
275249
self.predicates.map_pending_obligations(|o| o.obligation.clone())
276250
}
@@ -687,12 +661,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
687661
if obligation.predicate.is_known_global() {
688662
// no type variables present, can use evaluation for better caching.
689663
// FIXME: consider caching errors too.
690-
//
691-
// If the predicate is considered const, then we cannot use this because
692-
// it will cause false negatives in the ui tests.
693-
if !self.selcx.is_predicate_const(obligation.predicate)
694-
&& infcx.predicate_must_hold_considering_regions(obligation)
695-
{
664+
if infcx.predicate_must_hold_considering_regions(obligation) {
696665
debug!(
697666
"selecting trait at depth {} evaluated to holds",
698667
obligation.recursion_depth
@@ -746,12 +715,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
746715
if obligation.predicate.is_global(tcx) {
747716
// no type variables present, can use evaluation for better caching.
748717
// FIXME: consider caching errors too.
749-
//
750-
// If the predicate is considered const, then we cannot use this because
751-
// it will cause false negatives in the ui tests.
752-
if !self.selcx.is_predicate_const(obligation.predicate)
753-
&& self.selcx.infcx().predicate_must_hold_considering_regions(obligation)
754-
{
718+
if self.selcx.infcx().predicate_must_hold_considering_regions(obligation) {
755719
return ProcessResult::Changed(vec![]);
756720
} else {
757721
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, &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 & 40 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,23 +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-
match pred.constness {
324-
ty::BoundConstness::ConstIfConst if self.is_in_const_context => true,
325-
_ => false,
326-
}
327-
}
328-
329-
/// Returns `true` if the predicate is considered `const` to
330-
/// this selection context.
331-
pub fn is_predicate_const(&self, pred: ty::Predicate<'_>) -> bool {
332-
match pred.kind().skip_binder() {
333-
ty::PredicateKind::Trait(pred) => self.is_trait_predicate_const(pred),
334-
_ => false,
335-
}
336-
}
337-
338299
///////////////////////////////////////////////////////////////////////////
339300
// Selection
340301
//
@@ -1141,7 +1102,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11411102

11421103
for candidate in candidates {
11431104
// Respect const trait obligations
1144-
if self.is_trait_predicate_const(obligation.predicate.skip_binder()) {
1105+
if obligation.is_const() {
11451106
match candidate {
11461107
// const impl
11471108
ImplCandidate(def_id)

compiler/rustc_typeck/src/check/compare_method.rs

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

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

@@ -1436,9 +1430,7 @@ pub fn check_type_bounds<'tcx>(
14361430

14371431
// Check that all obligations are satisfied by the implementation's
14381432
// version.
1439-
if let Err(ref errors) =
1440-
inh.fulfillment_cx.borrow_mut().select_all_with_constness_or_error(&infcx, constness)
1441-
{
1433+
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
14421434
infcx.report_fulfillment_errors(errors, None, false);
14431435
return Err(ErrorReported);
14441436
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
643643

644644
#[instrument(skip(self), level = "debug")]
645645
pub(in super::super) fn select_all_obligations_or_error(&self) {
646-
if let Err(errors) = self
647-
.fulfillment_cx
648-
.borrow_mut()
649-
.select_all_with_constness_or_error(&self, self.inh.constness)
650-
{
646+
if let Err(errors) = self.fulfillment_cx.borrow_mut().select_all_or_error(&self) {
651647
self.report_fulfillment_errors(&errors, self.inh.body_id, false);
652648
}
653649
}
@@ -658,10 +654,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
658654
fallback_has_occurred: bool,
659655
mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
660656
) {
661-
let result = self
662-
.fulfillment_cx
663-
.borrow_mut()
664-
.select_with_constness_where_possible(self, self.inh.constness);
657+
let result = self.fulfillment_cx.borrow_mut().select_where_possible(self);
665658
if let Err(mut errors) = result {
666659
mutate_fulfillment_errors(&mut errors);
667660
self.report_fulfillment_errors(&errors, 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
}

src/test/ui/rfc-2632-const-trait-impl/assoc-type.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FIXME(fee1-dead): this should have a better error message
22
#![feature(const_trait_impl)]
3-
3+
// check-pass
44
struct NonConstAdd(i32);
55

66
impl std::ops::Add for NonConstAdd {
@@ -29,3 +29,5 @@ impl const Baz for NonConstAdd {
2929
}
3030

3131
fn main() {}
32+
33+
// TODO: this test should not pass

src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)