Skip to content

Commit 85b723c

Browse files
committed
Revert "Auto merge of #91354 - fee1-dead:const_env, r=spastorino"
This reverts commit 18bb8c6, reversing changes made to d9baa36.
1 parent 3e21768 commit 85b723c

File tree

49 files changed

+428
-376
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+428
-376
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_middle::ty::fold::TypeFoldable;
3131
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef, UserSubsts};
3232
use rustc_middle::ty::{
3333
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, OpaqueTypeKey, RegionVid,
34-
ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
34+
ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex, WithConstness,
3535
};
3636
use rustc_span::def_id::CRATE_DEF_ID;
3737
use rustc_span::{Span, DUMMY_SP};

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

-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,6 @@ 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);
220218
// see comment in eval_to_allocation_raw_provider for what we're doing here
221219
if key.param_env.reveal() == Reveal::All {
222220
let mut key = key;
@@ -251,7 +249,6 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
251249
tcx: TyCtxt<'tcx>,
252250
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
253251
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
254-
assert!(key.param_env.constness() == hir::Constness::Const);
255252
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
256253
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
257254
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was

compiler/rustc_const_eval/src/interpret/eval_context.rs

-1
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
918918
} else {
919919
self.param_env
920920
};
921-
let param_env = param_env.with_const();
922921
let val = self.tcx.eval_to_allocation_raw(param_env.and(gid))?;
923922
self.raw_const_to_mplace(val)
924923
}

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

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

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! See the `Qualif` trait for more info.
44
55
use rustc_errors::ErrorReported;
6+
use rustc_hir as hir;
67
use rustc_infer::infer::TyCtxtInferExt;
78
use rustc_middle::mir::*;
89
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty};
@@ -166,7 +167,7 @@ impl Qualif for NeedsNonConstDrop {
166167
);
167168

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

compiler/rustc_hir/src/hir.rs

+25
Original file line numberDiff line numberDiff line change
@@ -3230,6 +3230,31 @@ impl<'hir> Node<'hir> {
32303230
}
32313231
}
32323232

3233+
/// Returns `Constness::Const` when this node is a const fn/impl/item.
3234+
pub fn constness_for_typeck(&self) -> Constness {
3235+
match self {
3236+
Node::Item(Item {
3237+
kind: ItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3238+
..
3239+
})
3240+
| Node::TraitItem(TraitItem {
3241+
kind: TraitItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3242+
..
3243+
})
3244+
| Node::ImplItem(ImplItem {
3245+
kind: ImplItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3246+
..
3247+
})
3248+
| Node::Item(Item { kind: ItemKind::Impl(Impl { constness, .. }), .. }) => *constness,
3249+
3250+
Node::Item(Item { kind: ItemKind::Const(..), .. })
3251+
| Node::TraitItem(TraitItem { kind: TraitItemKind::Const(..), .. })
3252+
| Node::ImplItem(ImplItem { kind: ImplItemKind::Const(..), .. }) => Constness::Const,
3253+
3254+
_ => Constness::NotConst,
3255+
}
3256+
}
3257+
32333258
pub fn as_owner(self) -> Option<OwnerNode<'hir>> {
32343259
match self {
32353260
Node::Item(i) => Some(OwnerNode::Item(i)),

compiler/rustc_infer/src/traits/engine.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::infer::InferCtxt;
22
use crate::traits::Obligation;
33
use rustc_data_structures::fx::FxHashMap;
4+
use rustc_hir as hir;
45
use rustc_hir::def_id::DefId;
5-
use rustc_middle::ty::{self, ToPredicate, Ty};
6+
use rustc_middle::ty::{self, ToPredicate, Ty, WithConstness};
67

78
use super::FulfillmentError;
89
use super::{ObligationCause, PredicateObligation};
@@ -47,9 +48,26 @@ pub trait TraitEngine<'tcx>: 'tcx {
4748

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

51+
fn select_all_with_constness_or_error(
52+
&mut self,
53+
infcx: &InferCtxt<'_, 'tcx>,
54+
_constness: hir::Constness,
55+
) -> Vec<FulfillmentError<'tcx>> {
56+
self.select_all_or_error(infcx)
57+
}
58+
5059
fn select_where_possible(&mut self, infcx: &InferCtxt<'_, 'tcx>)
5160
-> Vec<FulfillmentError<'tcx>>;
5261

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+
}
70+
5371
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
5472

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

compiler/rustc_infer/src/traits/mod.rs

-10
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,6 @@ 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-
8272
// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
8373
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
8474
static_assert_size!(PredicateObligation<'_>, 32);

compiler/rustc_infer/src/traits/util.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use smallvec::smallvec;
33
use crate::infer::outlives::components::{push_outlives_components, Component};
44
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
55
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
6-
use rustc_middle::ty::{self, ToPredicate, TyCtxt};
6+
use rustc_middle::ty::{self, ToPredicate, TyCtxt, WithConstness};
77
use rustc_span::symbol::Ident;
88
use rustc_span::Span;
99

@@ -328,8 +328,8 @@ pub fn transitive_bounds_that_define_assoc_type<'tcx>(
328328
));
329329
for (super_predicate, _) in super_predicates.predicates {
330330
let subst_predicate = super_predicate.subst_supertrait(tcx, &trait_ref);
331-
if let Some(binder) = subst_predicate.to_opt_poly_trait_pred() {
332-
stack.push(binder.map_bound(|t| t.trait_ref));
331+
if let Some(binder) = subst_predicate.to_opt_poly_trait_ref() {
332+
stack.push(binder.value);
333333
}
334334
}
335335

@@ -362,8 +362,8 @@ impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToT
362362

363363
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
364364
while let Some(obligation) = self.base_iterator.next() {
365-
if let Some(data) = obligation.predicate.to_opt_poly_trait_pred() {
366-
return Some(data.map_bound(|t| t.trait_ref));
365+
if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() {
366+
return Some(data.value);
367367
}
368368
}
369369
None

compiler/rustc_middle/src/hir/map/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ impl<'hir> Map<'hir> {
474474
/// Panics if `LocalDefId` does not have an associated body.
475475
///
476476
/// This should only be used for determining the context of a body, a return
477-
/// value of `Some` does not always suggest that the owner of the body is `const`,
478-
/// just that it has to be checked as if it were.
477+
/// value of `Some` does not always suggest that the owner of the body is `const`.
479478
pub fn body_const_context(&self, did: LocalDefId) -> Option<ConstContext> {
480479
let hir_id = self.local_def_id_to_hir_id(did);
481480
let ccx = match self.body_owner_kind(hir_id) {

compiler/rustc_middle/src/mir/interpret/queries.rs

-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ impl<'tcx> TyCtxt<'tcx> {
6464
cid: GlobalId<'tcx>,
6565
span: Option<Span>,
6666
) -> EvalToConstValueResult<'tcx> {
67-
let param_env = param_env.with_const();
6867
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
6968
// improve caching of queries.
7069
let inputs = self.erase_regions(param_env.and(cid));
@@ -93,7 +92,6 @@ impl<'tcx> TyCtxt<'tcx> {
9392
gid: GlobalId<'tcx>,
9493
param_env: ty::ParamEnv<'tcx>,
9594
) -> Result<&'tcx mir::Allocation, ErrorHandled> {
96-
let param_env = param_env.with_const();
9795
trace!("eval_to_allocation: Need to compute {:?}", gid);
9896
let raw_const = self.eval_to_allocation_raw(param_env.and(gid))?;
9997
Ok(self.global_alloc(raw_const.alloc_id).unwrap_memory())

compiler/rustc_middle/src/traits/select.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ use rustc_hir::def_id::DefId;
1212
use rustc_query_system::cache::Cache;
1313

1414
pub type SelectionCache<'tcx> = Cache<
15-
ty::ParamEnvAnd<'tcx, ty::TraitPredicate<'tcx>>,
15+
(ty::ConstnessAnd<ty::ParamEnvAnd<'tcx, ty::TraitRef<'tcx>>>, ty::ImplPolarity),
1616
SelectionResult<'tcx, SelectionCandidate<'tcx>>,
1717
>;
1818

19-
pub type EvaluationCache<'tcx> =
20-
Cache<ty::ParamEnvAnd<'tcx, ty::PolyTraitPredicate<'tcx>>, EvaluationResult>;
19+
pub type EvaluationCache<'tcx> = Cache<
20+
(ty::ParamEnvAnd<'tcx, ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>>, ty::ImplPolarity),
21+
EvaluationResult,
22+
>;
2123

2224
/// The selection process begins by considering all impls, where
2325
/// clauses, and so forth that might resolve an obligation. Sometimes
@@ -101,7 +103,7 @@ pub enum SelectionCandidate<'tcx> {
101103
/// `false` if there are no *further* obligations.
102104
has_nested: bool,
103105
},
104-
ParamCandidate(ty::PolyTraitPredicate<'tcx>),
106+
ParamCandidate((ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>, ty::ImplPolarity)),
105107
ImplCandidate(DefId),
106108
AutoImplCandidate(DefId),
107109

compiler/rustc_middle/src/traits/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ impl<'tcx> Elaborator<'tcx> {
2626
.predicates
2727
.into_iter()
2828
.flat_map(|(pred, _)| {
29-
pred.subst_supertrait(self.tcx, &trait_ref).to_opt_poly_trait_pred()
29+
pred.subst_supertrait(self.tcx, &trait_ref).to_opt_poly_trait_ref()
3030
})
31-
.map(|t| t.map_bound(|pred| pred.trait_ref))
31+
.map(|t| t.value)
3232
.filter(|supertrait_ref| self.visited.insert(*supertrait_ref));
3333

3434
self.stack.extend(supertrait_refs);

0 commit comments

Comments
 (0)