Skip to content

Commit c02efb8

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 c02efb8

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

+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
@@ -94,8 +94,38 @@ help: use the path separator to refer to an item
9494
LL | <Type! {}>::get;
9595
| ~~~~~~~~~~~~
9696

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

112142
error[E0423]: expected value, found struct `Vec`
113-
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:51:9
143+
--> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:57:9
114144
|
115145
LL | Vec.new
116146
| ^^^
@@ -139,6 +169,66 @@ help: use the path separator to refer to an item
139169
LL | <Type!()>::new(0)
140170
| ~~~~~~~~~~~
141171

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

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

0 commit comments

Comments
 (0)