Skip to content

Commit e40bb15

Browse files
committed
Suggest using :: instead of . in more cases.
When `Foo.field` or `Foo.method()` exprs are encountered, suggest `Foo::field` or `Foo::method()` when Foo is a type alias, not just a struct, trait, or module. Also rename test for this suggestion from issue-22692.rs to something more meaningful.
1 parent 35fbbca commit e40bb15

File tree

4 files changed

+77
-21
lines changed

4 files changed

+77
-21
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15631563
}
15641564
};
15651565

1566-
let mut bad_struct_syntax_suggestion = |this: &mut Self, def_id: DefId| {
1566+
let bad_struct_syntax_suggestion = |this: &mut Self, err: &mut Diag<'_>, def_id: DefId| {
15671567
let (followed_by_brace, closing_brace) = this.followed_by_brace(span);
15681568

15691569
match source {
@@ -1737,12 +1737,10 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
17371737
}
17381738
}
17391739
(
1740-
Res::Def(kind @ (DefKind::Mod | DefKind::Trait), _),
1740+
Res::Def(kind @ (DefKind::Mod | DefKind::Trait | DefKind::TyAlias), _),
17411741
PathSource::Expr(Some(parent)),
1742-
) => {
1743-
if !path_sep(self, err, parent, kind) {
1744-
return false;
1745-
}
1742+
) if path_sep(self, err, parent, kind) => {
1743+
return true;
17461744
}
17471745
(
17481746
Res::Def(DefKind::Enum, def_id),
@@ -1774,13 +1772,13 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
17741772
let (ctor_def, ctor_vis, fields) = if let Some(struct_ctor) = struct_ctor {
17751773
if let PathSource::Expr(Some(parent)) = source {
17761774
if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
1777-
bad_struct_syntax_suggestion(self, def_id);
1775+
bad_struct_syntax_suggestion(self, err, def_id);
17781776
return true;
17791777
}
17801778
}
17811779
struct_ctor
17821780
} else {
1783-
bad_struct_syntax_suggestion(self, def_id);
1781+
bad_struct_syntax_suggestion(self, err, def_id);
17841782
return true;
17851783
};
17861784

@@ -1858,7 +1856,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
18581856
err.span_label(span, "constructor is not visible here due to private fields");
18591857
}
18601858
(Res::Def(DefKind::Union | DefKind::Variant, def_id), _) if ns == ValueNS => {
1861-
bad_struct_syntax_suggestion(self, def_id);
1859+
bad_struct_syntax_suggestion(self, err, def_id);
18621860
}
18631861
(Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id), _) if ns == ValueNS => {
18641862
match source {

src/tools/tidy/src/issues.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3635,7 +3635,6 @@ ui/resolve/issue-21221-1.rs
36353635
ui/resolve/issue-21221-2.rs
36363636
ui/resolve/issue-21221-3.rs
36373637
ui/resolve/issue-21221-4.rs
3638-
ui/resolve/issue-22692.rs
36393638
ui/resolve/issue-2330.rs
36403639
ui/resolve/issue-23305.rs
36413640
ui/resolve/issue-2356.rs

tests/ui/resolve/issue-22692.rs renamed to tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// see also https://github.com/rust-lang/rust/issues/22692
2+
3+
type Alias = Vec<u32>;
4+
5+
mod foo {
6+
fn bar() {}
7+
}
8+
19
fn main() {
210
let _ = String.new();
311
//~^ ERROR expected value, found type alias `String`
@@ -10,6 +18,18 @@ fn main() {
1018
let _ = Vec::<()>.with_capacity(1);
1119
//~^ ERROR expected value, found struct `Vec`
1220
//~| HELP use the path separator
21+
22+
let _ = Alias.new();
23+
//~^ ERROR expected value, found type alias `Alias`
24+
//~| HELP use the path separator
25+
26+
let _ = Alias.default;
27+
//~^ ERROR expected value, found type alias `Alias`
28+
//~| HELP use the path separator
29+
30+
let _ = foo.bar;
31+
//~^ ERROR expected value, found module `foo`
32+
//~| HELP use the path separator
1333
}
1434

1535
macro_rules! Type {

tests/ui/resolve/issue-22692.stderr renamed to tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.stderr

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
error[E0423]: expected value, found type alias `String`
2-
--> $DIR/issue-22692.rs:2:13
2+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:10:13
33
|
44
LL | let _ = String.new();
55
| ^^^^^^
66
|
7-
= note: can't use a type alias as a constructor
7+
help: use the path separator to refer to an item
8+
|
9+
LL | let _ = String::new();
10+
| ~~
811

912
error[E0423]: expected value, found type alias `String`
10-
--> $DIR/issue-22692.rs:6:13
13+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:14:13
1114
|
1215
LL | let _ = String.default;
1316
| ^^^^^^
1417
|
15-
= note: can't use a type alias as a constructor
18+
help: use the path separator to refer to an item
19+
|
20+
LL | let _ = String::default;
21+
| ~~
1622

1723
error[E0423]: expected value, found struct `Vec`
18-
--> $DIR/issue-22692.rs:10:13
24+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:18:13
1925
|
2026
LL | let _ = Vec::<()>.with_capacity(1);
2127
| ^^^^^^^^^
@@ -25,8 +31,41 @@ help: use the path separator to refer to an item
2531
LL | let _ = Vec::<()>::with_capacity(1);
2632
| ~~
2733

34+
error[E0423]: expected value, found type alias `Alias`
35+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:22:13
36+
|
37+
LL | let _ = Alias.new();
38+
| ^^^^^
39+
|
40+
help: use the path separator to refer to an item
41+
|
42+
LL | let _ = Alias::new();
43+
| ~~
44+
45+
error[E0423]: expected value, found type alias `Alias`
46+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:26:13
47+
|
48+
LL | let _ = Alias.default;
49+
| ^^^^^
50+
|
51+
help: use the path separator to refer to an item
52+
|
53+
LL | let _ = Alias::default;
54+
| ~~
55+
56+
error[E0423]: expected value, found module `foo`
57+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:30:13
58+
|
59+
LL | let _ = foo.bar;
60+
| ^^^
61+
|
62+
help: use the path separator to refer to an item
63+
|
64+
LL | let _ = foo::bar;
65+
| ~~
66+
2867
error[E0423]: expected value, found struct `std::cell::Cell`
29-
--> $DIR/issue-22692.rs:17:9
68+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
3069
|
3170
LL | ::std::cell::Cell
3271
| ^^^^^^^^^^^^^^^^^
@@ -41,7 +80,7 @@ LL | <Type!()>::get();
4180
| ~~~~~~~~~~~
4281

4382
error[E0423]: expected value, found struct `std::cell::Cell`
44-
--> $DIR/issue-22692.rs:17:9
83+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
4584
|
4685
LL | ::std::cell::Cell
4786
| ^^^^^^^^^^^^^^^^^
@@ -56,7 +95,7 @@ LL | <Type! {}>::get;
5695
| ~~~~~~~~~~~~
5796

5897
error[E0423]: expected value, found struct `Vec`
59-
--> $DIR/issue-22692.rs:26:9
98+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:46:9
6099
|
61100
LL | Vec.new()
62101
| ^^^
@@ -71,7 +110,7 @@ LL | Vec::new()
71110
| ~~
72111

73112
error[E0423]: expected value, found struct `Vec`
74-
--> $DIR/issue-22692.rs:31:9
113+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:51:9
75114
|
76115
LL | Vec.new
77116
| ^^^
@@ -86,7 +125,7 @@ LL | Vec::new
86125
| ~~
87126

88127
error[E0423]: expected value, found struct `std::cell::Cell`
89-
--> $DIR/issue-22692.rs:17:9
128+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
90129
|
91130
LL | ::std::cell::Cell
92131
| ^^^^^^^^^^^^^^^^^
@@ -100,6 +139,6 @@ help: use the path separator to refer to an item
100139
LL | <Type!()>::new(0)
101140
| ~~~~~~~~~~~
102141

103-
error: aborting due to 8 previous errors
142+
error: aborting due to 11 previous errors
104143

105144
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)