Skip to content

Commit ce16958

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 8c61cd4 commit ce16958

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
@@ -1566,7 +1566,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15661566
}
15671567
};
15681568

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

15721572
match source {
@@ -1740,12 +1740,10 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
17401740
}
17411741
}
17421742
(
1743-
Res::Def(kind @ (DefKind::Mod | DefKind::Trait), _),
1743+
Res::Def(kind @ (DefKind::Mod | DefKind::Trait | DefKind::TyAlias), _),
17441744
PathSource::Expr(Some(parent)),
1745-
) => {
1746-
if !path_sep(self, err, parent, kind) {
1747-
return false;
1748-
}
1745+
) if path_sep(self, err, parent, kind) => {
1746+
return true;
17491747
}
17501748
(
17511749
Res::Def(DefKind::Enum, def_id),
@@ -1777,13 +1775,13 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
17771775
let (ctor_def, ctor_vis, fields) = if let Some(struct_ctor) = struct_ctor {
17781776
if let PathSource::Expr(Some(parent)) = source {
17791777
if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
1780-
bad_struct_syntax_suggestion(self, def_id);
1778+
bad_struct_syntax_suggestion(self, err, def_id);
17811779
return true;
17821780
}
17831781
}
17841782
struct_ctor
17851783
} else {
1786-
bad_struct_syntax_suggestion(self, def_id);
1784+
bad_struct_syntax_suggestion(self, err, def_id);
17871785
return true;
17881786
};
17891787

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

src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -3619,7 +3619,6 @@ ui/resolve/issue-21221-1.rs
36193619
ui/resolve/issue-21221-2.rs
36203620
ui/resolve/issue-21221-3.rs
36213621
ui/resolve/issue-21221-4.rs
3622-
ui/resolve/issue-22692.rs
36233622
ui/resolve/issue-2330.rs
36243623
ui/resolve/issue-23305.rs
36253624
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
| ^^^^^^
@@ -11,7 +11,7 @@ LL + let _ = String::new();
1111
|
1212

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

2525
error[E0423]: expected value, found struct `Vec`
26-
--> $DIR/issue-22692.rs:10:13
26+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:18:13
2727
|
2828
LL | let _ = Vec::<()>.with_capacity(1);
2929
| ^^^^^^^^^
@@ -34,8 +34,41 @@ LL - let _ = Vec::<()>.with_capacity(1);
3434
LL + let _ = Vec::<()>::with_capacity(1);
3535
|
3636

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

5386
error[E0423]: expected value, found struct `std::cell::Cell`
54-
--> $DIR/issue-22692.rs:17:9
87+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
5588
|
5689
LL | ::std::cell::Cell
5790
| ^^^^^^^^^^^^^^^^^
@@ -67,7 +100,7 @@ LL + <Type! {}>::get;
67100
|
68101

69102
error[E0423]: expected value, found struct `Vec`
70-
--> $DIR/issue-22692.rs:26:9
103+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:46:9
71104
|
72105
LL | Vec.new()
73106
| ^^^
@@ -83,7 +116,7 @@ LL + Vec::new()
83116
|
84117

85118
error[E0423]: expected value, found struct `Vec`
86-
--> $DIR/issue-22692.rs:31:9
119+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:51:9
87120
|
88121
LL | Vec.new
89122
| ^^^
@@ -99,7 +132,7 @@ LL + Vec::new
99132
|
100133

101134
error[E0423]: expected value, found struct `std::cell::Cell`
102-
--> $DIR/issue-22692.rs:17:9
135+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9
103136
|
104137
LL | ::std::cell::Cell
105138
| ^^^^^^^^^^^^^^^^^
@@ -114,6 +147,6 @@ LL - Type!().new(0)
114147
LL + <Type!()>::new(0)
115148
|
116149

117-
error: aborting due to 8 previous errors
150+
error: aborting due to 11 previous errors
118151

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

0 commit comments

Comments
 (0)