Skip to content

Commit f33a8c6

Browse files
committed
Auto merge of #134033 - matthiaskrgr:rollup-69fswyh, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #131669 (lint: change help for pointers to dyn types in FFI) - #133104 (crashes: add test for #131451) - #133767 (Add more info on type/trait mismatches for different crate versions) - #133861 (Add allocate_bytes and refactor allocate_str in InterpCx for raw byte…) - #133976 (Removed Unnecessary Spaces From RELEASES.md) - #133987 (Define acronym for thread local storage) - #133992 (Actually walk into lifetimes and attrs in `EarlyContextAndPass`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f415c07 + d4b5345 commit f33a8c6

40 files changed

+912
-280
lines changed

RELEASES.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ Compatibility Notes
503503
* We have renamed `std::panic::PanicInfo` to `std::panic::PanicHookInfo`. The old name will continue to work as an alias, but will result in a deprecation warning starting in Rust 1.82.0.
504504

505505
`core::panic::PanicInfo` will remain unchanged, however, as this is now a *different type*.
506-
506+
507507
The reason is that these types have different roles: `std::panic::PanicHookInfo` is the argument to the [panic hook](https://doc.rust-lang.org/stable/std/panic/fn.set_hook.html) in std context (where panics can have an arbitrary payload), while `core::panic::PanicInfo` is the argument to the [`#[panic_handler]`](https://doc.rust-lang.org/nomicon/panic-handler.html) in no_std context (where panics always carry a formatted *message*). Separating these types allows us to add more useful methods to these types, such as `std::panic::PanicHookInfo::payload_as_str()` and `core::panic::PanicInfo::message()`.
508508

509509
* The new sort implementations may panic if a type's implementation of [`Ord`](https://doc.rust-lang.org/std/cmp/trait.Ord.html) (or the given comparison function) does not implement a [total order](https://en.wikipedia.org/wiki/Total_order) as the trait requires. `Ord`'s supertraits (`PartialOrd`, `Eq`, and `PartialEq`) must also be consistent. The previous implementations would not "notice" any problem, but the new implementations have a good chance of detecting inconsistencies, throwing a panic rather than returning knowingly unsorted data.
@@ -584,7 +584,7 @@ Stabilized APIs
584584
- [`impl Default for Arc<CStr>`](https://doc.rust-lang.org/beta/alloc/sync/struct.Arc.html#impl-Default-for-Arc%3CCStr%3E)
585585
- [`impl Default for Arc<[T]>`](https://doc.rust-lang.org/beta/alloc/sync/struct.Arc.html#impl-Default-for-Arc%3C%5BT%5D%3E)
586586
- [`impl IntoIterator for Box<[T]>`](https://doc.rust-lang.org/beta/alloc/boxed/struct.Box.html#impl-IntoIterator-for-Box%3C%5BI%5D,+A%3E)
587-
- [`impl FromIterator<String> for Box<str>`](https://doc.rust-lang.org/beta/alloc/boxed/struct.Box.html#impl-FromIterator%3CString%3E-for-Box%3Cstr%3E)
587+
- [`impl FromIterator<String> for Box<str>`](https://doc.rust-lang.org/beta/alloc/boxed/struct.Box.html#impl-FromIterator%3CString%3E-for-Box%3Cstr%3E)
588588
- [`impl FromIterator<char> for Box<str>`](https://doc.rust-lang.org/beta/alloc/boxed/struct.Box.html#impl-FromIterator%3Cchar%3E-for-Box%3Cstr%3E)
589589
- [`LazyCell`](https://doc.rust-lang.org/beta/core/cell/struct.LazyCell.html)
590590
- [`LazyLock`](https://doc.rust-lang.org/beta/std/sync/struct.LazyLock.html)
@@ -1816,7 +1816,7 @@ Compiler
18161816
- [Detect uninhabited types early in const eval](https://github.com/rust-lang/rust/pull/109435/)
18171817
- [Switch to LLD as default linker for {arm,thumb}v4t-none-eabi](https://github.com/rust-lang/rust/pull/109721/)
18181818
- [Add tier 3 target `loongarch64-unknown-linux-gnu`](https://github.com/rust-lang/rust/pull/96971)
1819-
- [Add tier 3 target for `i586-pc-nto-qnx700` (QNX Neutrino RTOS, version 7.0)](https://github.com/rust-lang/rust/pull/109173/),
1819+
- [Add tier 3 target for `i586-pc-nto-qnx700` (QNX Neutrino RTOS, version 7.0)](https://github.com/rust-lang/rust/pull/109173/),
18201820
- [Insert alignment checks for pointer dereferences as debug assertions](https://github.com/rust-lang/rust/pull/98112)
18211821
This catches undefined behavior at runtime, and may cause existing code to fail.
18221822

@@ -2023,7 +2023,7 @@ Compatibility Notes
20232023
If `tools = [...]` is set in config.toml, we will respect a missing rustdoc in that list. By
20242024
default rustdoc remains included. To retain the prior behavior explicitly add `"rustdoc"` to the
20252025
list.
2026-
2026+
20272027
<a id="1.69.0-Internal-Changes"></a>
20282028

20292029
Internal Changes

compiler/rustc_const_eval/src/interpret/place.rs

+32-13
Original file line numberDiff line numberDiff line change
@@ -1018,29 +1018,48 @@ where
10181018
self.allocate_dyn(layout, kind, MemPlaceMeta::None)
10191019
}
10201020

1021-
/// Returns a wide MPlace of type `str` to a new 1-aligned allocation.
1022-
/// Immutable strings are deduplicated and stored in global memory.
1023-
pub fn allocate_str(
1021+
/// Allocates a sequence of bytes in the interpreter's memory.
1022+
/// For immutable allocations, uses deduplication to reuse existing memory.
1023+
/// For mutable allocations, creates a new unique allocation.
1024+
pub fn allocate_bytes(
10241025
&mut self,
1025-
str: &str,
1026+
bytes: &[u8],
1027+
align: Align,
10261028
kind: MemoryKind<M::MemoryKind>,
10271029
mutbl: Mutability,
1028-
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
1029-
let tcx = self.tcx.tcx;
1030-
1030+
) -> InterpResult<'tcx, Pointer<M::Provenance>> {
10311031
// Use cache for immutable strings.
1032-
let ptr = if mutbl.is_not() {
1032+
if mutbl.is_not() {
10331033
// Use dedup'd allocation function.
10341034
let salt = M::get_global_alloc_salt(self, None);
1035-
let id = tcx.allocate_bytes_dedup(str.as_bytes(), salt);
1035+
let id = self.tcx.allocate_bytes_dedup(bytes, salt);
10361036

10371037
// Turn untagged "global" pointers (obtained via `tcx`) into the machine pointer to the allocation.
1038-
M::adjust_alloc_root_pointer(&self, Pointer::from(id), Some(kind))?
1038+
M::adjust_alloc_root_pointer(&self, Pointer::from(id), Some(kind))
10391039
} else {
1040-
self.allocate_bytes_ptr(str.as_bytes(), Align::ONE, kind, mutbl)?
1041-
};
1042-
let meta = Scalar::from_target_usize(u64::try_from(str.len()).unwrap(), self);
1040+
// Allocate new memory for mutable data.
1041+
self.allocate_bytes_ptr(bytes, align, kind, mutbl)
1042+
}
1043+
}
1044+
1045+
/// Allocates a string in the interpreter's memory with metadata for length.
1046+
/// Uses `allocate_bytes` internally but adds string-specific metadata handling.
1047+
pub fn allocate_str(
1048+
&mut self,
1049+
str: &str,
1050+
kind: MemoryKind<M::MemoryKind>,
1051+
mutbl: Mutability,
1052+
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
1053+
let bytes = str.as_bytes();
1054+
let ptr = self.allocate_bytes(bytes, Align::ONE, kind, mutbl)?;
1055+
1056+
// Create length metadata for the string.
1057+
let meta = Scalar::from_target_usize(u64::try_from(bytes.len()).unwrap(), self);
1058+
1059+
// Get layout for Rust's str type.
10431060
let layout = self.layout_of(self.tcx.types.str_).unwrap();
1061+
1062+
// Combine pointer and metadata into a wide pointer.
10441063
interp_ok(self.ptr_with_meta_to_mplace(
10451064
ptr.into(),
10461065
MemPlaceMeta::Meta(meta),

compiler/rustc_lint/messages.ftl

+11-2
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ lint_improper_ctypes_128bit = 128-bit integers don't currently have a known stab
359359
lint_improper_ctypes_array_help = consider passing a pointer to the array
360360
361361
lint_improper_ctypes_array_reason = passing raw arrays by value is not FFI-safe
362-
lint_improper_ctypes_box = box cannot be represented as a single pointer
363362
364363
lint_improper_ctypes_char_help = consider using `u32` or `libc::wchar_t` instead
365364
@@ -377,7 +376,9 @@ lint_improper_ctypes_enum_repr_help =
377376
lint_improper_ctypes_enum_repr_reason = enum has no representation hint
378377
lint_improper_ctypes_fnptr_help = consider using an `extern fn(...) -> ...` function pointer instead
379378
379+
lint_improper_ctypes_fnptr_indirect_reason = the function pointer to `{$ty}` is FFI-unsafe due to `{$inner_ty}`
380380
lint_improper_ctypes_fnptr_reason = this function pointer has Rust-specific calling convention
381+
381382
lint_improper_ctypes_non_exhaustive = this enum is non-exhaustive
382383
lint_improper_ctypes_non_exhaustive_variant = this enum has non-exhaustive variants
383384
@@ -388,7 +389,11 @@ lint_improper_ctypes_opaque = opaque types have no C equivalent
388389
lint_improper_ctypes_pat_help = consider using the base type instead
389390
390391
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
391-
lint_improper_ctypes_slice_help = consider using a raw pointer instead
392+
393+
lint_improper_ctypes_sized_ptr_to_unsafe_type =
394+
this reference (`{$ty}`) is ABI-compatible with a C pointer, but `{$inner_ty}` itself does not have a C layout
395+
396+
lint_improper_ctypes_slice_help = consider using a raw pointer to the slice's first element (and a length) instead
392397
393398
lint_improper_ctypes_slice_reason = slices have no C equivalent
394399
lint_improper_ctypes_str_help = consider using `*const u8` and a length instead
@@ -414,6 +419,10 @@ lint_improper_ctypes_union_layout_help = consider adding a `#[repr(C)]` or `#[re
414419
lint_improper_ctypes_union_layout_reason = this union has unspecified layout
415420
lint_improper_ctypes_union_non_exhaustive = this union is non-exhaustive
416421
422+
lint_improper_ctypes_unsized_box = this box for an unsized type contains metadata, which makes it incompatible with a C pointer
423+
lint_improper_ctypes_unsized_ptr = this pointer to an unsized type contains metadata, which makes it incompatible with a C pointer
424+
lint_improper_ctypes_unsized_ref = this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
425+
417426
lint_incomplete_include =
418427
include macro expected single expression in source
419428

compiler/rustc_lint/src/early.rs

+2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
245245

246246
fn visit_lifetime(&mut self, lt: &'a ast::Lifetime, _: ast_visit::LifetimeCtxt) {
247247
self.check_id(lt.id);
248+
ast_visit::walk_lifetime(self, lt);
248249
}
249250

250251
fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) {
@@ -259,6 +260,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
259260

260261
fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
261262
lint_callback!(self, check_attribute, attr);
263+
ast_visit::walk_attribute(self, attr);
262264
}
263265

264266
fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) {

compiler/rustc_lint/src/hidden_unicode_codepoints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::lints::{
99
use crate::{EarlyContext, EarlyLintPass, LintContext};
1010

1111
declare_lint! {
12+
#[allow(text_direction_codepoint_in_literal)]
1213
/// The `text_direction_codepoint_in_literal` lint detects Unicode codepoints that change the
1314
/// visual representation of text on screen in a way that does not correspond to their on
1415
/// memory representation.

compiler/rustc_lint/src/lints.rs

+36-9
Original file line numberDiff line numberDiff line change
@@ -1851,13 +1851,44 @@ pub(crate) struct UnpredictableFunctionPointerComparisonsSuggestion<'a> {
18511851
pub right: Span,
18521852
}
18531853

1854+
pub(crate) struct ImproperCTypesLayer<'a> {
1855+
pub ty: Ty<'a>,
1856+
pub inner_ty: Option<Ty<'a>>,
1857+
pub note: DiagMessage,
1858+
pub span_note: Option<Span>,
1859+
pub help: Option<DiagMessage>,
1860+
}
1861+
1862+
impl<'a> Subdiagnostic for ImproperCTypesLayer<'a> {
1863+
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
1864+
self,
1865+
diag: &mut Diag<'_, G>,
1866+
f: &F,
1867+
) {
1868+
diag.arg("ty", self.ty);
1869+
if let Some(ty) = self.inner_ty {
1870+
diag.arg("inner_ty", ty);
1871+
}
1872+
1873+
if let Some(help) = self.help {
1874+
let msg = f(diag, help.into());
1875+
diag.help(msg);
1876+
}
1877+
1878+
let msg = f(diag, self.note.into());
1879+
diag.note(msg);
1880+
if let Some(note) = self.span_note {
1881+
let msg = f(diag, fluent::lint_note.into());
1882+
diag.span_note(note, msg);
1883+
};
1884+
}
1885+
}
1886+
18541887
pub(crate) struct ImproperCTypes<'a> {
18551888
pub ty: Ty<'a>,
18561889
pub desc: &'a str,
18571890
pub label: Span,
1858-
pub help: Option<DiagMessage>,
1859-
pub note: DiagMessage,
1860-
pub span_note: Option<Span>,
1891+
pub reasons: Vec<ImproperCTypesLayer<'a>>,
18611892
}
18621893

18631894
// Used because of the complexity of Option<DiagMessage>, DiagMessage, and Option<Span>
@@ -1867,12 +1898,8 @@ impl<'a> LintDiagnostic<'a, ()> for ImproperCTypes<'_> {
18671898
diag.arg("ty", self.ty);
18681899
diag.arg("desc", self.desc);
18691900
diag.span_label(self.label, fluent::lint_label);
1870-
if let Some(help) = self.help {
1871-
diag.help(help);
1872-
}
1873-
diag.note(self.note);
1874-
if let Some(note) = self.span_note {
1875-
diag.span_note(note, fluent::lint_note);
1901+
for reason in self.reasons.into_iter() {
1902+
diag.subdiagnostic(reason);
18761903
}
18771904
}
18781905
}

0 commit comments

Comments
 (0)