Skip to content

Commit ee6c0da

Browse files
committed
Auto merge of #72562 - RalfJung:rollup-2ngjgwi, r=RalfJung
Rollup of 5 pull requests Successful merges: - #71940 (Add `len` and `slice_from_raw_parts` to `NonNull<[T]>`) - #72525 (Miri casts: do not blindly rely on dest type) - #72537 (Fix InlineAsmOperand expresions being visited twice during liveness checking) - #72544 (librustc_middle: Rename upvars query to upvars_mentioned) - #72551 (First draft documenting Debug stability.) Failed merges: r? @ghost
2 parents 997d953 + 7c9fdb3 commit ee6c0da

File tree

18 files changed

+212
-136
lines changed

18 files changed

+212
-136
lines changed

src/libcore/fmt/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@ impl Display for Arguments<'_> {
441441
/// `enum`s, it will use the name of the variant and, if applicable, `(`, then the
442442
/// `Debug` values of the fields, then `)`.
443443
///
444+
/// # Stability
445+
///
446+
/// Derived `Debug` formats are not stable, and so may change with future Rust
447+
/// versions. Additionally, `Debug` implementations of types provided by the
448+
/// standard library (`libstd`, `libcore`, `liballoc`, etc.) are not stable, and
449+
/// may also change with future Rust versions.
450+
///
444451
/// # Examples
445452
///
446453
/// Deriving an implementation:

src/libcore/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
#![feature(const_generics)]
8888
#![feature(const_ptr_offset_from)]
8989
#![feature(const_result)]
90+
#![feature(const_slice_from_raw_parts)]
91+
#![feature(const_slice_ptr_len)]
9092
#![feature(const_type_name)]
9193
#![feature(custom_inner_attributes)]
9294
#![feature(decl_macro)]

src/libcore/ptr/non_null.rs

+59
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,65 @@ impl<T: ?Sized> NonNull<T> {
142142
}
143143
}
144144

145+
impl<T> NonNull<[T]> {
146+
/// Creates a non-null raw slice from a thin pointer and a length.
147+
///
148+
/// The `len` argument is the number of **elements**, not the number of bytes.
149+
///
150+
/// This function is safe, but dereferencing the return value is unsafe.
151+
/// See the documentation of [`slice::from_raw_parts`] for slice safety requirements.
152+
///
153+
/// [`slice::from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
154+
///
155+
/// # Examples
156+
///
157+
/// ```rust
158+
/// #![feature(nonnull_slice_from_raw_parts)]
159+
///
160+
/// use std::ptr::NonNull;
161+
///
162+
/// // create a slice pointer when starting out with a pointer to the first element
163+
/// let mut x = [5, 6, 7];
164+
/// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
165+
/// let slice = NonNull::slice_from_raw_parts(nonnull_pointer, 3);
166+
/// assert_eq!(unsafe { slice.as_ref()[2] }, 7);
167+
/// ```
168+
///
169+
/// (Note that this example artifically demonstrates a use of this method,
170+
/// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.)
171+
#[unstable(feature = "nonnull_slice_from_raw_parts", issue = "71941")]
172+
#[rustc_const_unstable(feature = "const_nonnull_slice_from_raw_parts", issue = "71941")]
173+
#[inline]
174+
pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self {
175+
// SAFETY: `data` is a `NonNull` pointer which is necessarily non-null
176+
unsafe { Self::new_unchecked(super::slice_from_raw_parts_mut(data.as_ptr(), len)) }
177+
}
178+
179+
/// Returns the length of a non-null raw slice.
180+
///
181+
/// The returned value is the number of **elements**, not the number of bytes.
182+
///
183+
/// This function is safe, even when the non-null raw slice cannot be dereferenced to a slice
184+
/// because the pointer does not have a valid address.
185+
///
186+
/// # Examples
187+
///
188+
/// ```rust
189+
/// #![feature(slice_ptr_len, nonnull_slice_from_raw_parts)]
190+
///
191+
/// use std::ptr::NonNull;
192+
///
193+
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
194+
/// assert_eq!(slice.len(), 3);
195+
/// ```
196+
#[unstable(feature = "slice_ptr_len", issue = "71146")]
197+
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
198+
#[inline]
199+
pub const fn len(self) -> usize {
200+
self.as_ptr().len()
201+
}
202+
}
203+
145204
#[stable(feature = "nonnull", since = "1.25.0")]
146205
impl<T: ?Sized> Clone for NonNull<T> {
147206
#[inline]

src/librustc_middle/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ macro_rules! arena_types {
6161
[few] privacy_access_levels: rustc_middle::middle::privacy::AccessLevels,
6262
[few] foreign_module: rustc_middle::middle::cstore::ForeignModule,
6363
[few] foreign_modules: Vec<rustc_middle::middle::cstore::ForeignModule>,
64-
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
64+
[] upvars_mentioned: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
6565
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
6666
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<$tcx>,
6767
[] attribute: rustc_ast::ast::Attribute,

src/librustc_middle/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24392439
};
24402440
let mut struct_fmt = fmt.debug_struct(&name);
24412441

2442-
if let Some(upvars) = tcx.upvars(def_id) {
2442+
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
24432443
for (&var_id, place) in upvars.keys().zip(places) {
24442444
let var_name = tcx.hir().name(var_id);
24452445
struct_fmt.field(&var_name.as_str(), place);
@@ -2458,7 +2458,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24582458
let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
24592459
let mut struct_fmt = fmt.debug_struct(&name);
24602460

2461-
if let Some(upvars) = tcx.upvars(def_id) {
2461+
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
24622462
for (&var_id, place) in upvars.keys().zip(places) {
24632463
let var_name = tcx.hir().name(var_id);
24642464
struct_fmt.field(&var_name.as_str(), place);

src/librustc_middle/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ rustc_queries! {
10401040
desc { "generating a postorder list of CrateNums" }
10411041
}
10421042

1043-
query upvars(_: DefId) -> Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>> {
1043+
query upvars_mentioned(_: DefId) -> Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>> {
10441044
eval_always
10451045
}
10461046
query maybe_unused_trait_import(def_id: LocalDefId) -> bool {

src/librustc_middle/ty/print/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ pub trait PrettyPrinter<'tcx>:
610610
let mut sep = " ";
611611
for (&var_id, upvar_ty) in self
612612
.tcx()
613-
.upvars(did)
613+
.upvars_mentioned(did)
614614
.as_ref()
615615
.iter()
616616
.flat_map(|v| v.keys())
@@ -659,7 +659,7 @@ pub trait PrettyPrinter<'tcx>:
659659
let mut sep = " ";
660660
for (&var_id, upvar_ty) in self
661661
.tcx()
662-
.upvars(did)
662+
.upvars_mentioned(did)
663663
.as_ref()
664664
.iter()
665665
.flat_map(|v| v.keys())

src/librustc_mir/borrow_check/diagnostics/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
377377
self.describe_field_from_ty(&ty, field, variant_index)
378378
}
379379
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
380-
// `tcx.upvars(def_id)` returns an `Option`, which is `None` in case
380+
// `tcx.upvars_mentioned(def_id)` returns an `Option`, which is `None` in case
381381
// the closure comes from another crate. But in that case we wouldn't
382382
// be borrowck'ing it, so we can just unwrap:
383-
let (&var_id, _) =
384-
self.infcx.tcx.upvars(def_id).unwrap().get_index(field.index()).unwrap();
383+
let (&var_id, _) = self
384+
.infcx
385+
.tcx
386+
.upvars_mentioned(def_id)
387+
.unwrap()
388+
.get_index(field.index())
389+
.unwrap();
385390

386391
self.infcx.tcx.hir().name(var_id).to_string()
387392
}
@@ -809,7 +814,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
809814
let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind;
810815
debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
811816
if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr {
812-
for (upvar, place) in self.infcx.tcx.upvars(def_id)?.values().zip(places) {
817+
for (upvar, place) in self.infcx.tcx.upvars_mentioned(def_id)?.values().zip(places) {
813818
match place {
814819
Operand::Copy(place) | Operand::Move(place)
815820
if target_place == place.as_ref() =>

0 commit comments

Comments
 (0)