Skip to content

Commit

Permalink
query-ify const_field
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-i-m committed Jun 3, 2019
1 parent c747f31 commit 74919df
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 12 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Expand Up @@ -2703,7 +2703,6 @@ dependencies = [
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_data_structures 0.0.0",
"rustc_metadata 0.0.0",
"rustc_mir 0.0.0",
"rustc_target 0.0.0",
"syntax 0.0.0",
"syntax_pos 0.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/librustc/dep_graph/dep_node.rs
Expand Up @@ -49,6 +49,7 @@
//! user of the `DepNode` API of having to know how to compute the expected
//! fingerprint for a given set of node parameters.

use crate::mir;
use crate::mir::interpret::GlobalId;
use crate::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
use crate::hir::map::DefPathHash;
Expand Down
10 changes: 10 additions & 0 deletions src/librustc/query/mod.rs
Expand Up @@ -4,6 +4,7 @@ use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
use crate::ty::subst::SubstsRef;
use crate::dep_graph::SerializedDepNodeIndex;
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
use crate::mir;
use crate::mir::interpret::GlobalId;
use crate::traits;
use crate::traits::query::{
Expand Down Expand Up @@ -431,6 +432,15 @@ rustc_queries! {
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
}
}

/// Extracts a field of a (variant of a) const.
query const_field(
key: ty::ParamEnvAnd<'tcx, (&'tcx ty::Const<'tcx>, mir::Field)>
) -> &'tcx ty::Const<'tcx> {
eval_always
no_force
desc { "extract field of const" }
}
}

TypeChecking {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/ty/print/mod.rs
Expand Up @@ -7,9 +7,10 @@ use rustc_data_structures::fx::FxHashSet;

// `pretty` is a separate module only for organization.
mod pretty;
pub mod obsolete;
pub use self::pretty::*;

pub mod obsolete;

pub trait Print<'gcx, 'tcx, P> {
type Output;
type Error;
Expand Down
9 changes: 9 additions & 0 deletions src/librustc/ty/query/keys.rs
Expand Up @@ -127,6 +127,15 @@ impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
}
}

impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
fn query_crate(&self) -> CrateNum {
LOCAL_CRATE
}
fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
DUMMY_SP
}
}

impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
fn query_crate(&self) -> CrateNum {
self.def_id().krate
Expand Down
9 changes: 2 additions & 7 deletions src/librustc_codegen_ssa/mir/constant.rs
@@ -1,5 +1,4 @@
use rustc::mir::interpret::ErrorHandled;
use rustc_mir::const_eval::const_field;
use rustc::mir;
use rustc_data_structures::indexed_vec::Idx;
use rustc::ty::{self, Ty};
Expand Down Expand Up @@ -46,12 +45,8 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
_ => bug!("invalid simd shuffle type: {}", c.ty),
};
let values: Vec<_> = (0..fields).map(|field| {
let field = const_field(
bx.tcx(),
ty::ParamEnv::reveal_all(),
None,
mir::Field::new(field as usize),
c,
let field = bx.tcx().const_field(
ty::ParamEnv::reveal_all().and((&c, mir::Field::new(field as usize)))
);
if let Some(prim) = field.val.try_to_scalar() {
let layout = bx.layout_of(field_ty);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/const_eval.rs
Expand Up @@ -470,7 +470,7 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
}
}

/// Projects to a field of a (variant of a) const.
/// Extracts a field of a (variant of a) const.
// this function uses `unwrap` copiously, because an already validated constant must have valid
// fields and can thus never fail outside of compiler bugs
pub fn const_field<'a, 'tcx>(
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_mir/hair/pattern/mod.rs
Expand Up @@ -5,7 +5,7 @@ mod check_match;

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

use crate::const_eval::{const_field, const_variant_index};
use crate::const_eval::const_variant_index;

use crate::hair::util::UserAnnotatedTyHelpers;
use crate::hair::constant::*;
Expand Down Expand Up @@ -949,7 +949,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
debug!("const_to_pat: cv={:#?} id={:?}", cv, id);
let adt_subpattern = |i, variant_opt| {
let field = Field::new(i);
let val = const_field(self.tcx, self.param_env, variant_opt, field, cv);
let val = crate::const_eval::const_field(
self.tcx, self.param_env, variant_opt, field, cv
);
self.const_to_pat(instance, val, id, span)
};
let adt_subpatterns = |n, variant_opt| {
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_mir/lib.rs
Expand Up @@ -63,6 +63,10 @@ pub fn provide(providers: &mut Providers<'_>) {
providers.const_eval = const_eval::const_eval_provider;
providers.const_eval_raw = const_eval::const_eval_raw_provider;
providers.check_match = hair::pattern::check_match;
providers.const_field = |tcx, param_env_and_value| {
let (param_env, (value, field)) = param_env_and_value.into_parts();
const_eval::const_field(tcx, param_env, None, field, value)
};
}

__build_diagnostic_array! { librustc_mir, DIAGNOSTICS }

0 comments on commit 74919df

Please sign in to comment.