Skip to content

Commit 76cbf00

Browse files
committed
Auto merge of rust-lang#71151 - Dylan-DPC:rollup-6rt4h7b, r=Dylan-DPC
Rollup of 8 pull requests Successful merges: - rust-lang#70657 (Allow `try`-blocks in places where an open delim is expected) - rust-lang#70947 (tighten CTFE safety net for accesses to globals) - rust-lang#70949 (simplify `vec!` macro) - rust-lang#71002 (fix target & runtool args order) - rust-lang#71082 (ptr: introduce len() method on raw slices) - rust-lang#71128 (Remove unused single_step flag) - rust-lang#71133 (Tighten time complexity on the doc of sort_by_key) - rust-lang#71135 (Update books) Failed merges: r? @ghost
2 parents d12f030 + db3addb commit 76cbf00

25 files changed

+222
-50
lines changed

src/doc/embedded-book

src/liballoc/macros.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ macro_rules! vec {
4242
($elem:expr; $n:expr) => (
4343
$crate::vec::from_elem($elem, $n)
4444
);
45-
($($x:expr),*) => (
46-
<[_]>::into_vec(box [$($x),*])
45+
($($x:expr),+ $(,)?) => (
46+
<[_]>::into_vec(box [$($x),+])
4747
);
48-
($($x:expr,)*) => ($crate::vec![$($x),*])
4948
}
5049

5150
// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is

src/liballoc/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<T> [T] {
254254

255255
/// Sorts the slice with a key extraction function.
256256
///
257-
/// This sort is stable (i.e., does not reorder equal elements) and `O(m n log(m n))`
257+
/// This sort is stable (i.e., does not reorder equal elements) and `O(m n log n)`
258258
/// worst-case, where the key function is `O(m)`.
259259
///
260260
/// For expensive key functions (e.g. functions that are not simple property accesses or

src/libcore/ptr/const_ptr.rs

+28
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,34 @@ impl<T: ?Sized> *const T {
706706
}
707707
}
708708

709+
#[cfg(not(bootstrap))]
710+
#[lang = "const_slice_ptr"]
711+
impl<T> *const [T] {
712+
/// Returns the length of a raw slice.
713+
///
714+
/// The returned value is the number of **elements**, not the number of bytes.
715+
///
716+
/// This function is safe, even when the raw slice cannot be cast to a slice
717+
/// reference because the pointer is null or unaligned.
718+
///
719+
/// # Examples
720+
///
721+
/// ```rust
722+
/// #![feature(slice_ptr_len)]
723+
///
724+
/// use std::ptr;
725+
///
726+
/// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
727+
/// assert_eq!(slice.len(), 3);
728+
/// ```
729+
#[inline]
730+
#[unstable(feature = "slice_ptr_len", issue = "71146")]
731+
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
732+
pub const fn len(self) -> usize {
733+
unsafe { Repr { rust: self }.raw }.len
734+
}
735+
}
736+
709737
// Equality for pointers
710738
#[stable(feature = "rust1", since = "1.0.0")]
711739
impl<T: ?Sized> PartialEq for *const T {

src/libcore/ptr/mut_ptr.rs

+28
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,34 @@ impl<T: ?Sized> *mut T {
894894
}
895895
}
896896

897+
#[cfg(not(bootstrap))]
898+
#[lang = "mut_slice_ptr"]
899+
impl<T> *mut [T] {
900+
/// Returns the length of a raw slice.
901+
///
902+
/// The returned value is the number of **elements**, not the number of bytes.
903+
///
904+
/// This function is safe, even when the raw slice cannot be cast to a slice
905+
/// reference because the pointer is null or unaligned.
906+
///
907+
/// # Examples
908+
///
909+
/// ```rust
910+
/// #![feature(slice_ptr_len)]
911+
///
912+
/// use std::ptr;
913+
///
914+
/// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
915+
/// assert_eq!(slice.len(), 3);
916+
/// ```
917+
#[inline]
918+
#[unstable(feature = "slice_ptr_len", issue = "71146")]
919+
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
920+
pub const fn len(self) -> usize {
921+
unsafe { Repr { rust_mut: self }.raw }.len
922+
}
923+
}
924+
897925
// Equality for pointers
898926
#[stable(feature = "rust1", since = "1.0.0")]
899927
impl<T: ?Sized> PartialEq for *mut T {

src/libcore/slice/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@ impl<T> [T] {
16971697
/// elements.
16981698
///
16991699
/// This sort is unstable (i.e., may reorder equal elements), in-place
1700-
/// (i.e., does not allocate), and `O(m n log(m n))` worst-case, where the key function is
1700+
/// (i.e., does not allocate), and `O(m n log n)` worst-case, where the key function is
17011701
/// `O(m)`.
17021702
///
17031703
/// # Current implementation

src/librustc_expand/expand.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
507507
expanded_fragments.push(Vec::new());
508508
}
509509
expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
510-
if !self.cx.ecfg.single_step {
511-
invocations.extend(new_invocations.into_iter().rev());
512-
}
510+
invocations.extend(new_invocations.into_iter().rev());
513511
}
514512

515513
self.cx.current_expansion = orig_expansion_data;
@@ -1819,7 +1817,6 @@ pub struct ExpansionConfig<'feat> {
18191817
pub recursion_limit: usize,
18201818
pub trace_mac: bool,
18211819
pub should_test: bool, // If false, strip `#[test]` nodes
1822-
pub single_step: bool,
18231820
pub keep_macs: bool,
18241821
}
18251822

@@ -1831,7 +1828,6 @@ impl<'feat> ExpansionConfig<'feat> {
18311828
recursion_limit: 1024,
18321829
trace_mac: false,
18331830
should_test: false,
1834-
single_step: false,
18351831
keep_macs: false,
18361832
}
18371833
}

src/librustc_hir/lang_items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ language_item_table! {
135135
SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl, Target::Impl;
136136
ConstPtrImplItem, "const_ptr", const_ptr_impl, Target::Impl;
137137
MutPtrImplItem, "mut_ptr", mut_ptr_impl, Target::Impl;
138+
ConstSlicePtrImplItem, "const_slice_ptr", const_slice_ptr_impl, Target::Impl;
139+
MutSlicePtrImplItem, "mut_slice_ptr", mut_slice_ptr_impl, Target::Impl;
138140
I8ImplItem, "i8", i8_impl, Target::Impl;
139141
I16ImplItem, "i16", i16_impl, Target::Impl;
140142
I32ImplItem, "i32", i32_impl, Target::Impl;

src/librustc_lint/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ trait UnusedDelimLint {
384384
fn is_expr_delims_necessary(inner: &ast::Expr, followed_by_block: bool) -> bool {
385385
followed_by_block
386386
&& match inner.kind {
387-
ast::ExprKind::Ret(_) | ast::ExprKind::Break(..) => true,
387+
ExprKind::Ret(_) | ExprKind::Break(..) => true,
388388
_ => parser::contains_exterior_struct_lit(&inner),
389389
}
390390
}

src/librustc_mir/const_eval/machine.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,30 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
353353
static_def_id: Option<DefId>,
354354
is_write: bool,
355355
) -> InterpResult<'tcx> {
356-
if is_write && allocation.mutability == Mutability::Not {
357-
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
358-
} else if is_write {
359-
Err(ConstEvalErrKind::ModifiedGlobal.into())
360-
} else if memory_extra.can_access_statics || static_def_id.is_none() {
361-
// `static_def_id.is_none()` indicates this is not a static, but a const or so.
362-
Ok(())
356+
if is_write {
357+
// Write access. These are never allowed, but we give a targeted error message.
358+
if allocation.mutability == Mutability::Not {
359+
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
360+
} else {
361+
Err(ConstEvalErrKind::ModifiedGlobal.into())
362+
}
363363
} else {
364-
Err(ConstEvalErrKind::ConstAccessesStatic.into())
364+
// Read access. These are usually allowed, with some exceptions.
365+
if memory_extra.can_access_statics {
366+
// Machine configuration allows us read from anything (e.g., `static` initializer).
367+
Ok(())
368+
} else if static_def_id.is_some() {
369+
// Machine configuration does not allow us to read statics
370+
// (e.g., `const` initializer).
371+
Err(ConstEvalErrKind::ConstAccessesStatic.into())
372+
} else {
373+
// Immutable global, this read is fine.
374+
// But make sure we never accept a read from something mutable, that would be
375+
// unsound. The reason is that as the content of this allocation may be different
376+
// now and at run-time, so if we permit reading now we might return the wrong value.
377+
assert_eq!(allocation.mutability, Mutability::Not);
378+
Ok(())
379+
}
365380
}
366381
}
367382
}

src/librustc_parse/parser/expr.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1846,11 +1846,9 @@ impl<'a> Parser<'a> {
18461846
}
18471847

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

18561854
/// Parses an `async move? {...}` expression.

src/librustc_typeck/check/method/probe.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
649649
}
650650
}
651651
ty::RawPtr(ty::TypeAndMut { ty: _, mutbl }) => {
652-
let lang_def_id = match mutbl {
653-
hir::Mutability::Not => lang_items.const_ptr_impl(),
654-
hir::Mutability::Mut => lang_items.mut_ptr_impl(),
652+
let (lang_def_id1, lang_def_id2) = match mutbl {
653+
hir::Mutability::Not => {
654+
(lang_items.const_ptr_impl(), lang_items.const_slice_ptr_impl())
655+
}
656+
hir::Mutability::Mut => {
657+
(lang_items.mut_ptr_impl(), lang_items.mut_slice_ptr_impl())
658+
}
655659
};
656-
self.assemble_inherent_impl_for_primitive(lang_def_id);
660+
self.assemble_inherent_impl_for_primitive(lang_def_id1);
661+
self.assemble_inherent_impl_for_primitive(lang_def_id2);
657662
}
658663
ty::Int(i) => {
659664
let lang_def_id = match i {

src/librustc_typeck/coherence/inherent_impls.rs

+24
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
112112
item.span,
113113
);
114114
}
115+
ty::RawPtr(ty::TypeAndMut { ty: inner, mutbl: hir::Mutability::Not })
116+
if matches!(inner.kind, ty::Slice(_)) =>
117+
{
118+
self.check_primitive_impl(
119+
def_id,
120+
lang_items.const_slice_ptr_impl(),
121+
None,
122+
"const_slice_ptr",
123+
"*const [T]",
124+
item.span,
125+
);
126+
}
127+
ty::RawPtr(ty::TypeAndMut { ty: inner, mutbl: hir::Mutability::Mut })
128+
if matches!(inner.kind, ty::Slice(_)) =>
129+
{
130+
self.check_primitive_impl(
131+
def_id,
132+
lang_items.mut_slice_ptr_impl(),
133+
None,
134+
"mut_slice_ptr",
135+
"*mut [T]",
136+
item.span,
137+
);
138+
}
115139
ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Not }) => {
116140
self.check_primitive_impl(
117141
def_id,

src/librustdoc/passes/collect_trait_impls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
6262
lang_items.slice_u8_alloc_impl(),
6363
lang_items.const_ptr_impl(),
6464
lang_items.mut_ptr_impl(),
65+
lang_items.const_slice_ptr_impl(),
66+
lang_items.mut_slice_ptr_impl(),
6567
];
6668

6769
for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) {

src/librustdoc/test.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@ fn run_test(
263263
if no_run && !compile_fail {
264264
compiler.arg("--emit=metadata");
265265
}
266-
compiler.arg("--target").arg(target.to_string());
266+
compiler.arg("--target").arg(match target {
267+
TargetTriple::TargetTriple(s) => s,
268+
TargetTriple::TargetPath(path) => {
269+
path.to_str().expect("target path must be valid unicode").to_string()
270+
}
271+
});
267272

268273
compiler.arg("-");
269274
compiler.stdin(Stdio::piped());
@@ -312,8 +317,8 @@ fn run_test(
312317

313318
if let Some(tool) = runtool {
314319
cmd = Command::new(tool);
315-
cmd.arg(output_file);
316320
cmd.args(runtool_args);
321+
cmd.arg(output_file);
317322
} else {
318323
cmd = Command::new(output_file);
319324
}
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// run-pass
12
// compile-flags: --edition 2018
23

34
#![feature(try_blocks)]
45

56
fn main() {
6-
match try { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `try`
7+
match try { } {
8+
Err(()) => (),
9+
Ok(()) => (),
10+
}
711
}

src/test/ui/try-block/try-block-in-match.stderr

-10
This file was deleted.

src/test/ui/try-block/try-block-in-while.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
#![feature(try_blocks)]
44

55
fn main() {
6-
while try { false } {} //~ ERROR expected expression, found reserved keyword `try`
6+
while try { false } {}
7+
//~^ ERROR the trait bound `bool: std::ops::Try` is not satisfied
78
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: expected expression, found reserved keyword `try`
2-
--> $DIR/try-block-in-while.rs:6:11
1+
error[E0277]: the trait bound `bool: std::ops::Try` is not satisfied
2+
--> $DIR/try-block-in-while.rs:6:15
33
|
44
LL | while try { false } {}
5-
| ^^^ expected expression
5+
| ^^^^^^^^^ the trait `std::ops::Try` is not implemented for `bool`
6+
|
7+
= note: required by `std::ops::Try::from_ok`
68

79
error: aborting due to previous error
810

11+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// check-pass
2+
// compile-flags: --edition 2018
3+
4+
#![feature(try_blocks)]
5+
#![warn(unused_parens, unused_braces)]
6+
7+
fn consume<T>(_: Result<T, T>) -> T { todo!() }
8+
9+
fn main() {
10+
consume((try {}));
11+
//~^ WARN unnecessary parentheses
12+
13+
consume({ try {} });
14+
//~^ WARN unnecessary braces
15+
16+
match (try {}) {
17+
//~^ WARN unnecessary parentheses
18+
Ok(()) | Err(()) => (),
19+
}
20+
21+
if let Err(()) = (try {}) {}
22+
//~^ WARN unnecessary parentheses
23+
24+
match (try {}) {
25+
//~^ WARN unnecessary parentheses
26+
Ok(()) | Err(()) => (),
27+
}
28+
}

0 commit comments

Comments
 (0)