Skip to content

Commit f2f20f9

Browse files
committed
When giving a suggestion to use :: instead of . where the rhs is a macro giving a type,
make it also work when the rhs is a type alias, not just a struct.
1 parent 9c58163 commit f2f20f9

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15261526
Applicability::MaybeIncorrect,
15271527
);
15281528
true
1529-
} else if kind == DefKind::Struct
1529+
} else if matches!(kind, DefKind::Struct | DefKind::TyAlias)
15301530
&& let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
15311531
&& let Ok(snippet) = this.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
15321532
{

tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.rs

+45
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ macro_rules! Type {
3939
//~| ERROR expected value, found struct `std::cell::Cell`
4040
//~| ERROR expected value, found struct `std::cell::Cell`
4141
};
42+
(alias) => {
43+
Alias
44+
//~^ ERROR expected value, found type alias `Alias`
45+
//~| ERROR expected value, found type alias `Alias`
46+
//~| ERROR expected value, found type alias `Alias`
47+
};
4248
}
4349

4450
macro_rules! create {
@@ -56,6 +62,34 @@ macro_rules! create {
5662
Type!().new(0)
5763
//~^ HELP use the path separator
5864
};
65+
(macro method alias) => {
66+
Type!(alias).new(0)
67+
//~^ HELP use the path separator
68+
};
69+
}
70+
71+
macro_rules! check_ty {
72+
($Ty:ty) => {
73+
// This one doesn't give the <$Ty>:: suggestion even for structs,
74+
// because it errors in the parser before name resolution.
75+
$Ty.foo
76+
//~^ ERROR expected expression, found type `Alias`
77+
};
78+
}
79+
macro_rules! check_ident {
80+
($Ident:ident) => {
81+
Alias.$Ident
82+
//~^ ERROR expected value, found type alias `Alias`
83+
//~| HELP use the path separator
84+
};
85+
}
86+
macro_rules! check_ty_ident {
87+
($Ty:ty, $Ident:ident) => {
88+
// This one doesn't give the <$Ty>:: suggestion even for structs,
89+
// because it errors in the parser before name resolution.
90+
$Ty.$Ident
91+
//~^ ERROR expected expression, found type `Alias`
92+
};
5993
}
6094

6195
fn interaction_with_macros() {
@@ -70,11 +104,22 @@ fn interaction_with_macros() {
70104
Type! {}.get;
71105
//~^ HELP use the path separator
72106

107+
Type!(alias).get();
108+
//~^ HELP use the path separator
109+
110+
Type! {alias}.get;
111+
//~^ HELP use the path separator
112+
73113
//
74114
// Ensure that the suggestion is shown for expressions inside of macro definitions.
75115
//
76116

77117
let _ = create!(type method);
78118
let _ = create!(type field);
79119
let _ = create!(macro method);
120+
let _ = create!(macro method alias);
121+
122+
let _ = check_ty!(Alias);
123+
let _ = check_ident!(foo);
124+
let _ = check_ty_ident!(Alias, foo);
80125
}

tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.stderr

+85-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
error: expected expression, found type `Alias`
2+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:75:9
3+
|
4+
LL | $Ty.foo
5+
| ^^^ expected expression
6+
...
7+
LL | let _ = check_ty!(Alias);
8+
| ---------------- in this macro invocation
9+
|
10+
= note: this error originates in the macro `check_ty` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: expected expression, found type `Alias`
13+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:90:9
14+
|
15+
LL | $Ty.$Ident
16+
| ^^^ expected expression
17+
...
18+
LL | let _ = check_ty_ident!(Alias, foo);
19+
| --------------------------- in this macro invocation
20+
|
21+
= note: this error originates in the macro `check_ty_ident` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
123
error[E0423]: expected value, found struct `String`
224
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:10:13
325
|
@@ -94,8 +116,38 @@ help: use the path separator to refer to an item
94116
LL | <Type! {}>::get;
95117
| ~~~~~~~~~~~~
96118

119+
error[E0423]: expected value, found type alias `Alias`
120+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:43:9
121+
|
122+
LL | Alias
123+
| ^^^^^
124+
...
125+
LL | Type!(alias).get();
126+
| ------------ in this macro invocation
127+
|
128+
= note: this error originates in the macro `Type` (in Nightly builds, run with -Z macro-backtrace for more info)
129+
help: use the path separator to refer to an item
130+
|
131+
LL | <Type!(alias)>::get();
132+
| ~~~~~~~~~~~~~~~~
133+
134+
error[E0423]: expected value, found type alias `Alias`
135+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:43:9
136+
|
137+
LL | Alias
138+
| ^^^^^
139+
...
140+
LL | Type! {alias}.get;
141+
| ------------- in this macro invocation
142+
|
143+
= note: this error originates in the macro `Type` (in Nightly builds, run with -Z macro-backtrace for more info)
144+
help: use the path separator to refer to an item
145+
|
146+
LL | <Type! {alias}>::get;
147+
| ~~~~~~~~~~~~~~~~~
148+
97149
error[E0423]: expected value, found struct `Vec`
98-
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:46:9
150+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:52:9
99151
|
100152
LL | Vec.new()
101153
| ^^^
@@ -110,7 +162,7 @@ LL | Vec::new()
110162
| ~~
111163

112164
error[E0423]: expected value, found struct `Vec`
113-
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:51:9
165+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:57:9
114166
|
115167
LL | Vec.new
116168
| ^^^
@@ -139,6 +191,36 @@ help: use the path separator to refer to an item
139191
LL | <Type!()>::new(0)
140192
| ~~~~~~~~~~~
141193

142-
error: aborting due to 11 previous errors
194+
error[E0423]: expected value, found type alias `Alias`
195+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:43:9
196+
|
197+
LL | Alias
198+
| ^^^^^
199+
...
200+
LL | let _ = create!(macro method alias);
201+
| --------------------------- in this macro invocation
202+
|
203+
= note: this error originates in the macro `Type` which comes from the expansion of the macro `create` (in Nightly builds, run with -Z macro-backtrace for more info)
204+
help: use the path separator to refer to an item
205+
|
206+
LL | <Type!(alias)>::new(0)
207+
| ~~~~~~~~~~~~~~~~
208+
209+
error[E0423]: expected value, found type alias `Alias`
210+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:81:9
211+
|
212+
LL | Alias.$Ident
213+
| ^^^^^
214+
...
215+
LL | let _ = check_ident!(foo);
216+
| ----------------- in this macro invocation
217+
|
218+
= note: this error originates in the macro `check_ident` (in Nightly builds, run with -Z macro-backtrace for more info)
219+
help: use the path separator to refer to an item
220+
|
221+
LL | <Alias>::$Ident
222+
| ~~~~~~~~~
223+
224+
error: aborting due to 17 previous errors
143225

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

0 commit comments

Comments
 (0)