Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,42 +622,42 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {

match use_tree.kind {
ast::UseTreeKind::Simple(rename) => {
let mut ident = use_tree.ident();
let mut module_path = prefix;
let mut source = module_path.pop().unwrap();
let source = module_path.pop().unwrap();

// `true` for `...::{self [as target]}` imports, `false` otherwise.
let type_ns_only = nested && source.ident.name == kw::SelfLower;

// Suggest `use prefix::{self};` for `use prefix::self;`
if source.ident.name == kw::SelfLower
&& let Some(parent) = module_path.pop()
&& let Some(parent) = module_path.last()
&& !type_ns_only
&& (parent.ident.name != kw::PathRoot
|| self.r.path_root_is_crate_root(parent.ident))
{
// Suggest `use prefix::{self};` for `use prefix::self;`
if !type_ns_only
&& (parent.ident.name != kw::PathRoot
|| self.r.path_root_is_crate_root(parent.ident))
{
let span_with_rename = match rename {
Some(rename) => source.ident.span.to(rename.span),
None => source.ident.span,
};

self.r.report_error(
parent.ident.span.shrink_to_hi().to(source.ident.span),
ResolutionError::SelfImportsOnlyAllowedWithin {
root: parent.ident.name == kw::PathRoot,
span_with_rename,
},
);
}
let span_with_rename = match rename {
Some(rename) => source.ident.span.to(rename.span),
None => source.ident.span,
};

let self_span = source.ident.span;
source = parent;
if rename.is_none() {
ident = Ident::new(source.ident.name, self_span);
}
self.r.report_error(
parent.ident.span.shrink_to_hi().to(source.ident.span),
ResolutionError::SelfImportsOnlyAllowedWithin {
root: parent.ident.name == kw::PathRoot,
span_with_rename,
},
);
}

let ident = if source.ident.name == kw::SelfLower
&& rename.is_none()
&& let Some(parent) = module_path.last()
{
Ident::new(parent.ident.name, source.ident.span)
} else {
use_tree.ident()
};

match source.ident.name {
kw::DollarCrate => {
if !module_path.is_empty() {
Expand Down Expand Up @@ -694,7 +694,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
}
}
// Deny `use ::{self};` after edition 2015
kw::PathRoot if !self.r.path_root_is_crate_root(source.ident) => {
kw::SelfLower
if let Some(parent) = module_path.last()
&& parent.ident.name == kw::PathRoot
&& !self.r.path_root_is_crate_root(parent.ident) =>
{
self.r.dcx().span_err(use_tree.span, "extern prelude cannot be imported");
return;
}
Expand Down
18 changes: 11 additions & 7 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Result<Decl<'ra>, Determinacy> {
match module {
ModuleOrUniformRoot::Module(module) => {
if ns == TypeNS
&& ident.name == kw::Super
&& let Some(module) =
self.resolve_super_in_module(ident, Some(module), parent_scope)
{
return Ok(module.self_decl.unwrap());
if ns == TypeNS {
if ident.name == kw::SelfLower {
return Ok(module.self_decl.unwrap());
}
if ident.name == kw::Super
&& let Some(module) =
self.resolve_super_in_module(ident, Some(module), parent_scope)
{
return Ok(module.self_decl.unwrap());
}
}

let (ident_key, def) = IdentKey::new_adjusted(ident, module.expansion);
Expand Down Expand Up @@ -1018,7 +1022,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
{
let module = self.resolve_crate_root(ident);
return Ok(module.self_decl.unwrap());
} else if ident.name == kw::Super || ident.name == kw::SelfLower {
} else if ident.name == kw::Super {
// FIXME: Implement these with renaming requirements so that e.g.
// `use super;` doesn't work, but `use super as name;` does.
// Fall through here to get an error from `early_resolve_...`.
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/imports/cycle-import-in-std-1.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
error[E0432]: unresolved import `ops`
--> $DIR/cycle-import-in-std-1.rs:5:11
--> $DIR/cycle-import-in-std-1.rs:5:5
|
LL | use ops::{self as std};
| ^^^^^^^^^^^ no external crate `ops`
| ^^^
|
= help: consider importing this module instead:
std::ops
help: a similar path exists
|
LL | use core::ops::{self as std};
| ++++++

error: aborting due to 1 previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/imports/cycle-import-in-std-2.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
error[E0432]: unresolved import `ops`
--> $DIR/cycle-import-in-std-2.rs:5:11
--> $DIR/cycle-import-in-std-2.rs:5:5
|
LL | use ops::{self as std};
| ^^^^^^^^^^^ no external crate `ops`
| ^^^
|
= help: consider importing this module instead:
std::ops
help: a similar path exists
|
LL | use core::ops::{self as std};
| ++++++

error: aborting due to 1 previous error

Expand Down
7 changes: 6 additions & 1 deletion tests/ui/imports/issue-28388-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0432]: unresolved import `foo`
--> $DIR/issue-28388-1.rs:4:5
|
LL | use foo::{};
| ^^^^^^^ no `foo` in the root
| ^^^ use of unresolved module or unlinked crate `foo`
|
help: you might be missing a crate named `foo`, add it to your project and import it in your code
|
LL + extern crate foo;
|

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/imports/issue-38293.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0432]: unresolved import `foo::f`
--> $DIR/issue-38293.rs:7:14
--> $DIR/issue-38293.rs:7:10
|
LL | use foo::f::{self};
| ^^^^ no `f` in `foo`
| ^ expected type, found function `f` in `foo`

error[E0423]: expected function, found module `baz`
--> $DIR/issue-38293.rs:16:5
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/imports/issue-45829/import-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ LL | use foo::self;
= note: `foo` must be defined only once in the type namespace of this module
help: you can use `as` to change the binding name of the import
|
LL - use foo::self;
LL + use foo as other_foo;
|
LL | use foo::self as other_foo;
| ++++++++++++

error[E0252]: the name `A` is defined multiple times
--> $DIR/import-self.rs:16:11
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/privacy/private-variant-reexport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod m2 {
}

mod m3 {
pub use ::E::V::{self}; //~ ERROR `V` is only public within the crate, and cannot be re-exported outside
pub use ::E::V::{self}; //~ ERROR unresolved import `E::V`
}

#[deny(unused_imports)]
Expand Down
10 changes: 4 additions & 6 deletions tests/ui/privacy/private-variant-reexport.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ note: consider marking `V` as `pub` in the imported module
LL | pub use ::E::{V};
| ^

error[E0365]: `V` is only public within the crate, and cannot be re-exported outside
--> $DIR/private-variant-reexport.rs:11:22
error[E0432]: unresolved import `E::V`
--> $DIR/private-variant-reexport.rs:11:18
|
LL | pub use ::E::V::{self};
| ^^^^ re-export of crate public `V`
|
= note: consider declaring type or module `V` with `pub`
| ^ `V` is a variant, not a module

error: glob import doesn't reexport anything with visibility `pub` because no imported item is public enough
--> $DIR/private-variant-reexport.rs:16:13
Expand Down Expand Up @@ -56,5 +54,5 @@ LL | pub use ::E::*;

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0364, E0365.
Some errors have detailed explanations: E0364, E0432.
For more information about an error, try `rustc --explain E0364`.
2 changes: 1 addition & 1 deletion tests/ui/resolve/resolve-bad-import-prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {}; // OK
use ::{}; // OK
use m::{}; // OK
use E::{}; // OK
use S::{}; // FIXME, this and `use S::{self};` should be an error
use S::{}; //~ ERROR unresolved import `S`
use Tr::{}; // FIXME, this and `use Tr::{self};` should be an error
use Nonexistent::{}; //~ ERROR unresolved import `Nonexistent`

Expand Down
15 changes: 13 additions & 2 deletions tests/ui/resolve/resolve-bad-import-prefix.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
error[E0432]: unresolved import `S`
--> $DIR/resolve-bad-import-prefix.rs:11:5
|
LL | use S::{};
| ^ `S` is a struct, not a module

error[E0432]: unresolved import `Nonexistent`
--> $DIR/resolve-bad-import-prefix.rs:13:5
|
LL | use Nonexistent::{};
| ^^^^^^^^^^^^^^^ no `Nonexistent` in the root
| ^^^^^^^^^^^ use of unresolved module or unlinked crate `Nonexistent`
|
help: you might be missing a crate named `Nonexistent`, add it to your project and import it in your code
|
LL + extern crate Nonexistent;
|

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0432`.
9 changes: 7 additions & 2 deletions tests/ui/use/use-mod/use-mod-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ LL | use std::mem::{self};
| + +

error[E0432]: unresolved import `crate::foo`
--> $DIR/use-mod-4.rs:1:5
--> $DIR/use-mod-4.rs:1:12
|
LL | use crate::foo::self;
| ^^^^^^^^^^^^^^^^ no `foo` in the root
| ^^^ use of unresolved module or unlinked crate `foo`
|
help: you might be missing a crate named `foo`, add it to your project and import it in your code
|
LL + extern crate foo;
|

error: aborting due to 3 previous errors

Expand Down
38 changes: 24 additions & 14 deletions tests/ui/use/use-path-segment-kw.e2015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -935,39 +935,49 @@ help: try renaming it with a name
LL | use $crate::{self as name};
| +++++++

error[E0432]: unresolved import `foobar`
error[E0433]: cannot find module or crate `foobar` in the crate root
--> $DIR/use-path-segment-kw.rs:187:17
|
LL | pub use foobar::qux::self;
| ^^^^^^
| ^^^^^^ use of unresolved module or unlinked crate `foobar`
|
help: a similar path exists
help: you might be missing a crate named `foobar`, add it to your project and import it in your code
|
LL | pub use self::foobar::qux::self;
| ++++++

error[E0432]: unresolved import `foobar`
--> $DIR/use-path-segment-kw.rs:189:17
LL + extern crate foobar;
|
LL | pub use foobar::self as _self3;
| ^^^^^^^^^^^^^^^^^^^^^^ no `foobar` in the root

error[E0432]: unresolved import `foobar`
error[E0433]: cannot find module or crate `foobar` in the crate root
--> $DIR/use-path-segment-kw.rs:191:17
|
LL | pub use foobar::baz::{self};
| ^^^^^^ use of unresolved module or unlinked crate `foobar`
|
help: you might be missing a crate named `foobar`, add it to your project and import it in your code
|
LL + extern crate foobar;
|

error[E0432]: unresolved import `foobar`
--> $DIR/use-path-segment-kw.rs:189:17
|
LL | pub use foobar::self as _self3;
| ^^^^^^
|
help: a similar path exists
|
LL | pub use self::foobar::baz::{self};
LL | pub use self::foobar::self as _self3;
| ++++++

error[E0432]: unresolved import `foobar`
--> $DIR/use-path-segment-kw.rs:192:26
--> $DIR/use-path-segment-kw.rs:192:17
|
LL | pub use foobar::{self as _nested_self3};
| ^^^^^^^^^^^^^^^^^^^^^ no `foobar` in the root
| ^^^^^^
|
help: a similar path exists
|
LL | pub use self::foobar::{self as _nested_self3};
| ++++++

error[E0433]: `self` in paths can only be used in start position
--> $DIR/use-path-segment-kw.rs:215:36
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/use/use-path-segment-kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ mod foo {

type D3 = foobar::self; //~ ERROR `self` in paths can only be used in start position
pub use foobar::qux::self; //~ ERROR `self` imports are only allowed within a { } list
//[e2015]~^ ERROR unresolved import `foobar`
//[e2015]~^ ERROR cannot find module or crate `foobar` in the crate root
pub use foobar::self as _self3; //~ ERROR `self` imports are only allowed within a { } list
//[e2015]~^ ERROR unresolved import `foobar`
pub use foobar::baz::{self}; //[e2015]~ ERROR unresolved import `foobar`
pub use foobar::baz::{self}; //[e2015]~ ERROR cannot find module or crate `foobar` in the crate root
pub use foobar::{self as _nested_self3}; //[e2015]~ ERROR unresolved import `foobar`

type D4 = crate::self; //~ ERROR `self` in paths can only be used in start position
Expand Down
Loading
Loading