diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index f03ceb54830c8..ba8222dc15218 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -173,18 +173,17 @@ pub(super) fn op_to_const<'tcx>( Immediate::ScalarPair(a, b) => { debug!("ScalarPair(a: {:?}, b: {:?})", a, b); // We know `offset` is relative to the allocation, so we can use `into_parts`. - let (data, start) = - match ecx.scalar_to_ptr(a.check_init().unwrap()).unwrap().into_parts() { - (Some(alloc_id), offset) => { - (ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes()) - } - (None, _offset) => ( - ecx.tcx.intern_const_alloc( - Allocation::from_bytes_byte_aligned_immutable(b"" as &[u8]), - ), - 0, - ), - }; + let (data, start) = match a.to_pointer(ecx).unwrap().into_parts() { + (Some(alloc_id), offset) => { + (ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes()) + } + (None, _offset) => ( + ecx.tcx.intern_const_alloc(Allocation::from_bytes_byte_aligned_immutable( + b"" as &[u8], + )), + 0, + ), + }; let len = b.to_machine_usize(ecx).unwrap(); let start = start.try_into().unwrap(); let len: usize = len.try_into().unwrap(); diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs index 883387851eaf0..c97c31eb9dadf 100644 --- a/compiler/rustc_const_eval/src/interpret/cast.rs +++ b/compiler/rustc_const_eval/src/interpret/cast.rs @@ -180,7 +180,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { assert!(cast_ty.is_integral()); let scalar = src.to_scalar()?; - let ptr = self.scalar_to_ptr(scalar)?; + let ptr = scalar.to_pointer(self)?; match ptr.into_pointer_or_addr() { Ok(ptr) => M::expose_ptr(self, ptr)?, Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP. @@ -299,7 +299,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } (&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => { let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?; - let old_vptr = self.scalar_to_ptr(old_vptr)?; + let old_vptr = old_vptr.to_pointer(self)?; let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?; if old_trait != data_a.principal() { throw_ub_format!("upcast on a pointer whose vtable does not match its type"); diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index fdf243c4108d3..150d6589b0807 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -629,7 +629,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Ok(Some((size, align))) } ty::Dynamic(..) => { - let vtable = self.scalar_to_ptr(metadata.unwrap_meta())?; + let vtable = metadata.unwrap_meta().to_pointer(self)?; // Read size and align from vtable (already checks size). Ok(Some(self.get_vtable_size_and_align(vtable)?)) } diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 23526edcc343a..376b8872c90ac 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -245,7 +245,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory if let ty::Dynamic(..) = tcx.struct_tail_erasing_lifetimes(referenced_ty, self.ecx.param_env).kind() { - let ptr = self.ecx.scalar_to_ptr(mplace.meta.unwrap_meta())?; + let ptr = mplace.meta.unwrap_meta().to_pointer(&tcx)?; if let Some(alloc_id) = ptr.provenance { // Explicitly choose const mode here, since vtables are immutable, even // if the reference of the fat pointer is mutable. diff --git a/compiler/rustc_const_eval/src/interpret/machine.rs b/compiler/rustc_const_eval/src/interpret/machine.rs index a938a9248e0c4..71ccd1799fa95 100644 --- a/compiler/rustc_const_eval/src/interpret/machine.rs +++ b/compiler/rustc_const_eval/src/interpret/machine.rs @@ -135,9 +135,6 @@ pub trait Machine<'mir, 'tcx>: Sized { /// Whether to enforce integers and floats being initialized. fn enforce_number_init(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool; - /// Whether to enforce integers and floats not having provenance. - fn enforce_number_no_provenance(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool; - /// Whether function calls should be [ABI](CallAbi)-checked. fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool { true @@ -300,13 +297,6 @@ pub trait Machine<'mir, 'tcx>: Sized { addr: u64, ) -> InterpResult<'tcx, Pointer>>; - /// Hook for returning a pointer from a transmute-like operation on an addr. - /// This is only needed to support Miri's (unsound) "allow-ptr-int-transmute" flag. - fn ptr_from_addr_transmute( - ecx: &InterpCx<'mir, 'tcx, Self>, - addr: u64, - ) -> Pointer>; - /// Marks a pointer as exposed, allowing it's provenance /// to be recovered. "Pointer-to-int cast" fn expose_ptr( @@ -469,11 +459,6 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) { true } - #[inline(always)] - fn enforce_number_no_provenance(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool { - true - } - #[inline(always)] fn checked_binop_checks_overflow(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool { true @@ -518,14 +503,6 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) { ptr } - #[inline(always)] - fn ptr_from_addr_transmute( - _ecx: &InterpCx<$mir, $tcx, Self>, - addr: u64, - ) -> Pointer> { - Pointer::from_addr(addr) - } - #[inline(always)] fn ptr_from_addr_cast( _ecx: &InterpCx<$mir, $tcx, Self>, diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 86914f50383e8..ed2c4edf9dd72 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -9,7 +9,6 @@ use std::assert_matches::assert_matches; use std::borrow::Cow; use std::collections::VecDeque; -use std::convert::TryFrom; use std::fmt; use std::ptr; @@ -1172,26 +1171,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { /// Machine pointer introspection. impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { - pub fn scalar_to_ptr( - &self, - scalar: Scalar, - ) -> InterpResult<'tcx, Pointer>> { - // We use `to_bits_or_ptr_internal` since we are just implementing the method people need to - // call to force getting out a pointer. - Ok( - match scalar - .to_bits_or_ptr_internal(self.pointer_size()) - .map_err(|s| err_ub!(ScalarSizeMismatch(s)))? - { - Err(ptr) => ptr.into(), - Ok(bits) => { - let addr = u64::try_from(bits).unwrap(); - M::ptr_from_addr_transmute(&self, addr) - } - }, - ) - } - /// Test if this value might be null. /// If the machine does not support ptr-to-int casts, this is conservative. pub fn scalar_may_be_null(&self, scalar: Scalar) -> InterpResult<'tcx, bool> { @@ -1199,7 +1178,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Ok(int) => int.is_null(), Err(_) => { // Can only happen during CTFE. - let ptr = self.scalar_to_ptr(scalar)?; + let ptr = scalar.to_pointer(self)?; match self.ptr_try_get_alloc_id(ptr) { Ok((alloc_id, offset, _)) => { let (size, _align, _kind) = self.get_alloc_info(alloc_id); diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 7e5c6feb048d2..de284bd3bae37 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -14,7 +14,7 @@ use rustc_target::abi::{VariantIdx, Variants}; use super::{ alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer, - PointerArithmetic, Provenance, Scalar, ScalarMaybeUninit, + Provenance, Scalar, ScalarMaybeUninit, }; /// An `Immediate` represents a single immediate self-contained Rust value. @@ -363,17 +363,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Abi::Scalar(s) if force => Some(s.primitive()), _ => None, }; - let read_provenance = |s: abi::Primitive, size| { - // Should be just `s.is_ptr()`, but we support a Miri flag that accepts more - // questionable ptr-int transmutes. - let number_may_have_provenance = !M::enforce_number_no_provenance(self); - s.is_ptr() || (number_may_have_provenance && size == self.pointer_size()) - }; if let Some(s) = scalar_layout { let size = s.size(self); assert_eq!(size, mplace.layout.size, "abi::Scalar size does not match layout size"); - let scalar = - alloc.read_scalar(alloc_range(Size::ZERO, size), read_provenance(s, size))?; + let scalar = alloc + .read_scalar(alloc_range(Size::ZERO, size), /*read_provenance*/ s.is_ptr())?; return Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout })); } let scalar_pair_layout = match mplace.layout.abi { @@ -391,10 +385,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let (a_size, b_size) = (a.size(self), b.size(self)); let b_offset = a_size.align_to(b.align(self).abi); assert!(b_offset.bytes() > 0); // in `operand_field` we use the offset to tell apart the fields - let a_val = - alloc.read_scalar(alloc_range(Size::ZERO, a_size), read_provenance(a, a_size))?; - let b_val = - alloc.read_scalar(alloc_range(b_offset, b_size), read_provenance(b, b_size))?; + let a_val = alloc.read_scalar( + alloc_range(Size::ZERO, a_size), + /*read_provenance*/ a.is_ptr(), + )?; + let b_val = alloc + .read_scalar(alloc_range(b_offset, b_size), /*read_provenance*/ b.is_ptr())?; return Ok(Some(ImmTy { imm: Immediate::ScalarPair(a_val, b_val), layout: mplace.layout, @@ -459,7 +455,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { &self, op: &OpTy<'tcx, M::Provenance>, ) -> InterpResult<'tcx, Pointer>> { - self.scalar_to_ptr(self.read_scalar(op)?.check_init()?) + self.read_scalar(op)?.to_pointer(self) } /// Turn the wide MPlace into a string (must already be dereferenced!) diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index c7d8a744f7c09..473da71a0ab9c 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -331,7 +331,7 @@ where Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)), }; - let mplace = MemPlace { ptr: self.scalar_to_ptr(ptr.check_init()?)?, meta }; + let mplace = MemPlace { ptr: ptr.to_pointer(self)?, meta }; // When deref'ing a pointer, the *static* alignment given by the type is what matters. let align = layout.align.abi; Ok(MPlaceTy { mplace, layout, align }) @@ -889,7 +889,7 @@ where &self, mplace: &MPlaceTy<'tcx, M::Provenance>, ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> { - let vtable = self.scalar_to_ptr(mplace.vtable())?; // also sanity checks the type + let vtable = mplace.vtable().to_pointer(self)?; // also sanity checks the type let (ty, _) = self.get_ptr_vtable(vtable)?; let layout = self.layout_of(ty)?; diff --git a/compiler/rustc_const_eval/src/interpret/terminator.rs b/compiler/rustc_const_eval/src/interpret/terminator.rs index 6fef7b3b3cf7c..42de0dbdca928 100644 --- a/compiler/rustc_const_eval/src/interpret/terminator.rs +++ b/compiler/rustc_const_eval/src/interpret/terminator.rs @@ -561,7 +561,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }; // Get the required information from the vtable. - let vptr = self.scalar_to_ptr(receiver_place.meta.unwrap_meta())?; + let vptr = receiver_place.meta.unwrap_meta().to_pointer(self)?; let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?; if dyn_trait != data.principal() { throw_ub_format!( diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index f2e104da04a41..d20f16755c391 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -312,7 +312,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env); match tail.kind() { ty::Dynamic(..) => { - let vtable = self.ecx.scalar_to_ptr(meta.unwrap_meta())?; + let vtable = meta.unwrap_meta().to_pointer(self.ecx)?; // Make sure it is a genuine vtable pointer. let (_ty, _trait) = try_validation!( self.ecx.get_ptr_vtable(vtable), @@ -517,15 +517,13 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' { "{:x}", value } expected { "initialized bytes" } ); } - if M::enforce_number_no_provenance(self.ecx) { - // As a special exception we *do* match on a `Scalar` here, since we truly want - // to know its underlying representation (and *not* cast it to an integer). - let is_ptr = value.check_init().map_or(false, |v| matches!(v, Scalar::Ptr(..))); - if is_ptr { - throw_validation_failure!(self.path, - { "{:x}", value } expected { "plain (non-pointer) bytes" } - ) - } + // As a special exception we *do* match on a `Scalar` here, since we truly want + // to know its underlying representation (and *not* cast it to an integer). + let is_ptr = value.check_init().map_or(false, |v| matches!(v, Scalar::Ptr(..))); + if is_ptr { + throw_validation_failure!(self.path, + { "{:x}", value } expected { "plain (non-pointer) bytes" } + ) } Ok(true) } @@ -568,7 +566,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // If we check references recursively, also check that this points to a function. if let Some(_) = self.ref_tracking { - let ptr = self.ecx.scalar_to_ptr(value)?; + let ptr = value.to_pointer(self.ecx)?; let _fn = try_validation!( self.ecx.get_ptr_fn(ptr), self.path, @@ -906,7 +904,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> match alloc.check_bytes( alloc_range(Size::ZERO, size), /*allow_uninit*/ !M::enforce_number_init(self.ecx), - /*allow_ptr*/ !M::enforce_number_no_provenance(self.ecx), + /*allow_ptr*/ false, ) { // In the happy case, we needn't check anything else. Ok(()) => {} diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index 17088cf13a543..834c114ee1c58 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -331,6 +331,19 @@ impl Scalar { } impl<'tcx, Prov: Provenance> Scalar { + pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer>> { + match self + .to_bits_or_ptr_internal(cx.pointer_size()) + .map_err(|s| err_ub!(ScalarSizeMismatch(s)))? + { + Err(ptr) => Ok(ptr.into()), + Ok(bits) => { + let addr = u64::try_from(bits).unwrap(); + Ok(Pointer::from_addr(addr)) + } + } + } + /// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you /// likely want to use instead. /// @@ -546,6 +559,11 @@ impl ScalarMaybeUninit { } impl<'tcx, Prov: Provenance> ScalarMaybeUninit { + #[inline(always)] + pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer>> { + self.check_init()?.to_pointer(cx) + } + #[inline(always)] pub fn to_bool(self) -> InterpResult<'tcx, bool> { self.check_init()?.to_bool() diff --git a/compiler/rustc_parse/src/lib.rs b/compiler/rustc_parse/src/lib.rs index d56e3773dc7a0..261adbfc34671 100644 --- a/compiler/rustc_parse/src/lib.rs +++ b/compiler/rustc_parse/src/lib.rs @@ -117,12 +117,12 @@ pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, sp: Option) -> Parser<'_> { panictry_buffer!(&sess.span_diagnostic, maybe_source_file_to_parser(sess, source_file)) } -/// Given a `source_file` and config, return a parser. Returns any buffered errors from lexing the +/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing the /// initial token stream. fn maybe_source_file_to_parser( sess: &ParseSess, diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index e3b62894d0f22..4d3736f79146c 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -220,7 +220,7 @@ fn handle_ebadf(r: io::Result, default: T) -> io::Result { /// /// fn main() -> io::Result<()> { /// let mut buffer = String::new(); -/// let mut stdin = io::stdin(); // We get `Stdin` here. +/// let stdin = io::stdin(); // We get `Stdin` here. /// stdin.read_line(&mut buffer)?; /// Ok(()) /// } diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 7a4ec889ac757..4b91f7ba096cb 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -16,8 +16,8 @@ use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; use crate::clean::{ - self, clean_fn_decl_from_did_and_sig, clean_middle_ty, clean_ty, clean_ty_generics, utils, - Attributes, AttributesExt, Clean, ImplKind, ItemId, Type, Visibility, + self, clean_fn_decl_from_did_and_sig, clean_middle_field, clean_middle_ty, clean_ty, + clean_ty_generics, utils, Attributes, AttributesExt, Clean, ImplKind, ItemId, Type, Visibility, }; use crate::core::DocContext; use crate::formats::item_type::ItemType; @@ -246,7 +246,7 @@ fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct { clean::Struct { struct_type: variant.ctor_kind, generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates), - fields: variant.fields.iter().map(|x| x.clean(cx)).collect(), + fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(), } } @@ -255,7 +255,7 @@ fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union { let variant = cx.tcx.adt_def(did).non_enum_variant(); let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates); - let fields = variant.fields.iter().map(|x| x.clean(cx)).collect(); + let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(); clean::Union { generics, fields } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 6160783f652f1..c9ef4748a4845 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -237,13 +237,22 @@ impl<'tcx> Clean<'tcx, Lifetime> for hir::Lifetime { } } -impl<'tcx> Clean<'tcx, Constant> for hir::ConstArg { - fn clean(&self, cx: &mut DocContext<'tcx>) -> Constant { - let def_id = cx.tcx.hir().body_owner_def_id(self.value.body).to_def_id(); - Constant { - type_: clean_middle_ty(cx.tcx.type_of(def_id), cx, Some(def_id)), - kind: ConstantKind::Anonymous { body: self.value.body }, - } +pub(crate) fn clean_const<'tcx>(constant: &hir::ConstArg, cx: &mut DocContext<'tcx>) -> Constant { + let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id(); + Constant { + type_: clean_middle_ty(cx.tcx.type_of(def_id), cx, Some(def_id)), + kind: ConstantKind::Anonymous { body: constant.value.body }, + } +} + +pub(crate) fn clean_middle_const<'tcx>( + constant: ty::Const<'tcx>, + cx: &mut DocContext<'tcx>, +) -> Constant { + // FIXME: instead of storing the stringified expression, store `self` directly instead. + Constant { + type_: clean_middle_ty(constant.ty(), cx, None), + kind: ConstantKind::TyConst { expr: constant.to_string() }, } } @@ -392,7 +401,7 @@ impl<'tcx> Clean<'tcx, Term> for ty::Term<'tcx> { fn clean(&self, cx: &mut DocContext<'tcx>) -> Term { match self { ty::Term::Ty(ty) => Term::Type(clean_middle_ty(*ty, cx, None)), - ty::Term::Const(c) => Term::Constant(c.clean(cx)), + ty::Term::Const(c) => Term::Constant(clean_middle_const(*c, cx)), } } } @@ -403,7 +412,7 @@ impl<'tcx> Clean<'tcx, Term> for hir::Term<'tcx> { hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)), hir::Term::Const(c) => { let def_id = cx.tcx.hir().local_def_id(c.hir_id); - Term::Constant(ty::Const::from_anon_const(cx.tcx, def_id).clean(cx)) + Term::Constant(clean_middle_const(ty::Const::from_anon_const(cx.tcx, def_id), cx)) } } } @@ -1468,8 +1477,10 @@ fn maybe_expand_private_type_alias<'tcx>( _ => None, }); if let Some(ct) = const_ { - substs - .insert(const_param_def_id.to_def_id(), SubstParam::Constant(ct.clean(cx))); + substs.insert( + const_param_def_id.to_def_id(), + SubstParam::Constant(clean_const(ct, cx)), + ); } // FIXME(const_generics_defaults) indices.consts += 1; @@ -1764,35 +1775,26 @@ pub(crate) fn clean_middle_ty<'tcx>( } } -impl<'tcx> Clean<'tcx, Constant> for ty::Const<'tcx> { - fn clean(&self, cx: &mut DocContext<'tcx>) -> Constant { - // FIXME: instead of storing the stringified expression, store `self` directly instead. - Constant { - type_: clean_middle_ty(self.ty(), cx, None), - kind: ConstantKind::TyConst { expr: self.to_string() }, - } - } -} - -impl<'tcx> Clean<'tcx, Item> for hir::FieldDef<'tcx> { - fn clean(&self, cx: &mut DocContext<'tcx>) -> Item { - let def_id = cx.tcx.hir().local_def_id(self.hir_id).to_def_id(); - clean_field(def_id, self.ident.name, clean_ty(self.ty, cx), cx) - } +pub(crate) fn clean_field<'tcx>(field: &hir::FieldDef<'tcx>, cx: &mut DocContext<'tcx>) -> Item { + let def_id = cx.tcx.hir().local_def_id(field.hir_id).to_def_id(); + clean_field_with_def_id(def_id, field.ident.name, clean_ty(field.ty, cx), cx) } -impl<'tcx> Clean<'tcx, Item> for ty::FieldDef { - fn clean(&self, cx: &mut DocContext<'tcx>) -> Item { - clean_field( - self.did, - self.name, - clean_middle_ty(cx.tcx.type_of(self.did), cx, Some(self.did)), - cx, - ) - } +pub(crate) fn clean_middle_field<'tcx>(field: &ty::FieldDef, cx: &mut DocContext<'tcx>) -> Item { + clean_field_with_def_id( + field.did, + field.name, + clean_middle_ty(cx.tcx.type_of(field.did), cx, Some(field.did)), + cx, + ) } -fn clean_field(def_id: DefId, name: Symbol, ty: Type, cx: &mut DocContext<'_>) -> Item { +pub(crate) fn clean_field_with_def_id( + def_id: DefId, + name: Symbol, + ty: Type, + cx: &mut DocContext<'_>, +) -> Item { let what_rustc_thinks = Item::from_def_id_and_parts(def_id, Some(name), StructFieldItem(ty), cx); if is_field_vis_inherited(cx.tcx, def_id) { @@ -1830,14 +1832,14 @@ impl<'tcx> Clean<'tcx, VariantStruct> for rustc_hir::VariantData<'tcx> { fn clean(&self, cx: &mut DocContext<'tcx>) -> VariantStruct { VariantStruct { struct_type: CtorKind::from_hir(self), - fields: self.fields().iter().map(|x| x.clean(cx)).collect(), + fields: self.fields().iter().map(|x| clean_field(x, cx)).collect(), } } } impl<'tcx> Clean<'tcx, Vec> for hir::VariantData<'tcx> { fn clean(&self, cx: &mut DocContext<'tcx>) -> Vec { - self.fields().iter().map(|x| x.clean(cx)).collect() + self.fields().iter().map(|x| clean_field(x, cx)).collect() } } @@ -1845,12 +1847,12 @@ impl<'tcx> Clean<'tcx, Item> for ty::VariantDef { fn clean(&self, cx: &mut DocContext<'tcx>) -> Item { let kind = match self.ctor_kind { CtorKind::Const => Variant::CLike, - CtorKind::Fn => { - Variant::Tuple(self.fields.iter().map(|field| field.clean(cx)).collect()) - } + CtorKind::Fn => Variant::Tuple( + self.fields.iter().map(|field| clean_middle_field(field, cx)).collect(), + ), CtorKind::Fictive => Variant::Struct(VariantStruct { struct_type: CtorKind::Fictive, - fields: self.fields.iter().map(|field| field.clean(cx)).collect(), + fields: self.fields.iter().map(|field| clean_middle_field(field, cx)).collect(), }), }; let what_rustc_thinks = @@ -1894,7 +1896,7 @@ impl<'tcx> Clean<'tcx, GenericArgs> for hir::GenericArgs<'tcx> { } hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()), hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)), - hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(ct.clean(cx))), + hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(clean_const(ct, cx))), hir::GenericArg::Infer(_inf) => GenericArg::Infer, }) .collect::>() @@ -1970,12 +1972,12 @@ fn clean_maybe_renamed_item<'tcx>( }), ItemKind::Union(ref variant_data, generics) => UnionItem(Union { generics: generics.clean(cx), - fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(), + fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(), }), ItemKind::Struct(ref variant_data, generics) => StructItem(Struct { struct_type: CtorKind::from_hir(variant_data), generics: generics.clean(cx), - fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(), + fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(), }), ItemKind::Impl(impl_) => return clean_impl(impl_, item.hir_id(), cx), // proc macros can have a name set by attributes diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 00d62b3748455..27ecea5cc40bd 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -2,9 +2,9 @@ use crate::clean::auto_trait::AutoTraitFinder; use crate::clean::blanket_impl::BlanketImplFinder; use crate::clean::render_macro_matchers::render_macro_matcher; use crate::clean::{ - clean_middle_ty, inline, Clean, Crate, ExternalCrate, Generic, GenericArg, GenericArgs, - ImportSource, Item, ItemKind, Lifetime, Path, PathSegment, Primitive, PrimitiveType, Type, - TypeBinding, Visibility, + clean_middle_const, clean_middle_ty, inline, Clean, Crate, ExternalCrate, Generic, GenericArg, + GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment, Primitive, + PrimitiveType, Type, TypeBinding, Visibility, }; use crate::core::DocContext; use crate::formats::item_type::ItemType; @@ -93,7 +93,7 @@ pub(crate) fn substs_to_args<'tcx>( None } GenericArgKind::Type(ty) => Some(GenericArg::Type(clean_middle_ty(ty, cx, None))), - GenericArgKind::Const(ct) => Some(GenericArg::Const(Box::new(ct.clean(cx)))), + GenericArgKind::Const(ct) => Some(GenericArg::Const(Box::new(clean_middle_const(ct, cx)))), })); ret_val }