Skip to content

Commit fd8e23c

Browse files
committed
Auto merge of #61105 - Centril:rollup-t9lemjf, r=Centril
Rollup of 6 pull requests Successful merges: - #59545 (Use arenas to avoid Lrc in queries #2) - #61054 (Suggest dereferencing on assignment to mutable borrow) - #61056 (tweak discriminant on non-nullary enum diagnostic) - #61082 (fix dangling reference in Vec::append) - #61086 (Box::into_unique: do the reborrow-to-raw *after* destroying the Box) - #61098 (Fix overflowing literal lint in loops) Failed merges: r? @ghost
2 parents 8869ee0 + 92fda92 commit fd8e23c

Some content is hidden

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

57 files changed

+555
-408
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,7 @@ dependencies = [
28772877
"rustc_errors 0.0.0",
28782878
"rustc_target 0.0.0",
28792879
"serialize 0.0.0",
2880+
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
28802881
"stable_deref_trait 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
28812882
"syntax 0.0.0",
28822883
"syntax_ext 0.0.0",

src/liballoc/boxed.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,16 @@ impl<T: ?Sized> Box<T> {
253253
#[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")]
254254
#[inline]
255255
#[doc(hidden)]
256-
pub fn into_unique(mut b: Box<T>) -> Unique<T> {
256+
pub fn into_unique(b: Box<T>) -> Unique<T> {
257+
let mut unique = b.0;
258+
mem::forget(b);
257259
// Box is kind-of a library type, but recognized as a "unique pointer" by
258260
// Stacked Borrows. This function here corresponds to "reborrowing to
259261
// a raw pointer", but there is no actual reborrow here -- so
260262
// without some care, the pointer we are returning here still carries
261-
// the `Uniq` tag. We round-trip through a mutable reference to avoid that.
262-
let unique = unsafe { b.0.as_mut() as *mut T };
263-
mem::forget(b);
264-
unsafe { Unique::new_unchecked(unique) }
263+
// the tag of `b`, with `Unique` permission.
264+
// We round-trip through a mutable reference to avoid that.
265+
unsafe { Unique::new_unchecked(unique.as_mut() as *mut T) }
265266
}
266267

267268
/// Consumes and leaks the `Box`, returning a mutable reference,

src/liballoc/tests/vec.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(not(miri))]
2-
31
use std::borrow::Cow;
42
use std::mem::size_of;
53
use std::{usize, isize};
@@ -763,6 +761,7 @@ fn from_into_inner() {
763761
it.next().unwrap();
764762
let vec = it.collect::<Vec<_>>();
765763
assert_eq!(vec, [2, 3]);
764+
#[cfg(not(miri))] // Miri does not support comparing dangling pointers
766765
assert!(ptr != vec.as_ptr());
767766
}
768767

@@ -971,6 +970,7 @@ fn test_reserve_exact() {
971970
}
972971

973972
#[test]
973+
#[cfg(not(miri))] // Miri does not support signalling OOM
974974
fn test_try_reserve() {
975975

976976
// These are the interesting cases:
@@ -1073,6 +1073,7 @@ fn test_try_reserve() {
10731073
}
10741074

10751075
#[test]
1076+
#[cfg(not(miri))] // Miri does not support signalling OOM
10761077
fn test_try_reserve_exact() {
10771078

10781079
// This is exactly the same as test_try_reserve with the method changed.

src/liballoc/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ impl<T> Vec<T> {
10941094
let count = (*other).len();
10951095
self.reserve(count);
10961096
let len = self.len();
1097-
ptr::copy_nonoverlapping(other as *const T, self.get_unchecked_mut(len), count);
1097+
ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count);
10981098
self.len += count;
10991099
}
11001100

src/libarena/lib.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,31 @@ impl DroplessArena {
486486
}
487487
}
488488

489+
#[inline]
490+
unsafe fn write_from_iter<T, I: Iterator<Item = T>>(
491+
&self,
492+
mut iter: I,
493+
len: usize,
494+
mem: *mut T,
495+
) -> &mut [T] {
496+
let mut i = 0;
497+
// Use a manual loop since LLVM manages to optimize it better for
498+
// slice iterators
499+
loop {
500+
let value = iter.next();
501+
if i >= len || value.is_none() {
502+
// We only return as many items as the iterator gave us, even
503+
// though it was supposed to give us `len`
504+
return slice::from_raw_parts_mut(mem, i);
505+
}
506+
ptr::write(mem.offset(i as isize), value.unwrap());
507+
i += 1;
508+
}
509+
}
510+
489511
#[inline]
490512
pub fn alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
491-
let mut iter = iter.into_iter();
513+
let iter = iter.into_iter();
492514
assert!(mem::size_of::<T>() != 0);
493515
assert!(!mem::needs_drop::<T>());
494516

@@ -505,10 +527,7 @@ impl DroplessArena {
505527
let size = len.checked_mul(mem::size_of::<T>()).unwrap();
506528
let mem = self.alloc_raw(size, mem::align_of::<T>()) as *mut _ as *mut T;
507529
unsafe {
508-
for i in 0..len {
509-
ptr::write(mem.offset(i as isize), iter.next().unwrap())
510-
}
511-
slice::from_raw_parts_mut(mem, len)
530+
self.write_from_iter(iter, len, mem)
512531
}
513532
}
514533
(_, _) => {

src/librustc/arena.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ use std::cell::RefCell;
66
use std::marker::PhantomData;
77
use smallvec::SmallVec;
88

9+
/// This declares a list of types which can be allocated by `Arena`.
10+
///
11+
/// The `few` modifier will cause allocation to use the shared arena and recording the destructor.
12+
/// This is faster and more memory efficient if there's only a few allocations of the type.
13+
/// Leaving `few` out will cause the type to get its own dedicated `TypedArena` which is
14+
/// faster and more memory efficient if there is lots of allocations.
15+
///
16+
/// Specifying the `decode` modifier will add decode impls for &T and &[T] where T is the type
17+
/// listed. These impls will appear in the implement_ty_decoder! macro.
918
#[macro_export]
1019
macro_rules! arena_types {
1120
($macro:path, $args:tt, $tcx:lifetime) => (
@@ -14,7 +23,7 @@ macro_rules! arena_types {
1423
rustc::hir::def_id::DefId,
1524
rustc::ty::subst::SubstsRef<$tcx>
1625
)>,
17-
[few] mir_keys: rustc::util::nodemap::DefIdSet,
26+
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
1827
[decode] specialization_graph: rustc::traits::specialization_graph::Graph,
1928
[] region_scope_tree: rustc::middle::region::ScopeTree,
2029
[] item_local_set: rustc::util::nodemap::ItemLocalSet,
@@ -58,6 +67,40 @@ macro_rules! arena_types {
5867
rustc::infer::canonical::Canonical<'tcx,
5968
rustc::infer::canonical::QueryResponse<'tcx, rustc::ty::Ty<'tcx>>
6069
>,
70+
[few] crate_inherent_impls: rustc::ty::CrateInherentImpls,
71+
[decode] borrowck: rustc::middle::borrowck::BorrowCheckResult,
72+
[few] upstream_monomorphizations:
73+
rustc::util::nodemap::DefIdMap<
74+
rustc_data_structures::fx::FxHashMap<
75+
rustc::ty::subst::SubstsRef<'tcx>,
76+
rustc::hir::def_id::CrateNum
77+
>
78+
>,
79+
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
80+
[decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
81+
[few] lint_levels: rustc::lint::LintLevelMap,
82+
[few] stability_index: rustc::middle::stability::Index<'tcx>,
83+
[few] features: syntax::feature_gate::Features,
84+
[few] all_traits: Vec<rustc::hir::def_id::DefId>,
85+
[few] privacy_access_levels: rustc::middle::privacy::AccessLevels,
86+
[few] target_features_whitelist: rustc_data_structures::fx::FxHashMap<
87+
String,
88+
Option<syntax::symbol::Symbol>
89+
>,
90+
[few] wasm_import_module_map: rustc_data_structures::fx::FxHashMap<
91+
rustc::hir::def_id::DefId,
92+
String
93+
>,
94+
[few] get_lib_features: rustc::middle::lib_features::LibFeatures,
95+
[few] defined_lib_features: rustc::middle::lang_items::LanguageItems,
96+
[few] visible_parent_map: rustc::util::nodemap::DefIdMap<rustc::hir::def_id::DefId>,
97+
[few] foreign_module: rustc::middle::cstore::ForeignModule,
98+
[few] foreign_modules: Vec<rustc::middle::cstore::ForeignModule>,
99+
[few] reachable_non_generics: rustc::util::nodemap::DefIdMap<
100+
rustc::middle::exported_symbols::SymbolExportLevel
101+
>,
102+
[few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>,
103+
[few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>,
61104
], $tcx);
62105
)
63106
}
@@ -119,7 +162,7 @@ pub trait ArenaAllocatable {}
119162

120163
impl<T: Copy> ArenaAllocatable for T {}
121164

122-
pub unsafe trait ArenaField<'tcx>: Sized {
165+
unsafe trait ArenaField<'tcx>: Sized {
123166
/// Returns a specific arena to allocate from.
124167
/// If None is returned, the DropArena will be used.
125168
fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;

src/librustc/lint/mod.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
pub use self::Level::*;
2222
pub use self::LintSource::*;
2323

24-
use rustc_data_structures::sync::{self, Lrc};
24+
use rustc_data_structures::sync;
2525

2626
use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
2727
use crate::hir::intravisit;
@@ -35,7 +35,7 @@ use crate::util::nodemap::NodeMap;
3535
use errors::{DiagnosticBuilder, DiagnosticId};
3636
use std::{hash, ptr};
3737
use syntax::ast;
38-
use syntax::source_map::{MultiSpan, ExpnFormat};
38+
use syntax::source_map::{MultiSpan, ExpnFormat, CompilerDesugaringKind};
3939
use syntax::early_buffered_lints::BufferedEarlyLintId;
4040
use syntax::edition::Edition;
4141
use syntax::symbol::{Symbol, sym};
@@ -767,7 +767,7 @@ pub fn maybe_lint_level_root(tcx: TyCtxt<'_, '_, '_>, id: hir::HirId) -> bool {
767767
}
768768

769769
fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
770-
-> Lrc<LintLevelMap>
770+
-> &'tcx LintLevelMap
771771
{
772772
assert_eq!(cnum, LOCAL_CRATE);
773773
let mut builder = LintLevelMapBuilder {
@@ -784,7 +784,7 @@ fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
784784
intravisit::walk_crate(&mut builder, krate);
785785
builder.levels.pop(push);
786786

787-
Lrc::new(builder.levels.build_map())
787+
tcx.arena.alloc(builder.levels.build_map())
788788
}
789789

790790
struct LintLevelMapBuilder<'a, 'tcx: 'a> {
@@ -887,21 +887,22 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
887887
};
888888

889889
match info.format {
890-
ExpnFormat::MacroAttribute(..) => return true, // definitely a plugin
891-
ExpnFormat::CompilerDesugaring(_) => return true, // well, it's "external"
892-
ExpnFormat::MacroBang(..) => {} // check below
893-
}
894-
895-
let def_site = match info.def_site {
896-
Some(span) => span,
897-
// no span for the def_site means it's an external macro
898-
None => return true,
899-
};
890+
ExpnFormat::MacroAttribute(..) => true, // definitely a plugin
891+
ExpnFormat::CompilerDesugaring(CompilerDesugaringKind::ForLoop) => false,
892+
ExpnFormat::CompilerDesugaring(_) => true, // well, it's "external"
893+
ExpnFormat::MacroBang(..) => {
894+
let def_site = match info.def_site {
895+
Some(span) => span,
896+
// no span for the def_site means it's an external macro
897+
None => return true,
898+
};
900899

901-
match sess.source_map().span_to_snippet(def_site) {
902-
Ok(code) => !code.starts_with("macro_rules"),
903-
// no snippet = external macro or compiler-builtin expansion
904-
Err(_) => true,
900+
match sess.source_map().span_to_snippet(def_site) {
901+
Ok(code) => !code.starts_with("macro_rules"),
902+
// no snippet = external macro or compiler-builtin expansion
903+
Err(_) => true,
904+
}
905+
}
905906
}
906907
}
907908

src/librustc/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ pub fn used_crates(tcx: TyCtxt<'_, '_, '_>, prefer: LinkagePreference)
256256
Some((cnum, path))
257257
})
258258
.collect::<Vec<_>>();
259-
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE);
260-
Lrc::make_mut(&mut ordering).reverse();
259+
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE).to_owned();
260+
ordering.reverse();
261261
libs.sort_by_cached_key(|&(a, _)| {
262262
ordering.iter().position(|x| *x == a)
263263
});

src/librustc/middle/resolve_lifetime.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::rustc::lint;
1515
use crate::session::Session;
1616
use crate::util::nodemap::{DefIdMap, FxHashMap, FxHashSet, HirIdMap, HirIdSet};
1717
use errors::{Applicability, DiagnosticBuilder};
18-
use rustc_data_structures::sync::Lrc;
1918
use rustc_macros::HashStable;
2019
use std::borrow::Cow;
2120
use std::cell::Cell;
@@ -211,10 +210,10 @@ struct NamedRegionMap {
211210
/// See [`NamedRegionMap`].
212211
#[derive(Default)]
213212
pub struct ResolveLifetimes {
214-
defs: FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Region>>>,
215-
late_bound: FxHashMap<LocalDefId, Lrc<FxHashSet<ItemLocalId>>>,
213+
defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
214+
late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
216215
object_lifetime_defaults:
217-
FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Lrc<Vec<ObjectLifetimeDefault>>>>>,
216+
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
218217
}
219218

220219
impl_stable_hash_for!(struct crate::middle::resolve_lifetime::ResolveLifetimes {
@@ -347,23 +346,21 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
347346

348347
named_region_map: |tcx, id| {
349348
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
350-
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id).cloned()
349+
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id)
351350
},
352351

353352
is_late_bound_map: |tcx, id| {
354353
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
355354
tcx.resolve_lifetimes(LOCAL_CRATE)
356355
.late_bound
357356
.get(&id)
358-
.cloned()
359357
},
360358

361359
object_lifetime_defaults_map: |tcx, id| {
362360
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
363361
tcx.resolve_lifetimes(LOCAL_CRATE)
364362
.object_lifetime_defaults
365363
.get(&id)
366-
.cloned()
367364
},
368365

369366
..*providers
@@ -379,7 +376,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
379376
fn resolve_lifetimes<'tcx>(
380377
tcx: TyCtxt<'_, 'tcx, 'tcx>,
381378
for_krate: CrateNum,
382-
) -> Lrc<ResolveLifetimes> {
379+
) -> &'tcx ResolveLifetimes {
383380
assert_eq!(for_krate, LOCAL_CRATE);
384381

385382
let named_region_map = krate(tcx);
@@ -388,24 +385,22 @@ fn resolve_lifetimes<'tcx>(
388385

389386
for (hir_id, v) in named_region_map.defs {
390387
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
391-
Lrc::get_mut(map).unwrap().insert(hir_id.local_id, v);
388+
map.insert(hir_id.local_id, v);
392389
}
393390
for hir_id in named_region_map.late_bound {
394391
let map = rl.late_bound
395392
.entry(hir_id.owner_local_def_id())
396393
.or_default();
397-
Lrc::get_mut(map).unwrap().insert(hir_id.local_id);
394+
map.insert(hir_id.local_id);
398395
}
399396
for (hir_id, v) in named_region_map.object_lifetime_defaults {
400397
let map = rl.object_lifetime_defaults
401398
.entry(hir_id.owner_local_def_id())
402399
.or_default();
403-
Lrc::get_mut(map)
404-
.unwrap()
405-
.insert(hir_id.local_id, Lrc::new(v));
400+
map.insert(hir_id.local_id, v);
406401
}
407402

408-
Lrc::new(rl)
403+
tcx.arena.alloc(rl)
409404
}
410405

411406
fn krate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> NamedRegionMap {

src/librustc/middle/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
883883
remaining_lib_features.remove(&Symbol::intern("test"));
884884

885885
let check_features =
886-
|remaining_lib_features: &mut FxHashMap<_, _>, defined_features: &Vec<_>| {
886+
|remaining_lib_features: &mut FxHashMap<_, _>, defined_features: &[_]| {
887887
for &(feature, since) in defined_features {
888888
if let Some(since) = since {
889889
if let Some(span) = remaining_lib_features.get(&feature) {
@@ -908,7 +908,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
908908
if remaining_lib_features.is_empty() {
909909
break;
910910
}
911-
check_features(&mut remaining_lib_features, &tcx.defined_lib_features(cnum));
911+
check_features(&mut remaining_lib_features, tcx.defined_lib_features(cnum));
912912
}
913913
}
914914

0 commit comments

Comments
 (0)