Skip to content

Commit acf50b7

Browse files
committed
Auto merge of #51806 - oli-obk:lowering_cleanups1, r=cramertj
Lowering cleanups [1/N]
2 parents 87ecf54 + 99575b5 commit acf50b7

File tree

19 files changed

+207
-116
lines changed

19 files changed

+207
-116
lines changed

Diff for: src/librustc/hir/intravisit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,11 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
607607
}
608608
visitor.visit_lifetime(lifetime);
609609
}
610-
TyImplTraitExistential(item_id, def_id, ref lifetimes) => {
610+
TyImplTraitExistential(_, def_id, ref lifetimes) => {
611+
// we are not recursing into the `existential` item, because it is already being visited
612+
// as part of the surrounding module. The `NodeId` just exists so we don't have to look
613+
// it up everywhere else in the compiler
611614
visitor.visit_def_mention(Def::Existential(def_id));
612-
visitor.visit_nested_item(item_id);
613615
walk_list!(visitor, visit_lifetime, lifetimes);
614616
}
615617
TyTypeof(ref expression) => {

Diff for: src/librustc/hir/lowering.rs

+103-57
Large diffs are not rendered by default.

Diff for: src/librustc/hir/map/collector.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,27 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
221221
// Make sure that the DepNode of some node coincides with the HirId
222222
// owner of that node.
223223
if cfg!(debug_assertions) {
224-
let hir_id_owner = self.definitions.node_to_hir_id(id).owner;
224+
let hir_id = self.definitions.node_to_hir_id(id);
225225

226-
if hir_id_owner != self.current_dep_node_owner {
226+
if hir_id.owner != self.current_dep_node_owner {
227227
let node_str = match self.definitions.opt_def_index(id) {
228228
Some(def_index) => {
229229
self.definitions.def_path(def_index).to_string_no_crate()
230230
}
231231
None => format!("{:?}", node)
232232
};
233233

234+
if hir_id == ::hir::DUMMY_HIR_ID {
235+
debug!("Maybe you forgot to lower the node id {:?}?", id);
236+
}
237+
234238
bug!("inconsistent DepNode for `{}`: \
235239
current_dep_node_owner={}, hir_id.owner={}",
236240
node_str,
237241
self.definitions
238242
.def_path(self.current_dep_node_owner)
239243
.to_string_no_crate(),
240-
self.definitions.def_path(hir_id_owner).to_string_no_crate())
244+
self.definitions.def_path(hir_id.owner).to_string_no_crate())
241245
}
242246
}
243247

Diff for: src/librustc/hir/map/def_collector.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl<'a> DefCollector<'a> {
7777
&mut self,
7878
id: NodeId,
7979
async_node_id: NodeId,
80+
return_impl_trait_id: NodeId,
8081
name: Name,
8182
span: Span,
8283
visit_fn: impl FnOnce(&mut DefCollector<'a>)
@@ -86,6 +87,7 @@ impl<'a> DefCollector<'a> {
8687
let fn_def_data = DefPathData::ValueNs(name.as_interned_str());
8788
let fn_def = self.create_def(id, fn_def_data, ITEM_LIKE_SPACE, span);
8889
return self.with_parent(fn_def, |this| {
90+
this.create_def(return_impl_trait_id, DefPathData::ImplTrait, REGULAR_SPACE, span);
8991
let closure_def = this.create_def(async_node_id,
9092
DefPathData::ClosureExpr,
9193
REGULAR_SPACE,
@@ -120,10 +122,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
120122
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
121123
return visit::walk_item(self, i);
122124
}
123-
ItemKind::Fn(_, FnHeader { asyncness: IsAsync::Async(async_node_id), .. }, ..) => {
125+
ItemKind::Fn(_, FnHeader { asyncness: IsAsync::Async {
126+
closure_id,
127+
return_impl_trait_id,
128+
}, .. }, ..) => {
124129
return self.visit_async_fn(
125130
i.id,
126-
async_node_id,
131+
closure_id,
132+
return_impl_trait_id,
127133
i.ident.name,
128134
i.span,
129135
|this| visit::walk_item(this, i)
@@ -227,11 +233,15 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
227233
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
228234
let def_data = match ii.node {
229235
ImplItemKind::Method(MethodSig {
230-
header: FnHeader { asyncness: IsAsync::Async(async_node_id), .. }, ..
236+
header: FnHeader { asyncness: IsAsync::Async {
237+
closure_id,
238+
return_impl_trait_id,
239+
}, .. }, ..
231240
}, ..) => {
232241
return self.visit_async_fn(
233242
ii.id,
234-
async_node_id,
243+
closure_id,
244+
return_impl_trait_id,
235245
ii.ident.name,
236246
ii.span,
237247
|this| visit::walk_impl_item(this, ii)
@@ -276,8 +286,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
276286

277287
// Async closures desugar to closures inside of closures, so
278288
// we must create two defs.
279-
if let IsAsync::Async(async_id) = asyncness {
280-
let async_def = self.create_def(async_id,
289+
if let IsAsync::Async { closure_id, .. } = asyncness {
290+
let async_def = self.create_def(closure_id,
281291
DefPathData::ClosureExpr,
282292
REGULAR_SPACE,
283293
expr.span);
@@ -301,6 +311,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
301311
fn visit_ty(&mut self, ty: &'a Ty) {
302312
match ty.node {
303313
TyKind::Mac(..) => return self.visit_macro_invoc(ty.id),
314+
TyKind::ImplTrait(node_id, _) => {
315+
self.create_def(node_id, DefPathData::ImplTrait, REGULAR_SPACE, ty.span);
316+
}
304317
_ => {}
305318
}
306319
visit::walk_ty(self, ty);

Diff for: src/librustc/hir/map/definitions.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,8 @@ pub enum DefPathData {
374374
StructCtor,
375375
/// A constant expression (see {ast,hir}::AnonConst).
376376
AnonConst,
377-
/// An `impl Trait` type node in argument position.
378-
UniversalImplTrait,
379-
/// An `impl Trait` type node in return position.
380-
ExistentialImplTrait,
377+
/// An `impl Trait` type node
378+
ImplTrait,
381379

382380
/// GlobalMetaData identifies a piece of crate metadata that is global to
383381
/// a whole crate (as opposed to just one item). GlobalMetaData components
@@ -641,8 +639,7 @@ impl DefPathData {
641639
ClosureExpr |
642640
StructCtor |
643641
AnonConst |
644-
ExistentialImplTrait |
645-
UniversalImplTrait => None
642+
ImplTrait => None
646643
}
647644
}
648645

@@ -672,8 +669,7 @@ impl DefPathData {
672669
ClosureExpr => "{{closure}}",
673670
StructCtor => "{{constructor}}",
674671
AnonConst => "{{constant}}",
675-
ExistentialImplTrait => "{{exist-impl-Trait}}",
676-
UniversalImplTrait => "{{univ-impl-Trait}}",
672+
ImplTrait => "{{impl-Trait}}",
677673
};
678674

679675
Symbol::intern(s).as_interned_str()

Diff for: src/librustc/ty/item_path.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
221221
data @ DefPathData::AnonConst |
222222
data @ DefPathData::MacroDef(..) |
223223
data @ DefPathData::ClosureExpr |
224-
data @ DefPathData::ExistentialImplTrait |
225-
data @ DefPathData::UniversalImplTrait |
224+
data @ DefPathData::ImplTrait |
226225
data @ DefPathData::GlobalMetaData(..) => {
227226
let parent_def_id = self.parent_def_id(def_id).unwrap();
228227
self.push_item_path(buffer, parent_def_id);

Diff for: src/librustc/util/ppaux.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ impl PrintContext {
291291
DefPathData::Field(_) |
292292
DefPathData::StructCtor |
293293
DefPathData::AnonConst |
294-
DefPathData::ExistentialImplTrait |
295-
DefPathData::UniversalImplTrait |
294+
DefPathData::ImplTrait |
296295
DefPathData::GlobalMetaData(_) => {
297296
// if we're making a symbol for something, there ought
298297
// to be a value or type-def or something in there

Diff for: src/librustc_driver/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ impl<'a> ReplaceBodyWithLoop<'a> {
669669
if let ast::FunctionRetTy::Ty(ref ty) = ret_ty.output {
670670
fn involves_impl_trait(ty: &ast::Ty) -> bool {
671671
match ty.node {
672-
ast::TyKind::ImplTrait(_) => true,
672+
ast::TyKind::ImplTrait(..) => true,
673673
ast::TyKind::Slice(ref subty) |
674674
ast::TyKind::Array(ref subty, _) |
675675
ast::TyKind::Ptr(ast::MutTy { ty: ref subty, .. }) |

Diff for: src/librustc_passes/ast_validation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
208208
}
209209
self.no_questions_in_bounds(bounds, "trait object types", false);
210210
}
211-
TyKind::ImplTrait(ref bounds) => {
211+
TyKind::ImplTrait(_, ref bounds) => {
212212
if !bounds.iter()
213213
.any(|b| if let GenericBound::Trait(..) = *b { true } else { false }) {
214214
self.err_handler().span_err(ty.span, "at least one trait must be specified");
@@ -505,7 +505,7 @@ impl<'a> NestedImplTraitVisitor<'a> {
505505

506506
impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
507507
fn visit_ty(&mut self, t: &'a Ty) {
508-
if let TyKind::ImplTrait(_) = t.node {
508+
if let TyKind::ImplTrait(..) = t.node {
509509
if let Some(outer_impl_trait) = self.outer_impl_trait {
510510
struct_span_err!(self.session, t.span, E0666,
511511
"nested `impl Trait` is not allowed")
@@ -570,7 +570,7 @@ impl<'a> ImplTraitProjectionVisitor<'a> {
570570
impl<'a> Visitor<'a> for ImplTraitProjectionVisitor<'a> {
571571
fn visit_ty(&mut self, t: &'a Ty) {
572572
match t.node {
573-
TyKind::ImplTrait(_) => {
573+
TyKind::ImplTrait(..) => {
574574
if self.is_banned {
575575
struct_span_err!(self.session, t.span, E0667,
576576
"`impl Trait` is not allowed in path parameters")

Diff for: src/librustc_resolve/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
777777
visit::walk_fn_ret_ty(self, &declaration.output);
778778

779779
// Resolve the function body, potentially inside the body of an async closure
780-
if let IsAsync::Async(async_closure_id) = asyncness {
781-
let rib_kind = ClosureRibKind(async_closure_id);
780+
if let IsAsync::Async { closure_id, .. } = asyncness {
781+
let rib_kind = ClosureRibKind(closure_id);
782782
self.ribs[ValueNS].push(Rib::new(rib_kind));
783783
self.label_ribs.push(Rib::new(rib_kind));
784784
}
@@ -3933,8 +3933,9 @@ impl<'a> Resolver<'a> {
39333933
// resolve the arguments within the proper scopes so that usages of them inside the
39343934
// closure are detected as upvars rather than normal closure arg usages.
39353935
ExprKind::Closure(
3936-
_, IsAsync::Async(inner_closure_id), _, ref fn_decl, ref body, _span) =>
3937-
{
3936+
_, IsAsync::Async { closure_id: inner_closure_id, .. }, _,
3937+
ref fn_decl, ref body, _span,
3938+
) => {
39383939
let rib_kind = ClosureRibKind(expr.id);
39393940
self.ribs[ValueNS].push(Rib::new(rib_kind));
39403941
self.label_ribs.push(Rib::new(rib_kind));

Diff for: src/librustc_save_analysis/sig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl Sig for ast::Ty {
306306
let nested = pprust::bounds_to_string(bounds);
307307
Ok(text_sig(nested))
308308
}
309-
ast::TyKind::ImplTrait(ref bounds) => {
309+
ast::TyKind::ImplTrait(_, ref bounds) => {
310310
// FIXME recurse into bounds
311311
let nested = pprust::bounds_to_string(bounds);
312312
Ok(text_sig(format!("impl {}", nested)))

Diff for: src/libsyntax/ast.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,11 @@ pub enum TyKind {
15471547
TraitObject(GenericBounds, TraitObjectSyntax),
15481548
/// An `impl Bound1 + Bound2 + Bound3` type
15491549
/// where `Bound` is a trait or a lifetime.
1550-
ImplTrait(GenericBounds),
1550+
///
1551+
/// The `NodeId` exists to prevent lowering from having to
1552+
/// generate `NodeId`s on the fly, which would complicate
1553+
/// the generation of `existential type` items significantly
1554+
ImplTrait(NodeId, GenericBounds),
15511555
/// No-op; kept solely so that we can pretty-print faithfully
15521556
Paren(P<Ty>),
15531557
/// Unused for now
@@ -1718,18 +1722,28 @@ pub enum Unsafety {
17181722

17191723
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
17201724
pub enum IsAsync {
1721-
Async(NodeId),
1725+
Async {
1726+
closure_id: NodeId,
1727+
return_impl_trait_id: NodeId,
1728+
},
17221729
NotAsync,
17231730
}
17241731

17251732
impl IsAsync {
17261733
pub fn is_async(self) -> bool {
1727-
if let IsAsync::Async(_) = self {
1734+
if let IsAsync::Async { .. } = self {
17281735
true
17291736
} else {
17301737
false
17311738
}
17321739
}
1740+
/// In case this is an `Async` return the `NodeId` for the generated impl Trait item
1741+
pub fn opt_return_id(self) -> Option<NodeId> {
1742+
match self {
1743+
IsAsync::Async { return_impl_trait_id, .. } => Some(return_impl_trait_id),
1744+
IsAsync::NotAsync => None,
1745+
}
1746+
}
17331747
}
17341748

17351749
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]

Diff for: src/libsyntax/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
17261726
"labels on blocks are unstable");
17271727
}
17281728
}
1729-
ast::ExprKind::Closure(_, ast::IsAsync::Async(_), ..) => {
1729+
ast::ExprKind::Closure(_, ast::IsAsync::Async { .. }, ..) => {
17301730
gate_feature_post!(&self, async_await, e.span, "async closures are unstable");
17311731
}
17321732
ast::ExprKind::Async(..) => {

Diff for: src/libsyntax/fold.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ pub trait Folder : Sized {
100100
noop_fold_fn_decl(d, self)
101101
}
102102

103+
fn fold_asyncness(&mut self, a: IsAsync) -> IsAsync {
104+
noop_fold_asyncness(a, self)
105+
}
106+
103107
fn fold_block(&mut self, b: P<Block>) -> P<Block> {
104108
noop_fold_block(b, self)
105109
}
@@ -396,8 +400,8 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
396400
TyKind::TraitObject(bounds, syntax) => {
397401
TyKind::TraitObject(bounds.move_map(|b| fld.fold_param_bound(b)), syntax)
398402
}
399-
TyKind::ImplTrait(bounds) => {
400-
TyKind::ImplTrait(bounds.move_map(|b| fld.fold_param_bound(b)))
403+
TyKind::ImplTrait(id, bounds) => {
404+
TyKind::ImplTrait(fld.new_id(id), bounds.move_map(|b| fld.fold_param_bound(b)))
401405
}
402406
TyKind::Mac(mac) => {
403407
TyKind::Mac(fld.fold_mac(mac))
@@ -669,6 +673,16 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
669673
}
670674
}
671675

676+
pub fn noop_fold_asyncness<T: Folder>(asyncness: IsAsync, fld: &mut T) -> IsAsync {
677+
match asyncness {
678+
IsAsync::Async { closure_id, return_impl_trait_id } => IsAsync::Async {
679+
closure_id: fld.new_id(closure_id),
680+
return_impl_trait_id: fld.new_id(return_impl_trait_id),
681+
},
682+
IsAsync::NotAsync => IsAsync::NotAsync,
683+
}
684+
}
685+
672686
pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
673687
decl.map(|FnDecl {inputs, output, variadic}| FnDecl {
674688
inputs: inputs.move_map(|x| fld.fold_arg(x)),
@@ -996,10 +1010,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T)
9961010
}
9971011

9981012
pub fn noop_fold_fn_header<T: Folder>(mut header: FnHeader, folder: &mut T) -> FnHeader {
999-
header.asyncness = match header.asyncness {
1000-
IsAsync::Async(node_id) => IsAsync::Async(folder.new_id(node_id)),
1001-
IsAsync::NotAsync => IsAsync::NotAsync,
1002-
};
1013+
header.asyncness = folder.fold_asyncness(header.asyncness);
10031014
header
10041015
}
10051016

@@ -1249,12 +1260,8 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
12491260
arms.move_map(|x| folder.fold_arm(x)))
12501261
}
12511262
ExprKind::Closure(capture_clause, asyncness, movability, decl, body, span) => {
1252-
let asyncness = match asyncness {
1253-
IsAsync::Async(node_id) => IsAsync::Async(folder.new_id(node_id)),
1254-
IsAsync::NotAsync => IsAsync::NotAsync,
1255-
};
12561263
ExprKind::Closure(capture_clause,
1257-
asyncness,
1264+
folder.fold_asyncness(asyncness),
12581265
movability,
12591266
folder.fold_fn_decl(decl),
12601267
folder.fold_expr(body),
@@ -1265,7 +1272,11 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
12651272
opt_label.map(|label| folder.fold_label(label)))
12661273
}
12671274
ExprKind::Async(capture_clause, node_id, body) => {
1268-
ExprKind::Async(capture_clause, folder.new_id(node_id), folder.fold_block(body))
1275+
ExprKind::Async(
1276+
capture_clause,
1277+
folder.new_id(node_id),
1278+
folder.fold_block(body),
1279+
)
12691280
}
12701281
ExprKind::Assign(el, er) => {
12711282
ExprKind::Assign(folder.fold_expr(el), folder.fold_expr(er))

0 commit comments

Comments
 (0)