Skip to content

Commit

Permalink
Change ByRef to a struct variant to clarify its fields via names
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jun 19, 2019
1 parent 23a1ebb commit 811b996
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/librustc/infer/freshen.rs
Expand Up @@ -260,7 +260,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
ConstValue::Param(_) |
ConstValue::Scalar(_) |
ConstValue::Slice { .. } |
ConstValue::ByRef(..) |
ConstValue::ByRef { .. } |
ConstValue::Unevaluated(..) => {}
}

Expand Down
23 changes: 15 additions & 8 deletions src/librustc/mir/interpret/value.rs
Expand Up @@ -43,13 +43,20 @@ pub enum ConstValue<'tcx> {
end: usize,
},

/// An allocation together with an offset into the allocation.
/// The alignment exists to allow `const_field` to have `ByRef` access to nonprimitive fields
/// of `repr(packed)` structs. The alignment may be lower than the type of this constant.
/// This permits reads with lower alignment than what the type would normally require.
/// FIXME(RalfJ,oli-obk): The alignment checks are part of miri, but const eval doesn't really
/// need them. Disabling them may be too hard though.
ByRef(Size, Align, &'tcx Allocation),
/// An value not represented/representable by `Scalar` or `Slice`
ByRef {
/// The alignment exists to allow `const_field` to have `ByRef` access to nonprimitive fields
/// of `repr(packed)` structs. The alignment may be lower than the type of this constant.
/// This permits reads with lower alignment than what the type would normally require.
/// FIXME(RalfJ,oli-obk): The alignment checks are part of miri, but const eval doesn't really
/// need them. Disabling them may be too hard though.
align: Align,
/// Offset into `alloc`
offset: Size,
/// The backing memory of the value, may contain more memory than needed for just the value
/// in order to share `Allocation`s between values
alloc: &'tcx Allocation,
},

/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
/// variants when the code is monomorphic enough for that.
Expand All @@ -66,7 +73,7 @@ impl<'tcx> ConstValue<'tcx> {
ConstValue::Param(_) |
ConstValue::Infer(_) |
ConstValue::Placeholder(_) |
ConstValue::ByRef(..) |
ConstValue::ByRef{ .. } |
ConstValue::Unevaluated(..) |
ConstValue::Slice { .. } => None,
ConstValue::Scalar(val) => Some(val),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/print/obsolete.rs
Expand Up @@ -186,7 +186,7 @@ impl DefPathBasedNames<'tcx> {
// as well as the unprintable types of constants (see `push_type_name` for more details).
pub fn push_const_name(&self, c: &Const<'tcx>, output: &mut String, debug: bool) {
match c.val {
ConstValue::Scalar(..) | ConstValue::Slice { .. } | ConstValue::ByRef(..) => {
ConstValue::Scalar(..) | ConstValue::Slice { .. } | ConstValue::ByRef { .. } => {
// FIXME(const_generics): we could probably do a better job here.
write!(output, "{:?}", c).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/relate.rs
Expand Up @@ -594,7 +594,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
ty: a.ty,
}))
}
(ConstValue::ByRef(..), _) => {
(ConstValue::ByRef { .. }, _) => {
bug!(
"non-Scalar ConstValue encountered in super_relate_consts {:?} {:?}",
a,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -1335,7 +1335,8 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
match *self {
ConstValue::ByRef(offset, align, alloc) => ConstValue::ByRef(offset, align, alloc),
ConstValue::ByRef { offset, align, alloc } =>
ConstValue::ByRef { offset, align, alloc },
ConstValue::Infer(ic) => ConstValue::Infer(ic.fold_with(folder)),
ConstValue::Param(p) => ConstValue::Param(p.fold_with(folder)),
ConstValue::Placeholder(p) => ConstValue::Placeholder(p),
Expand All @@ -1348,7 +1349,7 @@ impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
match *self {
ConstValue::ByRef(..) => false,
ConstValue::ByRef { .. } => false,
ConstValue::Infer(ic) => ic.visit_with(visitor),
ConstValue::Param(p) => p.visit_with(visitor),
ConstValue::Placeholder(_) => false,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/consts.rs
Expand Up @@ -71,7 +71,7 @@ pub fn codegen_static_initializer(
let static_ = cx.tcx.const_eval(param_env.and(cid))?;

let alloc = match static_.val {
ConstValue::ByRef(offset, align, alloc) if offset.bytes() == 0 && align == alloc.align => {
ConstValue::ByRef { offset, align, alloc } if offset.bytes() == 0 && align == alloc.align => {
alloc
},
_ => bug!("static const eval returned {:#?}", static_),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/operand.rs
Expand Up @@ -109,7 +109,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
let b_llval = bx.const_usize((end - start) as u64);
OperandValue::Pair(a_llval, b_llval)
},
ConstValue::ByRef(offset, align, alloc) => {
ConstValue::ByRef { offset, align, alloc } => {
return bx.load_operand(bx.from_const_alloc(layout, align, alloc, offset));
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/place.rs
Expand Up @@ -424,7 +424,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let layout = cx.layout_of(self.monomorphize(&ty));
match bx.tcx().const_eval(param_env.and(cid)) {
Ok(val) => match val.val {
mir::interpret::ConstValue::ByRef(offset, align, alloc) => {
mir::interpret::ConstValue::ByRef { offset, align, alloc } => {
bx.cx().from_const_alloc(layout, align, alloc, offset)
}
_ => bug!("promoteds should have an allocation: {:?}", val),
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_mir/const_eval.rs
Expand Up @@ -99,7 +99,7 @@ fn op_to_const<'tcx>(
Ok(mplace) => {
let ptr = mplace.ptr.to_ptr().unwrap();
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
ConstValue::ByRef(ptr.offset, mplace.align, alloc)
ConstValue::ByRef { offset: ptr.offset, align: mplace.align, alloc }
},
// see comment on `let try_as_immediate` above
Err(ImmTy { imm: Immediate::Scalar(x), .. }) => match x {
Expand All @@ -113,7 +113,7 @@ fn op_to_const<'tcx>(
let mplace = op.to_mem_place();
let ptr = mplace.ptr.to_ptr().unwrap();
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
ConstValue::ByRef(ptr.offset, mplace.align, alloc)
ConstValue::ByRef { offset: ptr.offset, align: mplace.align, alloc }
},
},
Err(ImmTy { imm: Immediate::ScalarPair(a, b), .. }) => {
Expand Down Expand Up @@ -541,11 +541,11 @@ fn validate_and_turn_into_const<'tcx>(
if tcx.is_static(def_id) || cid.promoted.is_some() {
let ptr = mplace.ptr.to_ptr()?;
Ok(tcx.mk_const(ty::Const {
val: ConstValue::ByRef(
ptr.offset,
mplace.align,
ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id),
),
val: ConstValue::ByRef {
offset: ptr.offset,
align: mplace.align,
alloc: ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id),
},
ty: mplace.layout.ty,
}))
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_mir/hair/pattern/_match.rs
Expand Up @@ -217,12 +217,12 @@ impl LiteralExpander<'tcx> {
// the easy case, deref a reference
(ConstValue::Scalar(Scalar::Ptr(p)), x, y) if x == y => {
let alloc = self.tcx.alloc_map.lock().unwrap_memory(p.alloc_id);
ConstValue::ByRef(
p.offset,
ConstValue::ByRef {
offset: p.offset,
// FIXME(oli-obk): this should be the type's layout
alloc.align,
align: alloc.align,
alloc,
)
}
},
// unsize array to slice if pattern is array but match value or other patterns are slice
(ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => {
Expand Down Expand Up @@ -1436,7 +1436,7 @@ fn slice_pat_covered_by_const<'tcx>(
suffix: &[Pattern<'tcx>],
) -> Result<bool, ErrorReported> {
let data: &[u8] = match (const_val.val, &const_val.ty.sty) {
(ConstValue::ByRef(offset, _, alloc), ty::Array(t, n)) => {
(ConstValue::ByRef { offset, alloc, .. }, ty::Array(t, n)) => {
assert_eq!(*t, tcx.types.u8);
let n = n.assert_usize(tcx).unwrap();
let ptr = Pointer::new(AllocId(0), offset);
Expand Down Expand Up @@ -1759,7 +1759,7 @@ fn specialize<'p, 'a: 'p, 'tcx>(
let (alloc, offset, n, ty) = match value.ty.sty {
ty::Array(t, n) => {
match value.val {
ConstValue::ByRef(offset, _, alloc) => (
ConstValue::ByRef { offset, alloc, .. } => (
alloc,
offset,
n.unwrap_usize(cx.tcx),
Expand All @@ -1779,7 +1779,7 @@ fn specialize<'p, 'a: 'p, 'tcx>(
(end - start) as u64,
t,
),
ConstValue::ByRef(..) => {
ConstValue::ByRef { .. } => {
// FIXME(oli-obk): implement `deref` for `ConstValue`
return None;
},
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/operand.rs
Expand Up @@ -538,7 +538,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
self.layout_of(self.monomorphize(val.ty)?)
})?;
let op = match val.val {
ConstValue::ByRef(offset, align, alloc) => {
ConstValue::ByRef { offset, align, alloc } => {
let id = self.tcx.alloc_map.lock().create_memory_alloc(alloc);
// We rely on mutability being set correctly in that allocation to prevent writes
// where none should happen.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/monomorphize/collector.rs
Expand Up @@ -1262,7 +1262,7 @@ fn collect_const<'tcx>(
ConstValue::Scalar(Scalar::Ptr(ptr)) =>
collect_miri(tcx, ptr.alloc_id, output),
ConstValue::Slice { data: alloc, start: _, end: _ } |
ConstValue::ByRef(_, _, alloc) => {
ConstValue::ByRef { alloc, .. } => {
for &((), id) in alloc.relocations.values() {
collect_miri(tcx, id, output);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -1448,8 +1448,8 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: DefId, span: Span)
};
let param_env = ty::ParamEnv::reveal_all();
if let Ok(static_) = tcx.const_eval(param_env.and(cid)) {
let alloc = if let ConstValue::ByRef(_, _, allocation) = static_.val {
allocation
let alloc = if let ConstValue::ByRef { alloc, .. } = static_.val {
alloc
} else {
bug!("Matching on non-ByRef static")
};
Expand Down

0 comments on commit 811b996

Please sign in to comment.