Skip to content

Commit 0b302f1

Browse files
committed
---
yaml --- r: 276820 b: refs/heads/try c: e17c48b h: refs/heads/master
1 parent 9537f9d commit 0b302f1

File tree

11 files changed

+327
-252
lines changed

11 files changed

+327
-252
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 6dbb0e86aec11050480beb76eade6fb805010ba7
33
refs/heads/snap-stage3: 235d77457d80b549dad3ac36d94f235208a1eafb
4-
refs/heads/try: 7ded11a58cf2f8037a511a8d340161c59fba9913
4+
refs/heads/try: e17c48bb243163e22f4d5a3b42499c642ae41dec
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
66
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
77
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try/src/librustc/ty/fold.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,21 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
116116
pub trait TypeFolder<'tcx> : Sized {
117117
fn tcx<'a>(&'a self) -> &'a TyCtxt<'tcx>;
118118

119+
/// Invoked by the `super_*` routines when we enter a region
120+
/// binding level (for example, when entering a function
121+
/// signature). This is used by clients that want to track the
122+
/// Debruijn index nesting level.
123+
fn enter_region_binder(&mut self) { }
124+
125+
/// Invoked by the `super_*` routines when we exit a region
126+
/// binding level. This is used by clients that want to
127+
/// track the Debruijn index nesting level.
128+
fn exit_region_binder(&mut self) { }
129+
119130
fn fold_binder<T>(&mut self, t: &Binder<T>) -> Binder<T>
120131
where T : TypeFoldable<'tcx>
121132
{
133+
// FIXME(#20526) this should replace `enter_region_binder`/`exit_region_binder`.
122134
t.super_fold_with(self)
123135
}
124136

@@ -185,9 +197,8 @@ pub trait TypeFolder<'tcx> : Sized {
185197
}
186198

187199
pub trait TypeVisitor<'tcx> : Sized {
188-
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
189-
t.super_visit_with(self)
190-
}
200+
fn enter_region_binder(&mut self) { }
201+
fn exit_region_binder(&mut self) { }
191202

192203
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
193204
t.super_visit_with(self)
@@ -285,11 +296,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx>
285296
{
286297
fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
287298

288-
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
299+
fn enter_region_binder(&mut self) {
289300
self.current_depth += 1;
290-
let t = t.super_fold_with(self);
301+
}
302+
303+
fn exit_region_binder(&mut self) {
291304
self.current_depth -= 1;
292-
t
293305
}
294306

295307
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
@@ -426,11 +438,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx>
426438
{
427439
fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
428440

429-
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
441+
fn enter_region_binder(&mut self) {
430442
self.current_depth += 1;
431-
let t = t.super_fold_with(self);
443+
}
444+
445+
fn exit_region_binder(&mut self) {
432446
self.current_depth -= 1;
433-
t
434447
}
435448

436449
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
@@ -583,11 +596,12 @@ struct HasEscapingRegionsVisitor {
583596
}
584597

585598
impl<'tcx> TypeVisitor<'tcx> for HasEscapingRegionsVisitor {
586-
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
599+
fn enter_region_binder(&mut self) {
587600
self.depth += 1;
588-
let result = t.super_visit_with(self);
601+
}
602+
603+
fn exit_region_binder(&mut self) {
589604
self.depth -= 1;
590-
result
591605
}
592606

593607
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {

branches/try/src/librustc/ty/structural_impls.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,21 @@ impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
190190

191191
impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
192192
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
193-
ty::Binder(self.0.fold_with(folder))
193+
folder.enter_region_binder();
194+
let result = ty::Binder(self.0.fold_with(folder));
195+
folder.exit_region_binder();
196+
result
194197
}
195198

196199
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
197200
folder.fold_binder(self)
198201
}
199202

200203
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
201-
self.0.visit_with(visitor)
202-
}
203-
204-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
205-
visitor.visit_binder(self)
204+
visitor.enter_region_binder();
205+
if self.0.visit_with(visitor) { return true }
206+
visitor.exit_region_binder();
207+
false
206208
}
207209
}
208210

@@ -218,11 +220,39 @@ impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for P<[T]> {
218220

219221
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for VecPerParamSpace<T> {
220222
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
221-
self.map(|elem| elem.fold_with(folder))
223+
224+
// Things in the Fn space take place under an additional level
225+
// of region binding relative to the other spaces. This is
226+
// because those entries are attached to a method, and methods
227+
// always introduce a level of region binding.
228+
229+
let result = self.map_enumerated(|(space, index, elem)| {
230+
if space == subst::FnSpace && index == 0 {
231+
// enter new level when/if we reach the first thing in fn space
232+
folder.enter_region_binder();
233+
}
234+
elem.fold_with(folder)
235+
});
236+
if result.len(subst::FnSpace) > 0 {
237+
// if there was anything in fn space, exit the region binding level
238+
folder.exit_region_binder();
239+
}
240+
result
222241
}
223242

224243
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
225-
self.iter().any(|elem| elem.visit_with(visitor))
244+
let mut entered_region_binder = false;
245+
let result = self.iter_enumerated().any(|(space, index, t)| {
246+
if space == subst::FnSpace && index == 0 {
247+
visitor.enter_region_binder();
248+
entered_region_binder = true;
249+
}
250+
t.visit_with(visitor)
251+
});
252+
if entered_region_binder {
253+
visitor.exit_region_binder();
254+
}
255+
result
226256
}
227257
}
228258

branches/try/src/librustc/ty/subst.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,12 @@ struct SubstFolder<'a, 'tcx: 'a> {
582582
impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
583583
fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
584584

585-
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
585+
fn enter_region_binder(&mut self) {
586586
self.region_binders_passed += 1;
587-
let t = t.super_fold_with(self);
587+
}
588+
589+
fn exit_region_binder(&mut self) {
588590
self.region_binders_passed -= 1;
589-
t
590591
}
591592

592593
fn fold_region(&mut self, r: ty::Region) -> ty::Region {

branches/try/src/librustc_resolve/build_reduced_graph.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
//! any imports resolved.
1515
1616
use DefModifiers;
17-
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
17+
use resolve_imports::ImportDirective;
18+
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
1819
use Module;
1920
use Namespace::{self, TypeNS, ValueNS};
2021
use {NameBinding, NameBindingKind};
@@ -27,7 +28,7 @@ use rustc::middle::def::*;
2728
use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
2829
use rustc::ty::VariantKind;
2930

30-
use syntax::ast::Name;
31+
use syntax::ast::{Name, NodeId};
3132
use syntax::attr::AttrMetaMethods;
3233
use syntax::parse::token::special_idents;
3334
use syntax::codemap::{Span, DUMMY_SP};
@@ -151,8 +152,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
151152
}
152153

153154
let subclass = ImportDirectiveSubclass::single(binding, source_name);
154-
self.unresolved_imports += 1;
155-
parent.add_import_directive(module_path,
155+
self.build_import_directive(parent,
156+
module_path,
156157
subclass,
157158
view_path.span,
158159
item.id,
@@ -202,8 +203,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
202203
}
203204
};
204205
let subclass = ImportDirectiveSubclass::single(rename, name);
205-
self.unresolved_imports += 1;
206-
parent.add_import_directive(module_path,
206+
self.build_import_directive(parent,
207+
module_path,
207208
subclass,
208209
source_item.span,
209210
source_item.node.id(),
@@ -212,8 +213,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
212213
}
213214
}
214215
ViewPathGlob(_) => {
215-
self.unresolved_imports += 1;
216-
parent.add_import_directive(module_path,
216+
self.build_import_directive(parent,
217+
module_path,
217218
GlobImport,
218219
view_path.span,
219220
item.id,
@@ -520,6 +521,39 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
520521
}
521522
}
522523

524+
/// Creates and adds an import directive to the given module.
525+
fn build_import_directive(&mut self,
526+
module_: Module<'b>,
527+
module_path: Vec<Name>,
528+
subclass: ImportDirectiveSubclass,
529+
span: Span,
530+
id: NodeId,
531+
is_public: bool,
532+
is_prelude: bool) {
533+
// Bump the reference count on the name. Or, if this is a glob, set
534+
// the appropriate flag.
535+
536+
match subclass {
537+
SingleImport { target, .. } => {
538+
module_.increment_outstanding_references_for(target, ValueNS, is_public);
539+
module_.increment_outstanding_references_for(target, TypeNS, is_public);
540+
}
541+
GlobImport if !is_prelude => {
542+
// Set the glob flag. This tells us that we don't know the
543+
// module's exports ahead of time.
544+
module_.inc_glob_count(is_public)
545+
}
546+
// Prelude imports are not included in the glob counts since they do not get added to
547+
// `resolved_globs` -- they are handled separately in `resolve_imports`.
548+
GlobImport => {}
549+
}
550+
551+
let directive =
552+
ImportDirective::new(module_path, subclass, span, id, is_public, is_prelude);
553+
module_.add_import_directive(directive);
554+
self.unresolved_imports += 1;
555+
}
556+
523557
/// Ensures that the reduced graph rooted at the given external module
524558
/// is built, building it if it is not.
525559
pub fn populate_module_if_necessary(&mut self, module: Module<'b>) {

branches/try/src/librustc_resolve/lib.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,8 @@ pub struct ModuleS<'a> {
828828
// is the NodeId of the local `extern crate` item (otherwise, `extern_crate_id` is None).
829829
extern_crate_id: Option<NodeId>,
830830

831-
resolutions: RefCell<HashMap<(Name, Namespace), &'a RefCell<NameResolution<'a>>>>,
832-
unresolved_imports: RefCell<Vec<&'a ImportDirective<'a>>>,
831+
resolutions: RefCell<HashMap<(Name, Namespace), NameResolution<'a>>>,
832+
unresolved_imports: RefCell<Vec<&'a ImportDirective>>,
833833

834834
// The module children of this node, including normal modules and anonymous modules.
835835
// Anonymous children are pseudo-modules that are implicitly created around items
@@ -849,8 +849,14 @@ pub struct ModuleS<'a> {
849849

850850
prelude: RefCell<Option<Module<'a>>>,
851851

852-
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective<'a>)>>,
853-
globs: RefCell<Vec<&'a ImportDirective<'a>>>,
852+
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective)>>,
853+
resolved_globs: RefCell<(Vec<Module<'a>> /* public */, Vec<Module<'a>> /* private */)>,
854+
855+
// The number of public glob imports in this module.
856+
public_glob_count: Cell<usize>,
857+
858+
// The number of private glob imports in this module.
859+
private_glob_count: Cell<usize>,
854860

855861
// Whether this module is populated. If not populated, any attempt to
856862
// access the children must be preceded with a
@@ -878,15 +884,22 @@ impl<'a> ModuleS<'a> {
878884
module_children: RefCell::new(NodeMap()),
879885
prelude: RefCell::new(None),
880886
glob_importers: RefCell::new(Vec::new()),
881-
globs: RefCell::new((Vec::new())),
887+
resolved_globs: RefCell::new((Vec::new(), Vec::new())),
888+
public_glob_count: Cell::new(0),
889+
private_glob_count: Cell::new(0),
882890
populated: Cell::new(!external),
883891
arenas: arenas
884892
}
885893
}
886894

895+
fn add_import_directive(&self, import_directive: ImportDirective) {
896+
let import_directive = self.arenas.alloc_import_directive(import_directive);
897+
self.unresolved_imports.borrow_mut().push(import_directive);
898+
}
899+
887900
fn for_each_child<F: FnMut(Name, Namespace, &'a NameBinding<'a>)>(&self, mut f: F) {
888901
for (&(name, ns), name_resolution) in self.resolutions.borrow().iter() {
889-
name_resolution.borrow().binding.map(|binding| f(name, ns, binding));
902+
name_resolution.binding.map(|binding| f(name, ns, binding));
890903
}
891904
}
892905

@@ -916,6 +929,11 @@ impl<'a> ModuleS<'a> {
916929
_ => false,
917930
}
918931
}
932+
933+
fn inc_glob_count(&self, is_public: bool) {
934+
let glob_count = if is_public { &self.public_glob_count } else { &self.private_glob_count };
935+
glob_count.set(glob_count.get() + 1);
936+
}
919937
}
920938

921939
impl<'a> fmt::Debug for ModuleS<'a> {
@@ -1117,8 +1135,7 @@ pub struct Resolver<'a, 'tcx: 'a> {
11171135
struct ResolverArenas<'a> {
11181136
modules: arena::TypedArena<ModuleS<'a>>,
11191137
name_bindings: arena::TypedArena<NameBinding<'a>>,
1120-
import_directives: arena::TypedArena<ImportDirective<'a>>,
1121-
name_resolutions: arena::TypedArena<RefCell<NameResolution<'a>>>,
1138+
import_directives: arena::TypedArena<ImportDirective>,
11221139
}
11231140

11241141
impl<'a> ResolverArenas<'a> {
@@ -1128,13 +1145,9 @@ impl<'a> ResolverArenas<'a> {
11281145
fn alloc_name_binding(&'a self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> {
11291146
self.name_bindings.alloc(name_binding)
11301147
}
1131-
fn alloc_import_directive(&'a self, import_directive: ImportDirective<'a>)
1132-
-> &'a ImportDirective {
1148+
fn alloc_import_directive(&'a self, import_directive: ImportDirective) -> &'a ImportDirective {
11331149
self.import_directives.alloc(import_directive)
11341150
}
1135-
fn alloc_name_resolution(&'a self) -> &'a RefCell<NameResolution<'a>> {
1136-
self.name_resolutions.alloc(Default::default())
1137-
}
11381151
}
11391152

11401153
#[derive(PartialEq)]
@@ -1203,7 +1216,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12031216
modules: arena::TypedArena::new(),
12041217
name_bindings: arena::TypedArena::new(),
12051218
import_directives: arena::TypedArena::new(),
1206-
name_resolutions: arena::TypedArena::new(),
12071219
}
12081220
}
12091221

0 commit comments

Comments
 (0)