Skip to content

Commit 36791da

Browse files
authored
Rollup merge of #71188 - Duddino:fix, r=matthewjasper
Fixed missing trait method suggests incorrect code (self parameter not named "self"). fixes #71150
2 parents 4d11c3f + f36f78f commit 36791da

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/librustc_typeck/check/mod.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -2251,26 +2251,39 @@ 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) if i == 0 => {
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())
2268+
if assoc.fn_has_self_parameter {
2269+
match ref_ty.kind {
2270+
ty::Param(param) if param.name == kw::SelfUpper => {
2271+
format!("&{}{}self", reg, mutability.prefix_str())
2272+
}
2273+
2274+
_ => format!("self: {}", ty),
22692275
}
2270-
_ => format!("_: {:?}", ty),
2276+
} else {
2277+
format!("_: {:?}", ty)
2278+
}
2279+
}
2280+
_ => {
2281+
if assoc.fn_has_self_parameter && i == 0 {
2282+
format!("self: {:?}", ty)
2283+
} else {
2284+
format!("_: {:?}", ty)
22712285
}
22722286
}
2273-
_ => format!("_: {:?}", ty),
22742287
})
22752288
})
22762289
.chain(std::iter::once(if sig.c_variadic { Some("...".to_string()) } else { None }))
@@ -2309,6 +2322,7 @@ fn suggestion_signature(assoc: &ty::AssocItem, tcx: TyCtxt<'_>) -> String {
23092322
tcx.fn_sig(assoc.def_id).skip_binder(),
23102323
assoc.ident,
23112324
tcx.predicates_of(assoc.def_id),
2325+
assoc,
23122326
)
23132327
}
23142328
ty::AssocKind::Type => format!("type {} = Type;", assoc.ident),

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

+4
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ 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;
8+
fn method5(self: &Box<Self>) -> Self::Type;
59
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
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`, `method5`
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`, `method5` 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!() }`
13+
= help: implement the missing item: `fn method5(self: &std::boxed::Box<Self>) -> <Self as m1::X>::Type { todo!() }`
1014

1115
error: aborting due to previous error
1216

0 commit comments

Comments
 (0)