Skip to content

Commit da2a8be

Browse files
committed
Don't suggest changing ret ty for trait methods
1 parent d8fb04e commit da2a8be

13 files changed

+19
-102
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -1178,41 +1178,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11781178
expr: &hir::Expr<'_>,
11791179
expected: Ty<'_>,
11801180
) {
1181-
let (def_id, ident, callee_str) = if let hir::ExprKind::Call(fun, _) = expr.kind
1181+
let (def_id, ident) = if let hir::ExprKind::Call(fun, _) = expr.kind
11821182
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = fun.kind
1183-
&& let hir::def::Res::Def(def_kind, def_id) = path.res
1184-
&& !matches!(def_kind, hir::def::DefKind::Ctor(..))
1183+
&& let hir::def::Res::Def(_, def_id) = path.res
11851184
{
1186-
(def_id, path.segments[0].ident, "function")
1185+
(def_id, path.segments[0].ident)
11871186
} else if let hir::ExprKind::MethodCall(method, ..) = expr.kind
11881187
&& let Some(def_id) = self.typeck_results.borrow().type_dependent_def_id(expr.hir_id)
1189-
&& !matches!(self.tcx.def_kind(def_id), hir::def::DefKind::Ctor(..))
11901188
{
1191-
(def_id, method.ident, "method")
1189+
(def_id, method.ident)
11921190
} else {
11931191
return;
11941192
};
1193+
if !matches!(self.tcx.def_kind(def_id), hir::def::DefKind::AssocFn | hir::def::DefKind::Fn)
1194+
{
1195+
return;
1196+
}
11951197
err.span_note(
11961198
self.tcx.def_span(def_id),
1197-
format!("the {callee_str} {ident} is defined here"),
1199+
format!("the {} {ident} is defined here", self.tcx.def_descr(def_id)),
11981200
);
11991201

12001202
if let Some(local_did) = def_id.as_local()
12011203
&& let Some(node) = self.tcx.opt_hir_node(self.tcx.local_def_id_to_hir_id(local_did))
1202-
&& let hir::Node::TraitItem(hir::TraitItem {
1203-
kind: hir::TraitItemKind::Fn(sig, ..),
1204-
..
1205-
})
1206-
| hir::Node::ImplItem(hir::ImplItem {
1207-
kind: hir::ImplItemKind::Fn(sig, ..), ..
1208-
})
1209-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. }) = node
1204+
&& !matches!(node, hir::Node::TraitItem(..))
1205+
&& let Some(sig) = node.fn_sig()
12101206
&& let ret_span = sig.decl.output.span()
12111207
&& !ret_span.from_expansion()
12121208
&& expected.has_concrete_skeleton()
12131209
{
1214-
let sugg =
1215-
if ret_span.is_empty() { format!("-> {expected}") } else { format!("{expected}") };
1210+
let sugg = match sig.decl.output {
1211+
hir::FnRetTy::DefaultReturn(..) => format!("-> {expected}"),
1212+
hir::FnRetTy::Return(..) => format!("{expected}"),
1213+
};
12161214
err.span_suggestion(
12171215
ret_span,
12181216
format!("consider changing {ident}'s return type"),

tests/ui/associated-type-bounds/do-not-look-at-parent-item-in-suggestion-for-type-param-of-current-assoc-item.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0308]: mismatched types
22
--> $DIR/do-not-look-at-parent-item-in-suggestion-for-type-param-of-current-assoc-item.rs:24:37
33
|
4-
LL | fn identify(&self) -> Self::Id;
5-
| -------- help: consider changing identify's return type: `&I`
6-
...
74
LL | let _low = self.lows.remove(low.identify()).unwrap();
85
| ------ ^^^^^^^^^^^^^^ expected `&I`, found associated type
96
| |

tests/ui/associated-types/associated-types-eq-3.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ help: consider constraining the associated type `<I as Foo>::A` to `Bar`
1717
|
1818
LL | fn foo2<I: Foo<A = Bar>>(x: I) {
1919
| +++++++++
20-
help: consider changing boo's return type
21-
|
22-
LL | fn boo(&self) -> Bar;
23-
| ~~~
2420

2521
error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
2622
--> $DIR/associated-types-eq-3.rs:38:10

tests/ui/impl-trait/equality2.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LL | let _: i32 = Leak::leak(hide(0_i32));
4343
|
4444
= note: expected type `i32`
4545
found associated type `<impl Foo as Leak>::T`
46-
note: the function Leak is defined here
46+
note: the method Leak is defined here
4747
--> $DIR/equality2.rs:13:5
4848
|
4949
LL | fn leak(self) -> Self::T;
@@ -52,10 +52,6 @@ help: consider constraining the associated type `<impl Foo as Leak>::T` to `i32`
5252
|
5353
LL | fn hide<T: Foo>(x: T) -> impl Foo<T = i32> {
5454
| +++++++++
55-
help: consider changing Leak's return type
56-
|
57-
LL | fn leak(self) -> i32;
58-
| ~~~
5955

6056
error[E0308]: mismatched types
6157
--> $DIR/equality2.rs:38:10

tests/ui/impl-trait/in-trait/opaque-in-impl-is-opaque.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0308]: mismatched types
22
--> $DIR/opaque-in-impl-is-opaque.rs:14:19
33
|
4-
LL | fn bar(&self) -> impl Display;
5-
| ------------ help: consider changing bar's return type: `&str`
6-
...
74
LL | fn bar(&self) -> impl Display {
85
| ------------ the found opaque type
96
...

tests/ui/issues/issue-22684.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0308]: mismatched types
22
--> $DIR/issue-22684.rs:17:17
33
|
4-
LL | fn bar(&self) -> bool { true }
5-
| ---- help: consider changing bar's return type: `()`
6-
...
74
LL | let _: () = foo::Foo.bar();
85
| -- ^^^^^^^^^^^^^^ expected `()`, found `bool`
96
| |

tests/ui/methods/method-ambig-one-trait-unknown-int-type.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ help: you can convert an `isize` to a `usize` and panic if the converted value d
4545
|
4646
LL | let y: usize = x.foo().try_into().unwrap();
4747
| ++++++++++++++++++++
48-
help: consider changing foo's return type
49-
|
50-
LL | fn foo(&self) -> usize;
51-
| ~~~~~
5248

5349
error: aborting due to 3 previous errors
5450

tests/ui/specialization/specialization-default-types.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ LL | Box::new(self)
2424
error[E0308]: mismatched types
2525
--> $DIR/specialization-default-types.rs:25:5
2626
|
27-
LL | fn generate(self) -> Self::Output;
28-
| ------------ help: consider changing Example's return type: `Box<T>`
29-
...
3027
LL | fn trouble<T>(t: T) -> Box<T> {
3128
| ------ expected `Box<T>` because of return type
3229
LL | Example::generate(t)
@@ -36,7 +33,7 @@ LL | Example::generate(t)
3633
found associated type `<T as Example>::Output`
3734
= help: consider constraining the associated type `<T as Example>::Output` to `Box<T>`
3835
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
39-
note: the function Example is defined here
36+
note: the method Example is defined here
4037
--> $DIR/specialization-default-types.rs:9:5
4138
|
4239
LL | fn generate(self) -> Self::Output;

tests/ui/suggestions/box-future-wrong-output.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0308]: mismatched types
22
--> $DIR/box-future-wrong-output.rs:20:39
33
|
4-
LL | fn boxed<'a>(self) -> BoxFuture<'a, Self::Output>
5-
| --------------------------- help: consider changing boxed's return type: `Pin<Box<(dyn Future<Output = bool> + Send + 'static)>>`
6-
...
74
LL | let _: BoxFuture<'static, bool> = async {}.boxed();
85
| ------------------------ ^^^^^^^^^^^^^^^^ expected `bool`, found `()`
96
| |

tests/ui/suggestions/trait-with-missing-associated-type-restriction-fixable.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
trait Trait<T = Self> {
55
type A;
66

7-
fn func(&self) -> usize;
7+
fn func(&self) -> Self::A;
88
}
99

1010
struct S<T>(T);

tests/ui/suggestions/trait-with-missing-associated-type-restriction-fixable.stderr

-28
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ help: consider constraining the associated type `<impl Trait as Trait>::A` to `u
2222
|
2323
LL | fn foo<'a, T: Trait + 'a>(&self, _: impl Trait, x: impl Trait<A = usize>, _: T) {
2424
| +++++++++++
25-
help: consider changing func's return type
26-
|
27-
LL | fn func(&self) -> usize;
28-
| ~~~~~
2925

3026
error[E0308]: mismatched types
3127
--> $DIR/trait-with-missing-associated-type-restriction-fixable.rs:17:13
@@ -51,10 +47,6 @@ help: consider constraining the associated type `<T as Trait>::A` to `usize`
5147
|
5248
LL | fn ban<T>(x: T) where T: Trait<A = usize> {
5349
| +++++++++++
54-
help: consider changing func's return type
55-
|
56-
LL | fn func(&self) -> usize;
57-
| ~~~~~
5850

5951
error[E0308]: mismatched types
6052
--> $DIR/trait-with-missing-associated-type-restriction-fixable.rs:22:9
@@ -80,10 +72,6 @@ help: consider constraining the associated type `<impl Trait as Trait>::A` to `u
8072
|
8173
LL | fn foo<'a, T: Trait + 'a>(_: impl Trait, x: impl Trait<A = usize>, _: T) {
8274
| +++++++++++
83-
help: consider changing func's return type
84-
|
85-
LL | fn func(&self) -> usize;
86-
| ~~~~~
8775

8876
error[E0308]: mismatched types
8977
--> $DIR/trait-with-missing-associated-type-restriction-fixable.rs:26:9
@@ -109,10 +97,6 @@ help: consider constraining the associated type `<T as Trait>::A` to `usize`
10997
|
11098
LL | fn bar<T: Trait<A = usize>>(x: T) {
11199
| +++++++++++
112-
help: consider changing func's return type
113-
|
114-
LL | fn func(&self) -> usize;
115-
| ~~~~~
116100

117101
error[E0308]: mismatched types
118102
--> $DIR/trait-with-missing-associated-type-restriction-fixable.rs:30:9
@@ -138,10 +122,6 @@ help: consider constraining the associated type `<impl Trait<i32> as Trait<i32>>
138122
|
139123
LL | fn foo2(x: impl Trait<i32, A = usize>) {
140124
| +++++++++++
141-
help: consider changing func's return type
142-
|
143-
LL | fn func(&self) -> usize;
144-
| ~~~~~
145125

146126
error[E0308]: mismatched types
147127
--> $DIR/trait-with-missing-associated-type-restriction-fixable.rs:34:9
@@ -167,10 +147,6 @@ help: consider constraining the associated type `<T as Trait<i32>>::A` to `usize
167147
|
168148
LL | fn bar2<T: Trait<i32, A = usize>>(x: T) {
169149
| +++++++++++
170-
help: consider changing func's return type
171-
|
172-
LL | fn func(&self) -> usize;
173-
| ~~~~~
174150

175151
error[E0308]: mismatched types
176152
--> $DIR/trait-with-missing-associated-type-restriction-fixable.rs:38:9
@@ -196,10 +172,6 @@ help: consider constraining the associated type `<T as Trait>::A` to `usize`
196172
|
197173
LL | fn ban<T>(x: T) where T: Trait<A = usize> {
198174
| +++++++++++
199-
help: consider changing func's return type
200-
|
201-
LL | fn func(&self) -> usize;
202-
| ~~~~~
203175

204176
error: aborting due to 7 previous errors
205177

tests/ui/suggestions/trait-with-missing-associated-type-restriction.stderr

-26
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ help: consider constraining the associated type `<impl Trait as Trait>::A` to `u
3636
|
3737
LL | fn foo(_: impl Trait, x: impl Trait<A = usize>) {
3838
| +++++++++++
39-
help: consider changing func's return type
40-
|
41-
LL | fn func(&self) -> usize;
42-
| ~~~~~
4339

4440
error[E0308]: mismatched types
4541
--> $DIR/trait-with-missing-associated-type-restriction.rs:18:9
@@ -65,10 +61,6 @@ help: consider constraining the associated type `<T as Trait>::A` to `usize`
6561
|
6662
LL | fn bar<T: Trait<A = usize>>(x: T) {
6763
| +++++++++++
68-
help: consider changing func's return type
69-
|
70-
LL | fn func(&self) -> usize;
71-
| ~~~~~
7264

7365
error[E0308]: mismatched types
7466
--> $DIR/trait-with-missing-associated-type-restriction.rs:22:9
@@ -94,10 +86,6 @@ help: consider constraining the associated type `<impl Trait<i32> as Trait<i32>>
9486
|
9587
LL | fn foo2(x: impl Trait<i32, A = usize>) {
9688
| +++++++++++
97-
help: consider changing func's return type
98-
|
99-
LL | fn func(&self) -> usize;
100-
| ~~~~~
10189

10290
error[E0308]: mismatched types
10391
--> $DIR/trait-with-missing-associated-type-restriction.rs:26:12
@@ -151,17 +139,10 @@ help: consider constraining the associated type `<T as Trait<i32>>::A` to `usize
151139
|
152140
LL | fn bar2<T: Trait<i32, A = usize>>(x: T) {
153141
| +++++++++++
154-
help: consider changing func's return type
155-
|
156-
LL | fn func(&self) -> usize;
157-
| ~~~~~
158142

159143
error[E0308]: mismatched types
160144
--> $DIR/trait-with-missing-associated-type-restriction.rs:31:9
161145
|
162-
LL | fn func(&self) -> Self::A;
163-
| ------- help: consider changing func's return type: `usize`
164-
...
165146
LL | fn baz<D: std::fmt::Debug, T: Trait<A = D>>(x: T) {
166147
| - found this type parameter
167148
LL | qux(x.func())
@@ -185,9 +166,6 @@ LL | fn qux(_: usize) {}
185166
error[E0308]: mismatched types
186167
--> $DIR/trait-with-missing-associated-type-restriction.rs:35:9
187168
|
188-
LL | fn func(&self) -> Self::A;
189-
| ------- help: consider changing func's return type: `usize`
190-
...
191169
LL | qux(x.func())
192170
| --- ^^^^^^^^ expected `usize`, found `()`
193171
| |
@@ -228,10 +206,6 @@ help: consider constraining the associated type `<T as Trait>::A` to `usize`
228206
|
229207
LL | fn ban<T>(x: T) where T: Trait<A = usize> {
230208
| +++++++++++
231-
help: consider changing func's return type
232-
|
233-
LL | fn func(&self) -> usize;
234-
| ~~~~~
235209

236210
error: aborting due to 9 previous errors
237211

tests/ui/typeck/issue-50687-ice-on-borrow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | let _: () = Borrow::borrow(&owned);
88
|
99
= note: expected unit type `()`
1010
found reference `&_`
11-
note: the function Borrow is defined here
11+
note: the method Borrow is defined here
1212
--> $SRC_DIR/core/src/borrow.rs:LL:COL
1313
help: consider dereferencing the borrow
1414
|

0 commit comments

Comments
 (0)