Skip to content

Commit 068c436

Browse files
committed
pass down information about the root tree and use that to guide lint
1 parent cce9132 commit 068c436

File tree

3 files changed

+62
-34
lines changed

3 files changed

+62
-34
lines changed

src/librustc_resolve/lib.rs

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
1313
html_root_url = "https://doc.rust-lang.org/nightly/")]
1414

15+
#![feature(crate_visibility_modifier)]
1516
#![feature(rustc_diagnostic_macros)]
1617
#![feature(slice_sort_by_cached_key)]
1718

@@ -1655,11 +1656,11 @@ impl<'a> Resolver<'a> {
16551656
.map(|seg| Ident::new(seg.name, span))
16561657
.collect();
16571658
// FIXME (Manishearth): Intra doc links won't get warned of epoch changes
1658-
match self.resolve_path(&path, Some(namespace), true, span, None) {
1659+
match self.resolve_path(&path, Some(namespace), true, span, CrateLint::No) {
16591660
PathResult::Module(module) => *def = module.def().unwrap(),
16601661
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 =>
16611662
*def = path_res.base_def(),
1662-
PathResult::NonModule(..) => match self.resolve_path(&path, None, true, span, None) {
1663+
PathResult::NonModule(..) => match self.resolve_path(&path, None, true, span, CrateLint::No) {
16631664
PathResult::Failed(span, msg, _) => {
16641665
error_callback(self, span, ResolutionError::FailedToResolve(&msg));
16651666
}
@@ -2378,8 +2379,13 @@ impl<'a> Resolver<'a> {
23782379
if def != Def::Err {
23792380
new_id = Some(def.def_id());
23802381
let span = trait_ref.path.span;
2381-
if let PathResult::Module(module) = self.resolve_path(&path, None, false, span,
2382-
Some(trait_ref.ref_id)) {
2382+
if let PathResult::Module(module) = self.resolve_path(
2383+
&path,
2384+
None,
2385+
false,
2386+
span,
2387+
CrateLint::SimplePath(trait_ref.ref_id),
2388+
) {
23832389
new_val = Some((module, trait_ref.clone()));
23842390
}
23852391
}
@@ -2839,7 +2845,7 @@ impl<'a> Resolver<'a> {
28392845
} else {
28402846
let mod_path = &path[..path.len() - 1];
28412847
let mod_prefix = match this.resolve_path(mod_path, Some(TypeNS),
2842-
false, span, None) {
2848+
false, span, CrateLint::No) {
28432849
PathResult::Module(module) => module.def(),
28442850
_ => None,
28452851
}.map_or(format!(""), |def| format!("{} ", def.kind_name()));
@@ -3169,7 +3175,7 @@ impl<'a> Resolver<'a> {
31693175
));
31703176
}
31713177

3172-
let result = match self.resolve_path(&path, Some(ns), true, span, Some(id)) {
3178+
let result = match self.resolve_path(&path, Some(ns), true, span, CrateLint::SimplePath(id)) {
31733179
PathResult::NonModule(path_res) => path_res,
31743180
PathResult::Module(module) if !module.is_normal() => {
31753181
PathResolution::new(module.def().unwrap())
@@ -3206,7 +3212,7 @@ impl<'a> Resolver<'a> {
32063212
path[0].name != keywords::CrateRoot.name() &&
32073213
path[0].name != keywords::DollarCrate.name() {
32083214
let unqualified_result = {
3209-
match self.resolve_path(&[*path.last().unwrap()], Some(ns), false, span, None) {
3215+
match self.resolve_path(&[*path.last().unwrap()], Some(ns), false, span, CrateLint::No) {
32103216
PathResult::NonModule(path_res) => path_res.base_def(),
32113217
PathResult::Module(module) => module.def().unwrap(),
32123218
_ => return Some(result),
@@ -3221,14 +3227,14 @@ impl<'a> Resolver<'a> {
32213227
Some(result)
32223228
}
32233229

3224-
fn resolve_path(&mut self,
3225-
path: &[Ident],
3226-
opt_ns: Option<Namespace>, // `None` indicates a module path
3227-
record_used: bool,
3228-
path_span: Span,
3229-
node_id: Option<NodeId>) // None indicates that we don't care about linting
3230-
// `::module` paths
3231-
-> PathResult<'a> {
3230+
fn resolve_path(
3231+
&mut self,
3232+
path: &[Ident],
3233+
opt_ns: Option<Namespace>, // `None` indicates a module path
3234+
record_used: bool,
3235+
path_span: Span,
3236+
crate_lint: CrateLint,
3237+
) -> PathResult<'a> {
32323238
let mut module = None;
32333239
let mut allow_super = true;
32343240
let mut second_binding = None;
@@ -3347,7 +3353,7 @@ impl<'a> Resolver<'a> {
33473353
return PathResult::NonModule(err_path_resolution());
33483354
} else if opt_ns.is_some() && (is_last || maybe_assoc) {
33493355
self.lint_if_path_starts_with_module(
3350-
node_id,
3356+
crate_lint,
33513357
path,
33523358
path_span,
33533359
second_binding,
@@ -3392,19 +3398,22 @@ impl<'a> Resolver<'a> {
33923398
}
33933399
}
33943400

3395-
self.lint_if_path_starts_with_module(node_id, path, path_span, second_binding);
3401+
self.lint_if_path_starts_with_module(crate_lint, path, path_span, second_binding);
33963402

33973403
PathResult::Module(module.unwrap_or(self.graph_root))
33983404
}
33993405

3400-
fn lint_if_path_starts_with_module(&self,
3401-
id: Option<NodeId>,
3402-
path: &[Ident],
3403-
path_span: Span,
3404-
second_binding: Option<&NameBinding>) {
3405-
let id = match id {
3406-
Some(id) => id,
3407-
None => return,
3406+
fn lint_if_path_starts_with_module(
3407+
&self,
3408+
crate_lint: CrateLint,
3409+
path: &[Ident],
3410+
path_span: Span,
3411+
second_binding: Option<&NameBinding>,
3412+
) {
3413+
let (diag_id, diag_span) = match crate_lint {
3414+
CrateLint::No => return,
3415+
CrateLint::SimplePath(id) => (id, path_span),
3416+
CrateLint::UsePath { root_id, root_span } => (root_id, root_span),
34083417
};
34093418

34103419
let first_name = match path.get(0) {
@@ -3440,7 +3449,7 @@ impl<'a> Resolver<'a> {
34403449
}
34413450
}
34423451

3443-
self.lint_path_starts_with_module(id, path_span);
3452+
self.lint_path_starts_with_module(diag_id, diag_span);
34443453
}
34453454

34463455
fn lint_path_starts_with_module(&self, id: NodeId, span: Span) {
@@ -3676,7 +3685,7 @@ impl<'a> Resolver<'a> {
36763685
// Search in module.
36773686
let mod_path = &path[..path.len() - 1];
36783687
if let PathResult::Module(module) = self.resolve_path(mod_path, Some(TypeNS),
3679-
false, span, None) {
3688+
false, span, CrateLint::No) {
36803689
add_module_candidates(module, &mut names);
36813690
}
36823691
}
@@ -4475,4 +4484,19 @@ pub enum MakeGlobMap {
44754484
No,
44764485
}
44774486

4487+
enum CrateLint {
4488+
/// Do not issue the lint
4489+
No,
4490+
4491+
/// This lint applies to some random path like `impl ::foo::Bar`
4492+
/// or whatever. In this case, we can take the span of that path.
4493+
SimplePath(NodeId),
4494+
4495+
/// This lint comes from a `use` statement. In this case, what we
4496+
/// care about really is the *root* `use` statement; e.g., if we
4497+
/// have nested things like `use a::{b, c}`, we care about the
4498+
/// `use a` part.
4499+
UsePath { root_id: NodeId, root_span: Span },
4500+
}
4501+
44784502
__build_diagnostic_array! { librustc_resolve, DIAGNOSTICS }

src/librustc_resolve/macros.rs

Lines changed: 3 additions & 3 deletions
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 {AmbiguityError, Resolver, ResolutionError, resolve_error};
11+
use {AmbiguityError, CrateLint, Resolver, ResolutionError, resolve_error};
1212
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
1313
use Namespace::{self, MacroNS};
1414
use build_reduced_graph::BuildReducedGraphVisitor;
@@ -441,7 +441,7 @@ impl<'a> Resolver<'a> {
441441
return Err(Determinacy::Determined);
442442
}
443443

444-
let def = match self.resolve_path(&path, Some(MacroNS), false, span, None) {
444+
let def = match self.resolve_path(&path, Some(MacroNS), false, span, CrateLint::No) {
445445
PathResult::NonModule(path_res) => match path_res.base_def() {
446446
Def::Err => Err(Determinacy::Determined),
447447
def @ _ => {
@@ -619,7 +619,7 @@ impl<'a> Resolver<'a> {
619619
pub fn finalize_current_module_macro_resolutions(&mut self) {
620620
let module = self.current_module;
621621
for &(ref path, span) in module.macro_resolutions.borrow().iter() {
622-
match self.resolve_path(&path, Some(MacroNS), true, span, None) {
622+
match self.resolve_path(&path, Some(MacroNS), true, span, CrateLint::No) {
623623
PathResult::NonModule(_) => {},
624624
PathResult::Failed(span, msg, _) => {
625625
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));

src/librustc_resolve/resolve_imports.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use self::ImportDirectiveSubclass::*;
1212

13-
use {AmbiguityError, Module, PerNS};
13+
use {AmbiguityError, CrateLint, Module, PerNS};
1414
use Namespace::{self, TypeNS, MacroNS};
1515
use {NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyError};
1616
use Resolver;
@@ -94,6 +94,10 @@ impl<'a> ImportDirective<'a> {
9494
pub fn is_glob(&self) -> bool {
9595
match self.subclass { ImportDirectiveSubclass::GlobImport { .. } => true, _ => false }
9696
}
97+
98+
crate fn crate_lint(&self) -> CrateLint {
99+
CrateLint::UsePath { root_id: self.root_id, root_span: self.root_span }
100+
}
97101
}
98102

99103
#[derive(Clone, Default, Debug)]
@@ -599,7 +603,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
599603
// while resolving its module path.
600604
directive.vis.set(ty::Visibility::Invisible);
601605
let result = self.resolve_path(&directive.module_path[..], None, false,
602-
directive.span, Some(directive.id));
606+
directive.span, directive.crate_lint());
603607
directive.vis.set(vis);
604608

605609
match result {
@@ -733,7 +737,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
733737
}
734738
}
735739

736-
let module_result = self.resolve_path(&module_path, None, true, span, Some(directive.id));
740+
let module_result = self.resolve_path(&module_path, None, true, span, directive.crate_lint());
737741
let module = match module_result {
738742
PathResult::Module(module) => module,
739743
PathResult::Failed(span, msg, false) => {
@@ -748,7 +752,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
748752
!(self_path.len() > 1 && is_special(self_path[1])) {
749753
self_path[0].name = keywords::SelfValue.name();
750754
self_result = Some(self.resolve_path(&self_path, None, false,
751-
span, None));
755+
span, CrateLint::No));
752756
}
753757
return if let Some(PathResult::Module(..)) = self_result {
754758
Some((span, format!("Did you mean `{}`?", names_to_string(&self_path[..]))))

0 commit comments

Comments
 (0)