Skip to content

Commit 1902d1e

Browse files
committed
Auto merge of #70275 - Dylan-DPC:rollup-1fbosob, r=Dylan-DPC
Rollup of 10 pull requests Successful merges: - #68099 (Amend Rc/Arc::from_raw() docs regarding unsafety) - #70172 (parse/lexer: support `StringReader::retokenize` called on external files.) - #70209 (parser: recover on `for<'a> |...| body` closures) - #70223 (fix type of const params in associated types.) - #70229 (more clippy fixes) - #70240 (Return NonZeroU64 from ThreadId::as_u64.) - #70250 (Remove wrong entry from RELEASES.md) - #70253 (Remove another wrong entry from RELEASES.md) - #70254 (couple more clippy fixes (let_and_return, if_same_then_else)) - #70266 (proc_macro_harness: Use item header spans for errors) Failed merges: r? @ghost
2 parents 5ae85f4 + 69c0bcd commit 1902d1e

File tree

58 files changed

+321
-255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+321
-255
lines changed

RELEASES.md

-3
Original file line numberDiff line numberDiff line change
@@ -4838,7 +4838,6 @@ Version 1.11.0 (2016-08-18)
48384838
Language
48394839
--------
48404840

4841-
* [`cfg_attr` works on `path` attributes](https://github.com/rust-lang/rust/pull/34546)
48424841
* [Support nested `cfg_attr` attributes](https://github.com/rust-lang/rust/pull/34216)
48434842
* [Allow statement-generating braced macro invocations at the end of blocks](https://github.com/rust-lang/rust/pull/34436)
48444843
* [Macros can be expanded inside of trait definitions](https://github.com/rust-lang/rust/pull/34213)
@@ -4957,8 +4956,6 @@ Version 1.10.0 (2016-07-07)
49574956
Language
49584957
--------
49594958

4960-
* [Allow `concat_idents!` in type positions as well as in expression
4961-
positions](https://github.com/rust-lang/rust/pull/33735).
49624959
* [`Copy` types are required to have a trivial implementation of `Clone`](https://github.com/rust-lang/rust/pull/33420).
49634960
[RFC 1521](https://github.com/rust-lang/rfcs/blob/master/text/1521-copy-clone-semantics.md).
49644961
* [Single-variant enums support the `#[repr(..)]` attribute](https://github.com/rust-lang/rust/pull/33355).

src/liballoc/rc.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ use crate::vec::Vec;
259259
#[cfg(test)]
260260
mod tests;
261261

262+
// This is repr(C) to future-proof against possible field-reordering, which
263+
// would interfere with otherwise safe [into|from]_raw() of transmutable
264+
// inner types.
265+
#[repr(C)]
262266
struct RcBox<T: ?Sized> {
263267
strong: Cell<usize>,
264268
weak: Cell<usize>,
@@ -580,15 +584,24 @@ impl<T: ?Sized> Rc<T> {
580584
}
581585
}
582586

583-
/// Constructs an `Rc` from a raw pointer.
587+
/// Constructs an `Rc<T>` from a raw pointer.
584588
///
585-
/// The raw pointer must have been previously returned by a call to a
586-
/// [`Rc::into_raw`][into_raw].
589+
/// The raw pointer must have been previously returned by a call to
590+
/// [`Rc<U>::into_raw`][into_raw] where `U` must have the same size
591+
/// and alignment as `T`. This is trivially true if `U` is `T`.
592+
/// Note that if `U` is not `T` but has the same size and alignment, this is
593+
/// basically like transmuting references of different types. See
594+
/// [`mem::transmute`][transmute] for more information on what
595+
/// restrictions apply in this case.
587596
///
588-
/// This function is unsafe because improper use may lead to memory problems. For example, a
589-
/// double-free may occur if the function is called twice on the same raw pointer.
597+
/// The user of `from_raw` has to make sure a specific value of `T` is only
598+
/// dropped once.
599+
///
600+
/// This function is unsafe because improper use may lead to memory unsafety,
601+
/// even if the returned `Rc<T>` is never accessed.
590602
///
591603
/// [into_raw]: struct.Rc.html#method.into_raw
604+
/// [transmute]: ../../std/mem/fn.transmute.html
592605
///
593606
/// # Examples
594607
///

src/liballoc/slice.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ mod hack {
145145
unsafe {
146146
let len = b.len();
147147
let b = Box::into_raw(b);
148-
let xs = Vec::from_raw_parts(b as *mut T, len, len);
149-
xs
148+
Vec::from_raw_parts(b as *mut T, len, len)
150149
}
151150
}
152151

src/liballoc/sync.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
287287
}
288288
}
289289

290+
// This is repr(C) to future-proof against possible field-reordering, which
291+
// would interfere with otherwise safe [into|from]_raw() of transmutable
292+
// inner types.
293+
#[repr(C)]
290294
struct ArcInner<T: ?Sized> {
291295
strong: atomic::AtomicUsize,
292296

@@ -577,15 +581,24 @@ impl<T: ?Sized> Arc<T> {
577581
}
578582
}
579583

580-
/// Constructs an `Arc` from a raw pointer.
584+
/// Constructs an `Arc<T>` from a raw pointer.
581585
///
582-
/// The raw pointer must have been previously returned by a call to a
583-
/// [`Arc::into_raw`][into_raw].
586+
/// The raw pointer must have been previously returned by a call to
587+
/// [`Arc<U>::into_raw`][into_raw] where `U` must have the same size and
588+
/// alignment as `T`. This is trivially true if `U` is `T`.
589+
/// Note that if `U` is not `T` but has the same size and alignment, this is
590+
/// basically like transmuting references of different types. See
591+
/// [`mem::transmute`][transmute] for more information on what
592+
/// restrictions apply in this case.
584593
///
585-
/// This function is unsafe because improper use may lead to memory problems. For example, a
586-
/// double-free may occur if the function is called twice on the same raw pointer.
594+
/// The user of `from_raw` has to make sure a specific value of `T` is only
595+
/// dropped once.
596+
///
597+
/// This function is unsafe because improper use may lead to memory unsafety,
598+
/// even if the returned `Arc<T>` is never accessed.
587599
///
588600
/// [into_raw]: struct.Arc.html#method.into_raw
601+
/// [transmute]: ../../std/mem/fn.transmute.html
589602
///
590603
/// # Examples
591604
///

src/librustc/hir/map/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,7 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
10441044
collector.finalize_and_compute_crate_hash(crate_disambiguator, &*tcx.cstore, cmdline_args)
10451045
};
10461046

1047-
let map = tcx.arena.alloc(IndexedHir { crate_hash, map });
1048-
1049-
map
1047+
tcx.arena.alloc(IndexedHir { crate_hash, map })
10501048
}
10511049

10521050
/// Identical to the `PpAnn` implementation for `hir::Crate`,

src/librustc/ty/layout.rs

+25-29
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
381381
// Field 5 would be the first element, so memory_index is i:
382382
// Note: if we didn't optimize, it's already right.
383383

384-
let memory_index;
385-
if optimize {
386-
memory_index = invert_mapping(&inverse_memory_index);
387-
} else {
388-
memory_index = inverse_memory_index;
389-
}
384+
let memory_index =
385+
if optimize { invert_mapping(&inverse_memory_index) } else { inverse_memory_index };
390386

391387
let size = min_size.align_to(align.abi);
392388
let mut abi = Abi::Aggregate { sized };
@@ -944,33 +940,33 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
944940
let offset = st[i].fields.offset(field_index) + niche.offset;
945941
let size = st[i].size;
946942

947-
let mut abi = match st[i].abi {
948-
Abi::Scalar(_) => Abi::Scalar(niche_scalar.clone()),
949-
Abi::ScalarPair(ref first, ref second) => {
950-
// We need to use scalar_unit to reset the
951-
// valid range to the maximal one for that
952-
// primitive, because only the niche is
953-
// guaranteed to be initialised, not the
954-
// other primitive.
955-
if offset.bytes() == 0 {
956-
Abi::ScalarPair(
957-
niche_scalar.clone(),
958-
scalar_unit(second.value),
959-
)
960-
} else {
961-
Abi::ScalarPair(
962-
scalar_unit(first.value),
963-
niche_scalar.clone(),
964-
)
943+
let abi = if st.iter().all(|v| v.abi.is_uninhabited()) {
944+
Abi::Uninhabited
945+
} else {
946+
match st[i].abi {
947+
Abi::Scalar(_) => Abi::Scalar(niche_scalar.clone()),
948+
Abi::ScalarPair(ref first, ref second) => {
949+
// We need to use scalar_unit to reset the
950+
// valid range to the maximal one for that
951+
// primitive, because only the niche is
952+
// guaranteed to be initialised, not the
953+
// other primitive.
954+
if offset.bytes() == 0 {
955+
Abi::ScalarPair(
956+
niche_scalar.clone(),
957+
scalar_unit(second.value),
958+
)
959+
} else {
960+
Abi::ScalarPair(
961+
scalar_unit(first.value),
962+
niche_scalar.clone(),
963+
)
964+
}
965965
}
966+
_ => Abi::Aggregate { sized: true },
966967
}
967-
_ => Abi::Aggregate { sized: true },
968968
};
969969

970-
if st.iter().all(|v| v.abi.is_uninhabited()) {
971-
abi = Abi::Uninhabited;
972-
}
973-
974970
let largest_niche =
975971
Niche::from_scalar(dl, offset, niche_scalar.clone());
976972

src/librustc/ty/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ impl<'tcx> TyCtxt<'tcx> {
746746
/// side-effects -- e.g., in order to report errors for erroneous programs.
747747
///
748748
/// Note: The optimization is only available during incr. comp.
749-
pub(super) fn ensure_query<Q: QueryDescription<'tcx> + 'tcx>(self, key: Q::Key) -> () {
749+
pub(super) fn ensure_query<Q: QueryDescription<'tcx> + 'tcx>(self, key: Q::Key) {
750750
if Q::EVAL_ALWAYS {
751751
let _ = self.get_query::<Q>(DUMMY_SP, key);
752752
return;

src/librustc_builtin_macros/deriving/generic/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,9 @@ impl<'a> MethodDef<'a> {
10561056
self_: field,
10571057
other: other_fields
10581058
.iter_mut()
1059-
.map(|l| match l.next().unwrap() {
1060-
(.., ex, _) => ex,
1059+
.map(|l| {
1060+
let (.., ex, _) = l.next().unwrap();
1061+
ex
10611062
})
10621063
.collect(),
10631064
attrs,

src/librustc_builtin_macros/proc_macro_harness.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_expand::base::{ExtCtxt, Resolver};
1010
use rustc_expand::expand::{AstFragment, ExpansionConfig};
1111
use rustc_session::parse::ParseSess;
1212
use rustc_span::hygiene::AstPass;
13+
use rustc_span::source_map::SourceMap;
1314
use rustc_span::symbol::{kw, sym};
1415
use rustc_span::{Span, DUMMY_SP};
1516
use smallvec::smallvec;
@@ -44,6 +45,7 @@ struct CollectProcMacros<'a> {
4445
macros: Vec<ProcMacro>,
4546
in_root: bool,
4647
handler: &'a rustc_errors::Handler,
48+
source_map: &'a SourceMap,
4749
is_proc_macro_crate: bool,
4850
is_test_crate: bool,
4951
}
@@ -65,6 +67,7 @@ pub fn inject(
6567
macros: Vec::new(),
6668
in_root: true,
6769
handler,
70+
source_map: sess.source_map(),
6871
is_proc_macro_crate,
6972
is_test_crate,
7073
};
@@ -195,7 +198,7 @@ impl<'a> CollectProcMacros<'a> {
195198
} else {
196199
"functions tagged with `#[proc_macro_derive]` must be `pub`"
197200
};
198-
self.handler.span_err(item.span, msg);
201+
self.handler.span_err(self.source_map.def_span(item.span), msg);
199202
}
200203
}
201204

@@ -214,7 +217,7 @@ impl<'a> CollectProcMacros<'a> {
214217
} else {
215218
"functions tagged with `#[proc_macro_attribute]` must be `pub`"
216219
};
217-
self.handler.span_err(item.span, msg);
220+
self.handler.span_err(self.source_map.def_span(item.span), msg);
218221
}
219222
}
220223

@@ -233,7 +236,7 @@ impl<'a> CollectProcMacros<'a> {
233236
} else {
234237
"functions tagged with `#[proc_macro]` must be `pub`"
235238
};
236-
self.handler.span_err(item.span, msg);
239+
self.handler.span_err(self.source_map.def_span(item.span), msg);
237240
}
238241
}
239242
}
@@ -244,7 +247,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
244247
if self.is_proc_macro_crate && attr::contains_name(&item.attrs, sym::macro_export) {
245248
let msg =
246249
"cannot export macro_rules! macros from a `proc-macro` crate type currently";
247-
self.handler.span_err(item.span, msg);
250+
self.handler.span_err(self.source_map.def_span(item.span), msg);
248251
}
249252
}
250253

@@ -295,7 +298,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
295298

296299
let attr = match found_attr {
297300
None => {
298-
self.check_not_pub_in_root(&item.vis, item.span);
301+
self.check_not_pub_in_root(&item.vis, self.source_map.def_span(item.span));
299302
let prev_in_root = mem::replace(&mut self.in_root, false);
300303
visit::walk_item(self, item);
301304
self.in_root = prev_in_root;

src/librustc_codegen_ssa/back/rpath.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {
8181
rpaths.extend_from_slice(&fallback_rpaths);
8282

8383
// Remove duplicates
84-
let rpaths = minimize_rpaths(&rpaths);
85-
86-
rpaths
84+
minimize_rpaths(&rpaths)
8785
}
8886

8987
fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {

src/librustc_codegen_ssa/back/write.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ fn generate_lto_work<B: ExtraBackendMethods>(
288288
B::run_thin_lto(cgcx, needs_thin_lto, import_only_modules).unwrap_or_else(|e| e.raise())
289289
};
290290

291-
let result = lto_modules
291+
lto_modules
292292
.into_iter()
293293
.map(|module| {
294294
let cost = module.cost();
@@ -303,9 +303,7 @@ fn generate_lto_work<B: ExtraBackendMethods>(
303303
0,
304304
)
305305
}))
306-
.collect();
307-
308-
result
306+
.collect()
309307
}
310308

311309
pub struct CompiledModules {

src/librustc_data_structures/profiling.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl SelfProfilerRef {
345345
) {
346346
drop(self.exec(event_filter, |profiler| {
347347
let event_id = StringId::new_virtual(query_invocation_id.0);
348-
let thread_id = std::thread::current().id().as_u64() as u32;
348+
let thread_id = std::thread::current().id().as_u64().get() as u32;
349349

350350
profiler.profiler.record_instant_event(
351351
event_kind(profiler),
@@ -522,7 +522,7 @@ impl<'a> TimingGuard<'a> {
522522
event_kind: StringId,
523523
event_id: EventId,
524524
) -> TimingGuard<'a> {
525-
let thread_id = std::thread::current().id().as_u64() as u32;
525+
let thread_id = std::thread::current().id().as_u64().get() as u32;
526526
let raw_profiler = &profiler.profiler;
527527
let timing_guard =
528528
raw_profiler.start_recording_interval_event(event_kind, event_id, thread_id);

src/librustc_infer/infer/canonical/canonicalizer.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
555555
// avoid allocations in those cases. We also don't use `indices` to
556556
// determine if a kind has been seen before until the limit of 8 has
557557
// been exceeded, to also avoid allocations for `indices`.
558-
let var = if !var_values.spilled() {
558+
if !var_values.spilled() {
559559
// `var_values` is stack-allocated. `indices` isn't used yet. Do a
560560
// direct linear search of `var_values`.
561561
if let Some(idx) = var_values.iter().position(|&k| k == kind) {
@@ -589,9 +589,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
589589
assert_eq!(variables.len(), var_values.len());
590590
BoundVar::new(variables.len() - 1)
591591
})
592-
};
593-
594-
var
592+
}
595593
}
596594

597595
/// Shorthand helper that creates a canonical region variable for

src/librustc_infer/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub type NormalizedTy<'tcx> = Normalized<'tcx, Ty<'tcx>>;
2323

2424
impl<'tcx, T> Normalized<'tcx, T> {
2525
pub fn with<U>(self, value: U) -> Normalized<'tcx, U> {
26-
Normalized { value: value, obligations: self.obligations }
26+
Normalized { value, obligations: self.obligations }
2727
}
2828
}
2929

src/librustc_infer/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct PredicateSet<'tcx> {
4747

4848
impl PredicateSet<'tcx> {
4949
fn new(tcx: TyCtxt<'tcx>) -> Self {
50-
Self { tcx: tcx, set: Default::default() }
50+
Self { tcx, set: Default::default() }
5151
}
5252

5353
fn insert(&mut self, pred: &ty::Predicate<'tcx>) -> bool {

src/librustc_metadata/dynamic_lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,12 @@ mod dl {
9494
let result = f();
9595

9696
let last_error = libc::dlerror() as *const _;
97-
let ret = if ptr::null() == last_error {
97+
if ptr::null() == last_error {
9898
Ok(result)
9999
} else {
100100
let s = CStr::from_ptr(last_error).to_bytes();
101101
Err(str::from_utf8(s).unwrap().to_owned())
102-
};
103-
104-
ret
102+
}
105103
}
106104
}
107105

0 commit comments

Comments
 (0)