Skip to content

Commit 4ecc85b

Browse files
committed
Auto merge of #38449 - eddyb:lazy-10, r=nikomatsakis
[10/n] Split constants and functions' arguments into disjoint bodies. _This is part of a series ([prev](#38053) | [next]()) of patches designed to rework rustc into an out-of-order on-demand pipeline model for both better feature support (e.g. [MIR-based](https://github.com/solson/miri) early constant evaluation) and incremental execution of compiler passes (e.g. type-checking), with beneficial consequences to IDE support as well. If any motivation is unclear, please ask for additional PR description clarifications or code comments._ <hr> Finishes the signature-body split started in #37918, namely: * `trait` items are separated just like `impl` items were, for uniformity, closing #37712 * `static`s, `const`s (including associated ones), `enum` discriminants and array lengths get bodies * even the count in "repeat expressions", i.e. `n` in `[x; n]`, which fixes #24414 * arguments' patterns are moved to the bodies, with the types staying in `FnDecl` * `&self` now desugars to `self: &Self` instead of `self: &_` (similarly for other `self` forms) * `astconv`'s and metadata's (for rustdoc) informative uses are explicitly ignored for the purposes of the dep graph. this could be fixed in the future by hashing the exact information being extracted about the arguments as opposed to generating a dependency on *the whole body*
2 parents 02b22ec + ee0ea95 commit 4ecc85b

File tree

124 files changed

+2280
-2757
lines changed

Some content is hidden

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

124 files changed

+2280
-2757
lines changed

src/librustc/cfg/construct.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,6 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
327327
self.opt_expr(base, field_cfg)
328328
}
329329

330-
hir::ExprRepeat(ref elem, ref count) => {
331-
self.straightline(expr, pred, [elem, count].iter().map(|&e| &**e))
332-
}
333-
334330
hir::ExprAssign(ref l, ref r) |
335331
hir::ExprAssignOp(_, ref l, ref r) => {
336332
self.straightline(expr, pred, [r, l].iter().map(|&e| &**e))
@@ -347,7 +343,8 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
347343
hir::ExprType(ref e, _) |
348344
hir::ExprUnary(_, ref e) |
349345
hir::ExprField(ref e, _) |
350-
hir::ExprTupField(ref e, _) => {
346+
hir::ExprTupField(ref e, _) |
347+
hir::ExprRepeat(ref e, _) => {
351348
self.straightline(expr, pred, Some(&**e).into_iter())
352349
}
353350

src/librustc/dep_graph/graph.rs

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ impl DepGraph {
5151
}
5252
}
5353

54+
/// True if we are actually building the full dep-graph.
55+
#[inline]
56+
pub fn is_fully_enabled(&self) -> bool {
57+
self.data.thread.is_fully_enabled()
58+
}
59+
5460
pub fn query(&self) -> DepGraphQuery<DefId> {
5561
self.data.thread.query()
5662
}

src/librustc/dep_graph/visit.rs

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
4545
debug!("Ended task {:?}", task_id);
4646
}
4747

48+
fn visit_trait_item(&mut self, i: &'tcx hir::TraitItem) {
49+
let trait_item_def_id = self.tcx.map.local_def_id(i.id);
50+
let task_id = (self.dep_node_fn)(trait_item_def_id);
51+
let _task = self.tcx.dep_graph.in_task(task_id.clone());
52+
debug!("Started task {:?}", task_id);
53+
self.tcx.dep_graph.read(DepNode::Hir(trait_item_def_id));
54+
self.visitor.visit_trait_item(i);
55+
debug!("Ended task {:?}", task_id);
56+
}
57+
4858
fn visit_impl_item(&mut self, i: &'tcx hir::ImplItem) {
4959
let impl_item_def_id = self.tcx.map.local_def_id(i.id);
5060
let task_id = (self.dep_node_fn)(impl_item_def_id);

src/librustc/hir/intravisit.rs

+72-72
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ pub trait Visitor<'v> : Sized {
177177
}
178178
}
179179

180+
/// Like `visit_nested_item()`, but for trait items. See
181+
/// `visit_nested_item()` for advice on when to override this
182+
/// method.
183+
#[allow(unused_variables)]
184+
fn visit_nested_trait_item(&mut self, id: TraitItemId) {
185+
let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
186+
if let Some(item) = opt_item {
187+
self.visit_trait_item(item);
188+
}
189+
}
190+
180191
/// Like `visit_nested_item()`, but for impl items. See
181192
/// `visit_nested_item()` for advice on when to override this
182193
/// method.
@@ -192,10 +203,10 @@ pub trait Visitor<'v> : Sized {
192203
/// visit_nested_item, does nothing by default unless you override
193204
/// `nested_visit_map` to return `Some(_)`, in which case it will walk the
194205
/// body.
195-
fn visit_body(&mut self, id: ExprId) {
196-
let opt_expr = self.nested_visit_map().intra().map(|map| map.expr(id));
197-
if let Some(expr) = opt_expr {
198-
self.visit_expr(expr);
206+
fn visit_nested_body(&mut self, id: BodyId) {
207+
let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
208+
if let Some(body) = opt_body {
209+
self.visit_body(body);
199210
}
200211
}
201212

@@ -205,6 +216,10 @@ pub trait Visitor<'v> : Sized {
205216
walk_item(self, i)
206217
}
207218

219+
fn visit_body(&mut self, b: &'v Body) {
220+
walk_body(self, b);
221+
}
222+
208223
/// When invoking `visit_all_item_likes()`, you need to supply an
209224
/// item-like visitor. This method converts a "intra-visit"
210225
/// visitor into an item-like visitor that walks the entire tree.
@@ -253,8 +268,6 @@ pub trait Visitor<'v> : Sized {
253268
fn visit_expr(&mut self, ex: &'v Expr) {
254269
walk_expr(self, ex)
255270
}
256-
fn visit_expr_post(&mut self, _ex: &'v Expr) {
257-
}
258271
fn visit_ty(&mut self, t: &'v Ty) {
259272
walk_ty(self, t)
260273
}
@@ -267,12 +280,15 @@ pub trait Visitor<'v> : Sized {
267280
fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
268281
walk_fn_decl(self, fd)
269282
}
270-
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: ExprId, s: Span, id: NodeId) {
283+
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
271284
walk_fn(self, fk, fd, b, s, id)
272285
}
273286
fn visit_trait_item(&mut self, ti: &'v TraitItem) {
274287
walk_trait_item(self, ti)
275288
}
289+
fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
290+
walk_trait_item_ref(self, ii)
291+
}
276292
fn visit_impl_item(&mut self, ii: &'v ImplItem) {
277293
walk_impl_item(self, ii)
278294
}
@@ -378,6 +394,14 @@ pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_i
378394
}
379395
}
380396

397+
pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
398+
for argument in &body.arguments {
399+
visitor.visit_id(argument.id);
400+
visitor.visit_pat(&argument.pat);
401+
}
402+
visitor.visit_expr(&body.value);
403+
}
404+
381405
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
382406
visitor.visit_id(local.id);
383407
visitor.visit_pat(&local.pat);
@@ -423,11 +447,11 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
423447
visitor.visit_id(item.id);
424448
visitor.visit_path(path, item.id);
425449
}
426-
ItemStatic(ref typ, _, ref expr) |
427-
ItemConst(ref typ, ref expr) => {
450+
ItemStatic(ref typ, _, body) |
451+
ItemConst(ref typ, body) => {
428452
visitor.visit_id(item.id);
429453
visitor.visit_ty(typ);
430-
visitor.visit_expr(expr);
454+
visitor.visit_nested_body(body);
431455
}
432456
ItemFn(ref declaration, unsafety, constness, abi, ref generics, body_id) => {
433457
visitor.visit_fn(FnKind::ItemFn(item.name,
@@ -469,21 +493,19 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
469493
visitor.visit_generics(type_parameters);
470494
walk_list!(visitor, visit_trait_ref, opt_trait_reference);
471495
visitor.visit_ty(typ);
472-
for impl_item_ref in impl_item_refs {
473-
visitor.visit_impl_item_ref(impl_item_ref);
474-
}
496+
walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
475497
}
476498
ItemStruct(ref struct_definition, ref generics) |
477499
ItemUnion(ref struct_definition, ref generics) => {
478500
visitor.visit_generics(generics);
479501
visitor.visit_id(item.id);
480502
visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
481503
}
482-
ItemTrait(_, ref generics, ref bounds, ref methods) => {
504+
ItemTrait(_, ref generics, ref bounds, ref trait_item_refs) => {
483505
visitor.visit_id(item.id);
484506
visitor.visit_generics(generics);
485507
walk_list!(visitor, visit_ty_param_bound, bounds);
486-
walk_list!(visitor, visit_trait_item, methods);
508+
walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
487509
}
488510
}
489511
walk_list!(visitor, visit_attribute, &item.attrs);
@@ -511,7 +533,7 @@ pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
511533
generics,
512534
parent_item_id,
513535
variant.span);
514-
walk_list!(visitor, visit_expr, &variant.node.disr_expr);
536+
walk_list!(visitor, visit_nested_body, variant.node.disr_expr);
515537
walk_list!(visitor, visit_attribute, &variant.node.attrs);
516538
}
517539

@@ -544,18 +566,18 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
544566
visitor.visit_ty(ty);
545567
walk_list!(visitor, visit_ty_param_bound, bounds);
546568
}
547-
TyArray(ref ty, ref expression) => {
569+
TyArray(ref ty, length) => {
548570
visitor.visit_ty(ty);
549-
visitor.visit_expr(expression)
571+
visitor.visit_nested_body(length)
550572
}
551573
TyPolyTraitRef(ref bounds) => {
552574
walk_list!(visitor, visit_ty_param_bound, bounds);
553575
}
554576
TyImplTrait(ref bounds) => {
555577
walk_list!(visitor, visit_ty_param_bound, bounds);
556578
}
557-
TyTypeof(ref expression) => {
558-
visitor.visit_expr(expression)
579+
TyTypeof(expression) => {
580+
visitor.visit_nested_body(expression)
559581
}
560582
TyInfer => {}
561583
}
@@ -662,9 +684,12 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v
662684
visitor.visit_name(foreign_item.span, foreign_item.name);
663685

664686
match foreign_item.node {
665-
ForeignItemFn(ref function_declaration, ref generics) => {
687+
ForeignItemFn(ref function_declaration, ref names, ref generics) => {
688+
visitor.visit_generics(generics);
666689
visitor.visit_fn_decl(function_declaration);
667-
visitor.visit_generics(generics)
690+
for name in names {
691+
visitor.visit_name(name.span, name.node);
692+
}
668693
}
669694
ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
670695
}
@@ -732,18 +757,8 @@ pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionR
732757
}
733758

734759
pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
735-
for argument in &function_declaration.inputs {
736-
visitor.visit_id(argument.id);
737-
visitor.visit_pat(&argument.pat);
738-
visitor.visit_ty(&argument.ty)
739-
}
740-
walk_fn_ret_ty(visitor, &function_declaration.output)
741-
}
742-
743-
pub fn walk_fn_decl_nopat<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
744-
for argument in &function_declaration.inputs {
745-
visitor.visit_id(argument.id);
746-
visitor.visit_ty(&argument.ty)
760+
for ty in &function_declaration.inputs {
761+
visitor.visit_ty(ty)
747762
}
748763
walk_fn_ret_ty(visitor, &function_declaration.output)
749764
}
@@ -763,42 +778,33 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
763778
pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
764779
function_kind: FnKind<'v>,
765780
function_declaration: &'v FnDecl,
766-
body_id: ExprId,
781+
body_id: BodyId,
767782
_span: Span,
768783
id: NodeId) {
769784
visitor.visit_id(id);
770785
visitor.visit_fn_decl(function_declaration);
771786
walk_fn_kind(visitor, function_kind);
772-
visitor.visit_body(body_id)
773-
}
774-
775-
pub fn walk_fn_with_body<'v, V: Visitor<'v>>(visitor: &mut V,
776-
function_kind: FnKind<'v>,
777-
function_declaration: &'v FnDecl,
778-
body: &'v Expr,
779-
_span: Span,
780-
id: NodeId) {
781-
visitor.visit_id(id);
782-
visitor.visit_fn_decl(function_declaration);
783-
walk_fn_kind(visitor, function_kind);
784-
visitor.visit_expr(body)
787+
visitor.visit_nested_body(body_id)
785788
}
786789

787790
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
788791
visitor.visit_name(trait_item.span, trait_item.name);
789792
walk_list!(visitor, visit_attribute, &trait_item.attrs);
790793
match trait_item.node {
791-
ConstTraitItem(ref ty, ref default) => {
794+
TraitItemKind::Const(ref ty, default) => {
792795
visitor.visit_id(trait_item.id);
793796
visitor.visit_ty(ty);
794-
walk_list!(visitor, visit_expr, default);
797+
walk_list!(visitor, visit_nested_body, default);
795798
}
796-
MethodTraitItem(ref sig, None) => {
799+
TraitItemKind::Method(ref sig, TraitMethod::Required(ref names)) => {
797800
visitor.visit_id(trait_item.id);
798801
visitor.visit_generics(&sig.generics);
799802
visitor.visit_fn_decl(&sig.decl);
803+
for name in names {
804+
visitor.visit_name(name.span, name.node);
805+
}
800806
}
801-
MethodTraitItem(ref sig, Some(body_id)) => {
807+
TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
802808
visitor.visit_fn(FnKind::Method(trait_item.name,
803809
sig,
804810
None,
@@ -808,14 +814,23 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
808814
trait_item.span,
809815
trait_item.id);
810816
}
811-
TypeTraitItem(ref bounds, ref default) => {
817+
TraitItemKind::Type(ref bounds, ref default) => {
812818
visitor.visit_id(trait_item.id);
813819
walk_list!(visitor, visit_ty_param_bound, bounds);
814820
walk_list!(visitor, visit_ty, default);
815821
}
816822
}
817823
}
818824

825+
pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
826+
// NB: Deliberately force a compilation error if/when new fields are added.
827+
let TraitItemRef { id, name, ref kind, span, ref defaultness } = *trait_item_ref;
828+
visitor.visit_nested_trait_item(id);
829+
visitor.visit_name(span, name);
830+
visitor.visit_associated_item_kind(kind);
831+
visitor.visit_defaultness(defaultness);
832+
}
833+
819834
pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
820835
// NB: Deliberately force a compilation error if/when new fields are added.
821836
let ImplItem { id: _, name, ref vis, ref defaultness, ref attrs, ref node, span } = *impl_item;
@@ -825,10 +840,10 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
825840
visitor.visit_defaultness(defaultness);
826841
walk_list!(visitor, visit_attribute, attrs);
827842
match *node {
828-
ImplItemKind::Const(ref ty, ref expr) => {
843+
ImplItemKind::Const(ref ty, body) => {
829844
visitor.visit_id(impl_item.id);
830845
visitor.visit_ty(ty);
831-
visitor.visit_expr(expr);
846+
visitor.visit_nested_body(body);
832847
}
833848
ImplItemKind::Method(ref sig, body_id) => {
834849
visitor.visit_fn(FnKind::Method(impl_item.name,
@@ -907,9 +922,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
907922
ExprArray(ref subexpressions) => {
908923
walk_list!(visitor, visit_expr, subexpressions);
909924
}
910-
ExprRepeat(ref element, ref count) => {
925+
ExprRepeat(ref element, count) => {
911926
visitor.visit_expr(element);
912-
visitor.visit_expr(count)
927+
visitor.visit_nested_body(count)
913928
}
914929
ExprStruct(ref qpath, ref fields, ref optional_base) => {
915930
visitor.visit_qpath(qpath, expression.id, expression.span);
@@ -1016,8 +1031,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10161031
}
10171032
}
10181033
}
1019-
1020-
visitor.visit_expr_post(expression)
10211034
}
10221035

10231036
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
@@ -1100,16 +1113,3 @@ impl<'a, 'ast> Visitor<'ast> for IdRangeComputingVisitor<'a, 'ast> {
11001113
self.result.add(id);
11011114
}
11021115
}
1103-
1104-
/// Computes the id range for a single fn body, ignoring nested items.
1105-
pub fn compute_id_range_for_fn_body<'v>(fk: FnKind<'v>,
1106-
decl: &'v FnDecl,
1107-
body: &'v Expr,
1108-
sp: Span,
1109-
id: NodeId,
1110-
map: &map::Map<'v>)
1111-
-> IdRange {
1112-
let mut visitor = IdRangeComputingVisitor::new(map);
1113-
walk_fn_with_body(&mut visitor, fk, decl, body, sp, id);
1114-
visitor.result()
1115-
}

src/librustc/hir/itemlikevisit.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use super::{Item, ImplItem};
11+
use super::{Item, ImplItem, TraitItem};
1212
use super::intravisit::Visitor;
1313

1414
/// The "item-like visitor" visitor defines only the top-level methods
@@ -58,6 +58,7 @@ use super::intravisit::Visitor;
5858
/// needed.
5959
pub trait ItemLikeVisitor<'hir> {
6060
fn visit_item(&mut self, item: &'hir Item);
61+
fn visit_trait_item(&mut self, trait_item: &'hir TraitItem);
6162
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem);
6263
}
6364

@@ -80,6 +81,10 @@ impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
8081
self.visitor.visit_item(item);
8182
}
8283

84+
fn visit_trait_item(&mut self, trait_item: &'hir TraitItem) {
85+
self.visitor.visit_trait_item(trait_item);
86+
}
87+
8388
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem) {
8489
self.visitor.visit_impl_item(impl_item);
8590
}

0 commit comments

Comments
 (0)