Skip to content

Commit

Permalink
Auto merge of rust-lang#85486 - RalfJung:rollup-4ibcxuu, r=RalfJung
Browse files Browse the repository at this point in the history
Rollup of 8 pull requests

Successful merges:

 - rust-lang#84717 (impl FromStr for proc_macro::Literal)
 - rust-lang#85169 (Add method-toggle to <details> for methods)
 - rust-lang#85287 (Expose `Concurrent` (private type in public i'face))
 - rust-lang#85315 (adding time complexity for partition_in_place iter method)
 - rust-lang#85439 (Add diagnostic item to `CStr`)
 - rust-lang#85464 (Fix UB in documented example for `ptr::swap`)
 - rust-lang#85470 (Fix invalid CSS rules for a:hover)
 - rust-lang#85472 (CTFE Machine: do not expose Allocation)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed May 20, 2021
2 parents 5ab0f37 + 9fa15ff commit 35bab92
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 48 deletions.
31 changes: 28 additions & 3 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::base::{ExtCtxt, ResolverExpand};

use rustc_ast as ast;
use rustc_ast::token;
use rustc_ast::token::Nonterminal;
use rustc_ast::token::NtIdent;
use rustc_ast::token::{self, Nonterminal, NtIdent, TokenKind};
use rustc_ast::tokenstream::{self, CanSynthesizeMissingTokens};
use rustc_ast::tokenstream::{DelimSpan, Spacing::*, TokenStream, TreeAndSpacing};
use rustc_ast_pretty::pprust;
Expand Down Expand Up @@ -541,6 +539,33 @@ impl server::Ident for Rustc<'_> {
}

impl server::Literal for Rustc<'_> {
fn from_str(&mut self, s: &str) -> Result<Self::Literal, ()> {
let override_span = None;
let stream = parse_stream_from_source_str(
FileName::proc_macro_source_code(s),
s.to_owned(),
self.sess,
override_span,
);
if stream.len() != 1 {
return Err(());
}
let tree = stream.into_trees().next().unwrap();
let token = match tree {
tokenstream::TokenTree::Token(token) => token,
tokenstream::TokenTree::Delimited { .. } => return Err(()),
};
let span_data = token.span.data();
if (span_data.hi.0 - span_data.lo.0) as usize != s.len() {
// There is a comment or whitespace adjacent to the literal.
return Err(());
}
let lit = match token.kind {
TokenKind::Literal(lit) => lit,
_ => return Err(()),
};
Ok(Literal { lit, span: self.call_site })
}
fn debug_kind(&mut self, literal: &Self::Literal) -> String {
format!("{:?}", literal.lit.kind)
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_mir/src/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
#[inline(always)]
fn memory_read(
_memory_extra: &Self::MemoryExtra,
_alloc: &Allocation<Self::PointerTag, Self::AllocExtra>,
_alloc_extra: &Self::AllocExtra,
_ptr: Pointer<Self::PointerTag>,
_size: Size,
) -> InterpResult<'tcx> {
Expand All @@ -324,7 +324,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
#[inline(always)]
fn memory_written(
_memory_extra: &mut Self::MemoryExtra,
_alloc: &mut Allocation<Self::PointerTag, Self::AllocExtra>,
_alloc_extra: &mut Self::AllocExtra,
_ptr: Pointer<Self::PointerTag>,
_size: Size,
) -> InterpResult<'tcx> {
Expand All @@ -335,8 +335,9 @@ pub trait Machine<'mir, 'tcx>: Sized {
#[inline(always)]
fn memory_deallocated(
_memory_extra: &mut Self::MemoryExtra,
_alloc: &mut Allocation<Self::PointerTag, Self::AllocExtra>,
_alloc_extra: &mut Self::AllocExtra,
_ptr: Pointer<Self::PointerTag>,
_size: Size,
) -> InterpResult<'tcx> {
Ok(())
}
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_mir/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
}

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

// Don't forget to remember size and align of this now-dead allocation
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size(), alloc.align));
let old = self.dead_alloc_map.insert(ptr.alloc_id, (size, alloc.align));
if old.is_some() {
bug!("Nothing can be deallocated twice");
}
Expand Down Expand Up @@ -591,7 +592,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
},
)?;
if let Some((ptr, alloc)) = ptr_and_alloc {
M::memory_read(&self.extra, alloc, ptr, size)?;
M::memory_read(&self.extra, &alloc.extra, ptr, size)?;
let range = alloc_range(ptr.offset, size);
Ok(Some(AllocRef { alloc, range, tcx: self.tcx, alloc_id: ptr.alloc_id }))
} else {
Expand Down Expand Up @@ -660,7 +661,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
// FIXME: can we somehow avoid looking up the allocation twice here?
// We cannot call `get_raw_mut` inside `check_and_deref_ptr` as that would duplicate `&mut self`.
let (alloc, extra) = self.get_raw_mut(ptr.alloc_id)?;
M::memory_written(extra, alloc, ptr, size)?;
M::memory_written(extra, &mut alloc.extra, ptr, size)?;
let range = alloc_range(ptr.offset, size);
Ok(Some(AllocRefMut { alloc, range, tcx, alloc_id: ptr.alloc_id }))
} else {
Expand Down Expand Up @@ -1029,7 +1030,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
Some(src_ptr) => src_ptr,
};
let src_alloc = self.get_raw(src.alloc_id)?;
M::memory_read(&self.extra, src_alloc, src, size)?;
M::memory_read(&self.extra, &src_alloc.extra, src, size)?;
// We need the `dest` ptr for the next operation, so we get it now.
// We already did the source checks and called the hooks so we are good to return early.
let dest = match dest {
Expand Down Expand Up @@ -1058,7 +1059,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

// Destination alloc preparations and access hooks.
let (dest_alloc, extra) = self.get_raw_mut(dest.alloc_id)?;
M::memory_written(extra, dest_alloc, dest, size * num_copies)?;
M::memory_written(extra, &mut dest_alloc.extra, dest, size * num_copies)?;
let dest_bytes = dest_alloc
.get_bytes_mut_ptr(&tcx, alloc_range(dest.offset, size * num_copies))
.as_mut_ptr();
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ symbols! {
Borrow,
Break,
C,
CStr,
CString,
Center,
Clone,
Expand Down
6 changes: 6 additions & 0 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,12 @@ pub trait Iterator {
///
/// The relative order of partitioned items is not maintained.
///
/// # Current implementation
/// Current algorithms tries finding the first element for which the predicate evaluates
/// to false, and the last element for which it evaluates to true and repeatedly swaps them.
///
/// Time Complexity: *O*(*N*)
///
/// See also [`is_partitioned()`] and [`partition()`].
///
/// [`is_partitioned()`]: Iterator::is_partitioned
Expand Down
8 changes: 5 additions & 3 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,12 @@ pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
/// ```
/// use std::ptr;
///
/// let mut array = [0, 1, 2, 3];
/// let mut array: [i32; 4] = [0, 1, 2, 3];
///
/// let array_ptr: *mut i32 = array.as_mut_ptr();
///
/// let x = array[0..].as_mut_ptr() as *mut [u32; 3]; // this is `array[0..3]`
/// let y = array[1..].as_mut_ptr() as *mut [u32; 3]; // this is `array[1..4]`
/// let x = array_ptr as *mut [i32; 3]; // this is `array[0..3]`
/// let y = unsafe { array_ptr.add(1) } as *mut [i32; 3]; // this is `array[1..4]`
///
/// unsafe {
/// ptr::swap(x, y);
Expand Down
14 changes: 14 additions & 0 deletions library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ macro_rules! with_api {
Literal {
fn drop($self: $S::Literal);
fn clone($self: &$S::Literal) -> $S::Literal;
fn from_str(s: &str) -> Result<$S::Literal, ()>;
fn debug_kind($self: &$S::Literal) -> String;
fn symbol($self: &$S::Literal) -> String;
fn suffix($self: &$S::Literal) -> Option<String>;
Expand Down Expand Up @@ -315,6 +316,19 @@ impl<T: Unmark> Unmark for Option<T> {
}
}

impl<T: Mark, E: Mark> Mark for Result<T, E> {
type Unmarked = Result<T::Unmarked, E::Unmarked>;
fn mark(unmarked: Self::Unmarked) -> Self {
unmarked.map(T::mark).map_err(E::mark)
}
}
impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
type Unmarked = Result<T::Unmarked, E::Unmarked>;
fn unmark(self) -> Self::Unmarked {
self.map(T::unmark).map_err(E::unmark)
}
}

macro_rules! mark_noop {
($($ty:ty),* $(,)?) => {
$(
Expand Down
28 changes: 28 additions & 0 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ pub struct LexError {
_inner: (),
}

impl LexError {
fn new() -> Self {
LexError { _inner: () }
}
}

#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
impl fmt::Display for LexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -1171,6 +1177,28 @@ impl Literal {
}
}

/// Parse a single literal from its stringified representation.
///
/// In order to parse successfully, the input string must not contain anything
/// but the literal token. Specifically, it must not contain whitespace or
/// comments in addition to the literal.
///
/// The resulting literal token will have a `Span::call_site()` span.
///
/// NOTE: some errors may cause panics instead of returning `LexError`. We
/// reserve the right to change these errors into `LexError`s later.
#[stable(feature = "proc_macro_literal_parse", since = "1.54.0")]
impl FromStr for Literal {
type Err = LexError;

fn from_str(src: &str) -> Result<Self, LexError> {
match bridge::client::Literal::from_str(src) {
Ok(literal) => Ok(Literal(literal)),
Err(()) => Err(LexError::new()),
}
}
}

// N.B., the bridge only provides `to_string`, implement `fmt::Display`
// based on it (the reverse of the usual relationship between the two).
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
Expand Down
1 change: 1 addition & 0 deletions library/std/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub struct CString {
///
/// [`&str`]: prim@str
#[derive(Hash)]
#[cfg_attr(not(test), rustc_diagnostic_item = "CStr")]
#[stable(feature = "rust1", since = "1.0.0")]
// FIXME:
// `fn from` in `impl From<&CStr> for Box<CStr>` current implementation relies
Expand Down
2 changes: 1 addition & 1 deletion library/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub mod test {
cli::{parse_opts, TestOpts},
filter_tests,
helpers::metrics::{Metric, MetricMap},
options::{Options, RunIgnored, RunStrategy, ShouldPanic},
options::{Concurrent, Options, RunIgnored, RunStrategy, ShouldPanic},
run_test, test_main, test_main_static,
test_result::{TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk},
time::{TestExecTime, TestTimeOptions},
Expand Down
9 changes: 6 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,8 +1352,11 @@ fn render_impl(
}
let w = if short_documented && trait_.is_some() { interesting } else { boring };

if !doc_buffer.is_empty() {
w.write_str("<details class=\"rustdoc-toggle\" open><summary>");
let toggled = !doc_buffer.is_empty();
if toggled {
let method_toggle_class =
if item_type == ItemType::Method { " method-toggle" } else { "" };
write!(w, "<details class=\"rustdoc-toggle{}\" open><summary>", method_toggle_class);
}
match *item.kind {
clean::MethodItem(..) | clean::TyMethodItem(_) => {
Expand Down Expand Up @@ -1453,7 +1456,7 @@ fn render_impl(
}

w.push_buffer(info_buffer);
if !doc_buffer.is_empty() {
if toggled {
w.write_str("</summary>");
w.push_buffer(doc_buffer);
w.push_str("</details>");
Expand Down
16 changes: 4 additions & 12 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,24 +924,16 @@ function hideThemeButtonState() {
});
}

if (hideMethodDocs) {
onEachLazy(document.getElementsByClassName("method"), function(e) {
var toggle = e.parentNode;
if (toggle) {
toggle = toggle.parentNode;
}
if (toggle && toggle.tagName === "DETAILS") {
toggle.open = false;
}
});
}

onEachLazy(document.getElementsByTagName("details"), function (e) {
var showLargeItem = !hideLargeItemContents && hasClass(e, "type-contents-toggle");
var showImplementor = !hideImplementors && hasClass(e, "implementors-toggle");
if (showLargeItem || showImplementor) {
e.open = true;
}
if (hideMethodDocs && hasClass(e, "method-toggle")) {
e.open = false;
}

});

var currentType = document.getElementsByClassName("type-decl")[0];
Expand Down
7 changes: 2 additions & 5 deletions src/librustdoc/html/static/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,14 @@ pre, .rustdoc.source .example-wrap {
color: #c5c5c5;
}

.content a:hover {
.search-results a:hover {
background-color: #777;
}

.content a:focus {
.search-results a:focus {
color: #000 !important;
background-color: #c6afb3;
}
.content a:focus {
color: #000 !important;
}
.search-results a {
color: #0096cf;
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ pre, .rustdoc.source .example-wrap {
color: #ddd;
}

.content a:hover {
.search-results a:hover {
background-color: #777;
}

.content a:focus {
.search-results a:focus {
color: #eee !important;
background-color: #616161;
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ pre, .rustdoc.source .example-wrap {
color: #4E4C4C;
}

.content a:hover {
.search-results a:hover {
background-color: #ddd;
}

.content a:focus {
.search-results a:focus {
color: #000 !important;
background-color: #ccc;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![feature(proc_macro_span)]
use proc_macro::{LineColumn, Punct, Spacing};

use proc_macro::{LineColumn, Punct};
pub fn test() {
test_line_column_ord();
test_punct_eq();
}

#[test]
fn test_line_column_ord() {
let line0_column0 = LineColumn { line: 0, column: 0 };
let line0_column1 = LineColumn { line: 0, column: 1 };
Expand All @@ -11,10 +13,9 @@ fn test_line_column_ord() {
assert!(line0_column1 < line1_column0);
}

#[test]
fn test_punct_eq() {
// Good enough if it typechecks, since proc_macro::Punct can't exist in a test.
fn _check(punct: Punct) {
let _ = punct == ':';
}
let colon_alone = Punct::new(':', Spacing::Alone);
assert_eq!(colon_alone, ':');
let colon_joint = Punct::new(':', Spacing::Joint);
assert_eq!(colon_joint, ':');
}
Loading

0 comments on commit 35bab92

Please sign in to comment.