Skip to content

Commit

Permalink
Auto merge of #71151 - Dylan-DPC:rollup-6rt4h7b, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Rollup of 8 pull requests

Successful merges:

 - #70657 (Allow `try`-blocks in places where an open delim is expected)
 - #70947 (tighten CTFE safety net for accesses to globals)
 - #70949 (simplify `vec!` macro)
 - #71002 (fix target & runtool args order)
 - #71082 (ptr: introduce len() method on raw slices)
 - #71128 (Remove unused single_step flag)
 - #71133 (Tighten time complexity on the doc of sort_by_key)
 - #71135 (Update books)

Failed merges:

r? @ghost
  • Loading branch information
bors committed Apr 15, 2020
2 parents d12f030 + db3addb commit 76cbf00
Show file tree
Hide file tree
Showing 25 changed files with 222 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/doc/embedded-book
Submodule embedded-book updated 1 files
+1 −0 triagebot.toml
2 changes: 1 addition & 1 deletion src/doc/rust-by-example
5 changes: 2 additions & 3 deletions src/liballoc/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ macro_rules! vec {
($elem:expr; $n:expr) => (
$crate::vec::from_elem($elem, $n)
);
($($x:expr),*) => (
<[_]>::into_vec(box [$($x),*])
($($x:expr),+ $(,)?) => (
<[_]>::into_vec(box [$($x),+])
);
($($x:expr,)*) => ($crate::vec![$($x),*])
}

// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<T> [T] {

/// Sorts the slice with a key extraction function.
///
/// This sort is stable (i.e., does not reorder equal elements) and `O(m n log(m n))`
/// This sort is stable (i.e., does not reorder equal elements) and `O(m n log n)`
/// worst-case, where the key function is `O(m)`.
///
/// For expensive key functions (e.g. functions that are not simple property accesses or
Expand Down
28 changes: 28 additions & 0 deletions src/libcore/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,34 @@ impl<T: ?Sized> *const T {
}
}

#[cfg(not(bootstrap))]
#[lang = "const_slice_ptr"]
impl<T> *const [T] {
/// Returns the length of a raw slice.
///
/// The returned value is the number of **elements**, not the number of bytes.
///
/// This function is safe, even when the raw slice cannot be cast to a slice
/// reference because the pointer is null or unaligned.
///
/// # Examples
///
/// ```rust
/// #![feature(slice_ptr_len)]
///
/// use std::ptr;
///
/// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
/// assert_eq!(slice.len(), 3);
/// ```
#[inline]
#[unstable(feature = "slice_ptr_len", issue = "71146")]
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
pub const fn len(self) -> usize {
unsafe { Repr { rust: self }.raw }.len
}
}

// Equality for pointers
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> PartialEq for *const T {
Expand Down
28 changes: 28 additions & 0 deletions src/libcore/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,34 @@ impl<T: ?Sized> *mut T {
}
}

#[cfg(not(bootstrap))]
#[lang = "mut_slice_ptr"]
impl<T> *mut [T] {
/// Returns the length of a raw slice.
///
/// The returned value is the number of **elements**, not the number of bytes.
///
/// This function is safe, even when the raw slice cannot be cast to a slice
/// reference because the pointer is null or unaligned.
///
/// # Examples
///
/// ```rust
/// #![feature(slice_ptr_len)]
///
/// use std::ptr;
///
/// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
/// assert_eq!(slice.len(), 3);
/// ```
#[inline]
#[unstable(feature = "slice_ptr_len", issue = "71146")]
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
pub const fn len(self) -> usize {
unsafe { Repr { rust_mut: self }.raw }.len
}
}

// Equality for pointers
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> PartialEq for *mut T {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ impl<T> [T] {
/// elements.
///
/// This sort is unstable (i.e., may reorder equal elements), in-place
/// (i.e., does not allocate), and `O(m n log(m n))` worst-case, where the key function is
/// (i.e., does not allocate), and `O(m n log n)` worst-case, where the key function is
/// `O(m)`.
///
/// # Current implementation
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_expand/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
expanded_fragments.push(Vec::new());
}
expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
if !self.cx.ecfg.single_step {
invocations.extend(new_invocations.into_iter().rev());
}
invocations.extend(new_invocations.into_iter().rev());
}

self.cx.current_expansion = orig_expansion_data;
Expand Down Expand Up @@ -1819,7 +1817,6 @@ pub struct ExpansionConfig<'feat> {
pub recursion_limit: usize,
pub trace_mac: bool,
pub should_test: bool, // If false, strip `#[test]` nodes
pub single_step: bool,
pub keep_macs: bool,
}

Expand All @@ -1831,7 +1828,6 @@ impl<'feat> ExpansionConfig<'feat> {
recursion_limit: 1024,
trace_mac: false,
should_test: false,
single_step: false,
keep_macs: false,
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_hir/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ language_item_table! {
SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl, Target::Impl;
ConstPtrImplItem, "const_ptr", const_ptr_impl, Target::Impl;
MutPtrImplItem, "mut_ptr", mut_ptr_impl, Target::Impl;
ConstSlicePtrImplItem, "const_slice_ptr", const_slice_ptr_impl, Target::Impl;
MutSlicePtrImplItem, "mut_slice_ptr", mut_slice_ptr_impl, Target::Impl;
I8ImplItem, "i8", i8_impl, Target::Impl;
I16ImplItem, "i16", i16_impl, Target::Impl;
I32ImplItem, "i32", i32_impl, Target::Impl;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ trait UnusedDelimLint {
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
followed_by_block
&& match inner.kind {
ast::ExprKind::Ret(_) | ast::ExprKind::Break(..) => true,
ExprKind::Ret(_) | ExprKind::Break(..) => true,
_ => parser::contains_exterior_struct_lit(&inner),
}
}
Expand Down
31 changes: 23 additions & 8 deletions src/librustc_mir/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,30 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
static_def_id: Option<DefId>,
is_write: bool,
) -> InterpResult<'tcx> {
if is_write && allocation.mutability == Mutability::Not {
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
} else if is_write {
Err(ConstEvalErrKind::ModifiedGlobal.into())
} else if memory_extra.can_access_statics || static_def_id.is_none() {
// `static_def_id.is_none()` indicates this is not a static, but a const or so.
Ok(())
if is_write {
// Write access. These are never allowed, but we give a targeted error message.
if allocation.mutability == Mutability::Not {
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
} else {
Err(ConstEvalErrKind::ModifiedGlobal.into())
}
} else {
Err(ConstEvalErrKind::ConstAccessesStatic.into())
// Read access. These are usually allowed, with some exceptions.
if memory_extra.can_access_statics {
// Machine configuration allows us read from anything (e.g., `static` initializer).
Ok(())
} else if static_def_id.is_some() {
// Machine configuration does not allow us to read statics
// (e.g., `const` initializer).
Err(ConstEvalErrKind::ConstAccessesStatic.into())
} else {
// Immutable global, this read is fine.
// But make sure we never accept a read from something mutable, that would be
// unsound. The reason is that as the content of this allocation may be different
// now and at run-time, so if we permit reading now we might return the wrong value.
assert_eq!(allocation.mutability, Mutability::Not);
Ok(())
}
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1846,11 +1846,9 @@ impl<'a> Parser<'a> {
}

fn is_try_block(&self) -> bool {
self.token.is_keyword(kw::Try) &&
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
self.token.uninterpolated_span().rust_2018() &&
// Prevent `while try {} {}`, `if try {} {} else {}`, etc.
!self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
self.token.is_keyword(kw::Try)
&& self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace))
&& self.token.uninterpolated_span().rust_2018()
}

/// Parses an `async move? {...}` expression.
Expand Down
13 changes: 9 additions & 4 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
}
}
ty::RawPtr(ty::TypeAndMut { ty: _, mutbl }) => {
let lang_def_id = match mutbl {
hir::Mutability::Not => lang_items.const_ptr_impl(),
hir::Mutability::Mut => lang_items.mut_ptr_impl(),
let (lang_def_id1, lang_def_id2) = match mutbl {
hir::Mutability::Not => {
(lang_items.const_ptr_impl(), lang_items.const_slice_ptr_impl())
}
hir::Mutability::Mut => {
(lang_items.mut_ptr_impl(), lang_items.mut_slice_ptr_impl())
}
};
self.assemble_inherent_impl_for_primitive(lang_def_id);
self.assemble_inherent_impl_for_primitive(lang_def_id1);
self.assemble_inherent_impl_for_primitive(lang_def_id2);
}
ty::Int(i) => {
let lang_def_id = match i {
Expand Down
24 changes: 24 additions & 0 deletions src/librustc_typeck/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
item.span,
);
}
ty::RawPtr(ty::TypeAndMut { ty: inner, mutbl: hir::Mutability::Not })
if matches!(inner.kind, ty::Slice(_)) =>
{
self.check_primitive_impl(
def_id,
lang_items.const_slice_ptr_impl(),
None,
"const_slice_ptr",
"*const [T]",
item.span,
);
}
ty::RawPtr(ty::TypeAndMut { ty: inner, mutbl: hir::Mutability::Mut })
if matches!(inner.kind, ty::Slice(_)) =>
{
self.check_primitive_impl(
def_id,
lang_items.mut_slice_ptr_impl(),
None,
"mut_slice_ptr",
"*mut [T]",
item.span,
);
}
ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Not }) => {
self.check_primitive_impl(
def_id,
Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/passes/collect_trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
lang_items.slice_u8_alloc_impl(),
lang_items.const_ptr_impl(),
lang_items.mut_ptr_impl(),
lang_items.const_slice_ptr_impl(),
lang_items.mut_slice_ptr_impl(),
];

for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) {
Expand Down
9 changes: 7 additions & 2 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@ fn run_test(
if no_run && !compile_fail {
compiler.arg("--emit=metadata");
}
compiler.arg("--target").arg(target.to_string());
compiler.arg("--target").arg(match target {
TargetTriple::TargetTriple(s) => s,
TargetTriple::TargetPath(path) => {
path.to_str().expect("target path must be valid unicode").to_string()
}
});

compiler.arg("-");
compiler.stdin(Stdio::piped());
Expand Down Expand Up @@ -312,8 +317,8 @@ fn run_test(

if let Some(tool) = runtool {
cmd = Command::new(tool);
cmd.arg(output_file);
cmd.args(runtool_args);
cmd.arg(output_file);
} else {
cmd = Command::new(output_file);
}
Expand Down
6 changes: 5 additions & 1 deletion src/test/ui/try-block/try-block-in-match.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// run-pass
// compile-flags: --edition 2018

#![feature(try_blocks)]

fn main() {
match try { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `try`
match try { } {
Err(()) => (),
Ok(()) => (),
}
}
10 changes: 0 additions & 10 deletions src/test/ui/try-block/try-block-in-match.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion src/test/ui/try-block/try-block-in-while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
#![feature(try_blocks)]

fn main() {
while try { false } {} //~ ERROR expected expression, found reserved keyword `try`
while try { false } {}
//~^ ERROR the trait bound `bool: std::ops::Try` is not satisfied
}
9 changes: 6 additions & 3 deletions src/test/ui/try-block/try-block-in-while.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
error: expected expression, found reserved keyword `try`
--> $DIR/try-block-in-while.rs:6:11
error[E0277]: the trait bound `bool: std::ops::Try` is not satisfied
--> $DIR/try-block-in-while.rs:6:15
|
LL | while try { false } {}
| ^^^ expected expression
| ^^^^^^^^^ the trait `std::ops::Try` is not implemented for `bool`
|
= note: required by `std::ops::Try::from_ok`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
28 changes: 28 additions & 0 deletions src/test/ui/try-block/try-block-unused-delims.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// check-pass
// compile-flags: --edition 2018

#![feature(try_blocks)]
#![warn(unused_parens, unused_braces)]

fn consume<T>(_: Result<T, T>) -> T { todo!() }

fn main() {
consume((try {}));
//~^ WARN unnecessary parentheses

consume({ try {} });
//~^ WARN unnecessary braces

match (try {}) {
//~^ WARN unnecessary parentheses
Ok(()) | Err(()) => (),
}

if let Err(()) = (try {}) {}
//~^ WARN unnecessary parentheses

match (try {}) {
//~^ WARN unnecessary parentheses
Ok(()) | Err(()) => (),
}
}
Loading

0 comments on commit 76cbf00

Please sign in to comment.