Skip to content

Commit 5639e52

Browse files
committed
move suggestions to its own method
1 parent 9db03b9 commit 5639e52

5 files changed

+107
-29
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
250250
let code = source.error_code(res.is_some());
251251
let mut err =
252252
self.r.session.struct_span_err_with_code(base_error.span, &base_error.msg, code);
253-
if let Some((trait_ref, self_ty)) =
254-
self.diagnostic_metadata.currently_processing_impl_trait.clone()
255-
&& let TyKind::Path(_, self_ty_path) = &self_ty.kind
256-
&& let PathResult::Module(ModuleOrUniformRoot::Module(module)) = self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None)
257-
&& let ModuleKind::Def(DefKind::Trait, ..) = module.kind
258-
&& trait_ref.path.span == span
259-
&& let PathSource::Trait(_) = source
260-
&& let Some(Res::Def(DefKind::Struct, _)) = res
261-
&& let Ok(self_ty_str) =
262-
self.r.session.source_map().span_to_snippet(self_ty.span)
263-
&& let Ok(trait_ref_str) =
264-
self.r.session.source_map().span_to_snippet(trait_ref.path.span)
265-
{
266-
err.multipart_suggestion(
267-
"consider swapping the struct and the trait",
268-
vec![(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)],
269-
Applicability::MaybeIncorrect,
270-
);
271-
}
253+
254+
self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);
272255

273256
if let Some(sugg) = base_error.suggestion {
274257
err.span_suggestion_verbose(sugg.0, sugg.1, sugg.2, Applicability::MaybeIncorrect);
@@ -704,6 +687,35 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
704687
}
705688
}
706689

690+
fn suggest_swapping_misplaced_self_ty_and_trait(
691+
&mut self,
692+
err: &mut Diagnostic,
693+
source: PathSource<'_>,
694+
res: Option<Res>,
695+
span: Span,
696+
) {
697+
if let Some((trait_ref, self_ty)) =
698+
self.diagnostic_metadata.currently_processing_impl_trait.clone()
699+
&& let TyKind::Path(_, self_ty_path) = &self_ty.kind
700+
&& let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
701+
self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None)
702+
&& let ModuleKind::Def(DefKind::Trait, ..) = module.kind
703+
&& trait_ref.path.span == span
704+
&& let PathSource::Trait(_) = source
705+
&& let Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) = res
706+
&& let Ok(self_ty_str) =
707+
self.r.session.source_map().span_to_snippet(self_ty.span)
708+
&& let Ok(trait_ref_str) =
709+
self.r.session.source_map().span_to_snippet(trait_ref.path.span)
710+
{
711+
err.multipart_suggestion(
712+
"`impl` items mention the trait being implemented first and the type it is being implemented for second",
713+
vec![(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)],
714+
Applicability::MaybeIncorrect,
715+
);
716+
}
717+
}
718+
707719
fn get_single_associated_item(
708720
&mut self,
709721
path: &[Segment],

src/test/ui/suggestions/suggest-swapping-self-ty-and-trait-edition-2021.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22

33
pub trait Trait<'a, T> {}
44

5-
pub struct Struct<T> {}
5+
pub struct Struct<T>;
6+
pub enum Enum<T> {}
7+
8+
pub union Union<T> {
9+
f1: usize,
10+
}
611

712
impl<'a, T> Struct<T> for Trait<'a, T> {}
813
//~^ ERROR expected trait, found struct `Struct`
914
//~| ERROR trait objects must include the `dyn` keyword
1015

16+
impl<'a, T> Enum<T> for Trait<'a, T> {}
17+
//~^ ERROR expected trait, found enum `Enum`
18+
19+
impl<'a, T> Union<T> for Trait<'a, T> {}
20+
//~^ ERROR expected trait, found union `Union`
21+
1122
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
error[E0404]: expected trait, found struct `Struct`
2-
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:7:13
2+
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:12:13
33
|
44
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
55
| ^^^^^^^^^ not a trait
66
|
7-
help: consider swapping the struct and the trait
7+
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
88
|
99
LL | impl<'a, T> Trait<'a, T> for Struct<T> {}
1010
| ~~~~~~~~~~~~ ~~~~~~~~~
1111

12+
error[E0404]: expected trait, found enum `Enum`
13+
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:16:13
14+
|
15+
LL | impl<'a, T> Enum<T> for Trait<'a, T> {}
16+
| ^^^^^^^ not a trait
17+
|
18+
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
19+
|
20+
LL | impl<'a, T> Trait<'a, T> for Enum<T> {}
21+
| ~~~~~~~~~~~~ ~~~~~~~
22+
23+
error[E0404]: expected trait, found union `Union`
24+
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:19:13
25+
|
26+
LL | impl<'a, T> Union<T> for Trait<'a, T> {}
27+
| ^^^^^^^^ not a trait
28+
|
29+
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
30+
|
31+
LL | impl<'a, T> Trait<'a, T> for Union<T> {}
32+
| ~~~~~~~~~~~~ ~~~~~~~~
33+
1234
error[E0782]: trait objects must include the `dyn` keyword
13-
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:7:27
35+
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:12:27
1436
|
1537
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
1638
| ^^^^^^^^^^^^
@@ -21,7 +43,7 @@ LL - impl<'a, T> Struct<T> for Trait<'a, T> {}
2143
LL + impl<'a, T> Struct<T> for dyn Trait<'a, T> {}
2244
|
2345

24-
error: aborting due to 2 previous errors
46+
error: aborting due to 4 previous errors
2547

2648
Some errors have detailed explanations: E0404, E0782.
2749
For more information about an error, try `rustc --explain E0404`.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
pub trait Trait<'a, T> {}
22

3-
pub struct Struct<T> {}
3+
pub struct Struct<T>;
4+
pub enum Enum<T> {}
5+
6+
pub union Union<T> {
7+
f1: usize,
8+
}
49

510
impl<'a, T> Struct<T> for Trait<'a, T> {}
611
//~^ ERROR expected trait, found struct `Struct`
712
//~| WARNING trait objects without an explicit `dyn` are deprecated
813
//~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
914

15+
impl<'a, T> Enum<T> for Trait<'a, T> {}
16+
//~^ ERROR expected trait, found enum `Enum`
17+
18+
impl<'a, T> Union<T> for Trait<'a, T> {}
19+
//~^ ERROR expected trait, found union `Union`
20+
1021
fn main() {}

src/test/ui/suggestions/suggest-swapping-self-ty-and-trait.stderr

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
error[E0404]: expected trait, found struct `Struct`
2-
--> $DIR/suggest-swapping-self-ty-and-trait.rs:5:13
2+
--> $DIR/suggest-swapping-self-ty-and-trait.rs:10:13
33
|
44
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
55
| ^^^^^^^^^ not a trait
66
|
7-
help: consider swapping the struct and the trait
7+
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
88
|
99
LL | impl<'a, T> Trait<'a, T> for Struct<T> {}
1010
| ~~~~~~~~~~~~ ~~~~~~~~~
1111

12+
error[E0404]: expected trait, found enum `Enum`
13+
--> $DIR/suggest-swapping-self-ty-and-trait.rs:15:13
14+
|
15+
LL | impl<'a, T> Enum<T> for Trait<'a, T> {}
16+
| ^^^^^^^ not a trait
17+
|
18+
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
19+
|
20+
LL | impl<'a, T> Trait<'a, T> for Enum<T> {}
21+
| ~~~~~~~~~~~~ ~~~~~~~
22+
23+
error[E0404]: expected trait, found union `Union`
24+
--> $DIR/suggest-swapping-self-ty-and-trait.rs:18:13
25+
|
26+
LL | impl<'a, T> Union<T> for Trait<'a, T> {}
27+
| ^^^^^^^^ not a trait
28+
|
29+
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
30+
|
31+
LL | impl<'a, T> Trait<'a, T> for Union<T> {}
32+
| ~~~~~~~~~~~~ ~~~~~~~~
33+
1234
warning: trait objects without an explicit `dyn` are deprecated
13-
--> $DIR/suggest-swapping-self-ty-and-trait.rs:5:27
35+
--> $DIR/suggest-swapping-self-ty-and-trait.rs:10:27
1436
|
1537
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
1638
| ^^^^^^^^^^^^
@@ -24,6 +46,6 @@ LL - impl<'a, T> Struct<T> for Trait<'a, T> {}
2446
LL + impl<'a, T> Struct<T> for dyn Trait<'a, T> {}
2547
|
2648

27-
error: aborting due to previous error; 1 warning emitted
49+
error: aborting due to 3 previous errors; 1 warning emitted
2850

2951
For more information about this error, try `rustc --explain E0404`.

0 commit comments

Comments
 (0)