Skip to content

Commit 74919df

Browse files
committed
query-ify const_field
1 parent c747f31 commit 74919df

File tree

9 files changed

+33
-12
lines changed

9 files changed

+33
-12
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2703,7 +2703,6 @@ dependencies = [
27032703
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
27042704
"rustc_data_structures 0.0.0",
27052705
"rustc_metadata 0.0.0",
2706-
"rustc_mir 0.0.0",
27072706
"rustc_target 0.0.0",
27082707
"syntax 0.0.0",
27092708
"syntax_pos 0.0.0",

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
//! user of the `DepNode` API of having to know how to compute the expected
5050
//! fingerprint for a given set of node parameters.
5151
52+
use crate::mir;
5253
use crate::mir::interpret::GlobalId;
5354
use crate::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
5455
use crate::hir::map::DefPathHash;

src/librustc/query/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
44
use crate::ty::subst::SubstsRef;
55
use crate::dep_graph::SerializedDepNodeIndex;
66
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
7+
use crate::mir;
78
use crate::mir::interpret::GlobalId;
89
use crate::traits;
910
use crate::traits::query::{
@@ -431,6 +432,15 @@ rustc_queries! {
431432
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
432433
}
433434
}
435+
436+
/// Extracts a field of a (variant of a) const.
437+
query const_field(
438+
key: ty::ParamEnvAnd<'tcx, (&'tcx ty::Const<'tcx>, mir::Field)>
439+
) -> &'tcx ty::Const<'tcx> {
440+
eval_always
441+
no_force
442+
desc { "extract field of const" }
443+
}
434444
}
435445

436446
TypeChecking {

src/librustc/ty/print/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use rustc_data_structures::fx::FxHashSet;
77

88
// `pretty` is a separate module only for organization.
99
mod pretty;
10-
pub mod obsolete;
1110
pub use self::pretty::*;
1211

12+
pub mod obsolete;
13+
1314
pub trait Print<'gcx, 'tcx, P> {
1415
type Output;
1516
type Error;

src/librustc/ty/query/keys.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
127127
}
128128
}
129129

130+
impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
131+
fn query_crate(&self) -> CrateNum {
132+
LOCAL_CRATE
133+
}
134+
fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
135+
DUMMY_SP
136+
}
137+
}
138+
130139
impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
131140
fn query_crate(&self) -> CrateNum {
132141
self.def_id().krate

src/librustc_codegen_ssa/mir/constant.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc::mir::interpret::ErrorHandled;
2-
use rustc_mir::const_eval::const_field;
32
use rustc::mir;
43
use rustc_data_structures::indexed_vec::Idx;
54
use rustc::ty::{self, Ty};
@@ -46,12 +45,8 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
4645
_ => bug!("invalid simd shuffle type: {}", c.ty),
4746
};
4847
let values: Vec<_> = (0..fields).map(|field| {
49-
let field = const_field(
50-
bx.tcx(),
51-
ty::ParamEnv::reveal_all(),
52-
None,
53-
mir::Field::new(field as usize),
54-
c,
48+
let field = bx.tcx().const_field(
49+
ty::ParamEnv::reveal_all().and((&c, mir::Field::new(field as usize)))
5550
);
5651
if let Some(prim) = field.val.try_to_scalar() {
5752
let layout = bx.layout_of(field_ty);

src/librustc_mir/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
470470
}
471471
}
472472

473-
/// Projects to a field of a (variant of a) const.
473+
/// Extracts a field of a (variant of a) const.
474474
// this function uses `unwrap` copiously, because an already validated constant must have valid
475475
// fields and can thus never fail outside of compiler bugs
476476
pub fn const_field<'a, 'tcx>(

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod check_match;
55

66
pub(crate) use self::check_match::check_match;
77

8-
use crate::const_eval::{const_field, const_variant_index};
8+
use crate::const_eval::const_variant_index;
99

1010
use crate::hair::util::UserAnnotatedTyHelpers;
1111
use crate::hair::constant::*;
@@ -949,7 +949,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
949949
debug!("const_to_pat: cv={:#?} id={:?}", cv, id);
950950
let adt_subpattern = |i, variant_opt| {
951951
let field = Field::new(i);
952-
let val = const_field(self.tcx, self.param_env, variant_opt, field, cv);
952+
let val = crate::const_eval::const_field(
953+
self.tcx, self.param_env, variant_opt, field, cv
954+
);
953955
self.const_to_pat(instance, val, id, span)
954956
};
955957
let adt_subpatterns = |n, variant_opt| {

src/librustc_mir/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ pub fn provide(providers: &mut Providers<'_>) {
6363
providers.const_eval = const_eval::const_eval_provider;
6464
providers.const_eval_raw = const_eval::const_eval_raw_provider;
6565
providers.check_match = hair::pattern::check_match;
66+
providers.const_field = |tcx, param_env_and_value| {
67+
let (param_env, (value, field)) = param_env_and_value.into_parts();
68+
const_eval::const_field(tcx, param_env, None, field, value)
69+
};
6670
}
6771

6872
__build_diagnostic_array! { librustc_mir, DIAGNOSTICS }

0 commit comments

Comments
 (0)