Skip to content

Commit ac15a62

Browse files
committed
fix APITIT being treated as a normal generic parameter in suggestions
1 parent 6ba0ce4 commit ac15a62

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ fn missing_items_err(
232232
};
233233

234234
// Obtain the level of indentation ending in `sugg_sp`.
235-
let padding =
236-
tcx.sess.source_map().indentation_before(sugg_sp).unwrap_or_else(|| String::new());
235+
let padding = tcx.sess.source_map().indentation_before(sugg_sp).unwrap_or_else(String::new);
237236
let (mut missing_trait_item, mut missing_trait_item_none, mut missing_trait_item_label) =
238237
(Vec::new(), Vec::new(), Vec::new());
239238

@@ -330,6 +329,7 @@ fn default_body_is_unstable(
330329
fn bounds_from_generic_predicates<'tcx>(
331330
tcx: TyCtxt<'tcx>,
332331
predicates: impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>,
332+
assoc: Option<ty::AssocItem>,
333333
) -> (String, String) {
334334
let mut types: FxIndexMap<Ty<'tcx>, Vec<DefId>> = FxIndexMap::default();
335335
let mut projections = vec![];
@@ -354,8 +354,14 @@ fn bounds_from_generic_predicates<'tcx>(
354354

355355
let mut where_clauses = vec![];
356356
let mut types_str = vec![];
357-
for (ty, bounds) in types {
357+
let generics = assoc.map(|a| tcx.generics_of(a.def_id)).map(|g| (g.own_counts().types, g));
358+
for (idx, (ty, bounds)) in types.into_iter().enumerate() {
358359
if let ty::Param(_) = ty.kind() {
360+
if generics.is_some_and(|(count, g)| {
361+
idx + 1 <= count && g.param_at(idx + 1, tcx).kind.is_synthetic()
362+
}) {
363+
continue;
364+
}
359365
let mut bounds_str = vec![];
360366
for bound in bounds {
361367
let mut projections_str = vec![];
@@ -472,10 +478,10 @@ fn fn_sig_suggestion<'tcx>(
472478
let output = if !output.is_unit() { format!(" -> {output}") } else { String::new() };
473479

474480
let safety = sig.safety.prefix_str();
475-
let (generics, where_clauses) = bounds_from_generic_predicates(tcx, predicates);
481+
let (generics, where_clauses) = bounds_from_generic_predicates(tcx, predicates, Some(assoc));
476482

477483
// FIXME: this is not entirely correct, as the lifetimes from borrowed params will
478-
// not be present in the `fn` definition, not will we account for renamed
484+
// not be present in the `fn` definition, nor will we account for renamed
479485
// lifetimes between the `impl` and the `trait`, but this should be good enough to
480486
// fill in a significant portion of the missing code, and other subsequent
481487
// suggestions can help the user fix the code.
@@ -511,6 +517,7 @@ fn suggestion_signature<'tcx>(
511517
let (generics, where_clauses) = bounds_from_generic_predicates(
512518
tcx,
513519
tcx.predicates_of(assoc.def_id).instantiate_own(tcx, args),
520+
None,
514521
);
515522
format!("type {}{generics} = /* Type */{where_clauses};", assoc.name())
516523
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ aux-build:dep.rs
2+
3+
extern crate dep;
4+
use dep::*;
5+
6+
struct Local;
7+
impl Trait for Local {}
8+
//~^ ERROR not all trait items implemented
9+
//~| HELP implement the missing item: `fn foo(_: impl Sized) { todo!() }`
10+
//~| HELP implement the missing item: `fn bar<T>(_: impl Sized) { todo!() }`
11+
12+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0046]: not all trait items implemented, missing: `foo`, `bar`
2+
--> $DIR/apitit-unimplemented-method.rs:7:1
3+
|
4+
LL | impl Trait for Local {}
5+
| ^^^^^^^^^^^^^^^^^^^^ missing `foo`, `bar` in implementation
6+
|
7+
= help: implement the missing item: `fn foo(_: impl Sized) { todo!() }`
8+
= help: implement the missing item: `fn bar<T>(_: impl Sized) { todo!() }`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0046`.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub trait Trait {
2+
fn foo(_: impl Sized);
3+
fn bar<T>(_: impl Sized);
4+
}

0 commit comments

Comments
 (0)