Skip to content

Commit 66500ef

Browse files
committed
add generator_kind query
1 parent 245062c commit 66500ef

File tree

9 files changed

+60
-40
lines changed

9 files changed

+60
-40
lines changed

src/librustc/query/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ rustc_queries! {
308308
/// Returns `Some(mutability)` if the node pointed to by `def_id` is a static item.
309309
query static_mutability(_: DefId) -> Option<hir::Mutability> {}
310310

311+
/// Returns `Some(generator_kind)` if the node pointed to by `def_id` is a generator.
312+
query generator_kind(_: DefId) -> Option<hir::GeneratorKind> {}
313+
311314
/// Gets a map with the variance of every item; use `item_variance` instead.
312315
query crate_variances(_: CrateNum) -> &'tcx ty::CrateVariancesMap<'tcx> {
313316
desc { "computing the variances for items in this crate" }

src/librustc/ty/context.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::dep_graph::DepGraph;
55
use crate::dep_graph::{self, DepConstructor};
66
use crate::hir::exports::Export;
77
use crate::hir::map as hir_map;
8-
use crate::hir::map::DefPathHash;
8+
use crate::hir::map::{DefPathData, DefPathHash};
99
use crate::ich::{NodeIdHashingMode, StableHashingContext};
1010
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
1111
use crate::lint::{struct_lint_level, LintSource};
@@ -1513,14 +1513,18 @@ impl<'tcx> TyCtxt<'tcx> {
15131513
)
15141514
}
15151515

1516-
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "closure")`).
1517-
pub fn article_and_description(
1518-
&self,
1519-
def_id: crate::hir::def_id::DefId,
1520-
) -> (&'static str, &'static str) {
1521-
match self.def_kind(def_id) {
1522-
Some(def_kind) => (def_kind.article(), def_kind.descr(def_id)),
1523-
None => self.type_of(def_id).kind.article_and_description(),
1516+
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "struct")`).
1517+
pub fn article_and_description(&self, def_id: DefId) -> (&'static str, &'static str) {
1518+
match self.def_key(def_id).disambiguated_data.data {
1519+
DefPathData::TypeNs(..) | DefPathData::ValueNs(..) | DefPathData::MacroNs(..) => {
1520+
let kind = self.def_kind(def_id).unwrap();
1521+
(kind.article(), kind.descr(def_id))
1522+
}
1523+
DefPathData::ClosureExpr => {
1524+
// TODO
1525+
todo!();
1526+
}
1527+
_ => bug!("article_and_description called on def_id {:?}", def_id),
15241528
}
15251529
}
15261530
}

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl MetadataBlob {
500500
}
501501
}
502502

503-
impl<'tcx> EntryKind<'tcx> {
503+
impl EntryKind {
504504
fn def_kind(&self) -> Option<DefKind> {
505505
Some(match *self {
506506
EntryKind::Const(..) => DefKind::Const,
@@ -614,11 +614,11 @@ impl<'a, 'tcx> CrateMetadata {
614614
self.root.proc_macro_data.and_then(|data| data.decode(self).find(|x| *x == id)).is_some()
615615
}
616616

617-
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind<'tcx>> {
617+
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
618618
self.root.per_def.kind.get(self, item_id).map(|k| k.decode(self))
619619
}
620620

621-
fn kind(&self, item_id: DefIndex) -> EntryKind<'tcx> {
621+
fn kind(&self, item_id: DefIndex) -> EntryKind {
622622
assert!(!self.is_proc_macro(item_id));
623623
self.maybe_kind(item_id).unwrap_or_else(|| {
624624
bug!(
@@ -723,7 +723,7 @@ impl<'a, 'tcx> CrateMetadata {
723723
fn get_variant(
724724
&self,
725725
tcx: TyCtxt<'tcx>,
726-
kind: &EntryKind<'_>,
726+
kind: &EntryKind,
727727
index: DefIndex,
728728
parent_did: DefId,
729729
) -> ty::VariantDef {
@@ -1390,6 +1390,13 @@ impl<'a, 'tcx> CrateMetadata {
13901390
}
13911391
}
13921392

1393+
fn generator_kind(&self, id: DefIndex) -> Option<hir::GeneratorKind> {
1394+
match self.kind(id) {
1395+
EntryKind::Generator(data) => Some(data.decode(self)),
1396+
_ => None,
1397+
}
1398+
}
1399+
13931400
fn fn_sig(&self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
13941401
self.root.per_def.fn_sig.get(self, id).unwrap().decode((self, tcx))
13951402
}
@@ -1499,8 +1506,8 @@ impl<'a, 'tcx> CrateMetadata {
14991506
);
15001507
debug!(
15011508
"CrateMetaData::imported_source_files alloc \
1502-
source_file {:?} original (start_pos {:?} end_pos {:?}) \
1503-
translated (start_pos {:?} end_pos {:?})",
1509+
source_file {:?} original (start_pos {:?} end_pos {:?}) \
1510+
translated (start_pos {:?} end_pos {:?})",
15041511
local_version.name,
15051512
start_pos,
15061513
end_pos,

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
134134
asyncness => { cdata.asyncness(def_id.index) }
135135
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
136136
static_mutability => { cdata.static_mutability(def_id.index) }
137+
generator_kind => { cdata.generator_kind(def_id.index) }
137138
def_kind => { cdata.def_kind(def_id.index) }
138139
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
139140
lookup_stability => {

src/librustc_metadata/rmeta/encoder.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'tcx> EncodeContext<'tcx> {
306306
assert!(
307307
last_min_end <= lazy.position,
308308
"make sure that the calls to `lazy*` \
309-
are in the same order as the metadata fields",
309+
are in the same order as the metadata fields",
310310
);
311311
lazy.position.get() - last_min_end.get()
312312
}
@@ -1248,12 +1248,7 @@ impl EncodeContext<'tcx> {
12481248
self.encode_deprecation(def_id);
12491249
}
12501250

1251-
fn encode_info_for_generic_param(
1252-
&mut self,
1253-
def_id: DefId,
1254-
kind: EntryKind<'tcx>,
1255-
encode_type: bool,
1256-
) {
1251+
fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) {
12571252
record!(self.per_def.kind[def_id] <- kind);
12581253
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
12591254
record!(self.per_def.span[def_id] <- self.tcx.def_span(def_id));
@@ -1271,11 +1266,8 @@ impl EncodeContext<'tcx> {
12711266
let ty = self.tcx.typeck_tables_of(def_id).node_type(hir_id);
12721267

12731268
record!(self.per_def.kind[def_id] <- match ty.kind {
1274-
ty::Generator(def_id, ..) => {
1275-
let layout = self.tcx.generator_layout(def_id);
1276-
let data = GeneratorData {
1277-
layout: layout.clone(),
1278-
};
1269+
ty::Generator(..) => {
1270+
let data = self.tcx.generator_kind(def_id).unwrap();
12791271
EntryKind::Generator(self.lazy(data))
12801272
}
12811273

src/librustc_metadata/rmeta/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ macro_rules! define_per_def_tables {
252252
}
253253

254254
define_per_def_tables! {
255-
kind: Table<DefIndex, Lazy!(EntryKind<'tcx>)>,
255+
kind: Table<DefIndex, Lazy<EntryKind>>,
256256
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
257257
span: Table<DefIndex, Lazy<Span>>,
258258
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
@@ -279,7 +279,7 @@ define_per_def_tables! {
279279
}
280280

281281
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
282-
enum EntryKind<'tcx> {
282+
enum EntryKind {
283283
Const(mir::ConstQualifs, Lazy<RenderedConst>),
284284
ImmStatic,
285285
MutStatic,
@@ -302,7 +302,7 @@ enum EntryKind<'tcx> {
302302
Mod(Lazy<ModData>),
303303
MacroDef(Lazy<MacroDef>),
304304
Closure,
305-
Generator(Lazy!(GeneratorData<'tcx>)),
305+
Generator(Lazy<hir::GeneratorKind>),
306306
Trait(Lazy<TraitData>),
307307
Impl(Lazy<ImplData>),
308308
Method(Lazy<MethodData>),

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
427427
errci.outlived_fr,
428428
);
429429

430-
let (_, escapes_from) =
431-
self.infcx.tcx.article_and_description(self.universal_regions.defining_ty.def_id());
430+
let (_, escapes_from) = self
431+
.infcx
432+
.tcx
433+
.article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
432434

433435
// Revert to the normal error in these cases.
434436
// Assignments aren't "escapes" in function items.
435437
if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
436438
|| (*category == ConstraintCategory::Assignment
437-
&& self.universal_regions.defining_ty.is_fn_def())
438-
|| self.universal_regions.defining_ty.is_const()
439+
&& self.regioncx.universal_regions().defining_ty.is_fn_def())
440+
|| self.regioncx.universal_regions().defining_ty.is_const()
439441
{
440442
return self.report_general_error(&ErrorConstraintInfo {
441443
fr_is_local: true,

src/librustc_mir/borrow_check/universal_regions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,16 @@ impl<'tcx> DefiningTy<'tcx> {
132132
}
133133
}
134134

135-
pub fn is_closure(&self) -> bool {
135+
pub fn is_fn_def(&self) -> bool {
136136
match *self {
137-
DefiningTy::Closure(..) => true,
137+
DefiningTy::FnDef(..) => true,
138138
_ => false,
139139
}
140140
}
141141

142-
pub fn is_fn_def(&self) -> bool {
142+
pub fn is_const(&self) -> bool {
143143
match *self {
144-
DefiningTy::FnDef(..) => true,
144+
DefiningTy::Const(..) => true,
145145
_ => false,
146146
}
147147
}

src/librustc_typeck/collect.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub fn provide(providers: &mut Providers<'_>) {
7676
impl_polarity,
7777
is_foreign_item,
7878
static_mutability,
79+
generator_kind,
7980
codegen_fn_attrs,
8081
collect_mod_item_types,
8182
..*providers
@@ -1006,7 +1007,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TraitDef {
10061007
.struct_span_err(
10071008
item.span,
10081009
"the `#[rustc_paren_sugar]` attribute is a temporary means of controlling \
1009-
which traits can use parenthetical notation",
1010+
which traits can use parenthetical notation",
10101011
)
10111012
.help("add `#![feature(unboxed_closures)]` to the crate attributes to use it")
10121013
.emit();
@@ -2106,7 +2107,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
21062107
ast_ty.span,
21072108
&format!(
21082109
"use of SIMD type `{}` in FFI is highly experimental and \
2109-
may result in invalid code",
2110+
may result in invalid code",
21102111
tcx.hir().hir_to_pretty_string(ast_ty.hir_id)
21112112
),
21122113
)
@@ -2145,6 +2146,16 @@ fn static_mutability(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::Mutability>
21452146
}
21462147
}
21472148

2149+
fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> {
2150+
match tcx.hir().get_if_local(def_id) {
2151+
Some(Node::Item(&hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => {
2152+
tcx.hir().body(body_id).generator_kind()
2153+
}
2154+
Some(_) => None,
2155+
_ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
2156+
}
2157+
}
2158+
21482159
fn from_target_feature(
21492160
tcx: TyCtxt<'_>,
21502161
id: DefId,

0 commit comments

Comments
 (0)