Skip to content

Commit 27ac518

Browse files
committed
Auto merge of #20179 - eddyb:blind-items, r=alexcrichton
This implements an important part of RFC PR 385 (see #18219).
2 parents 6869645 + 7136938 commit 27ac518

Some content is hidden

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

59 files changed

+1095
-1519
lines changed

src/doc/reference.md

+9-23
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,9 @@ Crates contain [items](#items), each of which may have some number of
803803
## Items
804804

805805
```{.ebnf .gram}
806-
item : mod_item | fn_item | type_item | struct_item | enum_item
807-
| static_item | trait_item | impl_item | extern_block ;
806+
item : extern_crate_decl | use_decl | mod_item | fn_item | type_item
807+
| struct_item | enum_item | static_item | trait_item | impl_item
808+
| extern_block ;
808809
```
809810

810811
An _item_ is a component of a crate; some module items can be defined in crate
@@ -818,6 +819,8 @@ execution, and may reside in read-only memory.
818819

819820
There are several kinds of item:
820821

822+
* [`extern crate` declarations](#extern-crate-declarations)
823+
* [`use` declarations](#use-declarations)
821824
* [modules](#modules)
822825
* [functions](#functions)
823826
* [type definitions](#type-definitions)
@@ -854,13 +857,10 @@ no notion of type abstraction: there are no first-class "forall" types.
854857

855858
```{.ebnf .gram}
856859
mod_item : "mod" ident ( ';' | '{' mod '}' );
857-
mod : [ view_item | item ] * ;
860+
mod : item * ;
858861
```
859862

860-
A module is a container for zero or more [view items](#view-items) and zero or
861-
more [items](#items). The view items manage the visibility of the items defined
862-
within the module, as well as the visibility of names from outside the module
863-
when referenced from inside the module.
863+
A module is a container for zero or more [items](#items).
864864

865865
A _module item_ is a module, surrounded in braces, named, and prefixed with the
866866
keyword `mod`. A module item introduces a new, named module into the tree of
@@ -918,19 +918,6 @@ mod thread {
918918
}
919919
```
920920

921-
#### View items
922-
923-
```{.ebnf .gram}
924-
view_item : extern_crate_decl | use_decl ;
925-
```
926-
927-
A view item manages the namespace of a module. View items do not define new
928-
items, but rather, simply change other items' visibility. There are two
929-
kinds of view items:
930-
931-
* [`extern crate` declarations](#extern-crate-declarations)
932-
* [`use` declarations](#use-declarations)
933-
934921
##### Extern crate declarations
935922

936923
```{.ebnf .gram}
@@ -2891,13 +2878,12 @@ Point3d {y: 0, z: 10, .. base};
28912878
### Block expressions
28922879

28932880
```{.ebnf .gram}
2894-
block_expr : '{' [ view_item ] *
2895-
[ stmt ';' | item ] *
2881+
block_expr : '{' [ stmt ';' | item ] *
28962882
[ expr ] '}' ;
28972883
```
28982884

28992885
A _block expression_ is similar to a module in terms of the declarations that
2900-
are possible. Each block conceptually introduces a new namespace scope. View
2886+
are possible. Each block conceptually introduces a new namespace scope. Use
29012887
items can bring new names into scopes and declared items are in scope for only
29022888
the block itself.
29032889

src/librustc/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ register_diagnostics! {
5252
E0140,
5353
E0152,
5454
E0153,
55+
E0154,
5556
E0157,
5657
E0158,
5758
E0161,

src/librustc/lint/builtin.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -1202,17 +1202,17 @@ impl LintPass for UnusedImportBraces {
12021202
lint_array!(UNUSED_IMPORT_BRACES)
12031203
}
12041204

1205-
fn check_view_item(&mut self, cx: &Context, view_item: &ast::ViewItem) {
1206-
match view_item.node {
1207-
ast::ViewItemUse(ref view_path) => {
1205+
fn check_item(&mut self, cx: &Context, item: &ast::Item) {
1206+
match item.node {
1207+
ast::ItemUse(ref view_path) => {
12081208
match view_path.node {
1209-
ast::ViewPathList(_, ref items, _) => {
1209+
ast::ViewPathList(_, ref items) => {
12101210
if items.len() == 1 {
12111211
match items[0].node {
12121212
ast::PathListIdent {ref name, ..} => {
12131213
let m = format!("braces around {} is unnecessary",
12141214
token::get_ident(*name).get());
1215-
cx.span_lint(UNUSED_IMPORT_BRACES, view_item.span,
1215+
cx.span_lint(UNUSED_IMPORT_BRACES, item.span,
12161216
&m[]);
12171217
},
12181218
_ => ()
@@ -1709,22 +1709,6 @@ impl LintPass for Stability {
17091709
}
17101710
}
17111711

1712-
fn check_view_item(&mut self, cx: &Context, item: &ast::ViewItem) {
1713-
// compiler-generated `extern crate` statements have a dummy span.
1714-
if item.span == DUMMY_SP { return }
1715-
1716-
let id = match item.node {
1717-
ast::ViewItemExternCrate(_, _, id) => id,
1718-
ast::ViewItemUse(..) => return,
1719-
};
1720-
let cnum = match cx.tcx.sess.cstore.find_extern_mod_stmt_cnum(id) {
1721-
Some(cnum) => cnum,
1722-
None => return,
1723-
};
1724-
let id = ast::DefId { krate: cnum, node: ast::CRATE_NODE_ID };
1725-
self.lint(cx, id, item.span);
1726-
}
1727-
17281712
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
17291713
if self.is_internal(cx, e.span) { return; }
17301714

@@ -1776,6 +1760,17 @@ impl LintPass for Stability {
17761760
if self.is_internal(cx, item.span) { return }
17771761

17781762
match item.node {
1763+
ast::ItemExternCrate(_) => {
1764+
// compiler-generated `extern crate` items have a dummy span.
1765+
if item.span == DUMMY_SP { return }
1766+
1767+
let cnum = match cx.tcx.sess.cstore.find_extern_mod_stmt_cnum(item.id) {
1768+
Some(cnum) => cnum,
1769+
None => return,
1770+
};
1771+
let id = ast::DefId { krate: cnum, node: ast::CRATE_NODE_ID };
1772+
self.lint(cx, id, item.span);
1773+
}
17791774
ast::ItemTrait(_, _, ref supertraits, _) => {
17801775
for t in supertraits.iter() {
17811776
if let ast::TraitTyParamBound(ref t, _) = *t {

src/librustc/lint/context.rs

-8
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,6 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
603603
})
604604
}
605605

606-
fn visit_view_item(&mut self, i: &ast::ViewItem) {
607-
self.with_lint_attrs(&i.attrs[], |cx| {
608-
run_lints!(cx, check_view_item, i);
609-
cx.visit_ids(|v| v.visit_view_item(i));
610-
visit::walk_view_item(cx, i);
611-
})
612-
}
613-
614606
fn visit_pat(&mut self, p: &ast::Pat) {
615607
run_lints!(self, check_pat, p);
616608
visit::walk_pat(self, p);

src/librustc/lint/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ pub trait LintPass {
128128
fn check_crate(&mut self, _: &Context, _: &ast::Crate) { }
129129
fn check_ident(&mut self, _: &Context, _: Span, _: ast::Ident) { }
130130
fn check_mod(&mut self, _: &Context, _: &ast::Mod, _: Span, _: ast::NodeId) { }
131-
fn check_view_item(&mut self, _: &Context, _: &ast::ViewItem) { }
132131
fn check_foreign_item(&mut self, _: &Context, _: &ast::ForeignItem) { }
133132
fn check_item(&mut self, _: &Context, _: &ast::Item) { }
134133
fn check_local(&mut self, _: &Context, _: &ast::Local) { }

src/librustc/metadata/creader.rs

+25-31
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ pub struct CrateReader<'a> {
4040
}
4141

4242
impl<'a, 'v> visit::Visitor<'v> for CrateReader<'a> {
43-
fn visit_view_item(&mut self, a: &ast::ViewItem) {
44-
self.process_view_item(a);
45-
visit::walk_view_item(self, a);
46-
}
4743
fn visit_item(&mut self, a: &ast::Item) {
4844
self.process_item(a);
4945
visit::walk_item(self, a);
@@ -64,9 +60,8 @@ fn dump_crates(cstore: &CStore) {
6460
})
6561
}
6662

67-
fn should_link(i: &ast::ViewItem) -> bool {
63+
fn should_link(i: &ast::Item) -> bool {
6864
!attr::contains_name(&i.attrs[], "no_link")
69-
7065
}
7166

7267
struct CrateInfo {
@@ -181,29 +176,10 @@ impl<'a> CrateReader<'a> {
181176
}
182177
}
183178

184-
fn process_view_item(&mut self, i: &ast::ViewItem) {
185-
if !should_link(i) {
186-
return;
187-
}
188-
189-
match self.extract_crate_info(i) {
190-
Some(info) => {
191-
let (cnum, _, _) = self.resolve_crate(&None,
192-
&info.ident[],
193-
&info.name[],
194-
None,
195-
i.span,
196-
PathKind::Crate);
197-
self.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
198-
}
199-
None => ()
200-
}
201-
}
202-
203-
fn extract_crate_info(&self, i: &ast::ViewItem) -> Option<CrateInfo> {
179+
fn extract_crate_info(&self, i: &ast::Item) -> Option<CrateInfo> {
204180
match i.node {
205-
ast::ViewItemExternCrate(ident, ref path_opt, id) => {
206-
let ident = token::get_ident(ident);
181+
ast::ItemExternCrate(ref path_opt) => {
182+
let ident = token::get_ident(i.ident);
207183
debug!("resolving extern crate stmt. ident: {} path_opt: {:?}",
208184
ident, path_opt);
209185
let name = match *path_opt {
@@ -218,16 +194,34 @@ impl<'a> CrateReader<'a> {
218194
Some(CrateInfo {
219195
ident: ident.get().to_string(),
220196
name: name,
221-
id: id,
197+
id: i.id,
222198
should_link: should_link(i),
223199
})
224200
}
225201
_ => None
226202
}
227203
}
228204

229-
fn process_item(&self, i: &ast::Item) {
205+
fn process_item(&mut self, i: &ast::Item) {
230206
match i.node {
207+
ast::ItemExternCrate(_) => {
208+
if !should_link(i) {
209+
return;
210+
}
211+
212+
match self.extract_crate_info(i) {
213+
Some(info) => {
214+
let (cnum, _, _) = self.resolve_crate(&None,
215+
&info.ident[],
216+
&info.name[],
217+
None,
218+
i.span,
219+
PathKind::Crate);
220+
self.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
221+
}
222+
None => ()
223+
}
224+
}
231225
ast::ItemForeignMod(ref fm) => {
232226
if fm.abi == abi::Rust || fm.abi == abi::RustIntrinsic {
233227
return;
@@ -533,7 +527,7 @@ impl<'a> CrateReader<'a> {
533527

534528
#[derive(Copy)]
535529
pub enum CrateOrString<'a> {
536-
Krate(&'a ast::ViewItem),
530+
Krate(&'a ast::Item),
537531
Str(&'a str)
538532
}
539533

src/librustc/metadata/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1456,8 +1456,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
14561456
rbml_w.end_tag();
14571457
}
14581458
}
1459-
ast::ItemMac(..) => {
1460-
// macros are encoded separately
1459+
ast::ItemExternCrate(_) | ast::ItemUse(_) |ast::ItemMac(..) => {
1460+
// these are encoded separately
14611461
}
14621462
}
14631463
}

src/librustc/middle/astencode.rs

-2
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,6 @@ impl Folder for NestedItemsDropper {
332332
}
333333
}).collect();
334334
let blk_sans_items = P(ast::Block {
335-
view_items: Vec::new(), // I don't know if we need the view_items
336-
// here, but it doesn't break tests!
337335
stmts: stmts_sans_items,
338336
expr: expr,
339337
id: id,

src/librustc/middle/reachable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
297297
// These are normal, nothing reachable about these
298298
// inherently and their children are already in the
299299
// worklist, as determined by the privacy pass
300+
ast::ItemExternCrate(_) | ast::ItemUse(_) |
300301
ast::ItemTy(..) | ast::ItemStatic(_, _, _) |
301302
ast::ItemMod(..) | ast::ItemForeignMod(..) |
302303
ast::ItemImpl(..) | ast::ItemTrait(..) |

src/librustc/middle/resolve_lifetime.rs

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
9494
// Fn lifetimes get added in visit_fn below:
9595
visit::walk_item(this, item);
9696
}
97+
ast::ItemExternCrate(_) |
98+
ast::ItemUse(_) |
9799
ast::ItemMod(..) |
98100
ast::ItemMac(..) |
99101
ast::ItemForeignMod(..) |

src/librustc/plugin/load.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ pub fn load_plugins(sess: &Session, krate: &ast::Crate,
7373
// We need to error on `#[macro_use] extern crate` when it isn't at the
7474
// crate root, because `$crate` won't work properly. Identify these by
7575
// spans, because the crate map isn't set up yet.
76-
for vi in krate.module.view_items.iter() {
77-
loader.span_whitelist.insert(vi.span);
76+
for item in krate.module.items.iter() {
77+
if let ast::ItemExternCrate(_) = item.node {
78+
loader.span_whitelist.insert(item.span);
79+
}
7880
}
7981

8082
visit::walk_crate(&mut loader, krate);
@@ -91,18 +93,21 @@ pub fn load_plugins(sess: &Session, krate: &ast::Crate,
9193

9294
// note that macros aren't expanded yet, and therefore macros can't add plugins.
9395
impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
94-
fn visit_view_item(&mut self, vi: &ast::ViewItem) {
96+
fn visit_item(&mut self, item: &ast::Item) {
9597
// We're only interested in `extern crate`.
96-
match vi.node {
97-
ast::ViewItemExternCrate(..) => (),
98-
_ => return,
98+
match item.node {
99+
ast::ItemExternCrate(_) => {}
100+
_ => {
101+
visit::walk_item(self, item);
102+
return;
103+
}
99104
}
100105

101106
// Parse the attributes relating to macro / plugin loading.
102107
let mut plugin_attr = None;
103108
let mut macro_selection = Some(HashSet::new()); // None => load all
104109
let mut reexport = HashSet::new();
105-
for attr in vi.attrs.iter() {
110+
for attr in item.attrs.iter() {
106111
let mut used = true;
107112
match attr.name().get() {
108113
"phase" => {
@@ -155,7 +160,10 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
155160
}
156161
}
157162

158-
self.load_plugin(CrateOrString::Krate(vi), plugin_attr, macro_selection, Some(reexport))
163+
self.load_plugin(CrateOrString::Krate(item),
164+
plugin_attr,
165+
macro_selection,
166+
Some(reexport))
159167
}
160168

161169
fn visit_mac(&mut self, _: &ast::Mac) {

src/librustc_back/svh.rs

-14
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ mod svh_visitor {
188188
SawLifetimeDef(token::InternedString),
189189

190190
SawMod,
191-
SawViewItem,
192191
SawForeignItem,
193192
SawItem,
194193
SawDecl,
@@ -436,19 +435,6 @@ mod svh_visitor {
436435
SawStmt(saw_stmt(&s.node)).hash(self.st); visit::walk_stmt(self, s)
437436
}
438437

439-
fn visit_view_item(&mut self, i: &ViewItem) {
440-
// Two kinds of view items can affect the ABI for a crate:
441-
// exported `pub use` view items (since that may expose
442-
// items that downstream crates can call), and `use
443-
// foo::Trait`, since changing that may affect method
444-
// resolution.
445-
//
446-
// The simplest approach to handling both of the above is
447-
// just to adopt the same simple-minded (fine-grained)
448-
// hash that I am deploying elsewhere here.
449-
SawViewItem.hash(self.st); visit::walk_view_item(self, i)
450-
}
451-
452438
fn visit_foreign_item(&mut self, i: &ForeignItem) {
453439
// FIXME (#14132) ideally we would incorporate privacy (or
454440
// perhaps reachability) somewhere here, so foreign items

src/librustc_driver/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl fold::Folder for ReplaceBodyWithLoop {
477477
e: Option<P<ast::Expr>>) -> P<ast::Block> {
478478
P(ast::Block {
479479
expr: e,
480-
view_items: vec![], stmts: vec![], rules: rules,
480+
stmts: vec![], rules: rules,
481481
id: ast::DUMMY_NODE_ID, span: codemap::DUMMY_SP,
482482
})
483483
}

0 commit comments

Comments
 (0)