Skip to content

Commit aafd75a

Browse files
committed
Auto merge of rust-lang#114134 - fee1-dead-contrib:rm-constness-from-param-env, r=oli-obk
Remove `constness` from `ParamEnv` This should be replaced by keyword generics/effects. cc rust-lang#110395 r? `@oli-obk`
2 parents 37343f4 + b0fa220 commit aafd75a

File tree

102 files changed

+419
-1057
lines changed

Some content is hidden

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

102 files changed

+419
-1057
lines changed

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,12 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
409409
}
410410

411411
if let ty::FnDef(def_id, args) = *constant.literal.ty().kind() {
412-
// const_trait_impl: use a non-const param env when checking that a FnDef type is well formed.
413-
// this is because the well-formedness of the function does not need to be proved to have `const`
414-
// impls for trait bounds.
415412
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, args);
416-
let prev = self.cx.param_env;
417-
self.cx.param_env = prev.without_const();
418413
self.cx.normalize_and_prove_instantiated_predicates(
419414
def_id,
420415
instantiated_predicates,
421416
locations,
422417
);
423-
self.cx.param_env = prev;
424418
}
425419
}
426420
}

Diff for: compiler/rustc_const_eval/src/const_eval/eval_queries.rs

-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
228228
tcx: TyCtxt<'tcx>,
229229
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
230230
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
231-
assert!(key.param_env.is_const());
232231
// see comment in eval_to_allocation_raw_provider for what we're doing here
233232
if key.param_env.reveal() == Reveal::All {
234233
let mut key = key;
@@ -269,7 +268,6 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
269268
tcx: TyCtxt<'tcx>,
270269
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
271270
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
272-
assert!(key.param_env.is_const());
273271
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
274272
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
275273
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was

Diff for: compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2828
&& tcx.constness(parent_id) == hir::Constness::Const
2929
}
3030

31-
/// Checks whether an item is considered to be `const`. If it is a constructor, it is const. If
32-
/// it is a trait impl/function, return if it has a `const` modifier. If it is an intrinsic,
33-
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
34-
/// `Constness::NotConst`.
31+
/// Checks whether an item is considered to be `const`. If it is a constructor, anonymous const,
32+
/// const block, const item or associated const, it is const. If it is a trait impl/function,
33+
/// return if it has a `const` modifier. If it is an intrinsic, report whether said intrinsic
34+
/// has a `rustc_const_{un,}stable` attribute. Otherwise, return `Constness::NotConst`.
3535
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3636
let node = tcx.hir().get_by_def_id(def_id);
3737

3838
match node {
39-
hir::Node::Ctor(_) => hir::Constness::Const,
39+
hir::Node::Ctor(_)
40+
| hir::Node::AnonConst(_)
41+
| hir::Node::ConstBlock(_)
42+
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => hir::Constness::Const,
4043
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
4144
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
4245
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other

Diff for: compiler/rustc_const_eval/src/interpret/eval_context.rs

-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
958958
} else {
959959
self.param_env
960960
};
961-
let param_env = param_env.with_const();
962961
let val = self.ctfe_query(span, |tcx| tcx.eval_to_allocation_raw(param_env.and(gid)))?;
963962
self.raw_const_to_mplace(val)
964963
}

Diff for: compiler/rustc_const_eval/src/interpret/operand.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
698698
ty::ConstKind::Unevaluated(uv) => {
699699
let instance = self.resolve(uv.def, uv.args)?;
700700
let cid = GlobalId { instance, promoted: None };
701-
self.ctfe_query(span, |tcx| {
702-
tcx.eval_to_valtree(self.param_env.with_const().and(cid))
703-
})?
704-
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
701+
self.ctfe_query(span, |tcx| tcx.eval_to_valtree(self.param_env.and(cid)))?
702+
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
705703
}
706704
ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
707705
span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {val:?}")

Diff for: compiler/rustc_const_eval/src/transform/check_consts/check.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::mem;
2121
use std::ops::Deref;
2222

2323
use super::ops::{self, NonConstOp, Status};
24-
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop, NeedsNonConstDrop};
24+
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop};
2525
use super::resolver::FlowSensitiveAnalysis;
2626
use super::{ConstCx, Qualif};
2727
use crate::const_eval::is_unstable_const_fn;
@@ -34,7 +34,7 @@ type QualifResults<'mir, 'tcx, Q> =
3434
pub struct Qualifs<'mir, 'tcx> {
3535
has_mut_interior: Option<QualifResults<'mir, 'tcx, HasMutInterior>>,
3636
needs_drop: Option<QualifResults<'mir, 'tcx, NeedsDrop>>,
37-
needs_non_const_drop: Option<QualifResults<'mir, 'tcx, NeedsNonConstDrop>>,
37+
// needs_non_const_drop: Option<QualifResults<'mir, 'tcx, NeedsNonConstDrop>>,
3838
}
3939

4040
impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
@@ -77,22 +77,27 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
7777
local: Local,
7878
location: Location,
7979
) -> bool {
80+
// FIXME(effects) replace with `NeedsNonconstDrop` after const traits work again
81+
/*
8082
let ty = ccx.body.local_decls[local].ty;
81-
if !NeedsNonConstDrop::in_any_value_of_ty(ccx, ty) {
83+
if !NeedsDrop::in_any_value_of_ty(ccx, ty) {
8284
return false;
8385
}
8486
8587
let needs_non_const_drop = self.needs_non_const_drop.get_or_insert_with(|| {
8688
let ConstCx { tcx, body, .. } = *ccx;
8789
88-
FlowSensitiveAnalysis::new(NeedsNonConstDrop, ccx)
90+
FlowSensitiveAnalysis::new(NeedsDrop, ccx)
8991
.into_engine(tcx, &body)
9092
.iterate_to_fixpoint()
9193
.into_results_cursor(&body)
9294
});
9395
9496
needs_non_const_drop.seek_before_primary_effect(location);
9597
needs_non_const_drop.get().contains(local)
98+
*/
99+
100+
self.needs_drop(ccx, local, location)
96101
}
97102

98103
/// Returns `true` if `local` is `HasMutInterior` at the given `Location`.
@@ -798,16 +803,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
798803
}
799804
Ok(Some(ImplSource::UserDefined(data))) => {
800805
let callee_name = tcx.item_name(callee);
801-
if let Some(&did) = tcx
802-
.associated_item_def_ids(data.impl_def_id)
803-
.iter()
804-
.find(|did| tcx.item_name(**did) == callee_name)
805-
{
806-
// using internal args is ok here, since this is only
807-
// used for the `resolve` call below
808-
fn_args = GenericArgs::identity_for_item(tcx, did);
809-
callee = did;
810-
}
811806

812807
if let hir::Constness::NotConst = tcx.constness(data.impl_def_id) {
813808
self.check_op(ops::FnCallNonConst {
@@ -820,6 +815,17 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
820815
});
821816
return;
822817
}
818+
819+
if let Some(&did) = tcx
820+
.associated_item_def_ids(data.impl_def_id)
821+
.iter()
822+
.find(|did| tcx.item_name(**did) == callee_name)
823+
{
824+
// using internal args is ok here, since this is only
825+
// used for the `resolve` call below
826+
fn_args = GenericArgs::identity_for_item(tcx, did);
827+
callee = did;
828+
}
823829
}
824830
_ if !tcx.is_const_fn_raw(callee) => {
825831
// At this point, it is only legal when the caller is in a trait
@@ -996,8 +1002,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
9961002
let mut err_span = self.span;
9971003
let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty;
9981004

1005+
// FIXME(effects) replace with `NeedsNonConstDrop` once we fix const traits
9991006
let ty_needs_non_const_drop =
1000-
qualifs::NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place);
1007+
qualifs::NeedsDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place);
10011008

10021009
debug!(?ty_of_dropped_place, ?ty_needs_non_const_drop);
10031010

Diff for: compiler/rustc_const_eval/src/transform/check_consts/post_drop_elaboration.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_span::{symbol::sym, Span};
55

66
use super::check::Qualifs;
77
use super::ops::{self, NonConstOp};
8-
use super::qualifs::{NeedsNonConstDrop, Qualif};
8+
use super::qualifs::{NeedsDrop, Qualif};
99
use super::ConstCx;
1010

1111
/// Returns `true` if we should use the more precise live drop checker that runs after drop
@@ -82,7 +82,9 @@ impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
8282
match &terminator.kind {
8383
mir::TerminatorKind::Drop { place: dropped_place, .. } => {
8484
let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
85-
if !NeedsNonConstDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
85+
86+
// FIXME(effects) use `NeedsNonConstDrop`
87+
if !NeedsDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
8688
// Instead of throwing a bug, we just return here. This is because we have to
8789
// run custom `const Drop` impls.
8890
return;

Diff for: compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ pub fn in_any_value_of_ty<'tcx>(
2323
ConstQualifs {
2424
has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
2525
needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
26-
needs_non_const_drop: NeedsNonConstDrop::in_any_value_of_ty(cx, ty),
26+
// FIXME(effects)
27+
needs_non_const_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
2728
custom_eq: CustomEq::in_any_value_of_ty(cx, ty),
2829
tainted_by_errors,
2930
}

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,7 @@ fn compare_method_predicate_entailment<'tcx>(
219219
// The key step here is to update the caller_bounds's predicates to be
220220
// the new hybrid bounds we computed.
221221
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_def_id);
222-
let param_env = ty::ParamEnv::new(
223-
tcx.mk_clauses(&hybrid_preds.predicates),
224-
Reveal::UserFacing,
225-
hir::Constness::NotConst,
226-
);
222+
let param_env = ty::ParamEnv::new(tcx.mk_clauses(&hybrid_preds.predicates), Reveal::UserFacing);
227223
let param_env = traits::normalize_param_env_or_error(tcx, param_env, normalize_cause);
228224

229225
let infcx = &tcx.infer_ctxt().build();
@@ -1923,11 +1919,7 @@ fn compare_type_predicate_entailment<'tcx>(
19231919

19241920
let impl_ty_span = tcx.def_span(impl_ty_def_id);
19251921
let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_def_id);
1926-
let param_env = ty::ParamEnv::new(
1927-
tcx.mk_clauses(&hybrid_preds.predicates),
1928-
Reveal::UserFacing,
1929-
hir::Constness::NotConst,
1930-
);
1922+
let param_env = ty::ParamEnv::new(tcx.mk_clauses(&hybrid_preds.predicates), Reveal::UserFacing);
19311923
let param_env = traits::normalize_param_env_or_error(tcx, param_env, normalize_cause);
19321924
let infcx = tcx.infer_ctxt().build();
19331925
let ocx = ObligationCtxt::new(&infcx);
@@ -2102,7 +2094,7 @@ pub(super) fn check_type_bounds<'tcx>(
21022094
.to_predicate(tcx),
21032095
),
21042096
};
2105-
ty::ParamEnv::new(tcx.mk_clauses(&predicates), Reveal::UserFacing, param_env.constness())
2097+
ty::ParamEnv::new(tcx.mk_clauses(&predicates), Reveal::UserFacing)
21062098
};
21072099
debug!(?normalize_param_env);
21082100

Diff for: compiler/rustc_hir_analysis/src/check/dropck.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
129129
// We don't need to normalize this param-env or anything, since we're only
130130
// substituting it with free params, so no additional param-env normalization
131131
// can occur on top of what has been done in the param_env query itself.
132-
let param_env = ty::EarlyBinder::bind(tcx.param_env(adt_def_id))
133-
.instantiate(tcx, adt_to_impl_args)
134-
.with_constness(tcx.constness(drop_impl_def_id));
132+
let param_env =
133+
ty::EarlyBinder::bind(tcx.param_env(adt_def_id)).instantiate(tcx, adt_to_impl_args);
135134

136135
for (pred, span) in tcx.predicates_of(drop_impl_def_id).instantiate_identity(tcx) {
137136
let normalize_cause = traits::ObligationCause::misc(span, adt_def_id);

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
7575
self.body_def_id,
7676
ObligationCauseCode::WellFormed(loc),
7777
);
78-
// for a type to be WF, we do not need to check if const trait predicates satisfy.
79-
let param_env = self.param_env.without_const();
8078
self.ocx.register_obligation(traits::Obligation::new(
8179
self.tcx(),
8280
cause,
83-
param_env,
81+
self.param_env,
8482
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg))),
8583
));
8684
}
@@ -504,7 +502,7 @@ fn augment_param_env<'tcx>(
504502
);
505503
// FIXME(compiler-errors): Perhaps there is a case where we need to normalize this
506504
// i.e. traits::normalize_param_env_or_error
507-
ty::ParamEnv::new(bounds, param_env.reveal(), param_env.constness())
505+
ty::ParamEnv::new(bounds, param_env.reveal())
508506
}
509507

510508
/// We use the following trait as an example throughout this function.
@@ -1415,7 +1413,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14151413
let wf_obligations = predicates.into_iter().flat_map(|(p, sp)| {
14161414
traits::wf::predicate_obligations(
14171415
infcx,
1418-
wfcx.param_env.without_const(),
1416+
wfcx.param_env,
14191417
wfcx.body_def_id,
14201418
p.as_predicate(),
14211419
sp,

Diff for: compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8181

8282
debug!(?bound_sig, ?liberated_sig);
8383

84-
let mut fcx = FnCtxt::new(self, self.param_env.without_const(), closure.def_id);
84+
let mut fcx = FnCtxt::new(self, self.param_env, closure.def_id);
8585
let generator_types = check_fn(
8686
&mut fcx,
8787
liberated_sig,

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13341334
t_cast,
13351335
t.span,
13361336
expr.span,
1337-
self.param_env.constness(),
1337+
hir::Constness::NotConst,
13381338
) {
13391339
Ok(cast_check) => {
13401340
debug!(
@@ -1428,7 +1428,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14281428

14291429
// Create a new function context.
14301430
let def_id = block.def_id;
1431-
let fcx = FnCtxt::new(self, self.param_env.with_const(), def_id);
1431+
let fcx = FnCtxt::new(self, self.param_env, def_id);
14321432
crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
14331433

14341434
let ty = fcx.check_expr_with_expectation(&body.value, expected);

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

-5
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4545

4646
debug!("FnCtxt::check_casts: {} deferred checks", deferred_cast_checks.len());
4747
for cast in deferred_cast_checks.drain(..) {
48-
let prev_env = self.param_env;
49-
self.param_env = self.param_env.with_constness(cast.constness);
50-
5148
cast.check(self);
52-
53-
self.param_env = prev_env;
5449
}
5550

5651
*self.deferred_cast_checks.borrow_mut() = deferred_cast_checks;

Diff for: compiler/rustc_hir_typeck/src/lib.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use rustc_middle::traits;
7171
use rustc_middle::ty::{self, Ty, TyCtxt};
7272
use rustc_session::config;
7373
use rustc_span::def_id::{DefId, LocalDefId};
74-
use rustc_span::{sym, Span};
74+
use rustc_span::Span;
7575

7676
fluent_messages! { "../messages.ftl" }
7777

@@ -182,11 +182,7 @@ fn typeck_with_fallback<'tcx>(
182182
let body = tcx.hir().body(body_id);
183183

184184
let param_env = tcx.param_env(def_id);
185-
let param_env = if tcx.has_attr(def_id, sym::rustc_do_not_const_check) {
186-
param_env.without_const()
187-
} else {
188-
param_env
189-
};
185+
190186
let inh = Inherited::new(tcx, def_id);
191187
let mut fcx = FnCtxt::new(&inh, param_env, def_id);
192188

@@ -263,11 +259,7 @@ fn typeck_with_fallback<'tcx>(
263259

264260
// Closure and generator analysis may run after fallback
265261
// because they don't constrain other type variables.
266-
// Closure analysis only runs on closures. Therefore they only need to fulfill non-const predicates (as of now)
267-
let prev_constness = fcx.param_env.constness();
268-
fcx.param_env = fcx.param_env.without_const();
269262
fcx.closure_analyze(body);
270-
fcx.param_env = fcx.param_env.with_constness(prev_constness);
271263
assert!(fcx.deferred_call_resolutions.borrow().is_empty());
272264
// Before the generator analysis, temporary scopes shall be marked to provide more
273265
// precise information on types to be captured.

Diff for: compiler/rustc_infer/src/traits/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ impl<'tcx> PredicateObligation<'tcx> {
7979
}
8080

8181
pub fn without_const(mut self, tcx: TyCtxt<'tcx>) -> PredicateObligation<'tcx> {
82-
self.param_env = self.param_env.without_const();
8382
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) = self.predicate.kind().skip_binder() && trait_pred.is_const_if_const() {
8483
self.predicate = tcx.mk_predicate(self.predicate.kind().map_bound(|_| ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred.without_const()))));
8584
}
@@ -88,14 +87,6 @@ impl<'tcx> PredicateObligation<'tcx> {
8887
}
8988

9089
impl<'tcx> PolyTraitObligation<'tcx> {
91-
/// Returns `true` if the trait predicate is considered `const` in its ParamEnv.
92-
pub fn is_const(&self) -> bool {
93-
matches!(
94-
(self.predicate.skip_binder().constness, self.param_env.constness()),
95-
(ty::BoundConstness::ConstIfConst, hir::Constness::Const)
96-
)
97-
}
98-
9990
pub fn derived_cause(
10091
&self,
10192
variant: impl FnOnce(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,

0 commit comments

Comments
 (0)