Skip to content

Commit

Permalink
Auto merge of #61105 - Centril:rollup-t9lemjf, r=Centril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed May 24, 2019
2 parents 8869ee0 + 92fda92 commit fd8e23c
Show file tree
Hide file tree
Showing 57 changed files with 555 additions and 408 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2877,6 +2877,7 @@ dependencies = [
"rustc_errors 0.0.0",
"rustc_target 0.0.0",
"serialize 0.0.0",
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
"stable_deref_trait 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntax 0.0.0",
"syntax_ext 0.0.0",
Expand Down
11 changes: 6 additions & 5 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,16 @@ impl<T: ?Sized> Box<T> {
#[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")]
#[inline]
#[doc(hidden)]
pub fn into_unique(mut b: Box<T>) -> Unique<T> {
pub fn into_unique(b: Box<T>) -> Unique<T> {
let mut unique = b.0;
mem::forget(b);
// Box is kind-of a library type, but recognized as a "unique pointer" by
// Stacked Borrows. This function here corresponds to "reborrowing to
// a raw pointer", but there is no actual reborrow here -- so
// without some care, the pointer we are returning here still carries
// the `Uniq` tag. We round-trip through a mutable reference to avoid that.
let unique = unsafe { b.0.as_mut() as *mut T };
mem::forget(b);
unsafe { Unique::new_unchecked(unique) }
// the tag of `b`, with `Unique` permission.
// We round-trip through a mutable reference to avoid that.
unsafe { Unique::new_unchecked(unique.as_mut() as *mut T) }
}

/// Consumes and leaks the `Box`, returning a mutable reference,
Expand Down
5 changes: 3 additions & 2 deletions src/liballoc/tests/vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg(not(miri))]

use std::borrow::Cow;
use std::mem::size_of;
use std::{usize, isize};
Expand Down Expand Up @@ -763,6 +761,7 @@ fn from_into_inner() {
it.next().unwrap();
let vec = it.collect::<Vec<_>>();
assert_eq!(vec, [2, 3]);
#[cfg(not(miri))] // Miri does not support comparing dangling pointers
assert!(ptr != vec.as_ptr());
}

Expand Down Expand Up @@ -971,6 +970,7 @@ fn test_reserve_exact() {
}

#[test]
#[cfg(not(miri))] // Miri does not support signalling OOM
fn test_try_reserve() {

// These are the interesting cases:
Expand Down Expand Up @@ -1073,6 +1073,7 @@ fn test_try_reserve() {
}

#[test]
#[cfg(not(miri))] // Miri does not support signalling OOM
fn test_try_reserve_exact() {

// This is exactly the same as test_try_reserve with the method changed.
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ impl<T> Vec<T> {
let count = (*other).len();
self.reserve(count);
let len = self.len();
ptr::copy_nonoverlapping(other as *const T, self.get_unchecked_mut(len), count);
ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count);
self.len += count;
}

Expand Down
29 changes: 24 additions & 5 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,31 @@ impl DroplessArena {
}
}

#[inline]
unsafe fn write_from_iter<T, I: Iterator<Item = T>>(
&self,
mut iter: I,
len: usize,
mem: *mut T,
) -> &mut [T] {
let mut i = 0;
// Use a manual loop since LLVM manages to optimize it better for
// slice iterators
loop {
let value = iter.next();
if i >= len || value.is_none() {
// We only return as many items as the iterator gave us, even
// though it was supposed to give us `len`
return slice::from_raw_parts_mut(mem, i);
}
ptr::write(mem.offset(i as isize), value.unwrap());
i += 1;
}
}

#[inline]
pub fn alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
let mut iter = iter.into_iter();
let iter = iter.into_iter();
assert!(mem::size_of::<T>() != 0);
assert!(!mem::needs_drop::<T>());

Expand All @@ -505,10 +527,7 @@ impl DroplessArena {
let size = len.checked_mul(mem::size_of::<T>()).unwrap();
let mem = self.alloc_raw(size, mem::align_of::<T>()) as *mut _ as *mut T;
unsafe {
for i in 0..len {
ptr::write(mem.offset(i as isize), iter.next().unwrap())
}
slice::from_raw_parts_mut(mem, len)
self.write_from_iter(iter, len, mem)
}
}
(_, _) => {
Expand Down
47 changes: 45 additions & 2 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ use std::cell::RefCell;
use std::marker::PhantomData;
use smallvec::SmallVec;

/// This declares a list of types which can be allocated by `Arena`.
///
/// The `few` modifier will cause allocation to use the shared arena and recording the destructor.
/// This is faster and more memory efficient if there's only a few allocations of the type.
/// Leaving `few` out will cause the type to get its own dedicated `TypedArena` which is
/// faster and more memory efficient if there is lots of allocations.
///
/// Specifying the `decode` modifier will add decode impls for &T and &[T] where T is the type
/// listed. These impls will appear in the implement_ty_decoder! macro.
#[macro_export]
macro_rules! arena_types {
($macro:path, $args:tt, $tcx:lifetime) => (
Expand All @@ -14,7 +23,7 @@ macro_rules! arena_types {
rustc::hir::def_id::DefId,
rustc::ty::subst::SubstsRef<$tcx>
)>,
[few] mir_keys: rustc::util::nodemap::DefIdSet,
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
[decode] specialization_graph: rustc::traits::specialization_graph::Graph,
[] region_scope_tree: rustc::middle::region::ScopeTree,
[] item_local_set: rustc::util::nodemap::ItemLocalSet,
Expand Down Expand Up @@ -58,6 +67,40 @@ macro_rules! arena_types {
rustc::infer::canonical::Canonical<'tcx,
rustc::infer::canonical::QueryResponse<'tcx, rustc::ty::Ty<'tcx>>
>,
[few] crate_inherent_impls: rustc::ty::CrateInherentImpls,
[decode] borrowck: rustc::middle::borrowck::BorrowCheckResult,
[few] upstream_monomorphizations:
rustc::util::nodemap::DefIdMap<
rustc_data_structures::fx::FxHashMap<
rustc::ty::subst::SubstsRef<'tcx>,
rustc::hir::def_id::CrateNum
>
>,
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
[decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
[few] lint_levels: rustc::lint::LintLevelMap,
[few] stability_index: rustc::middle::stability::Index<'tcx>,
[few] features: syntax::feature_gate::Features,
[few] all_traits: Vec<rustc::hir::def_id::DefId>,
[few] privacy_access_levels: rustc::middle::privacy::AccessLevels,
[few] target_features_whitelist: rustc_data_structures::fx::FxHashMap<
String,
Option<syntax::symbol::Symbol>
>,
[few] wasm_import_module_map: rustc_data_structures::fx::FxHashMap<
rustc::hir::def_id::DefId,
String
>,
[few] get_lib_features: rustc::middle::lib_features::LibFeatures,
[few] defined_lib_features: rustc::middle::lang_items::LanguageItems,
[few] visible_parent_map: rustc::util::nodemap::DefIdMap<rustc::hir::def_id::DefId>,
[few] foreign_module: rustc::middle::cstore::ForeignModule,
[few] foreign_modules: Vec<rustc::middle::cstore::ForeignModule>,
[few] reachable_non_generics: rustc::util::nodemap::DefIdMap<
rustc::middle::exported_symbols::SymbolExportLevel
>,
[few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>,
[few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>,
], $tcx);
)
}
Expand Down Expand Up @@ -119,7 +162,7 @@ pub trait ArenaAllocatable {}

impl<T: Copy> ArenaAllocatable for T {}

pub unsafe trait ArenaField<'tcx>: Sized {
unsafe trait ArenaField<'tcx>: Sized {
/// Returns a specific arena to allocate from.
/// If None is returned, the DropArena will be used.
fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;
Expand Down
37 changes: 19 additions & 18 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
pub use self::Level::*;
pub use self::LintSource::*;

use rustc_data_structures::sync::{self, Lrc};
use rustc_data_structures::sync;

use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
use crate::hir::intravisit;
Expand All @@ -35,7 +35,7 @@ use crate::util::nodemap::NodeMap;
use errors::{DiagnosticBuilder, DiagnosticId};
use std::{hash, ptr};
use syntax::ast;
use syntax::source_map::{MultiSpan, ExpnFormat};
use syntax::source_map::{MultiSpan, ExpnFormat, CompilerDesugaringKind};
use syntax::early_buffered_lints::BufferedEarlyLintId;
use syntax::edition::Edition;
use syntax::symbol::{Symbol, sym};
Expand Down Expand Up @@ -767,7 +767,7 @@ pub fn maybe_lint_level_root(tcx: TyCtxt<'_, '_, '_>, id: hir::HirId) -> bool {
}

fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
-> Lrc<LintLevelMap>
-> &'tcx LintLevelMap
{
assert_eq!(cnum, LOCAL_CRATE);
let mut builder = LintLevelMapBuilder {
Expand All @@ -784,7 +784,7 @@ fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
intravisit::walk_crate(&mut builder, krate);
builder.levels.pop(push);

Lrc::new(builder.levels.build_map())
tcx.arena.alloc(builder.levels.build_map())
}

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

match info.format {
ExpnFormat::MacroAttribute(..) => return true, // definitely a plugin
ExpnFormat::CompilerDesugaring(_) => return true, // well, it's "external"
ExpnFormat::MacroBang(..) => {} // check below
}

let def_site = match info.def_site {
Some(span) => span,
// no span for the def_site means it's an external macro
None => return true,
};
ExpnFormat::MacroAttribute(..) => true, // definitely a plugin
ExpnFormat::CompilerDesugaring(CompilerDesugaringKind::ForLoop) => false,
ExpnFormat::CompilerDesugaring(_) => true, // well, it's "external"
ExpnFormat::MacroBang(..) => {
let def_site = match info.def_site {
Some(span) => span,
// no span for the def_site means it's an external macro
None => return true,
};

match sess.source_map().span_to_snippet(def_site) {
Ok(code) => !code.starts_with("macro_rules"),
// no snippet = external macro or compiler-builtin expansion
Err(_) => true,
match sess.source_map().span_to_snippet(def_site) {
Ok(code) => !code.starts_with("macro_rules"),
// no snippet = external macro or compiler-builtin expansion
Err(_) => true,
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ pub fn used_crates(tcx: TyCtxt<'_, '_, '_>, prefer: LinkagePreference)
Some((cnum, path))
})
.collect::<Vec<_>>();
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE);
Lrc::make_mut(&mut ordering).reverse();
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE).to_owned();
ordering.reverse();
libs.sort_by_cached_key(|&(a, _)| {
ordering.iter().position(|x| *x == a)
});
Expand Down
23 changes: 9 additions & 14 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::rustc::lint;
use crate::session::Session;
use crate::util::nodemap::{DefIdMap, FxHashMap, FxHashSet, HirIdMap, HirIdSet};
use errors::{Applicability, DiagnosticBuilder};
use rustc_data_structures::sync::Lrc;
use rustc_macros::HashStable;
use std::borrow::Cow;
use std::cell::Cell;
Expand Down Expand Up @@ -211,10 +210,10 @@ struct NamedRegionMap {
/// See [`NamedRegionMap`].
#[derive(Default)]
pub struct ResolveLifetimes {
defs: FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Region>>>,
late_bound: FxHashMap<LocalDefId, Lrc<FxHashSet<ItemLocalId>>>,
defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
object_lifetime_defaults:
FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Lrc<Vec<ObjectLifetimeDefault>>>>>,
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
}

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

named_region_map: |tcx, id| {
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id).cloned()
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id)
},

is_late_bound_map: |tcx, id| {
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
tcx.resolve_lifetimes(LOCAL_CRATE)
.late_bound
.get(&id)
.cloned()
},

object_lifetime_defaults_map: |tcx, id| {
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
tcx.resolve_lifetimes(LOCAL_CRATE)
.object_lifetime_defaults
.get(&id)
.cloned()
},

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

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

for (hir_id, v) in named_region_map.defs {
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
Lrc::get_mut(map).unwrap().insert(hir_id.local_id, v);
map.insert(hir_id.local_id, v);
}
for hir_id in named_region_map.late_bound {
let map = rl.late_bound
.entry(hir_id.owner_local_def_id())
.or_default();
Lrc::get_mut(map).unwrap().insert(hir_id.local_id);
map.insert(hir_id.local_id);
}
for (hir_id, v) in named_region_map.object_lifetime_defaults {
let map = rl.object_lifetime_defaults
.entry(hir_id.owner_local_def_id())
.or_default();
Lrc::get_mut(map)
.unwrap()
.insert(hir_id.local_id, Lrc::new(v));
map.insert(hir_id.local_id, v);
}

Lrc::new(rl)
tcx.arena.alloc(rl)
}

fn krate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> NamedRegionMap {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
remaining_lib_features.remove(&Symbol::intern("test"));

let check_features =
|remaining_lib_features: &mut FxHashMap<_, _>, defined_features: &Vec<_>| {
|remaining_lib_features: &mut FxHashMap<_, _>, defined_features: &[_]| {
for &(feature, since) in defined_features {
if let Some(since) = since {
if let Some(span) = remaining_lib_features.get(&feature) {
Expand All @@ -908,7 +908,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
if remaining_lib_features.is_empty() {
break;
}
check_features(&mut remaining_lib_features, &tcx.defined_lib_features(cnum));
check_features(&mut remaining_lib_features, tcx.defined_lib_features(cnum));
}
}

Expand Down
Loading

0 comments on commit fd8e23c

Please sign in to comment.