Skip to content

Commit 04ca378

Browse files
committed
Support use $crate; with a future compatibility warning.
1 parent 199ed20 commit 04ca378

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,27 @@ impl<'b> Resolver<'b> {
130130

131131
match view_path.node {
132132
ViewPathSimple(binding, ref full_path) => {
133-
let source_name = full_path.segments.last().unwrap().identifier.name;
134-
if source_name.as_str() == "mod" || source_name.as_str() == "self" {
133+
let mut source = full_path.segments.last().unwrap().identifier;
134+
let source_name = source.name.as_str();
135+
if source_name == "mod" || source_name == "self" {
135136
resolve_error(self,
136137
view_path.span,
137138
ResolutionError::SelfImportsOnlyAllowedWithin);
139+
} else if source_name == "$crate" && full_path.segments.len() == 1 {
140+
let crate_root = self.resolve_crate_var(source.ctxt);
141+
let crate_name = match crate_root.kind {
142+
ModuleKind::Def(_, name) => name,
143+
ModuleKind::Block(..) => unreachable!(),
144+
};
145+
source.name = crate_name;
146+
147+
self.session.struct_span_warn(item.span, "`$crate` may not be imported")
148+
.note("`use $crate;` was erroneously allowed and \
149+
will become a hard error in a future release")
150+
.emit();
138151
}
139152

140-
let subclass = ImportDirectiveSubclass::single(binding.name, source_name);
153+
let subclass = ImportDirectiveSubclass::single(binding.name, source.name);
141154
let span = view_path.span;
142155
self.add_import_directive(module_path, subclass, span, item.id, vis);
143156
}

src/librustc_resolve/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,13 +1584,7 @@ impl<'a> Resolver<'a> {
15841584
fn resolve_module_prefix(&mut self, module_path: &[Ident], span: Option<Span>)
15851585
-> ResolveResult<ModulePrefixResult<'a>> {
15861586
if &*module_path[0].name.as_str() == "$crate" {
1587-
let mut ctxt = module_path[0].ctxt;
1588-
while ctxt.source().0 != SyntaxContext::empty() {
1589-
ctxt = ctxt.source().0;
1590-
}
1591-
let module = self.invocations[&ctxt.source().1].module.get();
1592-
let crate_root = if module.is_local() { self.graph_root } else { module };
1593-
return Success(PrefixFound(crate_root, 1))
1587+
return Success(PrefixFound(self.resolve_crate_var(module_path[0].ctxt), 1));
15941588
}
15951589

15961590
// Start at the current module if we see `self` or `super`, or at the
@@ -1623,6 +1617,14 @@ impl<'a> Resolver<'a> {
16231617
return Success(PrefixFound(containing_module, i));
16241618
}
16251619

1620+
fn resolve_crate_var(&mut self, mut crate_var_ctxt: SyntaxContext) -> Module<'a> {
1621+
while crate_var_ctxt.source().0 != SyntaxContext::empty() {
1622+
crate_var_ctxt = crate_var_ctxt.source().0;
1623+
}
1624+
let module = self.invocations[&crate_var_ctxt.source().1].module.get();
1625+
if module.is_local() { self.graph_root } else { module }
1626+
}
1627+
16261628
// AST resolution
16271629
//
16281630
// We maintain a list of value ribs and type ribs.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[macro_export]
12+
macro_rules! m { () => { use $crate; } }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:import_crate_var.rs
12+
// error-pattern: `$crate` may not be imported
13+
// error-pattern: `use $crate;` was erroneously allowed and will become a hard error
14+
15+
#![feature(rustc_attrs)]
16+
17+
#[macro_use] extern crate import_crate_var;
18+
m!();
19+
20+
#[rustc_error]
21+
fn main() {}

0 commit comments

Comments
 (0)