Skip to content

Commit d034ae5

Browse files
committed
Auto merge of #50969 - nikomatsakis:issue-50673-broken-migration-lint, r=alexcrichton
fix suggestions with nested paths Fixes #50673 cc @Manishearth @petrochenkov r? @alexcrichton
2 parents e063518 + dfd2a13 commit d034ae5

11 files changed

+236
-43
lines changed

src/librustc_resolve/build_reduced_graph.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ impl<'a> Resolver<'a> {
100100
}
101101

102102
fn build_reduced_graph_for_use_tree(&mut self,
103+
root_use_tree: &ast::UseTree,
104+
root_id: NodeId,
103105
use_tree: &ast::UseTree,
104106
id: NodeId,
105107
vis: ty::Visibility,
@@ -182,7 +184,14 @@ impl<'a> Resolver<'a> {
182184
type_ns_only,
183185
};
184186
self.add_import_directive(
185-
module_path, subclass, use_tree.span, id, vis, expansion,
187+
module_path,
188+
subclass,
189+
use_tree.span,
190+
id,
191+
root_use_tree.span,
192+
root_id,
193+
vis,
194+
expansion,
186195
);
187196
}
188197
ast::UseTreeKind::Glob => {
@@ -191,7 +200,14 @@ impl<'a> Resolver<'a> {
191200
max_vis: Cell::new(ty::Visibility::Invisible),
192201
};
193202
self.add_import_directive(
194-
module_path, subclass, use_tree.span, id, vis, expansion,
203+
module_path,
204+
subclass,
205+
use_tree.span,
206+
id,
207+
root_use_tree.span,
208+
root_id,
209+
vis,
210+
expansion,
195211
);
196212
}
197213
ast::UseTreeKind::Nested(ref items) => {
@@ -226,7 +242,7 @@ impl<'a> Resolver<'a> {
226242

227243
for &(ref tree, id) in items {
228244
self.build_reduced_graph_for_use_tree(
229-
tree, id, vis, &prefix, true, item, expansion
245+
root_use_tree, root_id, tree, id, vis, &prefix, true, item, expansion
230246
);
231247
}
232248
}
@@ -249,7 +265,7 @@ impl<'a> Resolver<'a> {
249265
};
250266

251267
self.build_reduced_graph_for_use_tree(
252-
use_tree, item.id, vis, &prefix, false, item, expansion,
268+
use_tree, item.id, use_tree, item.id, vis, &prefix, false, item, expansion,
253269
);
254270
}
255271

@@ -266,10 +282,12 @@ impl<'a> Resolver<'a> {
266282
let binding =
267283
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
268284
let directive = self.arenas.alloc_import_directive(ImportDirective {
285+
root_id: item.id,
269286
id: item.id,
270287
parent,
271288
imported_module: Cell::new(Some(module)),
272289
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
290+
root_span: item.span,
273291
span: item.span,
274292
module_path: Vec::new(),
275293
vis: Cell::new(vis),
@@ -640,10 +658,12 @@ impl<'a> Resolver<'a> {
640658

641659
let (graph_root, arenas) = (self.graph_root, self.arenas);
642660
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
661+
root_id: item.id,
643662
id: item.id,
644663
parent: graph_root,
645664
imported_module: Cell::new(Some(module)),
646665
subclass: ImportDirectiveSubclass::MacroUse,
666+
root_span: span,
647667
span,
648668
module_path: Vec::new(),
649669
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),

src/librustc_resolve/lib.rs

+69-27
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

@@ -1634,11 +1635,17 @@ impl<'a> Resolver<'a> {
16341635
.map(|seg| Ident::new(seg.name, span))
16351636
.collect();
16361637
// FIXME (Manishearth): Intra doc links won't get warned of epoch changes
1637-
match self.resolve_path(&path, Some(namespace), true, span, None) {
1638+
match self.resolve_path(&path, Some(namespace), true, span, CrateLint::No) {
16381639
PathResult::Module(module) => *def = module.def().unwrap(),
16391640
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 =>
16401641
*def = path_res.base_def(),
1641-
PathResult::NonModule(..) => match self.resolve_path(&path, None, true, span, None) {
1642+
PathResult::NonModule(..) => match self.resolve_path(
1643+
&path,
1644+
None,
1645+
true,
1646+
span,
1647+
CrateLint::No,
1648+
) {
16421649
PathResult::Failed(span, msg, _) => {
16431650
error_callback(self, span, ResolutionError::FailedToResolve(&msg));
16441651
}
@@ -2352,8 +2359,13 @@ impl<'a> Resolver<'a> {
23522359
if def != Def::Err {
23532360
new_id = Some(def.def_id());
23542361
let span = trait_ref.path.span;
2355-
if let PathResult::Module(module) = self.resolve_path(&path, None, false, span,
2356-
Some(trait_ref.ref_id)) {
2362+
if let PathResult::Module(module) = self.resolve_path(
2363+
&path,
2364+
None,
2365+
false,
2366+
span,
2367+
CrateLint::SimplePath(trait_ref.ref_id),
2368+
) {
23572369
new_val = Some((module, trait_ref.clone()));
23582370
}
23592371
}
@@ -2813,7 +2825,7 @@ impl<'a> Resolver<'a> {
28132825
} else {
28142826
let mod_path = &path[..path.len() - 1];
28152827
let mod_prefix = match this.resolve_path(mod_path, Some(TypeNS),
2816-
false, span, None) {
2828+
false, span, CrateLint::No) {
28172829
PathResult::Module(module) => module.def(),
28182830
_ => None,
28192831
}.map_or(format!(""), |def| format!("{} ", def.kind_name()));
@@ -3143,7 +3155,13 @@ impl<'a> Resolver<'a> {
31433155
));
31443156
}
31453157

3146-
let result = match self.resolve_path(&path, Some(ns), true, span, Some(id)) {
3158+
let result = match self.resolve_path(
3159+
&path,
3160+
Some(ns),
3161+
true,
3162+
span,
3163+
CrateLint::SimplePath(id),
3164+
) {
31473165
PathResult::NonModule(path_res) => path_res,
31483166
PathResult::Module(module) if !module.is_normal() => {
31493167
PathResolution::new(module.def().unwrap())
@@ -3180,7 +3198,13 @@ impl<'a> Resolver<'a> {
31803198
path[0].name != keywords::CrateRoot.name() &&
31813199
path[0].name != keywords::DollarCrate.name() {
31823200
let unqualified_result = {
3183-
match self.resolve_path(&[*path.last().unwrap()], Some(ns), false, span, None) {
3201+
match self.resolve_path(
3202+
&[*path.last().unwrap()],
3203+
Some(ns),
3204+
false,
3205+
span,
3206+
CrateLint::No,
3207+
) {
31843208
PathResult::NonModule(path_res) => path_res.base_def(),
31853209
PathResult::Module(module) => module.def().unwrap(),
31863210
_ => return Some(result),
@@ -3195,14 +3219,14 @@ impl<'a> Resolver<'a> {
31953219
Some(result)
31963220
}
31973221

3198-
fn resolve_path(&mut self,
3199-
path: &[Ident],
3200-
opt_ns: Option<Namespace>, // `None` indicates a module path
3201-
record_used: bool,
3202-
path_span: Span,
3203-
node_id: Option<NodeId>) // None indicates that we don't care about linting
3204-
// `::module` paths
3205-
-> PathResult<'a> {
3222+
fn resolve_path(
3223+
&mut self,
3224+
path: &[Ident],
3225+
opt_ns: Option<Namespace>, // `None` indicates a module path
3226+
record_used: bool,
3227+
path_span: Span,
3228+
crate_lint: CrateLint,
3229+
) -> PathResult<'a> {
32063230
let mut module = None;
32073231
let mut allow_super = true;
32083232
let mut second_binding = None;
@@ -3321,7 +3345,7 @@ impl<'a> Resolver<'a> {
33213345
return PathResult::NonModule(err_path_resolution());
33223346
} else if opt_ns.is_some() && (is_last || maybe_assoc) {
33233347
self.lint_if_path_starts_with_module(
3324-
node_id,
3348+
crate_lint,
33253349
path,
33263350
path_span,
33273351
second_binding,
@@ -3366,19 +3390,22 @@ impl<'a> Resolver<'a> {
33663390
}
33673391
}
33683392

3369-
self.lint_if_path_starts_with_module(node_id, path, path_span, second_binding);
3393+
self.lint_if_path_starts_with_module(crate_lint, path, path_span, second_binding);
33703394

33713395
PathResult::Module(module.unwrap_or(self.graph_root))
33723396
}
33733397

3374-
fn lint_if_path_starts_with_module(&self,
3375-
id: Option<NodeId>,
3376-
path: &[Ident],
3377-
path_span: Span,
3378-
second_binding: Option<&NameBinding>) {
3379-
let id = match id {
3380-
Some(id) => id,
3381-
None => return,
3398+
fn lint_if_path_starts_with_module(
3399+
&self,
3400+
crate_lint: CrateLint,
3401+
path: &[Ident],
3402+
path_span: Span,
3403+
second_binding: Option<&NameBinding>,
3404+
) {
3405+
let (diag_id, diag_span) = match crate_lint {
3406+
CrateLint::No => return,
3407+
CrateLint::SimplePath(id) => (id, path_span),
3408+
CrateLint::UsePath { root_id, root_span } => (root_id, root_span),
33823409
};
33833410

33843411
let first_name = match path.get(0) {
@@ -3414,7 +3441,7 @@ impl<'a> Resolver<'a> {
34143441
}
34153442
}
34163443

3417-
self.lint_path_starts_with_module(id, path_span);
3444+
self.lint_path_starts_with_module(diag_id, diag_span);
34183445
}
34193446

34203447
fn lint_path_starts_with_module(&self, id: NodeId, span: Span) {
@@ -3650,7 +3677,7 @@ impl<'a> Resolver<'a> {
36503677
// Search in module.
36513678
let mod_path = &path[..path.len() - 1];
36523679
if let PathResult::Module(module) = self.resolve_path(mod_path, Some(TypeNS),
3653-
false, span, None) {
3680+
false, span, CrateLint::No) {
36543681
add_module_candidates(module, &mut names);
36553682
}
36563683
}
@@ -4427,4 +4454,19 @@ pub enum MakeGlobMap {
44274454
No,
44284455
}
44294456

4457+
enum CrateLint {
4458+
/// Do not issue the lint
4459+
No,
4460+
4461+
/// This lint applies to some random path like `impl ::foo::Bar`
4462+
/// or whatever. In this case, we can take the span of that path.
4463+
SimplePath(NodeId),
4464+
4465+
/// This lint comes from a `use` statement. In this case, what we
4466+
/// care about really is the *root* `use` statement; e.g., if we
4467+
/// have nested things like `use a::{b, c}`, we care about the
4468+
/// `use a` part.
4469+
UsePath { root_id: NodeId, root_span: Span },
4470+
}
4471+
44304472
__build_diagnostic_array! { librustc_resolve, DIAGNOSTICS }

src/librustc_resolve/macros.rs

+3-3
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;
@@ -436,7 +436,7 @@ impl<'a> Resolver<'a> {
436436
return Err(Determinacy::Determined);
437437
}
438438

439-
let def = match self.resolve_path(&path, Some(MacroNS), false, span, None) {
439+
let def = match self.resolve_path(&path, Some(MacroNS), false, span, CrateLint::No) {
440440
PathResult::NonModule(path_res) => match path_res.base_def() {
441441
Def::Err => Err(Determinacy::Determined),
442442
def @ _ => {
@@ -613,7 +613,7 @@ impl<'a> Resolver<'a> {
613613
pub fn finalize_current_module_macro_resolutions(&mut self) {
614614
let module = self.current_module;
615615
for &(ref path, span) in module.macro_resolutions.borrow().iter() {
616-
match self.resolve_path(&path, Some(MacroNS), true, span, None) {
616+
match self.resolve_path(&path, Some(MacroNS), true, span, CrateLint::No) {
617617
PathResult::NonModule(_) => {},
618618
PathResult::Failed(span, msg, _) => {
619619
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));

0 commit comments

Comments
 (0)