diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs index 4eb7be542e7a1..149b3431fba62 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs @@ -198,7 +198,7 @@ fn check_opaque_type_parameter_valid( GenericArgKind::Lifetime(lt) => { matches!(lt, ty::ReEarlyBound(_) | ty::ReFree(_)) } - GenericArgKind::Const(ct) => matches!(ct.val, ty::ConstKind::Param(_)), + GenericArgKind::Const(ct) => matches!(ct.val(), ty::ConstKind::Param(_)), }; if arg_is_param { diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index ddd077c22faf8..43f1ac30032a1 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -382,7 +382,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { } else { let tcx = self.tcx(); let maybe_uneval = match constant.literal { - ConstantKind::Ty(ct) => match ct.val { + ConstantKind::Ty(ct) => match ct.val() { ty::ConstKind::Unevaluated(uv) => Some(uv), _ => None, }, @@ -1947,7 +1947,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { fn check_operand(&mut self, op: &Operand<'tcx>, location: Location) { if let Operand::Constant(constant) = op { let maybe_uneval = match constant.literal { - ConstantKind::Ty(ct) => match ct.val { + ConstantKind::Ty(ct) => match ct.val() { ty::ConstKind::Unevaluated(uv) => Some(uv), _ => None, }, diff --git a/compiler/rustc_codegen_cranelift/src/base.rs b/compiler/rustc_codegen_cranelift/src/base.rs index 1b30edd293862..d844e5fe1fc39 100644 --- a/compiler/rustc_codegen_cranelift/src/base.rs +++ b/compiler/rustc_codegen_cranelift/src/base.rs @@ -661,7 +661,7 @@ fn codegen_stmt<'tcx>( let times = fx .monomorphize(times) .eval(fx.tcx, ParamEnv::reveal_all()) - .val + .val() .try_to_bits(fx.tcx.data_layout.pointer_size) .unwrap(); if operand.layout().size.bytes() == 0 { diff --git a/compiler/rustc_codegen_cranelift/src/constant.rs b/compiler/rustc_codegen_cranelift/src/constant.rs index 5c4991f1fb667..16622a2d24059 100644 --- a/compiler/rustc_codegen_cranelift/src/constant.rs +++ b/compiler/rustc_codegen_cranelift/src/constant.rs @@ -46,11 +46,11 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool { ConstantKind::Ty(ct) => ct, ConstantKind::Val(..) => continue, }; - match const_.val { + match const_.val() { ConstKind::Value(_) => {} ConstKind::Unevaluated(unevaluated) => { if let Err(err) = - fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) + fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), *unevaluated, None) { all_constants_ok = false; match err { @@ -127,16 +127,16 @@ pub(crate) fn codegen_constant<'tcx>( ConstantKind::Ty(ct) => ct, ConstantKind::Val(val, ty) => return codegen_const_value(fx, val, ty), }; - let const_val = match const_.val { - ConstKind::Value(const_val) => const_val, + let const_val = match const_.val() { + ConstKind::Value(const_val) => *const_val, ConstKind::Unevaluated(uv) if fx.tcx.is_static(uv.def.did) => { assert!(uv.substs(fx.tcx).is_empty()); assert!(uv.promoted.is_none()); - return codegen_static_ref(fx, uv.def.did, fx.layout_of(const_.ty)).to_cvalue(fx); + return codegen_static_ref(fx, uv.def.did, fx.layout_of(const_.ty())).to_cvalue(fx); } ConstKind::Unevaluated(unevaluated) => { - match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) { + match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), *unevaluated, None) { Ok(const_val) => const_val, Err(_) => { span_bug!(constant.span, "erroneous constant not captured by required_consts"); @@ -150,7 +150,7 @@ pub(crate) fn codegen_constant<'tcx>( | ConstKind::Error(_) => unreachable!("{:?}", const_), }; - codegen_const_value(fx, const_val, const_.ty) + codegen_const_value(fx, const_val, const_.ty()) } pub(crate) fn codegen_const_value<'tcx>( @@ -462,7 +462,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>( match operand { Operand::Constant(const_) => match const_.literal { ConstantKind::Ty(const_) => { - fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).val.try_to_value() + fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).val().try_to_value() } ConstantKind::Val(val, _) => Some(val), }, diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs index ab119ae25f5e8..ee5702a77e956 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs @@ -146,7 +146,7 @@ fn push_debuginfo_type_name<'tcx>( if cpp_like_names { output.push_str("array$<"); push_debuginfo_type_name(tcx, inner_type, true, output, visited); - match len.val { + match len.val() { ty::ConstKind::Param(param) => write!(output, ",{}>", param.name).unwrap(), _ => write!(output, ",{}>", len.eval_usize(tcx, ty::ParamEnv::reveal_all())) .unwrap(), @@ -154,7 +154,7 @@ fn push_debuginfo_type_name<'tcx>( } else { output.push('['); push_debuginfo_type_name(tcx, inner_type, true, output, visited); - match len.val { + match len.val() { ty::ConstKind::Param(param) => write!(output, "; {}]", param.name).unwrap(), _ => write!(output, "; {}]", len.eval_usize(tcx, ty::ParamEnv::reveal_all())) .unwrap(), @@ -584,18 +584,18 @@ fn push_generic_params_internal<'tcx>( } fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: &'tcx ty::Const<'tcx>, output: &mut String) { - match ct.val { + match ct.val() { ty::ConstKind::Param(param) => { write!(output, "{}", param.name) } - _ => match ct.ty.kind() { + _ => match ct.ty().kind() { ty::Int(ity) => { - let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty); + let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty()); let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128; write!(output, "{}", val) } ty::Uint(_) => { - let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty); + let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty()); write!(output, "{}", val) } ty::Bool => { @@ -610,7 +610,7 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: &'tcx ty::Const<'tcx>, output: let mut hasher = StableHasher::new(); hcx.while_hashing_spans(false, |hcx| { hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { - ct.val.hash_stable(hcx, &mut hasher); + ct.val().hash_stable(hcx, &mut hasher); }); }); // Let's only emit 64 bits of the hash value. That should be plenty for diff --git a/compiler/rustc_codegen_ssa/src/mir/constant.rs b/compiler/rustc_codegen_ssa/src/mir/constant.rs index 93b39dc8e9ee1..2288318616a73 100644 --- a/compiler/rustc_codegen_ssa/src/mir/constant.rs +++ b/compiler/rustc_codegen_ssa/src/mir/constant.rs @@ -29,16 +29,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { mir::ConstantKind::Ty(ct) => ct, mir::ConstantKind::Val(val, _) => return Ok(val), }; - match ct.val { + match ct.val() { ty::ConstKind::Unevaluated(ct) => self .cx .tcx() - .const_eval_resolve(ty::ParamEnv::reveal_all(), ct, None) + .const_eval_resolve(ty::ParamEnv::reveal_all(), *ct, None) .map_err(|err| { self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered"); err }), - ty::ConstKind::Value(value) => Ok(value), + ty::ConstKind::Value(value) => Ok(*value), err => span_bug!( constant.span, "encountered bad ConstKind after monomorphizing: {:?}", @@ -65,7 +65,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { .fields .iter() .map(|field| { - if let Some(prim) = field.val.try_to_scalar() { + if let Some(prim) = field.val().try_to_scalar() { let layout = bx.layout_of(field_ty); let scalar = match layout.abi { Abi::Scalar(x) => x, @@ -78,7 +78,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { }) .collect(); let llval = bx.const_struct(&values, false); - (llval, c.ty) + (llval, c.ty()) }) .unwrap_or_else(|_| { bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time"); diff --git a/compiler/rustc_const_eval/src/const_eval/mod.rs b/compiler/rustc_const_eval/src/const_eval/mod.rs index a334165df4cb1..93806b521ce71 100644 --- a/compiler/rustc_const_eval/src/const_eval/mod.rs +++ b/compiler/rustc_const_eval/src/const_eval/mod.rs @@ -146,7 +146,7 @@ pub(crate) fn destructure_const<'tcx>( let op = ecx.const_to_op(val, None).unwrap(); // We go to `usize` as we cannot allocate anything bigger anyway. - let (field_count, variant, down) = match val.ty.kind() { + let (field_count, variant, down) = match val.ty().kind() { ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op), ty::Adt(def, _) if def.variants.is_empty() => { return mir::DestructuredConst { variant: None, fields: &[] }; @@ -203,5 +203,5 @@ pub(crate) fn deref_const<'tcx>( }, }; - tcx.mk_const(ty::Const { val: ty::ConstKind::Value(op_to_const(&ecx, &mplace.into())), ty }) + tcx.mk_const(ty, ty::ConstKind::Value(op_to_const(&ecx, &mplace.into()))) } diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index 698742fe98ceb..916bf6ed088b4 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -170,7 +170,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }; let val = self.tcx.const_eval_global_id(self.param_env, gid, Some(self.tcx.span))?; - let val = self.const_val_to_op(val, ty, Some(dest.layout))?; + let val = self.const_val_to_op(&val, ty, Some(dest.layout))?; self.copy_op(&val, dest)?; } diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index b6682b13ed216..dc1497dc30ccd 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -552,7 +552,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { val: &ty::Const<'tcx>, layout: Option>, ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> { - match val.val { + match val.val() { ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric), ty::ConstKind::Error(_) => throw_inval!(AlreadyReported(ErrorReported)), ty::ConstKind::Unevaluated(uv) => { @@ -562,7 +562,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ty::ConstKind::Infer(..) | ty::ConstKind::Placeholder(..) => { span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", val) } - ty::ConstKind::Value(val_val) => self.const_val_to_op(val_val, val.ty, layout), + ty::ConstKind::Value(val_val) => self.const_val_to_op(val_val, val.ty(), layout), } } @@ -573,13 +573,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> { match val { mir::ConstantKind::Ty(ct) => self.const_to_op(ct, layout), - mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, ty, layout), + mir::ConstantKind::Val(val, ty) => self.const_val_to_op(val, ty, layout), } } crate fn const_val_to_op( &self, - val_val: ConstValue<'tcx>, + val_val: &ConstValue<'tcx>, ty: Ty<'tcx>, layout: Option>, ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> { @@ -596,20 +596,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let id = self.tcx.create_memory_alloc(alloc); // We rely on mutability being set correctly in that allocation to prevent writes // where none should happen. - let ptr = self.global_base_pointer(Pointer::new(id, offset))?; + let ptr = self.global_base_pointer(Pointer::new(id, *offset))?; Operand::Indirect(MemPlace::from_ptr(ptr.into(), layout.align.abi)) } - ConstValue::Scalar(x) => Operand::Immediate(tag_scalar(x)?.into()), + ConstValue::Scalar(x) => Operand::Immediate(tag_scalar(*x)?.into()), ConstValue::Slice { data, start, end } => { // We rely on mutability being set correctly in `data` to prevent writes // where none should happen. let ptr = Pointer::new( self.tcx.create_memory_alloc(data), - Size::from_bytes(start), // offset: `start` + Size::from_bytes(*start), // offset: `start` ); Operand::Immediate(Immediate::new_slice( Scalar::from_pointer(self.global_base_pointer(ptr)?, &*self.tcx), - u64::try_from(end.checked_sub(start).unwrap()).unwrap(), // len: `end - start` + u64::try_from(end.checked_sub(*start).unwrap()).unwrap(), // len: `end - start` self, )) } diff --git a/compiler/rustc_const_eval/src/interpret/util.rs b/compiler/rustc_const_eval/src/interpret/util.rs index a16388d5de219..c38a368b6a820 100644 --- a/compiler/rustc_const_eval/src/interpret/util.rs +++ b/compiler/rustc_const_eval/src/interpret/util.rs @@ -55,7 +55,7 @@ where assert!(matches!(ty.kind(), ty::Param(_))) } ty::subst::GenericArgKind::Const(ct) => { - assert!(matches!(ct.val, ty::ConstKind::Param(_))) + assert!(matches!(ct.val(), ty::ConstKind::Param(_))) } ty::subst::GenericArgKind::Lifetime(..) => (), }, @@ -69,7 +69,7 @@ where } fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow { - match c.val { + match c.val() { ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam), _ => c.super_visit_with(self), } diff --git a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs index abc5a3c6a5206..05ea5265f28ef 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs @@ -318,7 +318,8 @@ where // Check the qualifs of the value of `const` items. if let Some(ct) = constant.literal.const_for_ty() { - if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs_: _, promoted }) = ct.val { + if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs_: _, promoted }) = ct.val() + { // Use qualifs of the type for the promoted. Promoteds in MIR body should be possible // only for `NeedsNonConstDrop` with precise drop checking. This is the only const // check performed after the promotion. Verify that with an assertion. diff --git a/compiler/rustc_const_eval/src/transform/promote_consts.rs b/compiler/rustc_const_eval/src/transform/promote_consts.rs index 7bf378601e053..b1f09be0d7b62 100644 --- a/compiler/rustc_const_eval/src/transform/promote_consts.rs +++ b/compiler/rustc_const_eval/src/transform/promote_consts.rs @@ -849,9 +849,9 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> { span, user_ty: None, literal: tcx - .mk_const(ty::Const { + .mk_const( ty, - val: ty::ConstKind::Unevaluated(ty::Unevaluated { + ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs_: Some(InternalSubsts::for_item( tcx, @@ -866,7 +866,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> { )), promoted: Some(promoted_id), }), - }) + ) .into(), })) }; diff --git a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs index 934ada9932e71..9b84392b3a7c8 100644 --- a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs +++ b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs @@ -410,10 +410,10 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - match ct.val { + match ct.val() { ty::ConstKind::Infer(InferConst::Var(vid)) => { debug!("canonical: const var found with vid {:?}", vid); - match self.infcx.probe_const_var(vid) { + match self.infcx.probe_const_var(*vid) { Ok(c) => { debug!("(resolved to {:?})", c); return self.fold_const(c); @@ -435,7 +435,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> { bug!("encountered a fresh const during canonicalization") } ty::ConstKind::Bound(debruijn, _) => { - if debruijn >= self.binder_index { + if *debruijn >= self.binder_index { bug!("escaping bound type during canonicalization") } else { return ct; @@ -443,7 +443,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> { } ty::ConstKind::Placeholder(placeholder) => { return self.canonicalize_const_var( - CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderConst(placeholder) }, + CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderConst(*placeholder) }, ct, ); } @@ -640,10 +640,10 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> { self.fold_const(bound_to) } else { let var = self.canonical_var(info, const_var.into()); - self.tcx().mk_const(ty::Const { - val: ty::ConstKind::Bound(self.binder_index, var), - ty: self.fold_ty(const_var.ty), - }) + self.tcx().mk_const( + self.fold_ty(const_var.ty()), + ty::ConstKind::Bound(self.binder_index, var), + ) } } } diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index 0c26639e9b0fe..a82a85b0a3511 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -151,12 +151,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, name }) => { let universe_mapped = universe_map(universe); let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, name }; - self.tcx - .mk_const(ty::Const { - val: ty::ConstKind::Placeholder(placeholder_mapped), - ty: name.ty, - }) - .into() + self.tcx.mk_const(name.ty, ty::ConstKind::Placeholder(placeholder_mapped)).into() } } } diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 5b4a9d9dfad45..b226cee8518c7 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -438,7 +438,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { } } GenericArgKind::Const(result_value) => { - if let ty::Const { val: ty::ConstKind::Bound(debrujin, b), .. } = result_value { + if let ty::ConstKind::Bound(debrujin, b) = result_value.val() { // ...in which case we would set `canonical_vars[0]` to `Some(const X)`. // We only allow a `ty::INNERMOST` index in substitutions. diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index 09bfb3290f4ca..157a1fc1cd7cb 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -141,7 +141,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { let a_is_expected = relation.a_is_expected(); - match (a.val, b.val) { + match (a.val(), b.val()) { ( ty::ConstKind::Infer(InferConst::Var(a_vid)), ty::ConstKind::Infer(InferConst::Var(b_vid)), @@ -149,7 +149,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { self.inner .borrow_mut() .const_unification_table() - .unify_var_var(a_vid, b_vid) + .unify_var_var(*a_vid, *b_vid) .map_err(|e| const_unification_error(a_is_expected, e))?; return Ok(a); } @@ -161,11 +161,11 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { } (ty::ConstKind::Infer(InferConst::Var(vid)), _) => { - return self.unify_const_variable(relation.param_env(), vid, b, a_is_expected); + return self.unify_const_variable(relation.param_env(), *vid, b, a_is_expected); } (_, ty::ConstKind::Infer(InferConst::Var(vid))) => { - return self.unify_const_variable(relation.param_env(), vid, a, !a_is_expected); + return self.unify_const_variable(relation.param_env(), *vid, a, !a_is_expected); } (ty::ConstKind::Unevaluated(..), _) if self.tcx.lazy_normalization() => { // FIXME(#59490): Need to remove the leak check to accommodate @@ -722,11 +722,11 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { assert_eq!(c, c2); // we are abusing TypeRelation here; both LHS and RHS ought to be == - match c.val { + match c.val() { ty::ConstKind::Infer(InferConst::Var(vid)) => { let mut inner = self.infcx.inner.borrow_mut(); let variable_table = &mut inner.const_unification_table(); - let var_value = variable_table.probe_value(vid); + let var_value = variable_table.probe_value(*vid); match var_value.val { ConstVariableValue::Known { value: u } => { drop(inner); @@ -740,7 +740,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { origin: var_value.origin, val: ConstVariableValue::Unknown { universe: self.for_universe }, }); - Ok(self.tcx().mk_const_var(new_var_id, c.ty)) + Ok(self.tcx().mk_const_var(new_var_id, c.ty())) } } } @@ -754,10 +754,10 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> { substs, substs, )?; - Ok(self.tcx().mk_const(ty::Const { - ty: c.ty, - val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(uv.def, substs)), - })) + Ok(self.tcx().mk_const( + c.ty(), + ty::ConstKind::Unevaluated(ty::Unevaluated::new(uv.def, substs)), + )) } _ => relate::super_relate_consts(self, c, c), } @@ -951,7 +951,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> { debug_assert_eq!(c, _c); debug!("ConstInferUnifier: c={:?}", c); - match c.val { + match c.val() { ty::ConstKind::Infer(InferConst::Var(vid)) => { // Check if the current unification would end up // unifying `target_vid` with a const which contains @@ -963,13 +963,13 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> { .inner .borrow_mut() .const_unification_table() - .unioned(self.target_vid, vid) + .unioned(self.target_vid, *vid) { return Err(TypeError::CyclicConst(c)); } let var_value = - self.infcx.inner.borrow_mut().const_unification_table().probe_value(vid); + self.infcx.inner.borrow_mut().const_unification_table().probe_value(*vid); match var_value.val { ConstVariableValue::Known { value: u } => self.consts(u, u), ConstVariableValue::Unknown { universe } => { @@ -985,7 +985,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> { }, }, ); - Ok(self.tcx().mk_const_var(new_var_id, c.ty)) + Ok(self.tcx().mk_const_var(new_var_id, c.ty())) } } } @@ -999,10 +999,10 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> { substs, substs, )?; - Ok(self.tcx().mk_const(ty::Const { - ty: c.ty, - val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(uv.def, substs)), - })) + Ok(self.tcx().mk_const( + c.ty(), + ty::ConstKind::Unevaluated(ty::Unevaluated::new(uv.def, substs)), + )) } _ => relate::super_relate_consts(self, c, c), } diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index e00003face9ce..996250b887e2b 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -390,9 +390,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { } } GenericArgKind::Const(ct) => { - if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val { + if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val() { let origin = - self.inner.borrow_mut().const_unification_table().probe_value(vid).origin; + self.inner.borrow_mut().const_unification_table().probe_value(*vid).origin; if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) = origin.kind { diff --git a/compiler/rustc_infer/src/infer/freshen.rs b/compiler/rustc_infer/src/infer/freshen.rs index c40e409891bc2..6195d0ea934f6 100644 --- a/compiler/rustc_infer/src/infer/freshen.rs +++ b/compiler/rustc_infer/src/infer/freshen.rs @@ -222,25 +222,25 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - match ct.val { + match ct.val() { ty::ConstKind::Infer(ty::InferConst::Var(v)) => { let opt_ct = self .infcx .inner .borrow_mut() .const_unification_table() - .probe_value(v) + .probe_value(*v) .val .known(); return self.freshen_const( opt_ct, - ty::InferConst::Var(v), + ty::InferConst::Var(*v), ty::InferConst::Fresh, - ct.ty, + ct.ty(), ); } ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => { - if i >= self.const_freshen_count { + if *i >= self.const_freshen_count { bug!( "Encountered a freshend const with id {} \ but our counter is only at {}", diff --git a/compiler/rustc_infer/src/infer/fudge.rs b/compiler/rustc_infer/src/infer/fudge.rs index 773753a036326..bee2dfc9ff32b 100644 --- a/compiler/rustc_infer/src/infer/fudge.rs +++ b/compiler/rustc_infer/src/infer/fudge.rs @@ -231,7 +231,8 @@ impl<'a, 'tcx> TypeFolder<'tcx> for InferenceFudger<'a, 'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ty::ConstKind::Infer(ty::InferConst::Var(vid)), ty } = ct { + if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.val() { + let ty = ct.ty(); if self.const_vars.0.contains(&vid) { // This variable was created during the fudging. // Recreate it with a fresh variable here. diff --git a/compiler/rustc_infer/src/infer/higher_ranked/mod.rs b/compiler/rustc_infer/src/infer/higher_ranked/mod.rs index ae85e55da6ae3..7128f79c0e2b8 100644 --- a/compiler/rustc_infer/src/infer/higher_ranked/mod.rs +++ b/compiler/rustc_infer/src/infer/higher_ranked/mod.rs @@ -94,13 +94,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { }; let fld_c = |bound_var: ty::BoundVar, ty| { - self.tcx.mk_const(ty::Const { - val: ty::ConstKind::Placeholder(ty::PlaceholderConst { + self.tcx.mk_const( + ty, + ty::ConstKind::Placeholder(ty::PlaceholderConst { universe: next_universe, name: ty::BoundConst { var: bound_var, ty }, }), - ty, - }) + ) }; let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t, fld_c); diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index b874947cc6968..882768f218132 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1734,8 +1734,8 @@ impl TyOrConstInferVar<'tcx> { /// Tries to extract an inference variable from a constant, returns `None` /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`). pub fn maybe_from_const(ct: &'tcx ty::Const<'tcx>) -> Option { - match ct.val { - ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)), + match ct.val() { + ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(*v)), _ => None, } } @@ -1755,7 +1755,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct { + if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val() { self.infcx .inner .borrow_mut() diff --git a/compiler/rustc_infer/src/infer/nll_relate/mod.rs b/compiler/rustc_infer/src/infer/nll_relate/mod.rs index 29a9cbc7a99c3..e8623f7ee934d 100644 --- a/compiler/rustc_infer/src/infer/nll_relate/mod.rs +++ b/compiler/rustc_infer/src/infer/nll_relate/mod.rs @@ -619,7 +619,7 @@ where b = self.infcx.shallow_resolve(b); } - match b.val { + match b.val() { ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { // Forbid inference variables in the RHS. bug!("unexpected inference var {:?}", b) @@ -1001,14 +1001,14 @@ where a: &'tcx ty::Const<'tcx>, _: &'tcx ty::Const<'tcx>, ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> { - match a.val { + match a.val() { ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => { bug!("unexpected inference variable encountered in NLL generalization: {:?}", a); } ty::ConstKind::Infer(InferConst::Var(vid)) => { let mut inner = self.infcx.inner.borrow_mut(); let variable_table = &mut inner.const_unification_table(); - let var_value = variable_table.probe_value(vid); + let var_value = variable_table.probe_value(*vid); match var_value.val.known() { Some(u) => self.relate(u, u), None => { @@ -1016,7 +1016,7 @@ where origin: var_value.origin, val: ConstVariableValue::Unknown { universe: self.universe }, }); - Ok(self.tcx().mk_const_var(new_var_id, a.ty)) + Ok(self.tcx().mk_const_var(new_var_id, a.ty())) } } } diff --git a/compiler/rustc_infer/src/infer/resolve.rs b/compiler/rustc_infer/src/infer/resolve.rs index 4b08c2eb9c19e..c8d8155839ce5 100644 --- a/compiler/rustc_infer/src/infer/resolve.rs +++ b/compiler/rustc_infer/src/infer/resolve.rs @@ -239,10 +239,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> { c // micro-optimize -- if there is nothing in this const that this fold affects... } else { let c = self.infcx.shallow_resolve(c); - match c.val { + match c.val() { ty::ConstKind::Infer(InferConst::Var(vid)) => { - self.err = Some(FixupError::UnresolvedConst(vid)); - return self.tcx().const_error(c.ty); + self.err = Some(FixupError::UnresolvedConst(*vid)); + return self.tcx().const_error(c.ty()); } ty::ConstKind::Infer(InferConst::Fresh(_)) => { bug!("Unexpected const in full const resolver: {:?}", c); diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index f2e4e70a19779..433453d813da2 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2903,7 +2903,7 @@ impl ClashingExternDeclarations { } (Array(a_ty, a_const), Array(b_ty, b_const)) => { // For arrays, we also check the constness of the type. - a_const.val == b_const.val + a_const.val() == b_const.val() && structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind) } (Slice(a_ty), Slice(b_ty)) => { diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index eeb0a77adc0ad..e7620eae1dc18 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -122,7 +122,9 @@ provide! { <'tcx> tcx, def_id, other, cdata, promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) } thir_abstract_const => { cdata.get_thir_abstract_const(tcx, def_id.index) } unused_generic_params => { cdata.get_unused_generic_params(def_id.index) } - const_param_default => { tcx.mk_const(cdata.get_const_param_default(tcx, def_id.index)) } + const_param_default => { + tcx.mk_const_(cdata.get_const_param_default(tcx, def_id.index)) + } mir_const_qualif => { cdata.mir_const_qualif(def_id.index) } fn_sig => { cdata.fn_sig(def_id.index, tcx) } inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } diff --git a/compiler/rustc_middle/src/infer/canonical.rs b/compiler/rustc_middle/src/infer/canonical.rs index d764d45ba7e5c..85b280877f5c5 100644 --- a/compiler/rustc_middle/src/infer/canonical.rs +++ b/compiler/rustc_middle/src/infer/canonical.rs @@ -320,10 +320,10 @@ impl<'tcx> CanonicalVarValues<'tcx> { tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into() } GenericArgKind::Const(ct) => tcx - .mk_const(ty::Const { - ty: ct.ty, - val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)), - }) + .mk_const( + ct.ty(), + ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)), + ) .into(), }) .collect(), diff --git a/compiler/rustc_middle/src/infer/unify_key.rs b/compiler/rustc_middle/src/infer/unify_key.rs index 0b05dd5c0ba6f..7b5c505f52d7e 100644 --- a/compiler/rustc_middle/src/infer/unify_key.rs +++ b/compiler/rustc_middle/src/infer/unify_key.rs @@ -172,7 +172,7 @@ where V: snapshot_vec::VecLike>>, L: UndoLogs>>>, { - if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = c { + if let ty::ConstKind::Infer(InferConst::Var(vid)) = c.val() { match table.probe_value(*vid).val.known() { Some(c) => c, None => c, diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index cc31d8c2c1879..0e4fa9f4dcf11 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -587,12 +587,12 @@ impl<'tcx, Tag: Provenance> ScalarMaybeUninit { } /// Gets the bytes of a constant slice value. -pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] { +pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: &ConstValue<'tcx>) -> &'tcx [u8] { if let ConstValue::Slice { data, start, end } = val { - let len = end - start; + let len = *end - *start; data.get_bytes( cx, - AllocRange { start: Size::from_bytes(start), size: Size::from_bytes(len) }, + AllocRange { start: Size::from_bytes(*start), size: Size::from_bytes(len) }, ) .unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err)) } else { diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 253ac266bedaa..df17d8a818b2f 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2497,7 +2497,7 @@ pub enum ConstantKind<'tcx> { impl Constant<'tcx> { pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option { - match self.literal.const_for_ty()?.val.try_to_scalar() { + match self.literal.const_for_ty()?.val().try_to_scalar() { Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) { GlobalAlloc::Static(def_id) => { assert!(!tcx.is_thread_local_static(def_id)); @@ -2532,7 +2532,7 @@ impl ConstantKind<'tcx> { pub fn ty(&self) -> Ty<'tcx> { match self { - ConstantKind::Ty(c) => c.ty, + ConstantKind::Ty(c) => c.ty(), ConstantKind::Val(_, ty) => ty, } } @@ -2540,7 +2540,7 @@ impl ConstantKind<'tcx> { #[inline] pub fn try_to_value(self) -> Option> { match self { - ConstantKind::Ty(c) => c.val.try_to_value(), + ConstantKind::Ty(c) => c.val().try_to_value(), ConstantKind::Val(val, _) => Some(val), } } diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 8e1b887f87da7..113c72f5a9a9e 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -464,8 +464,9 @@ impl Visitor<'tcx> for ExtraComments<'tcx> { fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, _: Location) { self.super_const(constant); - let ty::Const { ty, val, .. } = constant; - if use_verbose(ty, false) { + let ty = constant.ty(); + let val = constant.val(); + if use_verbose(&ty, false) { self.push("ty::Const"); self.push(&format!("+ ty: {:?}", ty)); let val = match val { @@ -668,7 +669,7 @@ pub fn write_allocations<'tcx>( fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator + '_ { alloc.relocations().values().map(|id| *id) } - fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator + '_ { + fn alloc_ids_from_const<'a>(val: &'a ConstValue<'a>) -> impl Iterator + 'a { match val { ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _size)) => { Either::Left(Either::Left(std::iter::once(ptr.provenance))) @@ -690,7 +691,7 @@ pub fn write_allocations<'tcx>( } fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow { - if let ty::ConstKind::Value(val) = c.val { + if let ty::ConstKind::Value(val) = c.val() { self.0.extend(alloc_ids_from_const(val)); } c.super_visit_with(self) diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 434008ecb1f4f..ac6ba43718c1c 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -333,7 +333,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?)) + Ok(decoder.tcx().mk_const_(Decodable::decode(decoder)?)) } } diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 27e22ccac02a7..7696cb5692612 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -1,3 +1,5 @@ +use std::hash::{Hash, Hasher}; + use crate::mir::interpret::ConstValue; use crate::mir::interpret::{LitToConstInput, Scalar}; use crate::ty::{ @@ -18,18 +20,28 @@ pub use kind::*; pub use valtree::*; /// Typed constant value. -#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, PartialEq, Eq, Ord, PartialOrd)] #[derive(HashStable)] pub struct Const<'tcx> { - pub ty: Ty<'tcx>, + pub(super) ty: Ty<'tcx>, - pub val: ConstKind<'tcx>, + pub(super) val: ConstKind<'tcx>, } #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] static_assert_size!(Const<'_>, 48); impl<'tcx> Const<'tcx> { + #[inline] + pub fn ty(&self) -> Ty<'tcx> { + self.ty + } + + #[inline] + pub fn val(&self) -> &ConstKind<'tcx> { + &self.val + } + /// Literals and const generic parameters are eagerly converted to a constant, everything else /// becomes `Unevaluated`. pub fn from_anon_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx Self { @@ -58,14 +70,14 @@ impl<'tcx> Const<'tcx> { match Self::try_eval_lit_or_param(tcx, ty, expr) { Some(v) => v, - None => tcx.mk_const(ty::Const { - val: ty::ConstKind::Unevaluated(ty::Unevaluated { + None => tcx.mk_const( + ty, + ty::ConstKind::Unevaluated(ty::Unevaluated { def: def.to_global(), substs_: None, promoted: None, }), - ty, - }), + ), } } @@ -115,10 +127,7 @@ impl<'tcx> Const<'tcx> { let generics = tcx.generics_of(item_def_id.to_def_id()); let index = generics.param_def_id_to_index[&def_id]; let name = tcx.hir().name(hir_id); - Some(tcx.mk_const(ty::Const { - val: ty::ConstKind::Param(ty::ParamConst::new(index, name)), - ty, - })) + Some(tcx.mk_const(ty, ty::ConstKind::Param(ty::ParamConst::new(index, name)))) } _ => None, } @@ -150,14 +159,14 @@ impl<'tcx> Const<'tcx> { let substs = InlineConstSubsts::new(tcx, InlineConstSubstsParts { parent_substs, ty }) .substs; - tcx.mk_const(ty::Const { - val: ty::ConstKind::Unevaluated(ty::Unevaluated { + tcx.mk_const( + ty, + ty::ConstKind::Unevaluated(ty::Unevaluated { def: ty::WithOptConstParam::unknown(def_id).to_global(), substs_: Some(substs), promoted: None, }), - ty, - }) + ) } }; debug_assert!(!ret.has_free_regions(tcx)); @@ -167,7 +176,7 @@ impl<'tcx> Const<'tcx> { /// Interns the given value as a constant. #[inline] pub fn from_value(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>, ty: Ty<'tcx>) -> &'tcx Self { - tcx.mk_const(Self { val: ConstKind::Value(val), ty }) + tcx.mk_const(ty, ConstKind::Value(val)) } #[inline] @@ -259,6 +268,13 @@ impl<'tcx> Const<'tcx> { } } +impl Hash for &'tcx Const<'tcx> { + fn hash(&self, state: &mut H) { + let c: &'tcx Const<'tcx> = *self; + (c as *const Const<'tcx>).hash(state) + } +} + pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> { let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let default_def_id = match tcx.hir().get(hir_id) { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 8240273acad4c..75d67fb8a30e2 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -174,6 +174,11 @@ impl<'tcx> CtxtInterners<'tcx> { }) .0 } + + #[inline(never)] + fn intern_const(&self, const_: Const<'tcx>) -> &'tcx Const<'tcx> { + self.const_.intern(CstHash(const_), |c| Interned(self.arena.alloc(c.0))).0 + } } pub struct CommonTypes<'tcx> { @@ -929,13 +934,10 @@ impl<'tcx> CommonLifetimes<'tcx> { impl<'tcx> CommonConsts<'tcx> { fn new(interners: &CtxtInterners<'tcx>, types: &CommonTypes<'tcx>) -> CommonConsts<'tcx> { - let mk_const = |c| interners.const_.intern(c, |c| Interned(interners.arena.alloc(c))).0; + let mk_const = |ty, val| interners.intern_const(ty::Const { ty, val }); CommonConsts { - unit: mk_const(ty::Const { - val: ty::ConstKind::Value(ConstValue::Scalar(Scalar::ZST)), - ty: types.unit, - }), + unit: mk_const(types.unit, ty::ConstKind::Value(ConstValue::Scalar(Scalar::ZST))), } } } @@ -1209,7 +1211,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn const_error(self, ty: Ty<'tcx>) -> &'tcx Const<'tcx> { self.sess .delay_span_bug(DUMMY_SP, "ty::ConstKind::Error constructed but no error reported."); - self.mk_const(ty::Const { val: ty::ConstKind::Error(DelaySpanBugEmitted(())), ty }) + self.mk_const(ty, ty::ConstKind::Error(DelaySpanBugEmitted(()))) } pub fn consider_optimizing String>(self, msg: T) -> bool { @@ -2053,6 +2055,50 @@ impl<'tcx, T> Borrow<[T]> for Interned<'tcx, List> { } } +impl<'tcx> PartialEq for Interned<'tcx, Const<'tcx>> { + fn eq(&self, other: &Self) -> bool { + self.0.ty == other.0.ty && self.0.val == other.0.val + } +} + +impl<'tcx> Eq for Interned<'tcx, Const<'tcx>> {} + +#[repr(transparent)] +#[derive(Clone, Copy)] +struct CstHash<'tcx>(Const<'tcx>); + +impl<'tcx> Hash for CstHash<'tcx> { + fn hash(&self, state: &mut H) { + self.0.ty.hash(state); + self.0.val.hash(state); + } +} + +impl<'tcx> PartialEq for CstHash<'tcx> { + fn eq(&self, other: &Self) -> bool { + self.0.ty == other.0.ty && self.0.val == other.0.val + } +} + +impl Eq for CstHash<'tcx> {} + +impl<'tcx> Hash for Interned<'tcx, Const<'tcx>> { + fn hash(&self, state: &mut H) { + self.0.ty.hash(state); + self.0.val.hash(state); + } +} + +impl<'tcx> Borrow> for Interned<'tcx, Const<'tcx>> { + fn borrow(&self) -> &CstHash<'tcx> { + let c: &Const<'tcx> = &self.0; + unsafe { + // SAFETY: safe because CstHash is repr(transparent) over Const + &*(c as *const Const<'tcx> as *const CstHash<'tcx>) + } + } +} + macro_rules! direct_interners { ($($name:ident: $method:ident($ty:ty),)+) => { $(impl<'tcx> PartialEq for Interned<'tcx, $ty> { @@ -2087,7 +2133,6 @@ macro_rules! direct_interners { direct_interners! { region: mk_region(RegionKind), - const_: mk_const(Const<'tcx>), const_allocation: intern_const_alloc(Allocation), layout: intern_layout(Layout), } @@ -2201,6 +2246,17 @@ impl<'tcx> TyCtxt<'tcx> { Predicate { inner } } + #[inline] + pub fn mk_const(self, ty: Ty<'tcx>, val: ty::ConstKind<'tcx>) -> &'tcx Const<'tcx> { + self.interners.intern_const(ty::Const { ty, val }) + } + + /// Same as `mk_const` but interns a constructed `ty::Const` which one might get from decoding the on-disk cache + #[inline] + pub fn mk_const_(self, const_: ty::Const<'tcx>) -> &'tcx Const<'tcx> { + self.interners.intern_const(const_) + } + #[inline] pub fn reuse_or_mk_predicate( self, @@ -2410,7 +2466,7 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> &'tcx Const<'tcx> { - self.mk_const(ty::Const { val: ty::ConstKind::Infer(InferConst::Var(v)), ty }) + self.mk_const(ty, ty::ConstKind::Infer(InferConst::Var(v))) } #[inline] @@ -2430,7 +2486,7 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn mk_const_infer(self, ic: InferConst<'tcx>, ty: Ty<'tcx>) -> &'tcx ty::Const<'tcx> { - self.mk_const(ty::Const { val: ty::ConstKind::Infer(ic), ty }) + self.mk_const(ty, ty::ConstKind::Infer(ic)) } #[inline] @@ -2440,7 +2496,7 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn mk_const_param(self, index: u32, name: Symbol, ty: Ty<'tcx>) -> &'tcx Const<'tcx> { - self.mk_const(ty::Const { val: ty::ConstKind::Param(ParamConst { index, name }), ty }) + self.mk_const(ty, ty::ConstKind::Param(ParamConst { index, name })) } pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> GenericArg<'tcx> { diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index e16491dcc90b2..7aae80c62e212 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -717,13 +717,13 @@ impl<'tcx> TyCtxt<'tcx> { )) }, |c, ty| { - self.mk_const(ty::Const { - val: ty::ConstKind::Bound( + self.mk_const( + ty, + ty::ConstKind::Bound( ty::INNERMOST, ty::BoundVar::from_usize(c.as_usize() + bound_vars), ), - ty, - }) + ) }, ) } @@ -981,7 +981,7 @@ impl TypeFolder<'tcx> for Shifter<'tcx> { ct } else { let debruijn = debruijn.shifted_in(self.amount); - self.tcx.mk_const(ty::Const { val: ty::ConstKind::Bound(debruijn, bound_ct), ty }) + self.tcx.mk_const(ty, ty::ConstKind::Bound(debruijn, bound_ct)) } } else { ct.super_fold_with(self) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 5d9e7aaf72f8e..ab3a64c7c1ce0 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1230,7 +1230,7 @@ pub trait PrettyPrinter<'tcx>: { let contents = self.tcx().destructure_const( ty::ParamEnv::reveal_all() - .and(self.tcx().mk_const(ty::Const { val: ty::ConstKind::Value(ct), ty })), + .and(self.tcx().mk_const(ty, ty::ConstKind::Value(ct))), ); let fields = contents.fields.iter().copied(); diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index c7d8bec506f6b..f8465068bfb15 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -565,7 +565,7 @@ pub fn super_relate_consts>( // Currently, the values that can be unified are primitive types, // and those that derive both `PartialEq` and `Eq`, corresponding // to structural-match types. - let is_match = match (a.val, b.val) { + let is_match = match (a.val(), b.val()) { (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => { // The caller should handle these cases! bug!("var types encountered in super_relate_consts: {:?} {:?}", a, b) @@ -598,14 +598,14 @@ pub fn super_relate_consts>( au.substs(tcx), bu.substs(tcx), )?; - return Ok(tcx.mk_const(ty::Const { - val: ty::ConstKind::Unevaluated(ty::Unevaluated { + return Ok(tcx.mk_const( + a.ty, + ty::ConstKind::Unevaluated(ty::Unevaluated { def: au.def, substs_: Some(substs), promoted: au.promoted, }), - ty: a.ty, - })); + )); } _ => false, }; @@ -614,8 +614,8 @@ pub fn super_relate_consts>( fn check_const_value_eq>( relation: &mut R, - a_val: ConstValue<'tcx>, - b_val: ConstValue<'tcx>, + a_val: &ConstValue<'tcx>, + b_val: &ConstValue<'tcx>, // FIXME(oli-obk): these arguments should go away with valtrees a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>, diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 0f8e80806e31e..d3d2abf4ed521 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -1029,11 +1029,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> { fn super_fold_with>(self, folder: &mut F) -> Self { let ty = self.ty.fold_with(folder); let val = self.val.fold_with(folder); - if ty != self.ty || val != self.val { - folder.tcx().mk_const(ty::Const { ty, val }) - } else { - self - } + if ty != self.ty || val != self.val { folder.tcx().mk_const(ty, val) } else { self } } fn fold_with>(self, folder: &mut F) -> Self { diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 5e305ebba2ff4..79ac09d523d07 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -23,7 +23,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { inferred_ty: ty, }) }); - assert_eq!(literal.ty, ty); + assert_eq!(literal.ty(), ty); Constant { span, user_ty, literal: literal.into() } } ExprKind::StaticRef { literal, .. } => { diff --git a/compiler/rustc_mir_build/src/build/matches/simplify.rs b/compiler/rustc_mir_build/src/build/matches/simplify.rs index 4ce26cc8dff46..4f9a2c0ce779d 100644 --- a/compiler/rustc_mir_build/src/build/matches/simplify.rs +++ b/compiler/rustc_mir_build/src/build/matches/simplify.rs @@ -210,7 +210,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } PatKind::Range(PatRange { lo, hi, end }) => { - let (range, bias) = match *lo.ty.kind() { + let (range, bias) = match *lo.ty().kind() { ty::Char => { (Some(('\u{0000}' as u128, '\u{10FFFF}' as u128, Size::from_bits(32))), 0) } @@ -228,7 +228,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { _ => (None, 0), }; if let Some((min, max, sz)) = range { - if let (Some(lo), Some(hi)) = (lo.val.try_to_bits(sz), hi.val.try_to_bits(sz)) { + if let (Some(lo), Some(hi)) = + (lo.val().try_to_bits(sz), hi.val().try_to_bits(sz)) + { // We want to compare ranges numerically, but the order of the bitwise // representation of signed integers does not match their numeric order. // Thus, to correct the ordering, we need to shift the range of signed diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index a01df2372a097..deee8204d5962 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -59,8 +59,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }, PatKind::Range(range) => { - assert_eq!(range.lo.ty, match_pair.pattern.ty); - assert_eq!(range.hi.ty, match_pair.pattern.ty); + assert_eq!(range.lo.ty(), match_pair.pattern.ty); + assert_eq!(range.hi.ty(), match_pair.pattern.ty); Test { span: match_pair.pattern.span, kind: TestKind::Range(range) } } @@ -270,7 +270,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ty, ); } else if let [success, fail] = *make_target_blocks(self) { - assert_eq!(value.ty, ty); + assert_eq!(value.ty(), ty); let expect = self.literal_operand(test.span, value); let val = Operand::Copy(place); self.compare(block, success, fail, source_info, BinOp::Eq, expect, val); @@ -396,7 +396,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { _ => None, }; let opt_ref_ty = unsize(ty); - let opt_ref_test_ty = unsize(value.ty); + let opt_ref_test_ty = unsize(value.ty()); match (opt_ref_ty, opt_ref_test_ty) { // nothing to do, neither is an array (None, None) => {} @@ -653,7 +653,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let tcx = self.tcx; - let test_ty = test.lo.ty; + let test_ty = test.lo.ty(); let lo = compare_const_vals(tcx, test.lo, pat.hi, self.param_env, test_ty)?; let hi = compare_const_vals(tcx, test.hi, pat.lo, self.param_env, test_ty)?; @@ -782,8 +782,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let tcx = self.tcx; - let a = compare_const_vals(tcx, range.lo, value, self.param_env, range.lo.ty)?; - let b = compare_const_vals(tcx, value, range.hi, self.param_env, range.lo.ty)?; + let a = compare_const_vals(tcx, range.lo, value, self.param_env, range.lo.ty())?; + let b = compare_const_vals(tcx, value, range.hi, self.param_env, range.lo.ty())?; match (b, range.end) { (Less, _) | (Equal, RangeEnd::Included) if a != Greater => Some(true), diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index b4005ccd1cc42..02566c4b621de 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -711,14 +711,14 @@ impl<'tcx> Cx<'tcx> { // in case we are offsetting from a computed discriminant // and not the beginning of discriminants (which is always `0`) let substs = InternalSubsts::identity_for_item(self.tcx(), did); - let lhs = ty::Const { - val: ty::ConstKind::Unevaluated(ty::Unevaluated::new( + + let lhs = self.thir.exprs.push(mk_const(self.tcx().mk_const( + var_ty, + ty::ConstKind::Unevaluated(ty::Unevaluated::new( ty::WithOptConstParam::unknown(did), substs, )), - ty: var_ty, - }; - let lhs = self.thir.exprs.push(mk_const(self.tcx().mk_const(lhs))); + ))); let bin = ExprKind::Binary { op: BinOp::Add, lhs: lhs, rhs: offset }; self.thir.exprs.push(Expr { @@ -893,10 +893,7 @@ impl<'tcx> Cx<'tcx> { let name = self.tcx.hir().name(hir_id); let val = ty::ConstKind::Param(ty::ParamConst::new(index, name)); ExprKind::Literal { - literal: self.tcx.mk_const(ty::Const { - val, - ty: self.typeck_results().node_type(expr.hir_id), - }), + literal: self.tcx.mk_const(self.typeck_results().node_type(expr.hir_id), val), user_ty: None, const_id: Some(def_id), } @@ -906,13 +903,13 @@ impl<'tcx> Cx<'tcx> { let user_ty = self.user_substs_applied_to_res(expr.hir_id, res); debug!("convert_path_expr: (const) user_ty={:?}", user_ty); ExprKind::Literal { - literal: self.tcx.mk_const(ty::Const { - val: ty::ConstKind::Unevaluated(ty::Unevaluated::new( + literal: self.tcx.mk_const( + self.typeck_results().node_type(expr.hir_id), + ty::ConstKind::Unevaluated(ty::Unevaluated::new( ty::WithOptConstParam::unknown(def_id), substs, )), - ty: self.typeck_results().node_type(expr.hir_id), - }), + ), user_ty, const_id: Some(def_id), } diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index dd16e3cde75ae..5d475da49a06c 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -173,10 +173,11 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { // If we were able to successfully convert the const to some pat, // double-check that all types in the const implement `Structural`. - let structural = self.search_for_structural_match_violation(cv.ty); + let structural = self.search_for_structural_match_violation(cv.ty()); debug!( "search_for_structural_match_violation cv.ty: {:?} returned: {:?}", - cv.ty, structural + cv.ty(), + structural ); // This can occur because const qualification treats all associated constants as @@ -191,7 +192,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { } if let Some(msg) = structural { - if !self.type_may_have_partial_eq_impl(cv.ty) { + if !self.type_may_have_partial_eq_impl(cv.ty()) { // span_fatal avoids ICE from resolution of non-existent method (rare case). self.tcx().sess.span_fatal(self.span, &msg); } else if mir_structural_match_violation && !self.saw_const_match_lint.get() { @@ -270,7 +271,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { let tcx = self.tcx(); let param_env = self.param_env; - let kind = match cv.ty.kind() { + let kind = match cv.ty().kind() { ty::Float(_) => { if self.include_lint_checks { tcx.struct_span_lint_hir( @@ -294,14 +295,14 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { PatKind::Wild } ty::Adt(..) - if !self.type_may_have_partial_eq_impl(cv.ty) + if !self.type_may_have_partial_eq_impl(cv.ty()) // FIXME(#73448): Find a way to bring const qualification into parity with // `search_for_structural_match_violation` and then remove this condition. - && self.search_for_structural_match_violation(cv.ty).is_some() => + && self.search_for_structural_match_violation(cv.ty()).is_some() => { // Obtain the actual type that isn't annotated. If we just looked at `cv.ty` we // could get `Option`, even though `Option` is annotated with derive. - let msg = self.search_for_structural_match_violation(cv.ty).unwrap(); + let msg = self.search_for_structural_match_violation(cv.ty()).unwrap(); self.saw_const_match_error.set(true); if self.include_lint_checks { tcx.sess.span_err(self.span, &msg); @@ -319,7 +320,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { // details. // Backwards compatibility hack because we can't cause hard errors on these // types, so we compare them via `PartialEq::eq` at runtime. - ty::Adt(..) if !self.type_marked_structural(cv.ty) && self.behind_reference.get() => { + ty::Adt(..) if !self.type_marked_structural(cv.ty()) && self.behind_reference.get() => { if self.include_lint_checks && !self.saw_const_match_error.get() && !self.saw_const_match_lint.get() @@ -333,7 +334,8 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { let msg = format!( "to use a constant of type `{}` in a pattern, \ `{}` must be annotated with `#[derive(PartialEq, Eq)]`", - cv.ty, cv.ty, + cv.ty(), + cv.ty(), ); lint.build(&msg).emit() }, @@ -344,8 +346,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { // `PartialEq::eq` on it. return Err(fallback_to_const_ref(self)); } - ty::Adt(adt_def, _) if !self.type_marked_structural(cv.ty) => { - debug!("adt_def {:?} has !type_marked_structural for cv.ty: {:?}", adt_def, cv.ty); + ty::Adt(adt_def, _) if !self.type_marked_structural(cv.ty()) => { + debug!( + "adt_def {:?} has !type_marked_structural for cv.ty: {:?}", + adt_def, + cv.ty() + ); let path = tcx.def_path_str(adt_def.did); let msg = format!( "to use a constant of type `{}` in a pattern, \ @@ -389,7 +395,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { // These are not allowed and will error elsewhere anyway. ty::Dynamic(..) => { self.saw_const_match_error.set(true); - let msg = format!("`{}` cannot be used in patterns", cv.ty); + let msg = format!("`{}` cannot be used in patterns", cv.ty()); if self.include_lint_checks { tcx.sess.span_err(span, &msg); } else { @@ -546,7 +552,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { } _ => { self.saw_const_match_error.set(true); - let msg = format!("`{}` cannot be used in patterns", cv.ty); + let msg = format!("`{}` cannot be used in patterns", cv.ty()); if self.include_lint_checks { tcx.sess.span_err(span, &msg); } else { @@ -562,12 +568,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { && mir_structural_match_violation // FIXME(#73448): Find a way to bring const qualification into parity with // `search_for_structural_match_violation` and then remove this condition. - && self.search_for_structural_match_violation(cv.ty).is_some() + && self.search_for_structural_match_violation(cv.ty()).is_some() { self.saw_const_match_lint.set(true); // Obtain the actual type that isn't annotated. If we just looked at `cv.ty` we // could get `Option`, even though `Option` is annotated with derive. - let msg = self.search_for_structural_match_violation(cv.ty).unwrap().replace( + let msg = self.search_for_structural_match_violation(cv.ty()).unwrap().replace( "in a pattern,", "in a pattern, the constant's initializer must be trivial or", ); @@ -579,6 +585,6 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { ); } - Ok(Pat { span, ty: cv.ty, kind: Box::new(kind) }) + Ok(Pat { span, ty: cv.ty(), kind: Box::new(kind) }) } } diff --git a/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs index 368e3957dd0d9..c916de7843ff7 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs @@ -138,10 +138,10 @@ impl IntRange { param_env: ty::ParamEnv<'tcx>, value: &Const<'tcx>, ) -> Option { - if let Some((target_size, bias)) = Self::integral_size_and_signed_bias(tcx, value.ty) { - let ty = value.ty; + if let Some((target_size, bias)) = Self::integral_size_and_signed_bias(tcx, value.ty()) { + let ty = value.ty(); let val = (|| { - if let ty::ConstKind::Value(ConstValue::Scalar(scalar)) = value.val { + if let ty::ConstKind::Value(ConstValue::Scalar(scalar)) = value.val() { // For this specific pattern we can skip a lot of effort and go // straight to the result, after doing a bit of checking. (We // could remove this branch and just fall through, which @@ -1401,11 +1401,11 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> { } } &PatKind::Range(PatRange { lo, hi, end }) => { - let ty = lo.ty; + let ty = lo.ty(); ctor = if let Some(int_range) = IntRange::from_range( cx.tcx, - lo.eval_bits(cx.tcx, cx.param_env, lo.ty), - hi.eval_bits(cx.tcx, cx.param_env, hi.ty), + lo.eval_bits(cx.tcx, cx.param_env, lo.ty()), + hi.eval_bits(cx.tcx, cx.param_env, hi.ty()), ty, &end, ) { diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index ce80214c875fc..862d7e6d84737 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -126,8 +126,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { end: RangeEnd, span: Span, ) -> PatKind<'tcx> { - assert_eq!(lo.ty, ty); - assert_eq!(hi.ty, ty); + assert_eq!(lo.ty(), ty); + assert_eq!(hi.ty(), ty); let cmp = compare_const_vals(self.tcx, lo, hi, self.param_env, ty); match (end, cmp) { // `x..y` where `x < y`. @@ -514,7 +514,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { user_ty_span: span, }, }), - ty: const_.ty, + ty: const_.ty(), } } else { pattern @@ -545,7 +545,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { hir::ExprKind::ConstBlock(ref anon_const) => { let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id); let value = ty::Const::from_inline_const(self.tcx, anon_const_def_id); - if matches!(value.val, ConstKind::Param(_)) { + if matches!(value.val(), ConstKind::Param(_)) { let span = self.tcx.hir().span(anon_const.hir_id); self.errors.push(PatternError::ConstParamInPattern(span)); return PatKind::Wild; @@ -735,13 +735,13 @@ crate fn compare_const_vals<'tcx>( let fallback = || from_bool(a == b); // Use the fallback if any type differs - if a.ty != b.ty || a.ty != ty { + if a.ty() != b.ty() || a.ty() != ty { return fallback(); } // Early return for equal constants (so e.g. references to ZSTs can be compared, even if they // are just integer addresses). - if a.val == b.val { + if a.val() == b.val() { return from_bool(true); } @@ -776,7 +776,7 @@ crate fn compare_const_vals<'tcx>( if let ( ty::ConstKind::Value(a_val @ ConstValue::Slice { .. }), ty::ConstKind::Value(b_val @ ConstValue::Slice { .. }), - ) = (a.val, b.val) + ) = (a.val(), b.val()) { let a_bytes = get_slice_bytes(&tcx, a_val); let b_bytes = get_slice_bytes(&tcx, b_val); diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 63c637af5c21a..821e1234afbdf 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -486,7 +486,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { let err = ConstEvalErr::new(&self.ecx, error, Some(c.span)); if let Some(lint_root) = self.lint_root(source_info) { let lint_only = match c.literal { - ConstantKind::Ty(ct) => match ct.val { + ConstantKind::Ty(ct) => match ct.val() { // Promoteds must lint and not error as the user didn't ask for them ConstKind::Unevaluated(ty::Unevaluated { def: _, @@ -821,7 +821,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { ) { if let Rvalue::Use(Operand::Constant(c)) = rval { match c.literal { - ConstantKind::Ty(c) if matches!(c.val, ConstKind::Unevaluated(..)) => {} + ConstantKind::Ty(c) if matches!(c.val(), ConstKind::Unevaluated(..)) => {} _ => { trace!("skipping replace of Rvalue::Use({:?} because it is already a const", c); return; @@ -895,13 +895,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { literal: self .ecx .tcx - .mk_const(ty::Const { + .mk_const( ty, - val: ty::ConstKind::Value(ConstValue::ByRef { + ty::ConstKind::Value(ConstValue::ByRef { alloc, offset: Size::ZERO, }), - }) + ) .into(), }))); } diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 84a1e3fb600fd..a9166e0984e3f 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -624,7 +624,7 @@ impl Inliner<'tcx> { caller_body.required_consts.extend( callee_body.required_consts.iter().copied().filter(|&ct| { match ct.literal.const_for_ty() { - Some(ct) => matches!(ct.val, ConstKind::Unevaluated(_)), + Some(ct) => matches!(ct.val(), ConstKind::Unevaluated(_)), None => true, } }), diff --git a/compiler/rustc_mir_transform/src/required_consts.rs b/compiler/rustc_mir_transform/src/required_consts.rs index 8b64ad65ab35c..8bec9b4f054ed 100644 --- a/compiler/rustc_mir_transform/src/required_consts.rs +++ b/compiler/rustc_mir_transform/src/required_consts.rs @@ -15,7 +15,7 @@ impl<'a, 'tcx> RequiredConstsVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for RequiredConstsVisitor<'a, 'tcx> { fn visit_constant(&mut self, constant: &Constant<'tcx>, _: Location) { if let Some(ct) = constant.literal.const_for_ty() { - if let ConstKind::Unevaluated(_) = ct.val { + if let ConstKind::Unevaluated(_) = ct.val() { self.required_consts.push(*constant); } } diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 59988e69b5d3a..b41e038416a60 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -718,11 +718,11 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> { let literal = self.monomorphize(constant.literal); let val = match literal { mir::ConstantKind::Val(val, _) => val, - mir::ConstantKind::Ty(ct) => match ct.val { - ty::ConstKind::Value(val) => val, + mir::ConstantKind::Ty(ct) => match ct.val() { + ty::ConstKind::Value(val) => *val, ty::ConstKind::Unevaluated(ct) => { let param_env = ty::ParamEnv::reveal_all(); - match self.tcx.const_eval_resolve(param_env, ct, None) { + match self.tcx.const_eval_resolve(param_env, *ct, None) { // The `monomorphize` call should have evaluated that constant already. Ok(val) => val, Err(ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted) => return, @@ -746,10 +746,10 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> { let substituted_constant = self.monomorphize(*constant); let param_env = ty::ParamEnv::reveal_all(); - match substituted_constant.val { - ty::ConstKind::Value(val) => collect_const_value(self.tcx, val, self.output), + match substituted_constant.val() { + ty::ConstKind::Value(val) => collect_const_value(self.tcx, *val, self.output), ty::ConstKind::Unevaluated(unevaluated) => { - match self.tcx.const_eval_resolve(param_env, unevaluated, None) { + match self.tcx.const_eval_resolve(param_env, *unevaluated, None) { // The `monomorphize` call should have evaluated that constant already. Ok(val) => span_bug!( self.body.source_info(location).span, diff --git a/compiler/rustc_monomorphize/src/polymorphize.rs b/compiler/rustc_monomorphize/src/polymorphize.rs index 595080619da6f..6d4538c6b97c4 100644 --- a/compiler/rustc_monomorphize/src/polymorphize.rs +++ b/compiler/rustc_monomorphize/src/polymorphize.rs @@ -286,7 +286,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> { return ControlFlow::CONTINUE; } - match c.val { + match c.val() { ty::ConstKind::Param(param) => { debug!(?param); self.unused_parameters.clear(param.index); @@ -300,7 +300,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> { // If there is a promoted, don't look at the substs - since it will always contain // the generic parameters, instead, traverse the promoted MIR. let promoted = self.tcx.promoted_mir(def.did); - self.visit_body(&promoted[p]); + self.visit_body(&promoted[*p]); ControlFlow::CONTINUE } ty::ConstKind::Unevaluated(uv) @@ -361,7 +361,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a, 'tcx> { return ControlFlow::CONTINUE; } - match c.val { + match c.val() { ty::ConstKind::Param(param) => { if self.unused_parameters.contains(param.index).unwrap_or(false) { ControlFlow::CONTINUE diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 11668146f7b10..25287bbd5f8ab 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -286,7 +286,7 @@ where } fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow { - self.visit_ty(c.ty)?; + self.visit_ty(c.ty())?; let tcx = self.def_id_visitor.tcx(); if let Ok(Some(ct)) = AbstractConst::from_const(tcx, c) { self.visit_abstract_const_expr(tcx, ct)?; diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index de18614360ef5..4c2bca1382635 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -245,8 +245,8 @@ impl Printer<'tcx> for &mut SymbolPrinter<'tcx> { fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result { // only print integers - if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int { .. })) = ct.val { - if ct.ty.is_integral() { + if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int { .. })) = ct.val() { + if ct.ty().is_integral() { return self.pretty_print_const(ct, true); } } diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 0363ddb0e6eee..6b65795c5c0e9 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -577,7 +577,7 @@ impl Printer<'tcx> for &mut SymbolMangler<'tcx> { fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result { // We only mangle a typed value if the const can be evaluated. let ct = ct.eval(self.tcx, ty::ParamEnv::reveal_all()); - match ct.val { + match ct.val() { ty::ConstKind::Value(_) => {} // Placeholders (should be demangled as `_`). @@ -601,14 +601,14 @@ impl Printer<'tcx> for &mut SymbolMangler<'tcx> { } let start = self.out.len(); - match ct.ty.kind() { + match ct.ty().kind() { ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => { - self = ct.ty.print(self)?; + self = ct.ty().print(self)?; - let mut bits = ct.eval_bits(self.tcx, ty::ParamEnv::reveal_all(), ct.ty); + let mut bits = ct.eval_bits(self.tcx, ty::ParamEnv::reveal_all(), ct.ty()); // Negative integer values are mangled using `n` as a "sign prefix". - if let ty::Int(ity) = ct.ty.kind() { + if let ty::Int(ity) = ct.ty().kind() { let val = Integer::from_int_ty(&self.tcx, *ity).size().sign_extend(bits) as i128; if val < 0 { @@ -625,14 +625,14 @@ impl Printer<'tcx> for &mut SymbolMangler<'tcx> { // handle `&str` and include both `&` ("R") and `str` ("e") prefixes. ty::Ref(_, ty, hir::Mutability::Not) if *ty == self.tcx.types.str_ => { self.push("R"); - match ct.val { + match ct.val() { ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => { // NOTE(eddyb) the following comment was kept from `ty::print::pretty`: // The `inspect` here is okay since we checked the bounds, and there are no // relocations (we have an active `str` reference here). We don't use this // result to affect interpreter execution. let slice = - data.inspect_with_uninit_and_ptr_outside_interpreter(start..end); + data.inspect_with_uninit_and_ptr_outside_interpreter(*start..*end); let s = std::str::from_utf8(slice).expect("non utf8 str from miri"); self.push("e"); @@ -669,7 +669,7 @@ impl Printer<'tcx> for &mut SymbolMangler<'tcx> { Ok(this) }; - match *ct.ty.kind() { + match *ct.ty().kind() { ty::Array(..) => { self.push("A"); self = print_field_list(self)?; @@ -720,7 +720,7 @@ impl Printer<'tcx> for &mut SymbolMangler<'tcx> { } _ => { - bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct.ty, ct); + bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct.ty(), ct); } } diff --git a/compiler/rustc_trait_selection/src/opaque_types.rs b/compiler/rustc_trait_selection/src/opaque_types.rs index 75d57d78e3b02..b9bb7936afe51 100644 --- a/compiler/rustc_trait_selection/src/opaque_types.rs +++ b/compiler/rustc_trait_selection/src/opaque_types.rs @@ -290,7 +290,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> { fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { trace!("checking const {:?}", ct); // Find a const parameter - match ct.val { + match ct.val() { ty::ConstKind::Param(..) => { // Look it up in the substitution list. match self.map.get(&ct.into()).map(|k| k.unpack()) { @@ -311,7 +311,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> { ) .emit(); - self.tcx().const_error(ct.ty) + self.tcx().const_error(ct.ty()) } } } diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 54f7b91080dd9..39e64c4195bc3 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -806,13 +806,13 @@ impl AutoTraitFinder<'tcx> { } ty::PredicateKind::ConstEquate(c1, c2) => { let evaluate = |c: &'tcx ty::Const<'tcx>| { - if let ty::ConstKind::Unevaluated(unevaluated) = c.val { + if let ty::ConstKind::Unevaluated(unevaluated) = c.val() { match select.infcx().const_eval_resolve( obligation.param_env, - unevaluated, + *unevaluated, Some(obligation.cause.span), ) { - Ok(val) => Ok(ty::Const::from_value(select.tcx(), val, c.ty)), + Ok(val) => Ok(ty::Const::from_value(select.tcx(), val, c.ty())), Err(err) => Err(err), } } else { diff --git a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs index 6b5d37c0f4308..9078c0ddf3eb6 100644 --- a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs +++ b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs @@ -203,7 +203,7 @@ impl<'tcx> AbstractConst<'tcx> { tcx: TyCtxt<'tcx>, ct: &ty::Const<'tcx>, ) -> Result>, ErrorReported> { - match ct.val { + match ct.val() { ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv.shrink()), ty::ConstKind::Error(_) => Err(ErrorReported), _ => Ok(None), @@ -335,10 +335,12 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { self.recurse_build(self.body_id)?; for n in self.nodes.iter() { - if let Node::Leaf(ty::Const { val: ty::ConstKind::Unevaluated(ct), ty: _ }) = n { - // `AbstractConst`s should not contain any promoteds as they require references which - // are not allowed. - assert_eq!(ct.promoted, None); + if let Node::Leaf(c) = n { + if let ty::ConstKind::Unevaluated(ct) = c.val() { + // `AbstractConst`s should not contain any promoteds as they require references which + // are not allowed. + assert_eq!(ct.promoted, None); + } } } @@ -591,11 +593,11 @@ pub(super) fn try_unify<'tcx>( match (a.root(tcx), b.root(tcx)) { (Node::Leaf(a_ct), Node::Leaf(b_ct)) => { - if a_ct.ty != b_ct.ty { + if a_ct.ty() != b_ct.ty() { return false; } - match (a_ct.val, b_ct.val) { + match (a_ct.val(), b_ct.val()) { // We can just unify errors with everything to reduce the amount of // emitted errors here. (ty::ConstKind::Error(_), _) | (_, ty::ConstKind::Error(_)) => true, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs index 6128c119b6b76..1f8d662c60cbf 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs @@ -211,7 +211,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let type_string = self.tcx.type_of(def.did).to_string(); flags.push((sym::_Self, Some(format!("[{}]", type_string)))); - let len = len.val.try_to_value().and_then(|v| v.try_to_machine_usize(self.tcx)); + let len = + len.val().try_to_value().and_then(|v| v.try_to_machine_usize(self.tcx)); let string = match len { Some(n) => format!("[{}; {}]", type_string, n), None => format!("[{}; _]", type_string), diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index e121837c987ae..bddd49cfc84ed 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -584,7 +584,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> { // // Let's just see where this breaks :shrug: if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) = - (c1.val, c2.val) + (c1.val(), c2.val()) { if infcx.try_unify_abstract_consts(a.shrink(), b.shrink()) { return ProcessResult::Changed(vec![]); @@ -595,13 +595,13 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> { let stalled_on = &mut pending_obligation.stalled_on; let mut evaluate = |c: &'tcx Const<'tcx>| { - if let ty::ConstKind::Unevaluated(unevaluated) = c.val { + if let ty::ConstKind::Unevaluated(unevaluated) = c.val() { match self.selcx.infcx().const_eval_resolve( obligation.param_env, - unevaluated, + *unevaluated, Some(obligation.cause.span), ) { - Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)), + Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty())), Err(ErrorHandled::TooGeneric) => { stalled_on.extend( unevaluated diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index b8c66931cbe52..44fc84bfb2857 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -622,23 +622,22 @@ impl TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - match *ct { - ty::Const { val: ty::ConstKind::Bound(debruijn, _), ty: _ } + match ct.val() { + ty::ConstKind::Bound(debruijn, _) if debruijn.as_usize() + 1 > self.current_index.as_usize() + self.universe_indices.len() => { bug!("Bound vars outside of `self.universe_indices`"); } - ty::Const { val: ty::ConstKind::Bound(debruijn, bound_const), ty } - if debruijn >= self.current_index => - { - let universe = self.universe_for(debruijn); + ty::ConstKind::Bound(debruijn, bound_const) if *debruijn >= self.current_index => { + let ty = ct.ty(); + let universe = self.universe_for(*debruijn); let p = ty::PlaceholderConst { universe, - name: ty::BoundConst { var: bound_const, ty }, + name: ty::BoundConst { var: *bound_const, ty }, }; - self.mapped_consts.insert(p, bound_const); - self.infcx.tcx.mk_const(ty::Const { val: ty::ConstKind::Placeholder(p), ty }) + self.mapped_consts.insert(p, *bound_const); + self.infcx.tcx.mk_const(ty, ty::ConstKind::Placeholder(p)) } _ if ct.has_vars_bound_at_or_above(self.current_index) => ct.super_fold_with(self), _ => ct, @@ -758,7 +757,8 @@ impl TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> { } fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { - if let ty::Const { val: ty::ConstKind::Placeholder(p), ty } = *ct { + if let ty::ConstKind::Placeholder(p) = ct.val() { + let ty = ct.ty(); let replace_var = self.mapped_consts.get(&p); match replace_var { Some(replace_var) => { @@ -770,8 +770,7 @@ impl TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> { let db = ty::DebruijnIndex::from_usize( self.universe_indices.len() - index + self.current_index.as_usize() - 1, ); - self.tcx() - .mk_const(ty::Const { val: ty::ConstKind::Bound(db, *replace_var), ty }) + self.tcx().mk_const(ty, ty::ConstKind::Bound(db, *replace_var)) } None => ct, } diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index 1364cf1c99535..299d7fa001bb4 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -154,8 +154,8 @@ impl<'tcx> TypeVisitor<'tcx> for MaxEscapingBoundVarVisitor<'tcx> { } fn visit_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> ControlFlow { - match ct.val { - ty::ConstKind::Bound(debruijn, _) if debruijn >= self.outer_index => { + match ct.val() { + ty::ConstKind::Bound(debruijn, _) if *debruijn >= self.outer_index => { self.escaping = self.escaping.max(debruijn.as_usize() - self.outer_index.as_usize()); ControlFlow::CONTINUE diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 2f1f7971a7926..c458a7cbdff0d 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -946,7 +946,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // Lifetimes aren't allowed to change during unsizing. GenericArgKind::Lifetime(_) => None, - GenericArgKind::Const(ct) => match ct.val { + GenericArgKind::Const(ct) => match ct.val() { ty::ConstKind::Param(p) => Some(p.index), _ => None, }, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 2aa214694cb14..7b7afa73987a1 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -628,7 +628,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // // Let's just see where this breaks :shrug: if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) = - (c1.val, c2.val) + (c1.val(), c2.val()) { if self.infcx.try_unify_abstract_consts(a.shrink(), b.shrink()) { return Ok(EvaluatedToOk); @@ -637,14 +637,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } let evaluate = |c: &'tcx ty::Const<'tcx>| { - if let ty::ConstKind::Unevaluated(unevaluated) = c.val { + if let ty::ConstKind::Unevaluated(unevaluated) = c.val() { self.infcx .const_eval_resolve( obligation.param_env, - unevaluated, + *unevaluated, Some(obligation.cause.span), ) - .map(|val| ty::Const::from_value(self.tcx(), val, c.ty)) + .map(|val| ty::Const::from_value(self.tcx(), val, c.ty())) } else { Ok(c) } diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index cb47ba9c360da..50d90b2f07193 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -42,15 +42,15 @@ pub fn obligations<'a, 'tcx>( .into() } GenericArgKind::Const(ct) => { - match ct.val { + match ct.val() { ty::ConstKind::Infer(infer) => { - let resolved = infcx.shallow_resolve(infer); - if resolved == infer { + let resolved = infcx.shallow_resolve(*infer); + if resolved == *infer { // No progress. return None; } - infcx.tcx.mk_const(ty::Const { val: ty::ConstKind::Infer(resolved), ty: ct.ty }) + infcx.tcx.mk_const(ct.ty(), ty::ConstKind::Infer(resolved)) } _ => ct, } @@ -435,7 +435,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { GenericArgKind::Lifetime(_) => continue, GenericArgKind::Const(constant) => { - match constant.val { + match constant.val() { ty::ConstKind::Unevaluated(uv) => { assert!(uv.promoted.is_none()); let substs = uv.substs(self.tcx()); @@ -456,15 +456,15 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { )); } ty::ConstKind::Infer(infer) => { - let resolved = self.infcx.shallow_resolve(infer); + let resolved = self.infcx.shallow_resolve(*infer); // the `InferConst` changed, meaning that we made progress. - if resolved != infer { + if resolved != *infer { let cause = self.cause(traits::MiscObligation); - let resolved_constant = self.infcx.tcx.mk_const(ty::Const { - val: ty::ConstKind::Infer(resolved), - ..*constant - }); + let resolved_constant = self + .infcx + .tcx + .mk_const(constant.ty(), ty::ConstKind::Infer(resolved)); self.out.push(traits::Obligation::with_depth( cause, self.recursion_depth, diff --git a/compiler/rustc_traits/src/chalk/db.rs b/compiler/rustc_traits/src/chalk/db.rs index 1d457d6761fd0..db03dc255c2e1 100644 --- a/compiler/rustc_traits/src/chalk/db.rs +++ b/compiler/rustc_traits/src/chalk/db.rs @@ -743,10 +743,10 @@ fn bound_vars_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx> { } ty::GenericParamDefKind::Const { .. } => tcx - .mk_const(ty::Const { - val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)), - ty: tcx.type_of(param.def_id), - }) + .mk_const( + tcx.type_of(param.def_id), + ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)), + ) .into(), }) } @@ -763,7 +763,7 @@ fn binders_for<'tcx>( chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General) } ty::subst::GenericArgKind::Const(c) => { - chalk_ir::VariableKind::Const(c.ty.lower_into(interner)) + chalk_ir::VariableKind::Const(c.ty().lower_into(interner)) } }), ) diff --git a/compiler/rustc_traits/src/chalk/lowering.rs b/compiler/rustc_traits/src/chalk/lowering.rs index e24f699adf6b3..9e2cdf58c6636 100644 --- a/compiler/rustc_traits/src/chalk/lowering.rs +++ b/compiler/rustc_traits/src/chalk/lowering.rs @@ -369,8 +369,8 @@ impl<'tcx> LowerInto<'tcx, Ty<'tcx>> for &chalk_ir::Ty> { }, TyKind::Array(ty, c) => { let ty = ty.lower_into(interner); - let c = c.lower_into(interner); - ty::Array(ty, interner.tcx.mk_const(c)) + let (c_ty, c_val) = c.lower_into(interner); + ty::Array(ty, interner.tcx.mk_const(c_ty, c_val)) } TyKind::FnDef(id, substitution) => ty::FnDef(id.0, substitution.lower_into(interner)), TyKind::Closure(closure, substitution) => { @@ -488,10 +488,10 @@ impl<'tcx> LowerInto<'tcx, Region<'tcx>> for &chalk_ir::Lifetime LowerInto<'tcx, chalk_ir::Const>> for ty::Const<'tcx> { fn lower_into(self, interner: &RustInterner<'tcx>) -> chalk_ir::Const> { - let ty = self.ty.lower_into(interner); - let value = match self.val { + let ty = self.ty().lower_into(interner); + let value = match self.val() { ty::ConstKind::Value(val) => { - chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: val }) + chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: *val }) } ty::ConstKind::Bound(db, bound) => chalk_ir::ConstValue::BoundVar( chalk_ir::BoundVar::new(chalk_ir::DebruijnIndex::new(db.as_u32()), bound.index()), @@ -502,8 +502,10 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Const>> for ty::Const<'t } } -impl<'tcx> LowerInto<'tcx, ty::Const<'tcx>> for &chalk_ir::Const> { - fn lower_into(self, interner: &RustInterner<'tcx>) -> ty::Const<'tcx> { +impl<'tcx> LowerInto<'tcx, (Ty<'tcx>, ty::ConstKind<'tcx>)> + for &chalk_ir::Const> +{ + fn lower_into(self, interner: &RustInterner<'tcx>) -> (Ty<'tcx>, ty::ConstKind<'tcx>) { let data = self.data(interner); let ty = data.ty.lower_into(interner); let val = match data.value { @@ -515,7 +517,7 @@ impl<'tcx> LowerInto<'tcx, ty::Const<'tcx>> for &chalk_ir::Const unimplemented!(), chalk_ir::ConstValue::Concrete(c) => ty::ConstKind::Value(c.interned), }; - ty::Const { ty, val } + (ty, val) } } @@ -550,8 +552,8 @@ impl<'tcx> LowerInto<'tcx, ty::subst::GenericArg<'tcx>> r.into() } chalk_ir::GenericArgData::Const(c) => { - let c: ty::Const<'tcx> = c.lower_into(interner); - interner.tcx.mk_const(c).into() + let (ty, val) = c.lower_into(interner); + interner.tcx.mk_const(ty, val).into() } } } diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index f0a77cb39a622..d7c7cfeddcdd4 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -473,7 +473,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes( } fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow { - if let ty::ConstKind::Unevaluated(..) = c.val { + if let ty::ConstKind::Unevaluated(..) = c.val() { // FIXME(#72219) We currently don't detect lifetimes within substs // which would violate this check. Even though the particular substitution is not used // within the const, this should still be fixed. diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index cbfd8747ecf00..379757193481a 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -1316,13 +1316,10 @@ pub fn check_type_bounds<'tcx>( GenericParamDefKind::Const { .. } => { let bound_var = ty::BoundVariableKind::Const; bound_vars.push(bound_var); - tcx.mk_const(ty::Const { - ty: tcx.type_of(param.def_id), - val: ty::ConstKind::Bound( - ty::INNERMOST, - ty::BoundVar::from_usize(bound_vars.len() - 1), - ), - }) + tcx.mk_const( + tcx.type_of(param.def_id), + ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(bound_vars.len() - 1)), + ) .into() } }); diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index c1adc2894ccfc..642f76791bfa2 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -1249,7 +1249,7 @@ fn check_where_clauses<'tcx, 'fcx>( } fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow { - if let ty::ConstKind::Param(param) = c.val { + if let ty::ConstKind::Param(param) = c.val() { self.params.insert(param.index); } c.super_visit_with(self) diff --git a/compiler/rustc_typeck/src/check/writeback.rs b/compiler/rustc_typeck/src/check/writeback.rs index fdc8b6b5e6451..cd581422b9fdb 100644 --- a/compiler/rustc_typeck/src/check/writeback.rs +++ b/compiler/rustc_typeck/src/check/writeback.rs @@ -796,7 +796,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> { debug!("Resolver::fold_const: input const `{:?}` not fully resolvable", ct); self.report_const_error(ct); self.replaced_with_error = true; - self.tcx().const_error(ct.ty) + self.tcx().const_error(ct.ty()) } } } diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 2274db76c05fb..a613044e1ce95 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -2313,7 +2313,7 @@ fn const_evaluatable_predicates_of<'tcx>( fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) { let def_id = self.tcx.hir().local_def_id(c.hir_id); let ct = ty::Const::from_anon_const(self.tcx, def_id); - if let ty::ConstKind::Unevaluated(uv) = ct.val { + if let ty::ConstKind::Unevaluated(uv) = ct.val() { assert_eq!(uv.promoted, None); let span = self.tcx.hir().span(c.hir_id); self.preds.insert(( diff --git a/compiler/rustc_typeck/src/constrained_generic_params.rs b/compiler/rustc_typeck/src/constrained_generic_params.rs index 88877ad78525a..180761254c5de 100644 --- a/compiler/rustc_typeck/src/constrained_generic_params.rs +++ b/compiler/rustc_typeck/src/constrained_generic_params.rs @@ -87,13 +87,13 @@ impl<'tcx> TypeVisitor<'tcx> for ParameterCollector<'tcx> { } fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow { - match c.val { + match c.val() { ty::ConstKind::Unevaluated(..) if !self.include_nonconstraining => { // Constant expressions are not injective - return c.ty.visit_with(self); + return c.ty().visit_with(self); } ty::ConstKind::Param(data) => { - self.parameters.push(Parameter::from(data)); + self.parameters.push(Parameter::from(*data)); } _ => {} } diff --git a/compiler/rustc_typeck/src/variance/constraints.rs b/compiler/rustc_typeck/src/variance/constraints.rs index 33c27ce86ddb5..71355e83e8ff8 100644 --- a/compiler/rustc_typeck/src/variance/constraints.rs +++ b/compiler/rustc_typeck/src/variance/constraints.rs @@ -403,7 +403,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { ) { debug!("add_constraints_from_const(val={:?}, variance={:?})", val, variance); - match &val.val { + match val.val() { ty::ConstKind::Unevaluated(uv) => { let substs = uv.substs(self.tcx()); self.add_constraints_from_invariant_substs(current, substs, variance); diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 7040106568983..bd7aa3c57ee23 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1552,7 +1552,7 @@ impl<'tcx> Clean for ty::Const<'tcx> { fn clean(&self, cx: &mut DocContext<'_>) -> Constant { // FIXME: instead of storing the stringified expression, store `self` directly instead. Constant { - type_: self.ty.clean(cx), + type_: self.ty().clean(cx), kind: ConstantKind::TyConst { expr: self.to_string() }, } } diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 2fa7efcc6509b..18d350213630c 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -238,7 +238,7 @@ crate fn name_from_pat(p: &hir::Pat<'_>) -> Symbol { } crate fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String { - match n.val { + match n.val() { ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs_: _, promoted }) => { let mut s = if let Some(def) = def.as_local() { let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def.did); @@ -311,12 +311,12 @@ fn format_integer_with_underscore_sep(num: &str) -> String { fn print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: &'tcx ty::Const<'tcx>) -> String { // Use a slightly different format for integer types which always shows the actual value. // For all other types, fallback to the original `pretty_print_const`. - match (ct.val, ct.ty.kind()) { + match (ct.val(), ct.ty().kind()) { (ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Uint(ui)) => { format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str()) } (ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Int(i)) => { - let ty = tcx.lift(ct.ty).unwrap(); + let ty = tcx.lift(ct.ty()).unwrap(); let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size; let data = int.assert_bits(size); let sign_extended_data = size.sign_extend(data) as i128; diff --git a/src/tools/clippy/clippy_lints/src/large_const_arrays.rs b/src/tools/clippy/clippy_lints/src/large_const_arrays.rs index fe6814e35d0ca..21816b4609313 100644 --- a/src/tools/clippy/clippy_lints/src/large_const_arrays.rs +++ b/src/tools/clippy/clippy_lints/src/large_const_arrays.rs @@ -52,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays { if let ItemKind::Const(hir_ty, _) = &item.kind; let ty = hir_ty_to_ty(cx.tcx, hir_ty); if let ty::Array(element_type, cst) = ty.kind(); - if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.val; + if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.val(); if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx); if let Ok(element_size) = cx.layout_of(element_type).map(|l| l.size.bytes()); if self.maximum_allowed_size < element_count * element_size; diff --git a/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs b/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs index bbb6c1f902ce0..0545d024d6395 100644 --- a/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs +++ b/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs @@ -42,7 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays { if_chain! { if let ExprKind::Repeat(_, _) = expr.kind; if let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind(); - if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.val; + if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.val(); if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx); if let Ok(element_size) = cx.layout_of(element_type).map(|l| l.size.bytes()); if self.maximum_allowed_size < element_count * element_size; diff --git a/src/tools/clippy/clippy_lints/src/non_copy_const.rs b/src/tools/clippy/clippy_lints/src/non_copy_const.rs index 2a85a67fa099c..58176af232ecb 100644 --- a/src/tools/clippy/clippy_lints/src/non_copy_const.rs +++ b/src/tools/clippy/clippy_lints/src/non_copy_const.rs @@ -136,7 +136,7 @@ fn is_value_unfrozen_raw<'tcx>( ty: Ty<'tcx>, ) -> bool { fn inner<'tcx>(cx: &LateContext<'tcx>, val: &'tcx Const<'tcx>) -> bool { - match val.ty.kind() { + match val.ty().kind() { // the fact that we have to dig into every structs to search enums // leads us to the point checking `UnsafeCell` directly is the only option. ty::Adt(ty_def, ..) if Some(ty_def.did) == cx.tcx.lang_items().unsafe_cell_type() => true, diff --git a/src/tools/clippy/clippy_utils/src/consts.rs b/src/tools/clippy/clippy_utils/src/consts.rs index 04347672e0fbb..24af8f4648d61 100644 --- a/src/tools/clippy/clippy_utils/src/consts.rs +++ b/src/tools/clippy/clippy_utils/src/consts.rs @@ -562,9 +562,10 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> { pub fn miri_to_const(result: &ty::Const<'_>) -> Option { use rustc_middle::mir::interpret::ConstValue; - match result.val { + match result.val() { ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int(int))) => { - match result.ty.kind() { + let int = *int; + match result.ty().kind() { ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)), ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.assert_bits(int.size()))), ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits( @@ -583,10 +584,10 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option { _ => None, } }, - ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => match result.ty.kind() { + ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => match result.ty().kind() { ty::Ref(_, tam, _) => match tam.kind() { ty::Str => String::from_utf8( - data.inspect_with_uninit_and_ptr_outside_interpreter(start..end) + data.inspect_with_uninit_and_ptr_outside_interpreter(*start..*end) .to_owned(), ) .ok() @@ -595,7 +596,7 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option { }, _ => None, }, - ty::ConstKind::Value(ConstValue::ByRef { alloc, offset: _ }) => match result.ty.kind() { + ty::ConstKind::Value(ConstValue::ByRef { alloc, offset: _ }) => match result.ty().kind() { ty::Array(sub_type, len) => match sub_type.kind() { ty::Float(FloatTy::F32) => match miri_to_const(len) { Some(Constant::Int(len)) => alloc