Skip to content

Commit cf3e983

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 cb3ee99 commit cf3e983

File tree

3 files changed

+137
-4
lines changed

3 files changed

+137
-4
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

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

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

+43
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,32 @@ 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:ident) => {
73+
$Ty.foo
74+
//~^ ERROR expected value, found type alias `Alias`
75+
//~| HELP use the path separator
76+
};
77+
}
78+
macro_rules! check_ident {
79+
($Ident:ident) => {
80+
Alias.$Ident
81+
//~^ ERROR expected value, found type alias `Alias`
82+
//~| HELP use the path separator
83+
};
84+
}
85+
macro_rules! check_ty_ident {
86+
($Ty:ident, $Ident:ident) => {
87+
$Ty.$Ident
88+
//~^ ERROR expected value, found type alias `Alias`
89+
//~| HELP use the path separator
90+
};
5991
}
6092

6193
fn interaction_with_macros() {
@@ -70,11 +102,22 @@ fn interaction_with_macros() {
70102
Type! {}.get;
71103
//~^ HELP use the path separator
72104

105+
Type!(alias).get();
106+
//~^ HELP use the path separator
107+
108+
Type! {alias}.get;
109+
//~^ HELP use the path separator
110+
73111
//
74112
// Ensure that the suggestion is shown for expressions inside of macro definitions.
75113
//
76114

77115
let _ = create!(type method);
78116
let _ = create!(type field);
79117
let _ = create!(macro method);
118+
let _ = create!(macro method alias);
119+
120+
let _ = check_ty!(Alias);
121+
let _ = check_ident!(foo);
122+
let _ = check_ty_ident!(Alias, foo);
80123
}

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

+93-3
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,38 @@ LL - Type! {}.get;
9999
LL + <Type! {}>::get;
100100
|
101101

102+
error[E0423]: expected value, found type alias `Alias`
103+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:43:9
104+
|
105+
LL | Alias
106+
| ^^^^^
107+
...
108+
LL | Type!(alias).get();
109+
| ------------ in this macro invocation
110+
|
111+
= note: this error originates in the macro `Type` (in Nightly builds, run with -Z macro-backtrace for more info)
112+
help: use the path separator to refer to an item
113+
|
114+
LL | <Type!(alias)>::get();
115+
| ~~~~~~~~~~~~~~~~
116+
117+
error[E0423]: expected value, found type alias `Alias`
118+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:43:9
119+
|
120+
LL | Alias
121+
| ^^^^^
122+
...
123+
LL | Type! {alias}.get;
124+
| ------------- in this macro invocation
125+
|
126+
= note: this error originates in the macro `Type` (in Nightly builds, run with -Z macro-backtrace for more info)
127+
help: use the path separator to refer to an item
128+
|
129+
LL | <Type! {alias}>::get;
130+
| ~~~~~~~~~~~~~~~~~
131+
102132
error[E0423]: expected value, found struct `Vec`
103-
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:46:9
133+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:52:9
104134
|
105135
LL | Vec.new()
106136
| ^^^
@@ -116,7 +146,7 @@ LL + Vec::new()
116146
|
117147

118148
error[E0423]: expected value, found struct `Vec`
119-
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:51:9
149+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:57:9
120150
|
121151
LL | Vec.new
122152
| ^^^
@@ -147,6 +177,66 @@ LL - Type!().new(0)
147177
LL + <Type!()>::new(0)
148178
|
149179

150-
error: aborting due to 11 previous errors
180+
error[E0423]: expected value, found type alias `Alias`
181+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:43:9
182+
|
183+
LL | Alias
184+
| ^^^^^
185+
...
186+
LL | let _ = create!(macro method alias);
187+
| --------------------------- in this macro invocation
188+
|
189+
= 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)
190+
help: use the path separator to refer to an item
191+
|
192+
LL | <Type!(alias)>::new(0)
193+
| ~~~~~~~~~~~~~~~~
194+
195+
error[E0423]: expected value, found type alias `Alias`
196+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:73:9
197+
|
198+
LL | $Ty.foo
199+
| ^^^
200+
...
201+
LL | let _ = check_ty!(Alias);
202+
| ---------------- in this macro invocation
203+
|
204+
= note: this error originates in the macro `check_ty` (in Nightly builds, run with -Z macro-backtrace for more info)
205+
help: use the path separator to refer to an item
206+
|
207+
LL | $Ty::foo
208+
| ~~
209+
210+
error[E0423]: expected value, found type alias `Alias`
211+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:80:9
212+
|
213+
LL | Alias.$Ident
214+
| ^^^^^
215+
...
216+
LL | let _ = check_ident!(foo);
217+
| ----------------- in this macro invocation
218+
|
219+
= note: this error originates in the macro `check_ident` (in Nightly builds, run with -Z macro-backtrace for more info)
220+
help: use the path separator to refer to an item
221+
|
222+
LL | <Alias>::$Ident
223+
| ~~~~~~~~~
224+
225+
error[E0423]: expected value, found type alias `Alias`
226+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:87:9
227+
|
228+
LL | $Ty.$Ident
229+
| ^^^
230+
...
231+
LL | let _ = check_ty_ident!(Alias, foo);
232+
| --------------------------- in this macro invocation
233+
|
234+
= note: this error originates in the macro `check_ty_ident` (in Nightly builds, run with -Z macro-backtrace for more info)
235+
help: use the path separator to refer to an item
236+
|
237+
LL | <$Ty>::$Ident
238+
| ~~~~~~~
239+
240+
error: aborting due to 17 previous errors
151241

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

0 commit comments

Comments
 (0)