Skip to content

Commit b592b37

Browse files
committed
Auto merge of rust-lang#71566 - Dylan-DPC:rollup-9xoz6fg, r=Dylan-DPC
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
2 parents 0862458 + f70c9db commit b592b37

File tree

54 files changed

+369
-307
lines changed

Some content is hidden

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

54 files changed

+369
-307
lines changed

src/librustc_codegen_ssa/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
157157
let cleanup_kinds = analyze::cleanup_kinds(&mir);
158158
// Allocate a `Block` for every basic block, except
159159
// the start block, if nothing loops back to it.
160-
let reentrant_start_block = !mir.predecessors_for(mir::START_BLOCK).is_empty();
160+
let reentrant_start_block = !mir.predecessors()[mir::START_BLOCK].is_empty();
161161
let block_bxs: IndexVec<mir::BasicBlock, Bx::BasicBlock> = mir
162162
.basic_blocks()
163163
.indices()

src/librustc_data_structures/box_region.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
use std::cell::Cell;
1+
//! This module provides a way to deal with self-referential data.
2+
//!
3+
//! The main idea is to allocate such data in a generator frame and then
4+
//! give access to it by executing user-provided closures inside that generator.
5+
//! The module provides a safe abstraction for the latter task.
6+
//!
7+
//! The interface consists of two exported macros meant to be used together:
8+
//! * `declare_box_region_type` wraps a generator inside a struct with `access`
9+
//! method which accepts closures.
10+
//! * `box_region_allow_access` is a helper which should be called inside
11+
//! a generator to actually execute those closures.
12+
213
use std::marker::PhantomData;
314
use std::ops::{Generator, GeneratorState};
415
use std::pin::Pin;
@@ -14,24 +25,23 @@ impl AccessAction {
1425

1526
#[derive(Copy, Clone)]
1627
pub enum Action {
28+
Initial,
1729
Access(AccessAction),
1830
Complete,
1931
}
2032

21-
thread_local!(pub static BOX_REGION_ARG: Cell<Action> = Cell::new(Action::Complete));
22-
2333
pub struct PinnedGenerator<I, A, R> {
24-
generator: Pin<Box<dyn Generator<Yield = YieldType<I, A>, Return = R>>>,
34+
generator: Pin<Box<dyn Generator<Action, Yield = YieldType<I, A>, Return = R>>>,
2535
}
2636

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

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

4252
pub unsafe fn access(&mut self, closure: *mut dyn FnMut()) {
43-
BOX_REGION_ARG.with(|i| {
44-
i.set(Action::Access(AccessAction(closure)));
45-
});
46-
47-
// Call the generator, which in turn will call the closure in BOX_REGION_ARG
48-
if let GeneratorState::Complete(_) = Pin::new(&mut self.generator).resume(()) {
53+
// Call the generator, which in turn will call the closure
54+
if let GeneratorState::Complete(_) =
55+
Pin::new(&mut self.generator).resume(Action::Access(AccessAction(closure)))
56+
{
4957
panic!()
5058
}
5159
}
5260

5361
pub fn complete(&mut self) -> R {
5462
// Tell the generator we want it to complete, consuming it and yielding a result
55-
BOX_REGION_ARG.with(|i| i.set(Action::Complete));
56-
57-
let result = Pin::new(&mut self.generator).resume(());
63+
let result = Pin::new(&mut self.generator).resume(Action::Complete);
5864
if let GeneratorState::Complete(r) = result { r } else { panic!() }
5965
}
6066
}
@@ -89,7 +95,7 @@ macro_rules! declare_box_region_type {
8995
>);
9096

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

99105
$v fn access<F: for<$($lifetimes)*> FnOnce($($args,)*) -> R, R>(&mut self, f: F) -> R {
100106
// Turn the FnOnce closure into *mut dyn FnMut()
101-
// so we can pass it in to the generator using the BOX_REGION_ARG thread local
107+
// so we can pass it in to the generator
102108
let mut r = None;
103109
let mut f = Some(f);
104110
let mut_f: &mut dyn for<$($lifetimes)*> FnMut(($($args,)*)) =
@@ -140,9 +146,9 @@ macro_rules! declare_box_region_type {
140146
#[macro_export]
141147
#[allow_internal_unstable(fn_traits)]
142148
macro_rules! box_region_allow_access {
143-
(for($($lifetimes:tt)*), ($($args:ty),*), ($($exprs:expr),*) ) => {
149+
(for($($lifetimes:tt)*), ($($args:ty),*), ($($exprs:expr),*), $action:ident) => {
144150
loop {
145-
match $crate::box_region::BOX_REGION_ARG.with(|i| i.get()) {
151+
match $action {
146152
$crate::box_region::Action::Access(accessor) => {
147153
let accessor: &mut dyn for<$($lifetimes)*> FnMut($($args),*) = unsafe {
148154
::std::mem::transmute(accessor.get())
@@ -152,10 +158,11 @@ macro_rules! box_region_allow_access {
152158
let marker = $crate::box_region::Marker::<
153159
for<$($lifetimes)*> fn(($($args,)*))
154160
>::new();
155-
yield $crate::box_region::YieldType::Accessor(marker)
161+
$action = yield $crate::box_region::YieldType::Accessor(marker);
156162
};
157163
}
158164
$crate::box_region::Action::Complete => break,
165+
$crate::box_region::Action::Initial => panic!("unexpected box_region action: Initial"),
159166
}
160167
}
161168
}

src/librustc_hir/def.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ pub enum DefKind {
7777

7878
// Macro namespace
7979
Macro(MacroKind),
80+
81+
// Not namespaced (or they are, but we don't treat them so)
82+
ExternCrate,
83+
Use,
84+
ForeignMod,
85+
AnonConst,
86+
Field,
87+
LifetimeParam,
88+
GlobalAsm,
89+
Impl,
90+
Closure,
91+
Generator,
8092
}
8193

8294
impl DefKind {
@@ -113,6 +125,16 @@ impl DefKind {
113125
DefKind::TyParam => "type parameter",
114126
DefKind::ConstParam => "const parameter",
115127
DefKind::Macro(macro_kind) => macro_kind.descr(),
128+
DefKind::LifetimeParam => "lifetime parameter",
129+
DefKind::Use => "import",
130+
DefKind::ForeignMod => "foreign module",
131+
DefKind::AnonConst => "constant expression",
132+
DefKind::Field => "field",
133+
DefKind::Impl => "implementation",
134+
DefKind::Closure => "closure",
135+
DefKind::Generator => "generator",
136+
DefKind::ExternCrate => "extern crate",
137+
DefKind::GlobalAsm => "global assembly block",
116138
}
117139
}
118140

@@ -124,7 +146,10 @@ impl DefKind {
124146
| DefKind::AssocOpaqueTy
125147
| DefKind::AssocFn
126148
| DefKind::Enum
127-
| DefKind::OpaqueTy => "an",
149+
| DefKind::OpaqueTy
150+
| DefKind::Impl
151+
| DefKind::Use
152+
| DefKind::ExternCrate => "an",
128153
DefKind::Macro(macro_kind) => macro_kind.article(),
129154
_ => "a",
130155
}
@@ -155,6 +180,18 @@ impl DefKind {
155180
| DefKind::AssocConst => ns == Namespace::ValueNS,
156181

157182
DefKind::Macro(..) => ns == Namespace::MacroNS,
183+
184+
// Not namespaced.
185+
DefKind::AnonConst
186+
| DefKind::Field
187+
| DefKind::LifetimeParam
188+
| DefKind::ExternCrate
189+
| DefKind::Closure
190+
| DefKind::Generator
191+
| DefKind::Use
192+
| DefKind::ForeignMod
193+
| DefKind::GlobalAsm
194+
| DefKind::Impl => false,
158195
}
159196
}
160197
}

src/librustc_hir/hir.rs

-31
Original file line numberDiff line numberDiff line change
@@ -2452,27 +2452,6 @@ pub enum ItemKind<'hir> {
24522452
}
24532453

24542454
impl ItemKind<'_> {
2455-
pub fn descr(&self) -> &str {
2456-
match *self {
2457-
ItemKind::ExternCrate(..) => "extern crate",
2458-
ItemKind::Use(..) => "`use` import",
2459-
ItemKind::Static(..) => "static item",
2460-
ItemKind::Const(..) => "constant item",
2461-
ItemKind::Fn(..) => "function",
2462-
ItemKind::Mod(..) => "module",
2463-
ItemKind::ForeignMod(..) => "extern block",
2464-
ItemKind::GlobalAsm(..) => "global asm item",
2465-
ItemKind::TyAlias(..) => "type alias",
2466-
ItemKind::OpaqueTy(..) => "opaque type",
2467-
ItemKind::Enum(..) => "enum",
2468-
ItemKind::Struct(..) => "struct",
2469-
ItemKind::Union(..) => "union",
2470-
ItemKind::Trait(..) => "trait",
2471-
ItemKind::TraitAlias(..) => "trait alias",
2472-
ItemKind::Impl { .. } => "implementation",
2473-
}
2474-
}
2475-
24762455
pub fn generics(&self) -> Option<&Generics<'_>> {
24772456
Some(match *self {
24782457
ItemKind::Fn(_, ref generics, _)
@@ -2551,16 +2530,6 @@ pub enum ForeignItemKind<'hir> {
25512530
Type,
25522531
}
25532532

2554-
impl ForeignItemKind<'hir> {
2555-
pub fn descriptive_variant(&self) -> &str {
2556-
match *self {
2557-
ForeignItemKind::Fn(..) => "foreign function",
2558-
ForeignItemKind::Static(..) => "foreign static item",
2559-
ForeignItemKind::Type => "foreign type",
2560-
}
2561-
}
2562-
}
2563-
25642533
/// A variable captured by a closure.
25652534
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable_Generic)]
25662535
pub struct Upvar {

src/librustc_infer/infer/error_reporting/need_type_info.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
207207
.get_opt_name()
208208
.map(|parent_symbol| parent_symbol.to_string());
209209

210-
let type_parent_desc = self
211-
.tcx
212-
.def_kind(parent_def_id)
213-
.map(|parent_def_kind| parent_def_kind.descr(parent_def_id));
214-
215-
(parent_name, type_parent_desc)
210+
(parent_name, Some(self.tcx.def_kind(parent_def_id).descr(parent_def_id)))
216211
} else {
217212
(None, None)
218213
};

src/librustc_interface/passes.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ pub fn configure_and_expand(
109109
// its contents but the results of name resolution on those contents. Hopefully we'll push
110110
// this back at some point.
111111
let crate_name = crate_name.to_string();
112-
let (result, resolver) = BoxedResolver::new(static move || {
112+
let (result, resolver) = BoxedResolver::new(static move |mut action| {
113+
let _ = action;
113114
let sess = &*sess;
114115
let resolver_arenas = Resolver::arenas();
115116
let res = configure_and_expand_inner(
@@ -126,11 +127,11 @@ pub fn configure_and_expand(
126127
panic!()
127128
}
128129
Ok((krate, resolver)) => {
129-
yield BoxedResolver::initial_yield(Ok(krate));
130+
action = yield BoxedResolver::initial_yield(Ok(krate));
130131
resolver
131132
}
132133
};
133-
box_region_allow_access!(for(), (&mut Resolver<'_>), (&mut resolver));
134+
box_region_allow_access!(for(), (&mut Resolver<'_>), (&mut resolver), action);
134135
resolver.into_outputs()
135136
});
136137
result.map(|k| (k, resolver))

src/librustc_metadata/rmeta/decoder.rs

+26-30
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ impl MetadataBlob {
562562
}
563563

564564
impl EntryKind {
565-
fn def_kind(&self) -> Option<DefKind> {
566-
Some(match *self {
565+
fn def_kind(&self) -> DefKind {
566+
match *self {
567567
EntryKind::Const(..) => DefKind::Const,
568568
EntryKind::AssocConst(..) => DefKind::AssocConst,
569569
EntryKind::ImmStatic
@@ -587,14 +587,13 @@ impl EntryKind {
587587
EntryKind::Enum(..) => DefKind::Enum,
588588
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
589589
EntryKind::ForeignType => DefKind::ForeignTy,
590-
591-
EntryKind::ForeignMod
592-
| EntryKind::GlobalAsm
593-
| EntryKind::Impl(_)
594-
| EntryKind::Field
595-
| EntryKind::Generator(_)
596-
| EntryKind::Closure => return None,
597-
})
590+
EntryKind::Impl(_) => DefKind::Impl,
591+
EntryKind::Closure => DefKind::Closure,
592+
EntryKind::ForeignMod => DefKind::ForeignMod,
593+
EntryKind::GlobalAsm => DefKind::GlobalAsm,
594+
EntryKind::Field => DefKind::Field,
595+
EntryKind::Generator(_) => DefKind::Generator,
596+
}
598597
}
599598
}
600599

@@ -679,11 +678,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
679678
}
680679
}
681680

682-
fn def_kind(&self, index: DefIndex) -> Option<DefKind> {
681+
fn def_kind(&self, index: DefIndex) -> DefKind {
683682
if !self.is_proc_macro(index) {
684683
self.kind(index).def_kind()
685684
} else {
686-
Some(DefKind::Macro(macro_kind(self.raw_proc_macro(index))))
685+
DefKind::Macro(macro_kind(self.raw_proc_macro(index)))
687686
}
688687
}
689688

@@ -1009,20 +1008,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10091008
.get(self, child_index)
10101009
.unwrap_or(Lazy::empty());
10111010
for child_index in child_children.decode((self, sess)) {
1012-
if let Some(kind) = self.def_kind(child_index) {
1013-
callback(Export {
1014-
res: Res::Def(kind, self.local_def_id(child_index)),
1015-
ident: self.item_ident(child_index, sess),
1016-
vis: self.get_visibility(child_index),
1017-
span: self
1018-
.root
1019-
.tables
1020-
.span
1021-
.get(self, child_index)
1022-
.unwrap()
1023-
.decode((self, sess)),
1024-
});
1025-
}
1011+
let kind = self.def_kind(child_index);
1012+
callback(Export {
1013+
res: Res::Def(kind, self.local_def_id(child_index)),
1014+
ident: self.item_ident(child_index, sess),
1015+
vis: self.get_visibility(child_index),
1016+
span: self
1017+
.root
1018+
.tables
1019+
.span
1020+
.get(self, child_index)
1021+
.unwrap()
1022+
.decode((self, sess)),
1023+
});
10261024
}
10271025
continue;
10281026
}
@@ -1033,10 +1031,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10331031

10341032
let def_key = self.def_key(child_index);
10351033
let span = self.get_span(child_index, sess);
1036-
if let (Some(kind), true) = (
1037-
self.def_kind(child_index),
1038-
def_key.disambiguated_data.data.get_opt_name().is_some(),
1039-
) {
1034+
if def_key.disambiguated_data.data.get_opt_name().is_some() {
1035+
let kind = self.def_kind(child_index);
10401036
let ident = self.item_ident(child_index, sess);
10411037
let vis = self.get_visibility(child_index);
10421038
let def_id = self.local_def_id(child_index);

0 commit comments

Comments
 (0)