Skip to content

Commit 22f8bde

Browse files
committed
Auto merge of #91549 - fee1-dead:const_env, r=spastorino
Eliminate ConstnessAnd again Closes #91489. Closes #89432. Reverts #91491. Reverts #89450. r? `@spastorino`
2 parents 6bda5b3 + ffc9082 commit 22f8bde

File tree

69 files changed

+622
-458
lines changed

Some content is hidden

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

69 files changed

+622
-458
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, WithConstness,
34+
ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
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,6 +7,7 @@ use crate::interpret::{
77
};
88

99
use rustc_errors::ErrorReported;
10+
use rustc_hir as hir;
1011
use rustc_hir::def::DefKind;
1112
use rustc_middle::mir;
1213
use rustc_middle::mir::interpret::ErrorHandled;
@@ -215,6 +216,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
215216
tcx: TyCtxt<'tcx>,
216217
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
217218
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
219+
assert!(key.param_env.constness() == hir::Constness::Const);
218220
// see comment in eval_to_allocation_raw_provider for what we're doing here
219221
if key.param_env.reveal() == Reveal::All {
220222
let mut key = key;
@@ -249,6 +251,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
249251
tcx: TyCtxt<'tcx>,
250252
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
251253
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
254+
assert!(key.param_env.constness() == hir::Constness::Const);
252255
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
253256
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
254257
// 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
@@ -929,6 +929,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
929929
} else {
930930
self.param_env
931931
};
932+
let param_env = param_env.with_const();
932933
let val = self.tcx.eval_to_allocation_raw(param_env.and(gid))?;
933934
self.raw_const_to_mplace(val)
934935
}

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

+1-2
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

+1-2
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_data_structures/src/tagged_ptr/copy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ where
9494
// SAFETY: pointer_raw returns the original pointer
9595
unsafe { std::mem::transmute_copy(&self.pointer_raw()) }
9696
}
97+
#[inline]
9798
pub fn tag(&self) -> T {
9899
unsafe { T::from_usize(self.packed.get() >> Self::TAG_BIT_SHIFT) }
99100
}
101+
#[inline]
100102
pub fn set_tag(&mut self, tag: T) {
101103
let mut packed = self.packed.get();
102104
let new_tag = T::into_usize(tag) << Self::TAG_BIT_SHIFT;

compiler/rustc_hir/src/hir.rs

-25
Original file line numberDiff line numberDiff line change
@@ -3208,31 +3208,6 @@ impl<'hir> Node<'hir> {
32083208
}
32093209
}
32103210

3211-
/// Returns `Constness::Const` when this node is a const fn/impl/item.
3212-
pub fn constness_for_typeck(&self) -> Constness {
3213-
match self {
3214-
Node::Item(Item {
3215-
kind: ItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3216-
..
3217-
})
3218-
| Node::TraitItem(TraitItem {
3219-
kind: TraitItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3220-
..
3221-
})
3222-
| Node::ImplItem(ImplItem {
3223-
kind: ImplItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3224-
..
3225-
})
3226-
| Node::Item(Item { kind: ItemKind::Impl(Impl { constness, .. }), .. }) => *constness,
3227-
3228-
Node::Item(Item { kind: ItemKind::Const(..), .. })
3229-
| Node::TraitItem(TraitItem { kind: TraitItemKind::Const(..), .. })
3230-
| Node::ImplItem(ImplItem { kind: ImplItemKind::Const(..), .. }) => Constness::Const,
3231-
3232-
_ => Constness::NotConst,
3233-
}
3234-
}
3235-
32363211
pub fn as_owner(self) -> Option<OwnerNode<'hir>> {
32373212
match self {
32383213
Node::Item(i) => Some(OwnerNode::Item(i)),

compiler/rustc_infer/src/traits/engine.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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;
6-
use rustc_middle::ty::{self, ToPredicate, Ty, WithConstness};
5+
use rustc_middle::ty::{self, ToPredicate, Ty};
76

87
use super::FulfillmentError;
98
use super::{ObligationCause, PredicateObligation};
@@ -48,26 +47,9 @@ 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(
52-
&mut self,
53-
infcx: &InferCtxt<'_, 'tcx>,
54-
_constness: hir::Constness,
55-
) -> Vec<FulfillmentError<'tcx>> {
56-
self.select_all_or_error(infcx)
57-
}
58-
5950
fn select_where_possible(&mut self, infcx: &InferCtxt<'_, 'tcx>)
6051
-> Vec<FulfillmentError<'tcx>>;
6152

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-
7153
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
7254

7355
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,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_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, WithConstness};
6+
use rustc_middle::ty::{self, ToPredicate, TyCtxt};
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_ref() {
332-
stack.push(binder.value);
331+
if let Some(binder) = subst_predicate.to_opt_poly_trait_pred() {
332+
stack.push(binder.map_bound(|t| t.trait_ref));
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_ref() {
366-
return Some(data.value);
365+
if let Some(data) = obligation.predicate.to_opt_poly_trait_pred() {
366+
return Some(data.map_bound(|t| t.trait_ref));
367367
}
368368
}
369369
None

compiler/rustc_macros/src/query.rs

+20
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ enum QueryModifier {
5858

5959
/// Use a separate query provider for local and extern crates
6060
SeparateProvideExtern(Ident),
61+
62+
/// Always remap the ParamEnv's constness before hashing and passing to the query provider
63+
RemapEnvConstness(Ident),
6164
}
6265

6366
impl Parse for QueryModifier {
@@ -123,6 +126,8 @@ impl Parse for QueryModifier {
123126
Ok(QueryModifier::EvalAlways(modifier))
124127
} else if modifier == "separate_provide_extern" {
125128
Ok(QueryModifier::SeparateProvideExtern(modifier))
129+
} else if modifier == "remap_env_constness" {
130+
Ok(QueryModifier::RemapEnvConstness(modifier))
126131
} else {
127132
Err(Error::new(modifier.span(), "unknown query modifier"))
128133
}
@@ -222,6 +227,9 @@ struct QueryModifiers {
222227

223228
/// Use a separate query provider for local and extern crates
224229
separate_provide_extern: Option<Ident>,
230+
231+
/// Always remap the ParamEnv's constness before hashing.
232+
remap_env_constness: Option<Ident>,
225233
}
226234

227235
/// Process query modifiers into a struct, erroring on duplicates
@@ -236,6 +244,7 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
236244
let mut anon = None;
237245
let mut eval_always = None;
238246
let mut separate_provide_extern = None;
247+
let mut remap_env_constness = None;
239248
for modifier in query.modifiers.0.drain(..) {
240249
match modifier {
241250
QueryModifier::LoadCached(tcx, id, block) => {
@@ -335,6 +344,12 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
335344
}
336345
separate_provide_extern = Some(ident);
337346
}
347+
QueryModifier::RemapEnvConstness(ident) => {
348+
if remap_env_constness.is_some() {
349+
panic!("duplicate modifier `remap_env_constness` for query `{}`", query.name);
350+
}
351+
remap_env_constness = Some(ident)
352+
}
338353
}
339354
}
340355
let desc = desc.unwrap_or_else(|| {
@@ -351,6 +366,7 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
351366
anon,
352367
eval_always,
353368
separate_provide_extern,
369+
remap_env_constness,
354370
}
355371
}
356372

@@ -485,6 +501,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
485501
if let Some(separate_provide_extern) = &modifiers.separate_provide_extern {
486502
attributes.push(quote! { (#separate_provide_extern) });
487503
}
504+
// Pass on the remap_env_constness modifier
505+
if let Some(remap_env_constness) = &modifiers.remap_env_constness {
506+
attributes.push(quote! { (#remap_env_constness) });
507+
}
488508

489509
// This uses the span of the query definition for the commas,
490510
// which can be important if we later encounter any ambiguity

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ 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`.
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.
478479
pub fn body_const_context(&self, did: LocalDefId) -> Option<ConstContext> {
479480
let hir_id = self.local_def_id_to_hir_id(did);
480481
let ccx = match self.body_owner_kind(hir_id) {

compiler/rustc_middle/src/infer/canonical.rs

+8
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ impl<'tcx, R> Canonical<'tcx, QueryResponse<'tcx, R>> {
246246
}
247247
}
248248

249+
impl<'tcx, R> Canonical<'tcx, ty::ParamEnvAnd<'tcx, R>> {
250+
#[inline]
251+
pub fn without_const(mut self) -> Self {
252+
self.value = self.value.without_const();
253+
self
254+
}
255+
}
256+
249257
impl<'tcx, V> Canonical<'tcx, V> {
250258
/// Allows you to map the `value` of a canonical while keeping the
251259
/// same set of bound variables.

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

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

0 commit comments

Comments
 (0)