Skip to content

Commit 35bab92

Browse files
committed
Auto merge of #85486 - RalfJung:rollup-4ibcxuu, r=RalfJung
Rollup of 8 pull requests Successful merges: - #84717 (impl FromStr for proc_macro::Literal) - #85169 (Add method-toggle to <details> for methods) - #85287 (Expose `Concurrent` (private type in public i'face)) - #85315 (adding time complexity for partition_in_place iter method) - #85439 (Add diagnostic item to `CStr`) - #85464 (Fix UB in documented example for `ptr::swap`) - #85470 (Fix invalid CSS rules for a:hover) - #85472 (CTFE Machine: do not expose Allocation) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5ab0f37 + 9fa15ff commit 35bab92

File tree

19 files changed

+179
-48
lines changed

19 files changed

+179
-48
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::base::{ExtCtxt, ResolverExpand};
22

33
use rustc_ast as ast;
4-
use rustc_ast::token;
5-
use rustc_ast::token::Nonterminal;
6-
use rustc_ast::token::NtIdent;
4+
use rustc_ast::token::{self, Nonterminal, NtIdent, TokenKind};
75
use rustc_ast::tokenstream::{self, CanSynthesizeMissingTokens};
86
use rustc_ast::tokenstream::{DelimSpan, Spacing::*, TokenStream, TreeAndSpacing};
97
use rustc_ast_pretty::pprust;
@@ -541,6 +539,33 @@ impl server::Ident for Rustc<'_> {
541539
}
542540

543541
impl server::Literal for Rustc<'_> {
542+
fn from_str(&mut self, s: &str) -> Result<Self::Literal, ()> {
543+
let override_span = None;
544+
let stream = parse_stream_from_source_str(
545+
FileName::proc_macro_source_code(s),
546+
s.to_owned(),
547+
self.sess,
548+
override_span,
549+
);
550+
if stream.len() != 1 {
551+
return Err(());
552+
}
553+
let tree = stream.into_trees().next().unwrap();
554+
let token = match tree {
555+
tokenstream::TokenTree::Token(token) => token,
556+
tokenstream::TokenTree::Delimited { .. } => return Err(()),
557+
};
558+
let span_data = token.span.data();
559+
if (span_data.hi.0 - span_data.lo.0) as usize != s.len() {
560+
// There is a comment or whitespace adjacent to the literal.
561+
return Err(());
562+
}
563+
let lit = match token.kind {
564+
TokenKind::Literal(lit) => lit,
565+
_ => return Err(()),
566+
};
567+
Ok(Literal { lit, span: self.call_site })
568+
}
544569
fn debug_kind(&mut self, literal: &Self::Literal) -> String {
545570
format!("{:?}", literal.lit.kind)
546571
}

compiler/rustc_mir/src/interpret/machine.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
313313
#[inline(always)]
314314
fn memory_read(
315315
_memory_extra: &Self::MemoryExtra,
316-
_alloc: &Allocation<Self::PointerTag, Self::AllocExtra>,
316+
_alloc_extra: &Self::AllocExtra,
317317
_ptr: Pointer<Self::PointerTag>,
318318
_size: Size,
319319
) -> InterpResult<'tcx> {
@@ -324,7 +324,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
324324
#[inline(always)]
325325
fn memory_written(
326326
_memory_extra: &mut Self::MemoryExtra,
327-
_alloc: &mut Allocation<Self::PointerTag, Self::AllocExtra>,
327+
_alloc_extra: &mut Self::AllocExtra,
328328
_ptr: Pointer<Self::PointerTag>,
329329
_size: Size,
330330
) -> InterpResult<'tcx> {
@@ -335,8 +335,9 @@ pub trait Machine<'mir, 'tcx>: Sized {
335335
#[inline(always)]
336336
fn memory_deallocated(
337337
_memory_extra: &mut Self::MemoryExtra,
338-
_alloc: &mut Allocation<Self::PointerTag, Self::AllocExtra>,
338+
_alloc_extra: &mut Self::AllocExtra,
339339
_ptr: Pointer<Self::PointerTag>,
340+
_size: Size,
340341
) -> InterpResult<'tcx> {
341342
Ok(())
342343
}

compiler/rustc_mir/src/interpret/memory.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
343343
}
344344

345345
// Let the machine take some extra action
346-
M::memory_deallocated(&mut self.extra, &mut alloc, ptr)?;
346+
let size = alloc.size();
347+
M::memory_deallocated(&mut self.extra, &mut alloc.extra, ptr, size)?;
347348

348349
// Don't forget to remember size and align of this now-dead allocation
349-
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size(), alloc.align));
350+
let old = self.dead_alloc_map.insert(ptr.alloc_id, (size, alloc.align));
350351
if old.is_some() {
351352
bug!("Nothing can be deallocated twice");
352353
}
@@ -591,7 +592,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
591592
},
592593
)?;
593594
if let Some((ptr, alloc)) = ptr_and_alloc {
594-
M::memory_read(&self.extra, alloc, ptr, size)?;
595+
M::memory_read(&self.extra, &alloc.extra, ptr, size)?;
595596
let range = alloc_range(ptr.offset, size);
596597
Ok(Some(AllocRef { alloc, range, tcx: self.tcx, alloc_id: ptr.alloc_id }))
597598
} else {
@@ -660,7 +661,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
660661
// FIXME: can we somehow avoid looking up the allocation twice here?
661662
// We cannot call `get_raw_mut` inside `check_and_deref_ptr` as that would duplicate `&mut self`.
662663
let (alloc, extra) = self.get_raw_mut(ptr.alloc_id)?;
663-
M::memory_written(extra, alloc, ptr, size)?;
664+
M::memory_written(extra, &mut alloc.extra, ptr, size)?;
664665
let range = alloc_range(ptr.offset, size);
665666
Ok(Some(AllocRefMut { alloc, range, tcx, alloc_id: ptr.alloc_id }))
666667
} else {
@@ -1029,7 +1030,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
10291030
Some(src_ptr) => src_ptr,
10301031
};
10311032
let src_alloc = self.get_raw(src.alloc_id)?;
1032-
M::memory_read(&self.extra, src_alloc, src, size)?;
1033+
M::memory_read(&self.extra, &src_alloc.extra, src, size)?;
10331034
// We need the `dest` ptr for the next operation, so we get it now.
10341035
// We already did the source checks and called the hooks so we are good to return early.
10351036
let dest = match dest {
@@ -1058,7 +1059,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
10581059

10591060
// Destination alloc preparations and access hooks.
10601061
let (dest_alloc, extra) = self.get_raw_mut(dest.alloc_id)?;
1061-
M::memory_written(extra, dest_alloc, dest, size * num_copies)?;
1062+
M::memory_written(extra, &mut dest_alloc.extra, dest, size * num_copies)?;
10621063
let dest_bytes = dest_alloc
10631064
.get_bytes_mut_ptr(&tcx, alloc_range(dest.offset, size * num_copies))
10641065
.as_mut_ptr();

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ symbols! {
132132
Borrow,
133133
Break,
134134
C,
135+
CStr,
135136
CString,
136137
Center,
137138
Clone,

library/core/src/iter/traits/iterator.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,12 @@ pub trait Iterator {
18491849
///
18501850
/// The relative order of partitioned items is not maintained.
18511851
///
1852+
/// # Current implementation
1853+
/// Current algorithms tries finding the first element for which the predicate evaluates
1854+
/// to false, and the last element for which it evaluates to true and repeatedly swaps them.
1855+
///
1856+
/// Time Complexity: *O*(*N*)
1857+
///
18521858
/// See also [`is_partitioned()`] and [`partition()`].
18531859
///
18541860
/// [`is_partitioned()`]: Iterator::is_partitioned

library/core/src/ptr/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
342342
/// ```
343343
/// use std::ptr;
344344
///
345-
/// let mut array = [0, 1, 2, 3];
345+
/// let mut array: [i32; 4] = [0, 1, 2, 3];
346+
///
347+
/// let array_ptr: *mut i32 = array.as_mut_ptr();
346348
///
347-
/// let x = array[0..].as_mut_ptr() as *mut [u32; 3]; // this is `array[0..3]`
348-
/// let y = array[1..].as_mut_ptr() as *mut [u32; 3]; // this is `array[1..4]`
349+
/// let x = array_ptr as *mut [i32; 3]; // this is `array[0..3]`
350+
/// let y = unsafe { array_ptr.add(1) } as *mut [i32; 3]; // this is `array[1..4]`
349351
///
350352
/// unsafe {
351353
/// ptr::swap(x, y);

library/proc_macro/src/bridge/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ macro_rules! with_api {
107107
Literal {
108108
fn drop($self: $S::Literal);
109109
fn clone($self: &$S::Literal) -> $S::Literal;
110+
fn from_str(s: &str) -> Result<$S::Literal, ()>;
110111
fn debug_kind($self: &$S::Literal) -> String;
111112
fn symbol($self: &$S::Literal) -> String;
112113
fn suffix($self: &$S::Literal) -> Option<String>;
@@ -315,6 +316,19 @@ impl<T: Unmark> Unmark for Option<T> {
315316
}
316317
}
317318

319+
impl<T: Mark, E: Mark> Mark for Result<T, E> {
320+
type Unmarked = Result<T::Unmarked, E::Unmarked>;
321+
fn mark(unmarked: Self::Unmarked) -> Self {
322+
unmarked.map(T::mark).map_err(E::mark)
323+
}
324+
}
325+
impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
326+
type Unmarked = Result<T::Unmarked, E::Unmarked>;
327+
fn unmark(self) -> Self::Unmarked {
328+
self.map(T::unmark).map_err(E::unmark)
329+
}
330+
}
331+
318332
macro_rules! mark_noop {
319333
($($ty:ty),* $(,)?) => {
320334
$(

library/proc_macro/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ pub struct LexError {
9191
_inner: (),
9292
}
9393

94+
impl LexError {
95+
fn new() -> Self {
96+
LexError { _inner: () }
97+
}
98+
}
99+
94100
#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
95101
impl fmt::Display for LexError {
96102
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -1171,6 +1177,28 @@ impl Literal {
11711177
}
11721178
}
11731179

1180+
/// Parse a single literal from its stringified representation.
1181+
///
1182+
/// In order to parse successfully, the input string must not contain anything
1183+
/// but the literal token. Specifically, it must not contain whitespace or
1184+
/// comments in addition to the literal.
1185+
///
1186+
/// The resulting literal token will have a `Span::call_site()` span.
1187+
///
1188+
/// NOTE: some errors may cause panics instead of returning `LexError`. We
1189+
/// reserve the right to change these errors into `LexError`s later.
1190+
#[stable(feature = "proc_macro_literal_parse", since = "1.54.0")]
1191+
impl FromStr for Literal {
1192+
type Err = LexError;
1193+
1194+
fn from_str(src: &str) -> Result<Self, LexError> {
1195+
match bridge::client::Literal::from_str(src) {
1196+
Ok(literal) => Ok(Literal(literal)),
1197+
Err(()) => Err(LexError::new()),
1198+
}
1199+
}
1200+
}
1201+
11741202
// N.B., the bridge only provides `to_string`, implement `fmt::Display`
11751203
// based on it (the reverse of the usual relationship between the two).
11761204
#[stable(feature = "proc_macro_lib", since = "1.15.0")]

library/std/src/ffi/c_str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ pub struct CString {
185185
///
186186
/// [`&str`]: prim@str
187187
#[derive(Hash)]
188+
#[cfg_attr(not(test), rustc_diagnostic_item = "CStr")]
188189
#[stable(feature = "rust1", since = "1.0.0")]
189190
// FIXME:
190191
// `fn from` in `impl From<&CStr> for Box<CStr>` current implementation relies

library/test/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub mod test {
4949
cli::{parse_opts, TestOpts},
5050
filter_tests,
5151
helpers::metrics::{Metric, MetricMap},
52-
options::{Options, RunIgnored, RunStrategy, ShouldPanic},
52+
options::{Concurrent, Options, RunIgnored, RunStrategy, ShouldPanic},
5353
run_test, test_main, test_main_static,
5454
test_result::{TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk},
5555
time::{TestExecTime, TestTimeOptions},

0 commit comments

Comments
 (0)