Skip to content

Commit da59b8f

Browse files
author
Duddino
committed
fixed missing trait method suggests incorrect code (self parameter not named self)
1 parent 534a41a commit da59b8f

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,26 +2251,33 @@ fn fn_sig_suggestion(
22512251
sig: &ty::FnSig<'_>,
22522252
ident: Ident,
22532253
predicates: ty::GenericPredicates<'_>,
2254+
assoc: &ty::AssocItem,
22542255
) -> String {
22552256
let args = sig
22562257
.inputs()
22572258
.iter()
2258-
.map(|ty| {
2259+
.enumerate()
2260+
.map(|(i, ty)| {
22592261
Some(match ty.kind {
2260-
ty::Param(param) if param.name == kw::SelfUpper => "self".to_string(),
2261-
ty::Ref(reg, ref_ty, mutability) => {
2262+
ty::Param(_) if assoc.fn_has_self_parameter && i == 0 => "self".to_string(),
2263+
ty::Ref(reg, _ref_ty, mutability) => {
22622264
let reg = match &format!("{}", reg)[..] {
22632265
"'_" | "" => String::new(),
22642266
reg => format!("{} ", reg),
22652267
};
2266-
match ref_ty.kind {
2267-
ty::Param(param) if param.name == kw::SelfUpper => {
2268-
format!("&{}{}self", reg, mutability.prefix_str())
2269-
}
2270-
_ => format!("_: {:?}", ty),
2268+
if assoc.fn_has_self_parameter && i == 0 {
2269+
format!("&{}{}self", reg, mutability.prefix_str())
2270+
}else {
2271+
format!("_: {:?}", ty)
2272+
}
2273+
}
2274+
_ => {
2275+
if assoc.fn_has_self_parameter && i == 0 {
2276+
format!("self: {:?}", ty)
2277+
} else {
2278+
format!("_: {:?}", ty)
22712279
}
22722280
}
2273-
_ => format!("_: {:?}", ty),
22742281
})
22752282
})
22762283
.chain(std::iter::once(if sig.c_variadic { Some("...".to_string()) } else { None }))
@@ -2309,6 +2316,7 @@ fn suggestion_signature(assoc: &ty::AssocItem, tcx: TyCtxt<'_>) -> String {
23092316
tcx.fn_sig(assoc.def_id).skip_binder(),
23102317
assoc.ident,
23112318
tcx.predicates_of(assoc.def_id),
2319+
assoc,
23122320
)
23132321
}
23142322
ty::AssocKind::Type => format!("type {} = Type;", assoc.ident),

src/test/ui/missing/missing-items/auxiliary/m1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ pub trait X {
22
const CONSTANT: u32;
33
type Type;
44
fn method(&self, s: String) -> Self::Type;
5+
fn method2(self: Box<Self>, s: String) -> Self::Type;
6+
fn method3(other: &Self, s: String) -> Self::Type;
7+
fn method4(&self, other: &Self) -> Self::Type;
58
}

src/test/ui/missing/missing-items/m2.stderr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
error[E0046]: not all trait items implemented, missing: `CONSTANT`, `Type`, `method`
1+
error[E0046]: not all trait items implemented, missing: `CONSTANT`, `Type`, `method`, `method2`, `method3`, `method4`
22
--> $DIR/m2.rs:9:1
33
|
44
LL | impl m1::X for X {
5-
| ^^^^^^^^^^^^^^^^ missing `CONSTANT`, `Type`, `method` in implementation
5+
| ^^^^^^^^^^^^^^^^ missing `CONSTANT`, `Type`, `method`, `method2`, `method3`, `method4` in implementation
66
|
77
= help: implement the missing item: `const CONSTANT: u32 = 42;`
88
= help: implement the missing item: `type Type = Type;`
99
= help: implement the missing item: `fn method(&self, _: std::string::String) -> <Self as m1::X>::Type { todo!() }`
10+
= help: implement the missing item: `fn method2(self: std::boxed::Box<Self>, _: std::string::String) -> <Self as m1::X>::Type { todo!() }`
11+
= help: implement the missing item: `fn method3(_: &Self, _: std::string::String) -> <Self as m1::X>::Type { todo!() }`
12+
= help: implement the missing item: `fn method4(&self, _: &Self) -> <Self as m1::X>::Type { todo!() }`
1013

1114
error: aborting due to previous error
1215

0 commit comments

Comments
 (0)