Skip to content

Commit 37d3bea

Browse files
committed
Add ABSOLUTE_PATH_STARTING_WITH_MODULE epoch lint for path breakage
1 parent 56ace0a commit 37d3bea

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

src/librustc/lint/builtin.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ declare_lint! {
254254
"suggest using `dyn Trait` for trait objects"
255255
}
256256

257+
declare_lint! {
258+
pub ABSOLUTE_PATH_STARTING_WITH_MODULE,
259+
Allow,
260+
"fully qualified paths that start with a module name \
261+
instead of `crate`, `self`, or an extern crate name"
262+
}
263+
257264
declare_lint! {
258265
pub ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
259266
Warn,
@@ -314,6 +321,7 @@ impl LintPass for HardwiredLints {
314321
TYVAR_BEHIND_RAW_POINTER,
315322
ELIDED_LIFETIME_IN_PATH,
316323
BARE_TRAIT_OBJECT,
324+
ABSOLUTE_PATH_STARTING_WITH_MODULE,
317325
UNSTABLE_NAME_COLLISION,
318326
)
319327
}

src/librustc_lint/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern crate rustc_mir;
4040
extern crate syntax_pos;
4141

4242
use rustc::lint;
43-
use rustc::lint::builtin::BARE_TRAIT_OBJECT;
43+
use rustc::lint::builtin::{BARE_TRAIT_OBJECT, ABSOLUTE_PATH_STARTING_WITH_MODULE};
4444
use rustc::session;
4545
use rustc::util;
4646

@@ -278,6 +278,11 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
278278
// Note: this item represents future incompatibility of all unstable functions in the
279279
// standard library, and thus should never be removed or changed to an error.
280280
},
281+
FutureIncompatibleInfo {
282+
id: LintId::of(ABSOLUTE_PATH_STARTING_WITH_MODULE),
283+
reference: "issue TBD",
284+
edition: Some(Edition::Edition2018),
285+
},
281286
]);
282287

283288
// Register renamed and removed lints

src/librustc_resolve/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,7 +3189,7 @@ impl<'a> Resolver<'a> {
31893189
path[0].name != keywords::CrateRoot.name() &&
31903190
path[0].name != keywords::DollarCrate.name() {
31913191
let unqualified_result = {
3192-
match self.resolve_path(&[*path.last().unwrap()], Some(ns), false, span, Some(id)) {
3192+
match self.resolve_path(&[*path.last().unwrap()], Some(ns), false, span, None) {
31933193
PathResult::NonModule(path_res) => path_res.base_def(),
31943194
PathResult::Module(module) => module.def().unwrap(),
31953195
_ => return Some(result),
@@ -3209,7 +3209,8 @@ impl<'a> Resolver<'a> {
32093209
opt_ns: Option<Namespace>, // `None` indicates a module path
32103210
record_used: bool,
32113211
path_span: Span,
3212-
_node_id: Option<NodeId>)
3212+
node_id: Option<NodeId>) // None indicates that we don't care about linting
3213+
// `::module` paths
32133214
-> PathResult<'a> {
32143215
let mut module = None;
32153216
let mut allow_super = true;
@@ -3328,6 +3329,30 @@ impl<'a> Resolver<'a> {
33283329
format!("Not a module `{}`", ident),
33293330
is_last);
33303331
}
3332+
3333+
if let Some(id) = node_id {
3334+
if i == 1 && self.session.features_untracked().crate_in_paths
3335+
&& !self.session.rust_2018() {
3336+
let prev_name = path[0].name;
3337+
if prev_name == keywords::Extern.name() ||
3338+
prev_name == keywords::CrateRoot.name() {
3339+
let mut is_crate = false;
3340+
if let NameBindingKind::Import { directive: d, .. } = binding.kind {
3341+
if let ImportDirectiveSubclass::ExternCrate(..) = d.subclass {
3342+
is_crate = true;
3343+
}
3344+
}
3345+
3346+
if !is_crate {
3347+
self.session.buffer_lint(
3348+
lint::builtin::ABSOLUTE_PATH_STARTING_WITH_MODULE,
3349+
id, path_span,
3350+
"Fully-qualified paths must start with `self`, `super`,
3351+
`crate`, or an external crate name in the 2018 edition");
3352+
}
3353+
}
3354+
}
3355+
}
33313356
}
33323357
Err(Undetermined) => return PathResult::Indeterminate,
33333358
Err(Determined) => {

src/librustc_resolve/resolve_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
679679
!(self_path.len() > 1 && is_special(self_path[1])) {
680680
self_path[0].name = keywords::SelfValue.name();
681681
self_result = Some(self.resolve_path(&self_path, None, false,
682-
span, Some(directive.id)));
682+
span, None));
683683
}
684684
return if let Some(PathResult::Module(..)) = self_result {
685685
Some((span, format!("Did you mean `{}`?", names_to_string(&self_path[..]))))

0 commit comments

Comments
 (0)