Skip to content

Commit ad7b22e

Browse files
committed
Auto merge of #54005 - eddyb:uniform-paths-self-resolve, r=petrochenkov
rustc_resolve: allow `use crate_name;` under `uniform_paths`. Specifically, `use crate_name;` and `use crate_name::{self, ...};` are now allowed, whereas previously there would produce a (false positive) ambiguity error, as the ambiguity detection code was seeing the `crate_name` import as a locally-available definition to conflict with the crate. r? @petrochenkov cc @aturon @joshtriplett @Centril
2 parents a8c11d2 + 31fce91 commit ad7b22e

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

src/librustc_resolve/build_reduced_graph.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
181181
self.session.features_untracked().uniform_paths);
182182

183183
let source = module_path[0];
184+
185+
// HACK(eddyb) For `use x::{self, ...};`, use the ID of the
186+
// `self` nested import for the canary. This allows the
187+
// ambiguity reporting scope to ignore false positives
188+
// in the same way it does for `use x;` (by comparing IDs).
189+
let mut canary_id = id;
190+
if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
191+
for &(ref use_tree, id) in items {
192+
if let ast::UseTreeKind::Simple(..) = use_tree.kind {
193+
if use_tree.ident().name == keywords::SelfValue.name() {
194+
canary_id = id;
195+
break;
196+
}
197+
}
198+
}
199+
}
200+
184201
// Helper closure to emit a canary with the given base path.
185202
let emit = |this: &mut Self, base: Option<Ident>| {
186203
let subclass = SingleImport {
@@ -200,7 +217,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
200217
base.into_iter().collect(),
201218
subclass.clone(),
202219
source.span,
203-
id,
220+
canary_id,
204221
root_use_tree.span,
205222
root_id,
206223
ty::Visibility::Invisible,

src/librustc_resolve/resolve_imports.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,14 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
664664

665665
self.per_ns(|_, ns| {
666666
if let Some(result) = result[ns].get().ok() {
667+
if let NameBindingKind::Import { directive, .. } = result.kind {
668+
// Skip canaries that resolve to the import itself.
669+
// These come from `use crate_name;`, which isn't really
670+
// ambiguous, as the import can't actually shadow itself.
671+
if directive.id == import.id {
672+
return;
673+
}
674+
}
667675
if has_explicit_self {
668676
// There should only be one `self::x` (module-scoped) canary.
669677
assert_eq!(canary_results[ns].module_scope, None);
@@ -718,22 +726,6 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
718726

719727
errors = true;
720728

721-
// Special-case the error when `self::x` finds its own `use x;`.
722-
if has_external_crate &&
723-
results.module_scope == Some(span) &&
724-
results.block_scopes.is_empty() {
725-
let msg = format!("`{}` import is redundant", name);
726-
this.session.struct_span_err(span, &msg)
727-
.span_label(span,
728-
format!("refers to external crate `::{}`", name))
729-
.span_label(span,
730-
format!("defines `self::{}`, shadowing itself", name))
731-
.help(&format!("remove or write `::{}` explicitly instead", name))
732-
.note("relative `use` paths enabled by `#![feature(uniform_paths)]`")
733-
.emit();
734-
return;
735-
}
736-
737729
let msg = format!("`{}` import is ambiguous", name);
738730
let mut err = this.session.struct_span_err(span, &msg);
739731
let mut suggestion_choices = String::new();

src/test/ui/rust-2018/uniform-paths/redundant.rs src/test/run-pass/redundant.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@
1414

1515
use std;
1616

17-
fn main() {}
17+
mod foo {
18+
pub use std as my_std;
19+
}
20+
21+
mod bar {
22+
pub use std::{self};
23+
}
24+
25+
fn main() {
26+
self::std::io::stdout();
27+
foo::my_std::io::stdout();
28+
bar::std::io::stdout();
29+
}

src/test/ui/rust-2018/uniform-paths/redundant.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)