Skip to content

Commit fdad98d

Browse files
committed
Auto merge of #143254 - matthiaskrgr:rollup-7x8bxek, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #143019 (Ensure -V --verbose processes both codegen_backend and codegen-backend) - #143140 (give Pointer::into_parts a more scary name and offer a safer alternative) - #143175 (Make combining LLD with external LLVM config a hard error) - #143180 (Use `tracing-forest` instead of `tracing-tree` for bootstrap tracing) - #143223 (Improve macro stats printing) - #143228 (Handle build scripts better in `-Zmacro-stats` output.) - #143229 ([COMPILETEST-UNTANGLE 1/N] Move some some early config checks to the lib and move the compiletest binary) - #143246 (Subtree update of `rust-analyzer`) - #143248 (Update books) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f26e580 + 3f6dc54 commit fdad98d

File tree

188 files changed

+2458
-983
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+2458
-983
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub(crate) fn codegen_const_value<'tcx>(
133133
}
134134
}
135135
Scalar::Ptr(ptr, _size) => {
136-
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
136+
let (prov, offset) = ptr.prov_and_relative_offset();
137137
let alloc_id = prov.alloc_id();
138138
let base_addr = match fx.tcx.global_alloc(alloc_id) {
139139
GlobalAlloc::Memory(alloc) => {

compiler/rustc_codegen_gcc/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
240240
}
241241
}
242242
Scalar::Ptr(ptr, _size) => {
243-
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
243+
let (prov, offset) = ptr.prov_and_relative_offset();
244244
let alloc_id = prov.alloc_id();
245245
let base_addr = match self.tcx.global_alloc(alloc_id) {
246246
GlobalAlloc::Memory(alloc) => {

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
268268
}
269269
}
270270
Scalar::Ptr(ptr, _size) => {
271-
let (prov, offset) = ptr.into_parts();
271+
let (prov, offset) = ptr.prov_and_relative_offset();
272272
let global_alloc = self.tcx.global_alloc(prov.alloc_id());
273273
let base_addr = match global_alloc {
274274
GlobalAlloc::Memory(alloc) => {

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ pub(super) fn op_to_const<'tcx>(
209209

210210
match immediate {
211211
Left(ref mplace) => {
212-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
213-
let (prov, offset) = mplace.ptr().into_parts();
214-
let alloc_id = prov.expect("cannot have `fake` place for non-ZST type").alloc_id();
212+
let (prov, offset) =
213+
mplace.ptr().into_pointer_or_addr().unwrap().prov_and_relative_offset();
214+
let alloc_id = prov.alloc_id();
215215
ConstValue::Indirect { alloc_id, offset }
216216
}
217217
// see comment on `let force_as_immediate` above
@@ -232,9 +232,10 @@ pub(super) fn op_to_const<'tcx>(
232232
imm.layout.ty,
233233
);
234234
let msg = "`op_to_const` on an immediate scalar pair must only be used on slice references to the beginning of an actual allocation";
235-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
236-
let (prov, offset) = a.to_pointer(ecx).expect(msg).into_parts();
237-
let alloc_id = prov.expect(msg).alloc_id();
235+
let ptr = a.to_pointer(ecx).expect(msg);
236+
let (prov, offset) =
237+
ptr.into_pointer_or_addr().expect(msg).prov_and_relative_offset();
238+
let alloc_id = prov.alloc_id();
238239
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
239240
assert!(offset == abi::Size::ZERO, "{}", msg);
240241
let meta = b.to_target_usize(ecx).expect(msg);

compiler/rustc_const_eval/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
574574
if addr != 0 {
575575
diag.arg(
576576
"pointer",
577-
Pointer::<Option<CtfeProvenance>>::from_addr_invalid(addr).to_string(),
577+
Pointer::<Option<CtfeProvenance>>::without_provenance(addr).to_string(),
578578
);
579579
}
580580

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
747747
// Allow these casts, but make the pointer not dereferenceable.
748748
// (I.e., they behave like transmutation.)
749749
// This is correct because no pointers can ever be exposed in compile-time evaluation.
750-
interp_ok(Pointer::from_addr_invalid(addr))
750+
interp_ok(Pointer::without_provenance(addr))
751751
}
752752

753753
#[inline(always)]
@@ -756,8 +756,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
756756
ptr: Pointer<CtfeProvenance>,
757757
_size: i64,
758758
) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
759-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
760-
let (prov, offset) = ptr.into_parts();
759+
let (prov, offset) = ptr.prov_and_relative_offset();
761760
Some((prov.alloc_id(), offset, prov.immutable()))
762761
}
763762

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
15961596
Some((alloc_id, offset, extra)) => Ok((alloc_id, offset, extra)),
15971597
None => {
15981598
assert!(M::Provenance::OFFSET_IS_ADDR);
1599-
let (_, addr) = ptr.into_parts();
1599+
// Offset is absolute, as we just asserted.
1600+
let (_, addr) = ptr.into_raw_parts();
16001601
Err(addr.bytes())
16011602
}
16021603
},

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx, Prov: Provenance> MPlaceTy<'tcx, Prov> {
118118
pub fn fake_alloc_zst(layout: TyAndLayout<'tcx>) -> Self {
119119
assert!(layout.is_zst());
120120
let align = layout.align.abi;
121-
let ptr = Pointer::from_addr_invalid(align.bytes()); // no provenance, absolute address
121+
let ptr = Pointer::without_provenance(align.bytes()); // no provenance, absolute address
122122
MPlaceTy { mplace: MemPlace { ptr, meta: MemPlaceMeta::None, misaligned: None }, layout }
123123
}
124124

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
518518
Ub(DanglingIntPointer { addr: i, .. }) => DanglingPtrNoProvenance {
519519
ptr_kind,
520520
// FIXME this says "null pointer" when null but we need translate
521-
pointer: format!("{}", Pointer::<Option<AllocId>>::from_addr_invalid(i))
521+
pointer: format!("{}", Pointer::<Option<AllocId>>::without_provenance(i))
522522
},
523523
Ub(PointerOutOfBounds { .. }) => DanglingPtrOutOfBounds {
524524
ptr_kind
@@ -868,7 +868,9 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
868868
fn add_data_range(&mut self, ptr: Pointer<Option<M::Provenance>>, size: Size) {
869869
if let Some(data_bytes) = self.data_bytes.as_mut() {
870870
// We only have to store the offset, the rest is the same for all pointers here.
871-
let (_prov, offset) = ptr.into_parts();
871+
// The logic is agnostic to wether the offset is relative or absolute as long as
872+
// it is consistent.
873+
let (_prov, offset) = ptr.into_raw_parts();
872874
// Add this.
873875
data_bytes.add_range(offset, size);
874876
};
@@ -894,7 +896,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
894896
.as_mplace_or_imm()
895897
.expect_left("place must be in memory")
896898
.ptr();
897-
let (_prov, offset) = ptr.into_parts();
899+
let (_prov, offset) = ptr.into_raw_parts();
898900
offset
899901
}
900902

@@ -903,7 +905,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
903905
// Our value must be in memory, otherwise we would not have set up `data_bytes`.
904906
let mplace = self.ecx.force_allocation(place)?;
905907
// Determine starting offset and size.
906-
let (_prov, start_offset) = mplace.ptr().into_parts();
908+
let (_prov, start_offset) = mplace.ptr().into_raw_parts();
907909
let (size, _align) = self
908910
.ecx
909911
.size_and_align_of_val(&mplace)?

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,9 @@ fn get_backend_from_raw_matches(
11121112
matches: &Matches,
11131113
) -> Box<dyn CodegenBackend> {
11141114
let debug_flags = matches.opt_strs("Z");
1115-
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
1115+
let backend_name = debug_flags
1116+
.iter()
1117+
.find_map(|x| x.strip_prefix("codegen-backend=").or(x.strip_prefix("codegen_backend=")));
11161118
let target = parse_target_triple(early_dcx, matches);
11171119
let sysroot = Sysroot::new(matches.opt_str("sysroot").map(PathBuf::from));
11181120
let target = config::build_target_config(early_dcx, &target, sysroot.path());

compiler/rustc_interface/src/passes.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ fn configure_and_expand(
298298
fn print_macro_stats(ecx: &ExtCtxt<'_>) {
299299
use std::fmt::Write;
300300

301+
let crate_name = ecx.ecfg.crate_name.as_str();
302+
let crate_name = if crate_name == "build_script_build" {
303+
// This is a build script. Get the package name from the environment.
304+
let pkg_name =
305+
std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "<unknown crate>".to_string());
306+
format!("{pkg_name} build script")
307+
} else {
308+
crate_name.to_string()
309+
};
310+
301311
// No instability because we immediately sort the produced vector.
302312
#[allow(rustc::potential_query_instability)]
303313
let mut macro_stats: Vec<_> = ecx
@@ -327,7 +337,7 @@ fn print_macro_stats(ecx: &ExtCtxt<'_>) {
327337
// non-interleaving, though.
328338
let mut s = String::new();
329339
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
330-
_ = writeln!(s, "{prefix} MACRO EXPANSION STATS: {}", ecx.ecfg.crate_name);
340+
_ = writeln!(s, "{prefix} MACRO EXPANSION STATS: {}", crate_name);
331341
_ = writeln!(
332342
s,
333343
"{prefix} {:<name_w$}{:>uses_w$}{:>lines_w$}{:>avg_lines_w$}{:>bytes_w$}{:>avg_bytes_w$}",
@@ -341,20 +351,30 @@ fn print_macro_stats(ecx: &ExtCtxt<'_>) {
341351
}
342352
for (bytes, lines, uses, name, kind) in macro_stats {
343353
let mut name = ExpnKind::Macro(kind, *name).descr();
354+
let uses_with_underscores = thousands::usize_with_underscores(uses);
344355
let avg_lines = lines as f64 / uses as f64;
345356
let avg_bytes = bytes as f64 / uses as f64;
346-
if name.len() >= name_w {
347-
// If the name is long, print it on a line by itself, then
348-
// set the name to empty and print things normally, to show the
349-
// stats on the next line.
357+
358+
// Ensure the "Macro Name" and "Uses" columns are as compact as possible.
359+
let mut uses_w = uses_w;
360+
if name.len() + uses_with_underscores.len() >= name_w + uses_w {
361+
// The name would abut or overlap the uses value. Print the name
362+
// on a line by itself, then set the name to empty and print things
363+
// normally, to show the stats on the next line.
350364
_ = writeln!(s, "{prefix} {:<name_w$}", name);
351365
name = String::new();
352-
}
366+
} else if name.len() >= name_w {
367+
// The name won't abut or overlap with the uses value, but it does
368+
// overlap with the empty part of the uses column. Shrink the width
369+
// of the uses column to account for the excess name length.
370+
uses_w = uses_with_underscores.len() + 1
371+
};
372+
353373
_ = writeln!(
354374
s,
355375
"{prefix} {:<name_w$}{:>uses_w$}{:>lines_w$}{:>avg_lines_w$}{:>bytes_w$}{:>avg_bytes_w$}",
356376
name,
357-
thousands::usize_with_underscores(uses),
377+
uses_with_underscores,
358378
thousands::usize_with_underscores(lines),
359379
thousands::f64p1_with_underscores(avg_lines),
360380
thousands::usize_with_underscores(bytes),

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ impl<'tcx> ConstValue<'tcx> {
168168
return Some(&[]);
169169
}
170170
// Non-empty slice, must have memory. We know this is a relative pointer.
171-
let (inner_prov, offset) = ptr.into_parts();
172-
let data = tcx.global_alloc(inner_prov?.alloc_id()).unwrap_memory();
171+
let (inner_prov, offset) =
172+
ptr.into_pointer_or_addr().ok()?.prov_and_relative_offset();
173+
let data = tcx.global_alloc(inner_prov.alloc_id()).unwrap_memory();
173174
(data, offset.bytes(), offset.bytes() + len)
174175
}
175176
};

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl Allocation {
526526
let ptr_bytes = &mut bytes[idx..idx + ptr_size];
527527
let bits = read_target_uint(endian, ptr_bytes).unwrap();
528528
let (ptr_prov, ptr_offset) =
529-
adjust_ptr(Pointer::new(alloc_id, Size::from_bytes(bits)))?.into_parts();
529+
adjust_ptr(Pointer::new(alloc_id, Size::from_bytes(bits)))?.into_raw_parts();
530530
write_target_uint(endian, ptr_bytes, ptr_offset.bytes().into()).unwrap();
531531
new_provenance.push((offset, ptr_prov));
532532
}
@@ -769,7 +769,7 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
769769
// as-is into memory. This also double-checks that `val.size()` matches `range.size`.
770770
let (bytes, provenance) = match val.to_bits_or_ptr_internal(range.size)? {
771771
Right(ptr) => {
772-
let (provenance, offset) = ptr.into_parts();
772+
let (provenance, offset) = ptr.into_raw_parts();
773773
(u128::from(offset.bytes()), Some(provenance))
774774
}
775775
Left(data) => (data, None),

compiler/rustc_middle/src/mir/interpret/pointer.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl From<CtfeProvenance> for Pointer {
288288
impl<Prov> From<Pointer<Prov>> for Pointer<Option<Prov>> {
289289
#[inline(always)]
290290
fn from(ptr: Pointer<Prov>) -> Self {
291-
let (prov, offset) = ptr.into_parts();
291+
let (prov, offset) = ptr.into_raw_parts();
292292
Pointer::new(Some(prov), offset)
293293
}
294294
}
@@ -314,19 +314,17 @@ impl<Prov> Pointer<Option<Prov>> {
314314
assert!(Prov::OFFSET_IS_ADDR);
315315
self.offset
316316
}
317-
}
318317

319-
impl<Prov> Pointer<Option<Prov>> {
320318
/// Creates a pointer to the given address, with invalid provenance (i.e., cannot be used for
321319
/// any memory access).
322320
#[inline(always)]
323-
pub fn from_addr_invalid(addr: u64) -> Self {
321+
pub fn without_provenance(addr: u64) -> Self {
324322
Pointer { provenance: None, offset: Size::from_bytes(addr) }
325323
}
326324

327325
#[inline(always)]
328326
pub fn null() -> Self {
329-
Pointer::from_addr_invalid(0)
327+
Pointer::without_provenance(0)
330328
}
331329
}
332330

@@ -336,11 +334,11 @@ impl<Prov> Pointer<Prov> {
336334
Pointer { provenance, offset }
337335
}
338336

339-
/// Obtain the constituents of this pointer. Not that the meaning of the offset depends on the type `Prov`!
340-
/// This function must only be used in the implementation of `Machine::ptr_get_alloc`,
341-
/// and when a `Pointer` is taken apart to be stored efficiently in an `Allocation`.
337+
/// Obtain the constituents of this pointer. Note that the meaning of the offset depends on the
338+
/// type `Prov`! This is a low-level function that should only be used when absolutely
339+
/// necessary. Prefer `prov_and_relative_offset` if possible.
342340
#[inline(always)]
343-
pub fn into_parts(self) -> (Prov, Size) {
341+
pub fn into_raw_parts(self) -> (Prov, Size) {
344342
(self.provenance, self.offset)
345343
}
346344

@@ -361,3 +359,12 @@ impl<Prov> Pointer<Prov> {
361359
self.wrapping_offset(Size::from_bytes(i as u64), cx)
362360
}
363361
}
362+
363+
impl Pointer<CtfeProvenance> {
364+
/// Return the provenance and relative offset stored in this pointer. Safer alternative to
365+
/// `into_raw_parts` since the type ensures that the offset is indeed relative.
366+
#[inline(always)]
367+
pub fn prov_and_relative_offset(self) -> (CtfeProvenance, Size) {
368+
(self.provenance, self.offset)
369+
}
370+
}

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<Prov> Scalar<Prov> {
109109
/// Create a Scalar from a pointer with an `Option<_>` provenance (where `None` represents a
110110
/// plain integer / "invalid" pointer).
111111
pub fn from_maybe_pointer(ptr: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
112-
match ptr.into_parts() {
112+
match ptr.into_raw_parts() {
113113
(Some(prov), offset) => Scalar::from_pointer(Pointer::new(prov, offset), cx),
114114
(None, offset) => {
115115
Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
@@ -276,7 +276,7 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
276276
Right(ptr) => interp_ok(ptr.into()),
277277
Left(bits) => {
278278
let addr = u64::try_from(bits).unwrap();
279-
interp_ok(Pointer::from_addr_invalid(addr))
279+
interp_ok(Pointer::without_provenance(addr))
280280
}
281281
}
282282
}
@@ -299,7 +299,7 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
299299
Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
300300
} else {
301301
// We know `offset` is relative, since `OFFSET_IS_ADDR == false`.
302-
let (prov, offset) = ptr.into_parts();
302+
let (prov, offset) = ptr.into_raw_parts();
303303
// Because `OFFSET_IS_ADDR == false`, this unwrap can never fail.
304304
Err(Scalar::Ptr(Pointer::new(prov.get_alloc_id().unwrap(), offset), sz))
305305
}

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
17551755
) -> Result<(), PrintError> {
17561756
define_scoped_cx!(self);
17571757

1758-
let (prov, offset) = ptr.into_parts();
1758+
let (prov, offset) = ptr.prov_and_relative_offset();
17591759
match ty.kind() {
17601760
// Byte strings (&[u8; N])
17611761
ty::Ref(_, inner, _) => {

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ fn op_to_prop_const<'tcx>(
16361636
}
16371637

16381638
let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
1639-
let (prov, offset) = pointer.into_parts();
1639+
let (prov, offset) = pointer.prov_and_relative_offset();
16401640
let alloc_id = prov.alloc_id();
16411641
intern_const_alloc_for_constprop(ecx, alloc_id).discard_err()?;
16421642

0 commit comments

Comments
 (0)