Skip to content

Commit 564ee21

Browse files
committed
Auto merge of #146052 - matthiaskrgr:rollup-cfxx9m6, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #144443 (Make target pointer width in target json an integer) - #145174 (Ensure consistent drop for panicking drop in hint::select_unpredictable) - #145592 (Fix format string grammar in docs and improve alignment error message for #144023) - #145931 (Clarify that align_offset overaligns) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1bc901e + e3881cb commit 564ee21

File tree

27 files changed

+123
-55
lines changed

27 files changed

+123
-55
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ pub enum TargetDataLayoutErrors<'a> {
315315
MissingAlignment { cause: &'a str },
316316
InvalidAlignment { cause: &'a str, err: AlignFromBytesError },
317317
InconsistentTargetArchitecture { dl: &'a str, target: &'a str },
318-
InconsistentTargetPointerWidth { pointer_size: u64, target: u32 },
318+
InconsistentTargetPointerWidth { pointer_size: u64, target: u16 },
319319
InvalidBitsSize { err: String },
320320
UnknownPointerSpecification { err: String },
321321
}

compiler/rustc_ast_ir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl IntTy {
6969
})
7070
}
7171

72-
pub fn normalize(&self, target_width: u32) -> Self {
72+
pub fn normalize(&self, target_width: u16) -> Self {
7373
match self {
7474
IntTy::Isize => match target_width {
7575
16 => IntTy::I16,
@@ -148,7 +148,7 @@ impl UintTy {
148148
})
149149
}
150150

151-
pub fn normalize(&self, target_width: u32) -> Self {
151+
pub fn normalize(&self, target_width: u16) -> Self {
152152
match self {
153153
UintTy::Usize => match target_width {
154154
16 => UintTy::U16,

compiler/rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
"unix"
2323
],
2424
"target-mcount": "_mcount",
25-
"target-pointer-width": "32"
25+
"target-pointer-width": 32
2626
}

compiler/rustc_parse_format/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,9 @@ impl<'input> Parser<'input> {
858858
self.errors.insert(
859859
0,
860860
ParseError {
861-
description: "expected format parameter to occur after `:`".to_owned(),
861+
description:
862+
"expected alignment specifier after `:` in format string; example: `{:>?}`"
863+
.to_owned(),
862864
note: None,
863865
label: format!("expected `{}` to occur after `:`", alignment),
864866
span: range,

compiler/rustc_target/src/spec/json.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ impl Target {
2525
let mut base = Target {
2626
llvm_target: json.llvm_target,
2727
metadata: Default::default(),
28-
pointer_width: json
29-
.target_pointer_width
30-
.parse()
31-
.map_err(|err| format!("invalid target-pointer-width: {err}"))?,
28+
pointer_width: json.target_pointer_width,
3229
data_layout: json.data_layout,
3330
arch: json.arch,
3431
options: Default::default(),
@@ -245,19 +242,17 @@ impl ToJson for Target {
245242
target.update_to_cli();
246243

247244
macro_rules! target_val {
248-
($attr:ident) => {{
249-
let name = (stringify!($attr)).replace("_", "-");
250-
d.insert(name, target.$attr.to_json());
245+
($attr:ident) => {
246+
target_val!($attr, (stringify!($attr)).replace("_", "-"))
247+
};
248+
($attr:ident, $json_name:expr) => {{
249+
let name = $json_name;
250+
d.insert(name.into(), target.$attr.to_json());
251251
}};
252252
}
253253

254254
macro_rules! target_option_val {
255-
($attr:ident) => {{
256-
let name = (stringify!($attr)).replace("_", "-");
257-
if default.$attr != target.$attr {
258-
d.insert(name, target.$attr.to_json());
259-
}
260-
}};
255+
($attr:ident) => {{ target_option_val!($attr, (stringify!($attr)).replace("_", "-")) }};
261256
($attr:ident, $json_name:expr) => {{
262257
let name = $json_name;
263258
if default.$attr != target.$attr {
@@ -290,7 +285,7 @@ impl ToJson for Target {
290285

291286
target_val!(llvm_target);
292287
target_val!(metadata);
293-
d.insert("target-pointer-width".to_string(), self.pointer_width.to_string().to_json());
288+
target_val!(pointer_width, "target-pointer-width");
294289
target_val!(arch);
295290
target_val!(data_layout);
296291

@@ -463,7 +458,7 @@ struct TargetSpecJsonMetadata {
463458
#[serde(deny_unknown_fields)]
464459
struct TargetSpecJson {
465460
llvm_target: StaticCow<str>,
466-
target_pointer_width: String,
461+
target_pointer_width: u16,
467462
data_layout: StaticCow<str>,
468463
arch: StaticCow<str>,
469464

compiler/rustc_target/src/spec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,7 @@ pub struct Target {
23312331
/// Used for generating target documentation.
23322332
pub metadata: TargetMetadata,
23332333
/// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable.
2334-
pub pointer_width: u32,
2334+
pub pointer_width: u16,
23352335
/// Architecture to use for ABI considerations. Valid options include: "x86",
23362336
/// "x86_64", "arm", "aarch64", "mips", "powerpc", "powerpc64", and others.
23372337
pub arch: StaticCow<str>,

compiler/rustc_target/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn report_unused_fields() {
77
"arch": "powerpc64",
88
"data-layout": "e-m:e-i64:64-n32:64",
99
"llvm-target": "powerpc64le-elf",
10-
"target-pointer-width": "64",
10+
"target-pointer-width": 64,
1111
"code-mode": "foo"
1212
}
1313
"#;

library/alloc/src/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
//! sign := '+' | '-'
355355
//! width := count
356356
//! precision := count | '*'
357-
//! type := '?' | 'x?' | 'X?' | identifier
357+
//! type := '?' | 'x?' | 'X?' | 'o' | 'x' | 'X' | 'p' | 'b' | 'e' | 'E'
358358
//! count := parameter | integer
359359
//! parameter := argument '$'
360360
//! ```

library/compiler-builtins/etc/thumbv7em-none-eabi-renamed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
},
2020
"panic-strategy": "abort",
2121
"relocation-model": "static",
22-
"target-pointer-width": "32"
22+
"target-pointer-width": 32
2323
}

library/core/src/hint.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,12 +776,45 @@ pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T
776776
// Change this to use ManuallyDrop instead.
777777
let mut true_val = MaybeUninit::new(true_val);
778778
let mut false_val = MaybeUninit::new(false_val);
779+
780+
struct DropOnPanic<T> {
781+
// Invariant: valid pointer and points to an initialized value that is not further used,
782+
// i.e. it can be dropped by this guard.
783+
inner: *mut T,
784+
}
785+
786+
impl<T> Drop for DropOnPanic<T> {
787+
fn drop(&mut self) {
788+
// SAFETY: Must be guaranteed on construction of local type `DropOnPanic`.
789+
unsafe { self.inner.drop_in_place() }
790+
}
791+
}
792+
793+
let true_ptr = true_val.as_mut_ptr();
794+
let false_ptr = false_val.as_mut_ptr();
795+
779796
// SAFETY: The value that is not selected is dropped, and the selected one
780797
// is returned. This is necessary because the intrinsic doesn't drop the
781798
// value that is not selected.
782799
unsafe {
783-
crate::intrinsics::select_unpredictable(!condition, &mut true_val, &mut false_val)
784-
.assume_init_drop();
800+
// Extract the selected value first, ensure it is dropped as well if dropping the unselected
801+
// value panics. We construct a temporary by-pointer guard around the selected value while
802+
// dropping the unselected value. Arguments overlap here, so we can not use mutable
803+
// reference for these arguments.
804+
let guard = crate::intrinsics::select_unpredictable(condition, true_ptr, false_ptr);
805+
let drop = crate::intrinsics::select_unpredictable(condition, false_ptr, true_ptr);
806+
807+
// SAFETY: both pointers are well-aligned and point to initialized values inside a
808+
// `MaybeUninit` each. In both possible values for `condition` the pointer `guard` and
809+
// `drop` do not alias (even though the two argument pairs we have selected from did alias
810+
// each other).
811+
let guard = DropOnPanic { inner: guard };
812+
drop.drop_in_place();
813+
crate::mem::forget(guard);
814+
815+
// Note that it is important to use the values here. Reading from the pointer we got makes
816+
// LLVM forget the !unpredictable annotation sometimes (in tests, integer sized values in
817+
// particular seemed to confuse it, also observed in llvm/llvm-project #82340).
785818
crate::intrinsics::select_unpredictable(condition, true_val, false_val).assume_init()
786819
}
787820
}

0 commit comments

Comments
 (0)