Skip to content

Commit

Permalink
resolve: Desugar empty import groups into synthetic dummy imports
Browse files Browse the repository at this point in the history
so that they are correctly resolved on 2018 edition
  • Loading branch information
petrochenkov authored and pietroalbini committed Oct 29, 2018
1 parent 540d837 commit f7e7b3a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 98 deletions.
3 changes: 2 additions & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3094,7 +3094,8 @@ impl<'a> LoweringContext<'a> {
// Privatize the degenerate import base, used only to check
// the stability of `use a::{};`, to avoid it showing up as
// a re-export by accident when `pub`, e.g. in documentation.
let path = P(self.lower_path(id, &prefix, ParamMode::Explicit));
let def = self.expect_full_def_from_use(id).next().unwrap_or(Def::Err);
let path = P(self.lower_path_extra(def, &prefix, None, ParamMode::Explicit, None));
*vis = respan(prefix.span.shrink_to_lo(), hir::VisibilityKind::Inherited);
hir::ItemKind::Use(path, hir::UseKind::ListStem)
}
Expand Down
33 changes: 29 additions & 4 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
uniform_paths_canary_emitted = true;
}

let empty_for_self = |prefix: &[Segment]| {
prefix.is_empty() ||
prefix.len() == 1 && prefix[0].ident.name == keywords::CrateRoot.name()
};
match use_tree.kind {
ast::UseTreeKind::Simple(rename, ..) => {
let mut ident = use_tree.ident();
Expand All @@ -262,10 +266,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
if source.name == keywords::SelfValue.name() {
type_ns_only = true;

let empty_prefix = module_path.last().map_or(true, |ident| {
ident.name == keywords::CrateRoot.name()
});
if empty_prefix {
if empty_for_self(&module_path) {
resolve_error(
self,
use_tree.span,
Expand Down Expand Up @@ -394,6 +395,30 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
parent_scope.clone(), item, vis, root_span,
);
}

// Empty groups `a::b::{}` are turned into synthetic `self` imports
// `a::b::c::{self as _}`, so that their prefixes are correctly
// resolved and checked for privacy/stability/etc.
if items.is_empty() && !empty_for_self(&prefix) {
let new_span = prefix[prefix.len() - 1].ident.span;
let tree = ast::UseTree {
prefix: ast::Path::from_ident(
Ident::new(keywords::SelfValue.name(), new_span)
),
kind: ast::UseTreeKind::Simple(
Some(Ident::new(keywords::Underscore.name().gensymed(), new_span)),
ast::DUMMY_NODE_ID,
ast::DUMMY_NODE_ID,
),
span: use_tree.span,
};
self.build_reduced_graph_for_use_tree(
// This particular use tree
&tree, id, &prefix, true, uniform_paths_canary_emitted,
// The whole `use` item
parent_scope.clone(), item, ty::Visibility::Invisible, root_span,
);
}
}
}
}
Expand Down
72 changes: 7 additions & 65 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,23 +482,21 @@ enum PathSource<'a> {
TraitItem(Namespace),
// Path in `pub(path)`
Visibility,
// Path in `use a::b::{...};`
ImportPrefix,
}

impl<'a> PathSource<'a> {
fn namespace(self) -> Namespace {
match self {
PathSource::Type | PathSource::Trait(_) | PathSource::Struct |
PathSource::Visibility | PathSource::ImportPrefix => TypeNS,
PathSource::Visibility => TypeNS,
PathSource::Expr(..) | PathSource::Pat | PathSource::TupleStruct => ValueNS,
PathSource::TraitItem(ns) => ns,
}
}

fn global_by_default(self) -> bool {
match self {
PathSource::Visibility | PathSource::ImportPrefix => true,
PathSource::Visibility => true,
PathSource::Type | PathSource::Expr(..) | PathSource::Pat |
PathSource::Struct | PathSource::TupleStruct |
PathSource::Trait(_) | PathSource::TraitItem(..) => false,
Expand All @@ -510,7 +508,7 @@ impl<'a> PathSource<'a> {
PathSource::Type | PathSource::Expr(..) | PathSource::Pat |
PathSource::Struct | PathSource::TupleStruct => true,
PathSource::Trait(_) | PathSource::TraitItem(..) |
PathSource::Visibility | PathSource::ImportPrefix => false,
PathSource::Visibility => false,
}
}

Expand All @@ -522,7 +520,6 @@ impl<'a> PathSource<'a> {
PathSource::Struct => "struct, variant or union type",
PathSource::TupleStruct => "tuple struct/variant",
PathSource::Visibility => "module",
PathSource::ImportPrefix => "module or enum",
PathSource::TraitItem(ns) => match ns {
TypeNS => "associated type",
ValueNS => "method or associated constant",
Expand Down Expand Up @@ -587,10 +584,6 @@ impl<'a> PathSource<'a> {
Def::AssociatedTy(..) if ns == TypeNS => true,
_ => false,
},
PathSource::ImportPrefix => match def {
Def::Mod(..) | Def::Enum(..) => true,
_ => false,
},
PathSource::Visibility => match def {
Def::Mod(..) => true,
_ => false,
Expand Down Expand Up @@ -626,8 +619,8 @@ impl<'a> PathSource<'a> {
(PathSource::Pat, false) | (PathSource::TupleStruct, false) => "E0531",
(PathSource::TraitItem(..), true) => "E0575",
(PathSource::TraitItem(..), false) => "E0576",
(PathSource::Visibility, true) | (PathSource::ImportPrefix, true) => "E0577",
(PathSource::Visibility, false) | (PathSource::ImportPrefix, false) => "E0578",
(PathSource::Visibility, true) => "E0577",
(PathSource::Visibility, false) => "E0578",
}
}
}
Expand Down Expand Up @@ -2298,66 +2291,15 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
});
}

ItemKind::Use(ref use_tree) => {
// Imports are resolved as global by default, add starting root segment.
let path = Path {
segments: use_tree.prefix.make_root().into_iter().collect(),
span: use_tree.span,
};
self.resolve_use_tree(item.id, use_tree.span, item.id, use_tree, &path);
}

ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_) => {
ItemKind::Use(..) | ItemKind::ExternCrate(..) |
ItemKind::MacroDef(..) | ItemKind::GlobalAsm(..) => {
// do nothing, these are just around to be encoded
}

ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
}
}

/// For the most part, use trees are desugared into `ImportDirective` instances
/// when building the reduced graph (see `build_reduced_graph_for_use_tree`). But
/// there is one special case we handle here: an empty nested import like
/// `a::{b::{}}`, which desugares into...no import directives.
fn resolve_use_tree(
&mut self,
root_id: NodeId,
root_span: Span,
id: NodeId,
use_tree: &ast::UseTree,
prefix: &Path,
) {
match use_tree.kind {
ast::UseTreeKind::Nested(ref items) => {
let path = Path {
segments: prefix.segments
.iter()
.chain(use_tree.prefix.segments.iter())
.cloned()
.collect(),
span: prefix.span.to(use_tree.prefix.span),
};

if items.is_empty() {
// Resolve prefix of an import with empty braces (issue #28388).
self.smart_resolve_path_with_crate_lint(
id,
None,
&path,
PathSource::ImportPrefix,
CrateLint::UsePath { root_id, root_span },
);
} else {
for &(ref tree, nested_id) in items {
self.resolve_use_tree(root_id, root_span, nested_id, tree, &path);
}
}
}
ast::UseTreeKind::Simple(..) => {},
ast::UseTreeKind::Glob => {},
}
}

fn with_type_parameter_rib<'b, F>(&'b mut self, type_parameters: TypeParameters<'a, 'b>, f: F)
where F: FnOnce(&mut Resolver)
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-28388-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

// Prefix in imports with empty braces should be resolved and checked privacy, stability, etc.

use foo::{}; //~ ERROR cannot find module or enum `foo` in the crate root
use foo::{}; //~ ERROR unresolved import `foo`

fn main() {}
8 changes: 4 additions & 4 deletions src/test/ui/issues/issue-28388-1.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error[E0578]: cannot find module or enum `foo` in the crate root
error[E0432]: unresolved import `foo`
--> $DIR/issue-28388-1.rs:13:5
|
LL | use foo::{}; //~ ERROR cannot find module or enum `foo` in the crate root
| ^^^ not found in the crate root
LL | use foo::{}; //~ ERROR unresolved import `foo`
| ^^^^^^^ no `foo` in the root

error: aborting due to previous error

For more information about this error, try `rustc --explain E0578`.
For more information about this error, try `rustc --explain E0432`.
6 changes: 3 additions & 3 deletions src/test/ui/resolve/resolve-bad-import-prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use {}; // OK
use ::{}; // OK
use m::{}; // OK
use E::{}; // OK
use S::{}; //~ ERROR expected module or enum, found struct `S`
use Tr::{}; //~ ERROR expected module or enum, found trait `Tr`
use Nonexistent::{}; //~ ERROR cannot find module or enum `Nonexistent` in the crate root
use S::{}; // FIXME, this and `use S::{self};` should be an error
use Tr::{}; // FIXME, this and `use Tr::{self};` should be an error
use Nonexistent::{}; //~ ERROR unresolved import `Nonexistent`

fn main () {}
25 changes: 5 additions & 20 deletions src/test/ui/resolve/resolve-bad-import-prefix.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
error[E0577]: expected module or enum, found struct `S`
--> $DIR/resolve-bad-import-prefix.rs:20:5
|
LL | use S::{}; //~ ERROR expected module or enum, found struct `S`
| -^^^^
| |
| did you mean `E`?

error[E0577]: expected module or enum, found trait `Tr`
--> $DIR/resolve-bad-import-prefix.rs:21:5
|
LL | use Tr::{}; //~ ERROR expected module or enum, found trait `Tr`
| ^^^^^^ not a module or enum

error[E0578]: cannot find module or enum `Nonexistent` in the crate root
error[E0432]: unresolved import `Nonexistent`
--> $DIR/resolve-bad-import-prefix.rs:22:5
|
LL | use Nonexistent::{}; //~ ERROR cannot find module or enum `Nonexistent` in the crate root
| ^^^^^^^^^^^ not found in the crate root
LL | use Nonexistent::{}; //~ ERROR unresolved import `Nonexistent`
| ^^^^^^^^^^^^^^^ no `Nonexistent` in the root

error: aborting due to 3 previous errors
error: aborting due to previous error

Some errors occurred: E0577, E0578.
For more information about an error, try `rustc --explain E0577`.
For more information about this error, try `rustc --explain E0432`.

0 comments on commit f7e7b3a

Please sign in to comment.