Skip to content

Commit cd17b1d

Browse files
authored
Rollup merge of rust-lang#56211 - petrochenkov:fwd, r=petrochenkov
[master] Forward-ports from beta rust-lang#56206 + one commit from rust-lang#55884 that was accidentally missing in rust-lang#56042 due to an off-by-one mistake in commit ranges r? @ghost
2 parents baf45d6 + fe548e3 commit cd17b1d

File tree

9 files changed

+129
-35
lines changed

9 files changed

+129
-35
lines changed

src/librustc_resolve/lib.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -4710,7 +4710,18 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
47104710
ty::Visibility::Restricted(self.current_module.normal_ancestor_id)
47114711
}
47124712
ast::VisibilityKind::Restricted { ref path, id, .. } => {
4713-
// Visibilities are resolved as global by default, add starting root segment.
4713+
// For visibilities we are not ready to provide correct implementation of "uniform
4714+
// paths" right now, so on 2018 edition we only allow module-relative paths for now.
4715+
let first_ident = path.segments[0].ident;
4716+
if self.session.rust_2018() && !first_ident.is_path_segment_keyword() {
4717+
let msg = "relative paths are not supported in visibilities on 2018 edition";
4718+
self.session.struct_span_err(first_ident.span, msg)
4719+
.span_suggestion(path.span, "try", format!("crate::{}", path))
4720+
.emit();
4721+
return ty::Visibility::Public;
4722+
}
4723+
// On 2015 visibilities are resolved as crate-relative by default,
4724+
// add starting root segment if necessary.
47144725
let segments = path.make_root().iter().chain(path.segments.iter())
47154726
.map(|seg| Segment { ident: seg.ident, id: Some(seg.id) })
47164727
.collect::<Vec<_>>();
@@ -4988,10 +4999,10 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
49884999
err.span_suggestion_with_applicability(
49895000
binding.span,
49905001
&rename_msg,
4991-
match (&directive.subclass, snippet.as_ref()) {
4992-
(ImportDirectiveSubclass::SingleImport { .. }, "self") =>
5002+
match directive.subclass {
5003+
ImportDirectiveSubclass::SingleImport { type_ns_only: true, .. } =>
49935004
format!("self as {}", suggested_name),
4994-
(ImportDirectiveSubclass::SingleImport { source, .. }, _) =>
5005+
ImportDirectiveSubclass::SingleImport { source, .. } =>
49955006
format!(
49965007
"{} as {}{}",
49975008
&snippet[..((source.span.hi().0 - binding.span.lo().0) as usize)],
@@ -5002,13 +5013,13 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
50025013
""
50035014
}
50045015
),
5005-
(ImportDirectiveSubclass::ExternCrate { source, target, .. }, _) =>
5016+
ImportDirectiveSubclass::ExternCrate { source, target, .. } =>
50065017
format!(
50075018
"extern crate {} as {};",
50085019
source.unwrap_or(target.name),
50095020
suggested_name,
50105021
),
5011-
(_, _) => unreachable!(),
5022+
_ => unreachable!(),
50125023
},
50135024
Applicability::MaybeIncorrect,
50145025
);

src/librustc_resolve/resolve_imports.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
864864
}
865865
PathResult::NonModule(path_res) if path_res.base_def() == Def::Err => {
866866
// The error was already reported earlier.
867-
assert!(directive.imported_module.get().is_none());
867+
assert!(!self.ambiguity_errors.is_empty() ||
868+
directive.imported_module.get().is_none());
868869
return None;
869870
}
870871
PathResult::Indeterminate | PathResult::NonModule(..) => unreachable!(),

src/test/ui/imports/auxiliary/issue-56125.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub mod issue_56125 {}
2+
13
pub mod last_segment {
24
pub mod issue_56125 {}
35
}

src/test/ui/imports/issue-56125.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
// compile-flags:--extern issue_56125
33
// aux-build:issue-56125.rs
44

5-
use issue_56125::last_segment::*;
6-
//~^ ERROR `issue_56125` is ambiguous
7-
//~| ERROR unresolved import `issue_56125::last_segment`
8-
use issue_56125::non_last_segment::non_last_segment::*;
9-
//~^ ERROR `issue_56125` is ambiguous
10-
//~| ERROR failed to resolve: could not find `non_last_segment` in `issue_56125`
5+
#![feature(uniform_paths)]
6+
7+
mod m1 {
8+
use issue_56125::last_segment::*;
9+
//~^ ERROR `issue_56125` is ambiguous
10+
//~| ERROR unresolved import `issue_56125::last_segment`
11+
}
12+
13+
mod m2 {
14+
use issue_56125::non_last_segment::non_last_segment::*;
15+
//~^ ERROR `issue_56125` is ambiguous
16+
//~| ERROR failed to resolve: could not find `non_last_segment` in `issue_56125`
17+
}
18+
19+
mod m3 {
20+
mod empty {}
21+
use empty::issue_56125; //~ ERROR unresolved import `empty::issue_56125`
22+
use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
23+
}
1124

1225
fn main() {}
+40-19
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,67 @@
11
error[E0433]: failed to resolve: could not find `non_last_segment` in `issue_56125`
2-
--> $DIR/issue-56125.rs:8:18
2+
--> $DIR/issue-56125.rs:14:22
33
|
4-
LL | use issue_56125::non_last_segment::non_last_segment::*;
5-
| ^^^^^^^^^^^^^^^^ could not find `non_last_segment` in `issue_56125`
4+
LL | use issue_56125::non_last_segment::non_last_segment::*;
5+
| ^^^^^^^^^^^^^^^^ could not find `non_last_segment` in `issue_56125`
66

77
error[E0432]: unresolved import `issue_56125::last_segment`
8-
--> $DIR/issue-56125.rs:5:18
8+
--> $DIR/issue-56125.rs:8:22
99
|
10-
LL | use issue_56125::last_segment::*;
11-
| ^^^^^^^^^^^^ could not find `last_segment` in `issue_56125`
10+
LL | use issue_56125::last_segment::*;
11+
| ^^^^^^^^^^^^ could not find `last_segment` in `issue_56125`
12+
13+
error[E0432]: unresolved import `empty::issue_56125`
14+
--> $DIR/issue-56125.rs:21:9
15+
|
16+
LL | use empty::issue_56125; //~ ERROR unresolved import `empty::issue_56125`
17+
| ^^^^^^^^^^^^^^^^^^ no `issue_56125` in `m3::empty`
1218

1319
error[E0659]: `issue_56125` is ambiguous (name vs any other name during import resolution)
14-
--> $DIR/issue-56125.rs:5:5
20+
--> $DIR/issue-56125.rs:8:9
1521
|
16-
LL | use issue_56125::last_segment::*;
17-
| ^^^^^^^^^^^ ambiguous name
22+
LL | use issue_56125::last_segment::*;
23+
| ^^^^^^^^^^^ ambiguous name
1824
|
1925
= note: `issue_56125` could refer to an extern crate passed with `--extern`
2026
= help: use `::issue_56125` to refer to this extern crate unambiguously
2127
note: `issue_56125` could also refer to the module imported here
22-
--> $DIR/issue-56125.rs:5:5
28+
--> $DIR/issue-56125.rs:8:9
2329
|
24-
LL | use issue_56125::last_segment::*;
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
LL | use issue_56125::last_segment::*;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2632
= help: use `self::issue_56125` to refer to this module unambiguously
2733

2834
error[E0659]: `issue_56125` is ambiguous (name vs any other name during import resolution)
29-
--> $DIR/issue-56125.rs:8:5
35+
--> $DIR/issue-56125.rs:14:9
3036
|
31-
LL | use issue_56125::non_last_segment::non_last_segment::*;
32-
| ^^^^^^^^^^^ ambiguous name
37+
LL | use issue_56125::non_last_segment::non_last_segment::*;
38+
| ^^^^^^^^^^^ ambiguous name
3339
|
3440
= note: `issue_56125` could refer to an extern crate passed with `--extern`
3541
= help: use `::issue_56125` to refer to this extern crate unambiguously
3642
note: `issue_56125` could also refer to the module imported here
37-
--> $DIR/issue-56125.rs:5:5
43+
--> $DIR/issue-56125.rs:14:9
3844
|
39-
LL | use issue_56125::last_segment::*;
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
LL | use issue_56125::non_last_segment::non_last_segment::*;
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4147
= help: use `self::issue_56125` to refer to this module unambiguously
4248

43-
error: aborting due to 4 previous errors
49+
error[E0659]: `issue_56125` is ambiguous (name vs any other name during import resolution)
50+
--> $DIR/issue-56125.rs:22:9
51+
|
52+
LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
53+
| ^^^^^^^^^^^ ambiguous name
54+
|
55+
= note: `issue_56125` could refer to an extern crate passed with `--extern`
56+
= help: use `::issue_56125` to refer to this extern crate unambiguously
57+
note: `issue_56125` could also refer to the unresolved item imported here
58+
--> $DIR/issue-56125.rs:21:9
59+
|
60+
LL | use empty::issue_56125; //~ ERROR unresolved import `empty::issue_56125`
61+
| ^^^^^^^^^^^^^^^^^^
62+
= help: use `self::issue_56125` to refer to this unresolved item unambiguously
63+
64+
error: aborting due to 6 previous errors
4465

4566
Some errors occurred: E0432, E0433, E0659.
4667
For more information about an error, try `rustc --explain E0432`.

src/test/ui/issues/issue-45829/import-self.rs

+3
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ use foo as self;
1919

2020
use foo::self;
2121

22+
use foo::A;
23+
use foo::{self as A};
24+
2225
fn main() {}

src/test/ui/issues/issue-45829/import-self.stderr

+17-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ help: you can use `as` to change the binding name of the import
2525
LL | use foo::{self as other_foo};
2626
| ^^^^^^^^^^^^^^^^^
2727

28-
error: aborting due to 3 previous errors
28+
error[E0252]: the name `A` is defined multiple times
29+
--> $DIR/import-self.rs:23:11
30+
|
31+
LL | use foo::A;
32+
| ------ previous import of the type `A` here
33+
LL | use foo::{self as A};
34+
| ^^^^^^^^^ `A` reimported here
35+
|
36+
= note: `A` must be defined only once in the type namespace of this module
37+
help: you can use `as` to change the binding name of the import
38+
|
39+
LL | use foo::{self as OtherA};
40+
| ^^^^^^^^^^^^^^
41+
42+
error: aborting due to 4 previous errors
2943

30-
Some errors occurred: E0255, E0429.
31-
For more information about an error, try `rustc --explain E0255`.
44+
Some errors occurred: E0252, E0255, E0429.
45+
For more information about an error, try `rustc --explain E0252`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2018
2+
3+
mod m {
4+
pub(in crate) struct S1; // OK
5+
pub(in super) struct S2; // OK
6+
pub(in self) struct S3; // OK
7+
pub(in ::core) struct S4;
8+
//~^ ERROR visibilities can only be restricted to ancestor modules
9+
pub(in a::b) struct S5;
10+
//~^ ERROR relative paths are not supported in visibilities on 2018 edition
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: visibilities can only be restricted to ancestor modules
2+
--> $DIR/relative-2018.rs:7:12
3+
|
4+
LL | pub(in ::core) struct S4;
5+
| ^^^^^^
6+
7+
error: relative paths are not supported in visibilities on 2018 edition
8+
--> $DIR/relative-2018.rs:9:12
9+
|
10+
LL | pub(in a::b) struct S5;
11+
| ^---
12+
| |
13+
| help: try: `crate::a::b`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)