Skip to content

Commit ac21131

Browse files
committed
Auto merge of #63820 - oli-obk:eager_const_eval, r=nikomatsakis
Simplify eager normalization of constants r? @nikomatsakis
2 parents bbd48e6 + 181ed55 commit ac21131

File tree

4 files changed

+13
-81
lines changed

4 files changed

+13
-81
lines changed

src/librustc/traits/project.rs

+1-35
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use super::util;
1515
use crate::hir::def_id::DefId;
1616
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
1717
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
18-
use crate::mir::interpret::{GlobalId, ConstValue};
1918
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
2019
use rustc_macros::HashStable;
2120
use syntax::ast::Ident;
@@ -397,40 +396,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
397396
}
398397

399398
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
400-
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
401-
let tcx = self.selcx.tcx().global_tcx();
402-
let param_env = self.param_env;
403-
if !param_env.has_local_value() {
404-
if substs.needs_infer() || substs.has_placeholders() {
405-
let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
406-
let instance = ty::Instance::resolve(tcx, param_env, def_id, identity_substs);
407-
if let Some(instance) = instance {
408-
let cid = GlobalId {
409-
instance,
410-
promoted: None
411-
};
412-
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
413-
let evaluated = evaluated.subst(tcx, substs);
414-
return evaluated;
415-
}
416-
}
417-
} else {
418-
if !substs.has_local_value() {
419-
let instance = ty::Instance::resolve(tcx, param_env, def_id, substs);
420-
if let Some(instance) = instance {
421-
let cid = GlobalId {
422-
instance,
423-
promoted: None
424-
};
425-
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
426-
return evaluated;
427-
}
428-
}
429-
}
430-
}
431-
}
432-
}
433-
constant
399+
constant.eval(self.selcx.tcx(), self.param_env)
434400
}
435401
}
436402

src/librustc/traits/query/normalize.rs

+2-36
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use crate::infer::at::At;
66
use crate::infer::canonical::OriginalQueryValues;
77
use crate::infer::{InferCtxt, InferOk};
8-
use crate::mir::interpret::{GlobalId, ConstValue};
98
use crate::traits::project::Normalized;
109
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
1110
use crate::ty::fold::{TypeFoldable, TypeFolder};
12-
use crate::ty::subst::{Subst, InternalSubsts};
11+
use crate::ty::subst::Subst;
1312
use crate::ty::{self, Ty, TyCtxt};
1413

1514
use super::NoSolution;
@@ -191,40 +190,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
191190
}
192191

193192
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
194-
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
195-
let tcx = self.infcx.tcx.global_tcx();
196-
let param_env = self.param_env;
197-
if !param_env.has_local_value() {
198-
if substs.needs_infer() || substs.has_placeholders() {
199-
let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
200-
let instance = ty::Instance::resolve(tcx, param_env, def_id, identity_substs);
201-
if let Some(instance) = instance {
202-
let cid = GlobalId {
203-
instance,
204-
promoted: None,
205-
};
206-
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
207-
let evaluated = evaluated.subst(tcx, substs);
208-
return evaluated;
209-
}
210-
}
211-
} else {
212-
if !substs.has_local_value() {
213-
let instance = ty::Instance::resolve(tcx, param_env, def_id, substs);
214-
if let Some(instance) = instance {
215-
let cid = GlobalId {
216-
instance,
217-
promoted: None,
218-
};
219-
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
220-
return evaluated;
221-
}
222-
}
223-
}
224-
}
225-
}
226-
}
227-
constant
193+
constant.eval(self.infcx.tcx, self.param_env)
228194
}
229195
}
230196

src/test/ui/consts/const-size_of-cycle.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{con
44
LL | bytes: [u8; std::mem::size_of::<Foo>()]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
8+
--> $DIR/const-size_of-cycle.rs:6:17
9+
|
10+
LL | bytes: [u8; std::mem::size_of::<Foo>()]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
712
note: ...which requires const-evaluating `Foo::bytes::{{constant}}#0`...
813
--> $SRC_DIR/libcore/mem/mod.rs:LL:COL
914
|
1015
LL | intrinsics::size_of::<T>()
1116
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1217
= note: ...which requires computing layout of `Foo`...
1318
= note: ...which requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All, def_id: None }, value: [u8; _] }`...
14-
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
15-
--> $DIR/const-size_of-cycle.rs:6:17
16-
|
17-
LL | bytes: [u8; std::mem::size_of::<Foo>()]
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}#0`, completing the cycle
2020
note: cycle used when processing `Foo`
2121
--> $DIR/const-size_of-cycle.rs:5:1

src/test/ui/issues/issue-44415.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ error[E0391]: cycle detected when const-evaluating + checking `Foo::bytes::{{con
44
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
55
| ^^^^^^
66
|
7+
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
8+
--> $DIR/issue-44415.rs:6:17
9+
|
10+
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
11+
| ^^^^^^
712
note: ...which requires const-evaluating `Foo::bytes::{{constant}}#0`...
813
--> $DIR/issue-44415.rs:6:26
914
|
1015
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
1116
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1217
= note: ...which requires computing layout of `Foo`...
1318
= note: ...which requires normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All, def_id: None }, value: [u8; _] }`...
14-
note: ...which requires const-evaluating + checking `Foo::bytes::{{constant}}#0`...
15-
--> $DIR/issue-44415.rs:6:17
16-
|
17-
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
18-
| ^^^^^^
1919
= note: ...which again requires const-evaluating + checking `Foo::bytes::{{constant}}#0`, completing the cycle
2020
note: cycle used when processing `Foo`
2121
--> $DIR/issue-44415.rs:5:1

0 commit comments

Comments
 (0)