Skip to content

Commit

Permalink
Auto merge of rust-lang#71566 - Dylan-DPC:rollup-9xoz6fg, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Rollup of 5 pull requests

Successful merges:

 - rust-lang#70043 (Add all remaining `DefKind`s.)
 - rust-lang#71140 ([breaking change] Disallow statics initializing themselves)
 - rust-lang#71392 (Don't hold the predecessor cache lock longer than necessary)
 - rust-lang#71541 (Add regression test for rust-lang#26376)
 - rust-lang#71554 (Replace thread_local with generator resume arguments in box_region.)

Failed merges:

r? @ghost
  • Loading branch information
bors committed Apr 26, 2020
2 parents 0862458 + f70c9db commit b592b37
Show file tree
Hide file tree
Showing 54 changed files with 369 additions and 307 deletions.
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
let cleanup_kinds = analyze::cleanup_kinds(&mir);
// Allocate a `Block` for every basic block, except
// the start block, if nothing loops back to it.
let reentrant_start_block = !mir.predecessors_for(mir::START_BLOCK).is_empty();
let reentrant_start_block = !mir.predecessors()[mir::START_BLOCK].is_empty();
let block_bxs: IndexVec<mir::BasicBlock, Bx::BasicBlock> = mir
.basic_blocks()
.indices()
Expand Down
47 changes: 27 additions & 20 deletions src/librustc_data_structures/box_region.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
use std::cell::Cell;
//! This module provides a way to deal with self-referential data.
//!
//! The main idea is to allocate such data in a generator frame and then
//! give access to it by executing user-provided closures inside that generator.
//! The module provides a safe abstraction for the latter task.
//!
//! The interface consists of two exported macros meant to be used together:
//! * `declare_box_region_type` wraps a generator inside a struct with `access`
//! method which accepts closures.
//! * `box_region_allow_access` is a helper which should be called inside
//! a generator to actually execute those closures.

use std::marker::PhantomData;
use std::ops::{Generator, GeneratorState};
use std::pin::Pin;
Expand All @@ -14,24 +25,23 @@ impl AccessAction {

#[derive(Copy, Clone)]
pub enum Action {
Initial,
Access(AccessAction),
Complete,
}

thread_local!(pub static BOX_REGION_ARG: Cell<Action> = Cell::new(Action::Complete));

pub struct PinnedGenerator<I, A, R> {
generator: Pin<Box<dyn Generator<Yield = YieldType<I, A>, Return = R>>>,
generator: Pin<Box<dyn Generator<Action, Yield = YieldType<I, A>, Return = R>>>,
}

impl<I, A, R> PinnedGenerator<I, A, R> {
pub fn new<T: Generator<Yield = YieldType<I, A>, Return = R> + 'static>(
pub fn new<T: Generator<Action, Yield = YieldType<I, A>, Return = R> + 'static>(
generator: T,
) -> (I, Self) {
let mut result = PinnedGenerator { generator: Box::pin(generator) };

// Run it to the first yield to set it up
let init = match Pin::new(&mut result.generator).resume(()) {
let init = match Pin::new(&mut result.generator).resume(Action::Initial) {
GeneratorState::Yielded(YieldType::Initial(y)) => y,
_ => panic!(),
};
Expand All @@ -40,21 +50,17 @@ impl<I, A, R> PinnedGenerator<I, A, R> {
}

pub unsafe fn access(&mut self, closure: *mut dyn FnMut()) {
BOX_REGION_ARG.with(|i| {
i.set(Action::Access(AccessAction(closure)));
});

// Call the generator, which in turn will call the closure in BOX_REGION_ARG
if let GeneratorState::Complete(_) = Pin::new(&mut self.generator).resume(()) {
// Call the generator, which in turn will call the closure
if let GeneratorState::Complete(_) =
Pin::new(&mut self.generator).resume(Action::Access(AccessAction(closure)))
{
panic!()
}
}

pub fn complete(&mut self) -> R {
// Tell the generator we want it to complete, consuming it and yielding a result
BOX_REGION_ARG.with(|i| i.set(Action::Complete));

let result = Pin::new(&mut self.generator).resume(());
let result = Pin::new(&mut self.generator).resume(Action::Complete);
if let GeneratorState::Complete(r) = result { r } else { panic!() }
}
}
Expand Down Expand Up @@ -89,7 +95,7 @@ macro_rules! declare_box_region_type {
>);

impl $name {
fn new<T: ::std::ops::Generator<Yield = $yield_type, Return = $retc> + 'static>(
fn new<T: ::std::ops::Generator<$crate::box_region::Action, Yield = $yield_type, Return = $retc> + 'static>(
generator: T
) -> ($reti, Self) {
let (initial, pinned) = $crate::box_region::PinnedGenerator::new(generator);
Expand All @@ -98,7 +104,7 @@ macro_rules! declare_box_region_type {

$v fn access<F: for<$($lifetimes)*> FnOnce($($args,)*) -> R, R>(&mut self, f: F) -> R {
// Turn the FnOnce closure into *mut dyn FnMut()
// so we can pass it in to the generator using the BOX_REGION_ARG thread local
// so we can pass it in to the generator
let mut r = None;
let mut f = Some(f);
let mut_f: &mut dyn for<$($lifetimes)*> FnMut(($($args,)*)) =
Expand Down Expand Up @@ -140,9 +146,9 @@ macro_rules! declare_box_region_type {
#[macro_export]
#[allow_internal_unstable(fn_traits)]
macro_rules! box_region_allow_access {
(for($($lifetimes:tt)*), ($($args:ty),*), ($($exprs:expr),*) ) => {
(for($($lifetimes:tt)*), ($($args:ty),*), ($($exprs:expr),*), $action:ident) => {
loop {
match $crate::box_region::BOX_REGION_ARG.with(|i| i.get()) {
match $action {
$crate::box_region::Action::Access(accessor) => {
let accessor: &mut dyn for<$($lifetimes)*> FnMut($($args),*) = unsafe {
::std::mem::transmute(accessor.get())
Expand All @@ -152,10 +158,11 @@ macro_rules! box_region_allow_access {
let marker = $crate::box_region::Marker::<
for<$($lifetimes)*> fn(($($args,)*))
>::new();
yield $crate::box_region::YieldType::Accessor(marker)
$action = yield $crate::box_region::YieldType::Accessor(marker);
};
}
$crate::box_region::Action::Complete => break,
$crate::box_region::Action::Initial => panic!("unexpected box_region action: Initial"),
}
}
}
Expand Down
39 changes: 38 additions & 1 deletion src/librustc_hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ pub enum DefKind {

// Macro namespace
Macro(MacroKind),

// Not namespaced (or they are, but we don't treat them so)
ExternCrate,
Use,
ForeignMod,
AnonConst,
Field,
LifetimeParam,
GlobalAsm,
Impl,
Closure,
Generator,
}

impl DefKind {
Expand Down Expand Up @@ -113,6 +125,16 @@ impl DefKind {
DefKind::TyParam => "type parameter",
DefKind::ConstParam => "const parameter",
DefKind::Macro(macro_kind) => macro_kind.descr(),
DefKind::LifetimeParam => "lifetime parameter",
DefKind::Use => "import",
DefKind::ForeignMod => "foreign module",
DefKind::AnonConst => "constant expression",
DefKind::Field => "field",
DefKind::Impl => "implementation",
DefKind::Closure => "closure",
DefKind::Generator => "generator",
DefKind::ExternCrate => "extern crate",
DefKind::GlobalAsm => "global assembly block",
}
}

Expand All @@ -124,7 +146,10 @@ impl DefKind {
| DefKind::AssocOpaqueTy
| DefKind::AssocFn
| DefKind::Enum
| DefKind::OpaqueTy => "an",
| DefKind::OpaqueTy
| DefKind::Impl
| DefKind::Use
| DefKind::ExternCrate => "an",
DefKind::Macro(macro_kind) => macro_kind.article(),
_ => "a",
}
Expand Down Expand Up @@ -155,6 +180,18 @@ impl DefKind {
| DefKind::AssocConst => ns == Namespace::ValueNS,

DefKind::Macro(..) => ns == Namespace::MacroNS,

// Not namespaced.
DefKind::AnonConst
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::ExternCrate
| DefKind::Closure
| DefKind::Generator
| DefKind::Use
| DefKind::ForeignMod
| DefKind::GlobalAsm
| DefKind::Impl => false,
}
}
}
Expand Down
31 changes: 0 additions & 31 deletions src/librustc_hir/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2452,27 +2452,6 @@ pub enum ItemKind<'hir> {
}

impl ItemKind<'_> {
pub fn descr(&self) -> &str {
match *self {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "`use` import",
ItemKind::Static(..) => "static item",
ItemKind::Const(..) => "constant item",
ItemKind::Fn(..) => "function",
ItemKind::Mod(..) => "module",
ItemKind::ForeignMod(..) => "extern block",
ItemKind::GlobalAsm(..) => "global asm item",
ItemKind::TyAlias(..) => "type alias",
ItemKind::OpaqueTy(..) => "opaque type",
ItemKind::Enum(..) => "enum",
ItemKind::Struct(..) => "struct",
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Impl { .. } => "implementation",
}
}

pub fn generics(&self) -> Option<&Generics<'_>> {
Some(match *self {
ItemKind::Fn(_, ref generics, _)
Expand Down Expand Up @@ -2551,16 +2530,6 @@ pub enum ForeignItemKind<'hir> {
Type,
}

impl ForeignItemKind<'hir> {
pub fn descriptive_variant(&self) -> &str {
match *self {
ForeignItemKind::Fn(..) => "foreign function",
ForeignItemKind::Static(..) => "foreign static item",
ForeignItemKind::Type => "foreign type",
}
}
}

/// A variable captured by a closure.
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable_Generic)]
pub struct Upvar {
Expand Down
7 changes: 1 addition & 6 deletions src/librustc_infer/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
.get_opt_name()
.map(|parent_symbol| parent_symbol.to_string());

let type_parent_desc = self
.tcx
.def_kind(parent_def_id)
.map(|parent_def_kind| parent_def_kind.descr(parent_def_id));

(parent_name, type_parent_desc)
(parent_name, Some(self.tcx.def_kind(parent_def_id).descr(parent_def_id)))
} else {
(None, None)
};
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ pub fn configure_and_expand(
// its contents but the results of name resolution on those contents. Hopefully we'll push
// this back at some point.
let crate_name = crate_name.to_string();
let (result, resolver) = BoxedResolver::new(static move || {
let (result, resolver) = BoxedResolver::new(static move |mut action| {
let _ = action;
let sess = &*sess;
let resolver_arenas = Resolver::arenas();
let res = configure_and_expand_inner(
Expand All @@ -126,11 +127,11 @@ pub fn configure_and_expand(
panic!()
}
Ok((krate, resolver)) => {
yield BoxedResolver::initial_yield(Ok(krate));
action = yield BoxedResolver::initial_yield(Ok(krate));
resolver
}
};
box_region_allow_access!(for(), (&mut Resolver<'_>), (&mut resolver));
box_region_allow_access!(for(), (&mut Resolver<'_>), (&mut resolver), action);
resolver.into_outputs()
});
result.map(|k| (k, resolver))
Expand Down
56 changes: 26 additions & 30 deletions src/librustc_metadata/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ impl MetadataBlob {
}

impl EntryKind {
fn def_kind(&self) -> Option<DefKind> {
Some(match *self {
fn def_kind(&self) -> DefKind {
match *self {
EntryKind::Const(..) => DefKind::Const,
EntryKind::AssocConst(..) => DefKind::AssocConst,
EntryKind::ImmStatic
Expand All @@ -587,14 +587,13 @@ impl EntryKind {
EntryKind::Enum(..) => DefKind::Enum,
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
EntryKind::ForeignType => DefKind::ForeignTy,

EntryKind::ForeignMod
| EntryKind::GlobalAsm
| EntryKind::Impl(_)
| EntryKind::Field
| EntryKind::Generator(_)
| EntryKind::Closure => return None,
})
EntryKind::Impl(_) => DefKind::Impl,
EntryKind::Closure => DefKind::Closure,
EntryKind::ForeignMod => DefKind::ForeignMod,
EntryKind::GlobalAsm => DefKind::GlobalAsm,
EntryKind::Field => DefKind::Field,
EntryKind::Generator(_) => DefKind::Generator,
}
}
}

Expand Down Expand Up @@ -679,11 +678,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
}

fn def_kind(&self, index: DefIndex) -> Option<DefKind> {
fn def_kind(&self, index: DefIndex) -> DefKind {
if !self.is_proc_macro(index) {
self.kind(index).def_kind()
} else {
Some(DefKind::Macro(macro_kind(self.raw_proc_macro(index))))
DefKind::Macro(macro_kind(self.raw_proc_macro(index)))
}
}

Expand Down Expand Up @@ -1009,20 +1008,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.get(self, child_index)
.unwrap_or(Lazy::empty());
for child_index in child_children.decode((self, sess)) {
if let Some(kind) = self.def_kind(child_index) {
callback(Export {
res: Res::Def(kind, self.local_def_id(child_index)),
ident: self.item_ident(child_index, sess),
vis: self.get_visibility(child_index),
span: self
.root
.tables
.span
.get(self, child_index)
.unwrap()
.decode((self, sess)),
});
}
let kind = self.def_kind(child_index);
callback(Export {
res: Res::Def(kind, self.local_def_id(child_index)),
ident: self.item_ident(child_index, sess),
vis: self.get_visibility(child_index),
span: self
.root
.tables
.span
.get(self, child_index)
.unwrap()
.decode((self, sess)),
});
}
continue;
}
Expand All @@ -1033,10 +1031,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

let def_key = self.def_key(child_index);
let span = self.get_span(child_index, sess);
if let (Some(kind), true) = (
self.def_kind(child_index),
def_key.disambiguated_data.data.get_opt_name().is_some(),
) {
if def_key.disambiguated_data.data.get_opt_name().is_some() {
let kind = self.def_kind(child_index);
let ident = self.item_ident(child_index, sess);
let vis = self.get_visibility(child_index);
let def_id = self.local_def_id(child_index);
Expand Down
Loading

0 comments on commit b592b37

Please sign in to comment.