Skip to content

Commit b8f10df

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 6ac8878 commit b8f10df

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+7-9
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

-1
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

+20
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 struct `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

+42-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0423]: expected value, found struct `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
| ^^^^^^
@@ -10,7 +10,7 @@ LL | let _ = String::new();
1010
| ~~
1111

1212
error[E0423]: expected value, found struct `String`
13-
--> $DIR/issue-22692.rs:6:13
13+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:14:13
1414
|
1515
LL | let _ = String.default;
1616
| ^^^^^^
@@ -21,7 +21,7 @@ LL | let _ = String::default;
2121
| ~~
2222

2323
error[E0423]: expected value, found struct `Vec`
24-
--> $DIR/issue-22692.rs:10:13
24+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:18:13
2525
|
2626
LL | let _ = Vec::<()>.with_capacity(1);
2727
| ^^^^^^^^^
@@ -31,8 +31,41 @@ help: use the path separator to refer to an item
3131
LL | let _ = Vec::<()>::with_capacity(1);
3232
| ~~
3333

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+
3467
error[E0423]: expected value, found struct `std::cell::Cell`
35-
--> $DIR/issue-22692.rs:17:9
68+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
3669
|
3770
LL | ::std::cell::Cell
3871
| ^^^^^^^^^^^^^^^^^
@@ -47,7 +80,7 @@ LL | <Type!()>::get();
4780
| ~~~~~~~~~~~
4881

4982
error[E0423]: expected value, found struct `std::cell::Cell`
50-
--> $DIR/issue-22692.rs:17:9
83+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
5184
|
5285
LL | ::std::cell::Cell
5386
| ^^^^^^^^^^^^^^^^^
@@ -62,7 +95,7 @@ LL | <Type! {}>::get;
6295
| ~~~~~~~~~~~~
6396

6497
error[E0423]: expected value, found struct `Vec`
65-
--> $DIR/issue-22692.rs:26:9
98+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:46:9
6699
|
67100
LL | Vec.new()
68101
| ^^^
@@ -77,7 +110,7 @@ LL | Vec::new()
77110
| ~~
78111

79112
error[E0423]: expected value, found struct `Vec`
80-
--> $DIR/issue-22692.rs:31:9
113+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:51:9
81114
|
82115
LL | Vec.new
83116
| ^^^
@@ -92,7 +125,7 @@ LL | Vec::new
92125
| ~~
93126

94127
error[E0423]: expected value, found struct `std::cell::Cell`
95-
--> $DIR/issue-22692.rs:17:9
128+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
96129
|
97130
LL | ::std::cell::Cell
98131
| ^^^^^^^^^^^^^^^^^
@@ -106,6 +139,6 @@ help: use the path separator to refer to an item
106139
LL | <Type!()>::new(0)
107140
| ~~~~~~~~~~~
108141

109-
error: aborting due to 8 previous errors
142+
error: aborting due to 11 previous errors
110143

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

0 commit comments

Comments
 (0)