Skip to content

Commit 2f0c103

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

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ use rustc_infer::traits::ObligationCause;
8585
use rustc_middle::query::Providers;
8686
use rustc_middle::ty::error::{ExpectedFound, TypeError};
8787
use rustc_middle::ty::print::with_types_for_signature;
88-
use rustc_middle::ty::{self, GenericArgs, GenericArgsRef, Ty, TyCtxt, TypingMode};
88+
use rustc_middle::ty::{
89+
self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt, TypingMode,
90+
};
8991
use rustc_middle::{bug, span_bug};
9092
use rustc_session::parse::feature_err;
9193
use rustc_span::def_id::CRATE_DEF_ID;
@@ -232,8 +234,7 @@ fn missing_items_err(
232234
};
233235

234236
// 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());
237+
let padding = tcx.sess.source_map().indentation_before(sugg_sp).unwrap_or_else(String::new);
237238
let (mut missing_trait_item, mut missing_trait_item_none, mut missing_trait_item_label) =
238239
(Vec::new(), Vec::new(), Vec::new());
239240

@@ -330,6 +331,7 @@ fn default_body_is_unstable(
330331
fn bounds_from_generic_predicates<'tcx>(
331332
tcx: TyCtxt<'tcx>,
332333
predicates: impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>,
334+
assoc: ty::AssocItem,
333335
) -> (String, String) {
334336
let mut types: FxIndexMap<Ty<'tcx>, Vec<DefId>> = FxIndexMap::default();
335337
let mut projections = vec![];
@@ -353,34 +355,50 @@ fn bounds_from_generic_predicates<'tcx>(
353355
}
354356

355357
let mut where_clauses = vec![];
356-
let mut types_str = vec![];
357-
for (ty, bounds) in types {
358-
if let ty::Param(_) = ty.kind() {
359-
let mut bounds_str = vec![];
360-
for bound in bounds {
361-
let mut projections_str = vec![];
362-
for projection in &projections {
363-
let p = projection.skip_binder();
364-
if bound == tcx.parent(p.projection_term.def_id)
365-
&& p.projection_term.self_ty() == ty
366-
{
367-
let name = tcx.item_name(p.projection_term.def_id);
368-
projections_str.push(format!("{} = {}", name, p.term));
358+
let generics = tcx.generics_of(assoc.def_id);
359+
let types_str = generics
360+
.own_params
361+
.iter()
362+
.filter(|p| matches!(p.kind, GenericParamDefKind::Type { synthetic: false, .. }))
363+
.map(|p| {
364+
// we just checked that it's a type, so the unwrap can't fail
365+
let ty = tcx.mk_param_from_def(p).as_type().unwrap();
366+
if let Some(bounds) = types.get(&ty) {
367+
let mut bounds_str = vec![];
368+
for bound in bounds.iter().copied() {
369+
let mut projections_str = vec![];
370+
for projection in &projections {
371+
let p = projection.skip_binder();
372+
if bound == tcx.parent(p.projection_term.def_id)
373+
&& p.projection_term.self_ty() == ty
374+
{
375+
let name = tcx.item_name(p.projection_term.def_id);
376+
projections_str.push(format!("{} = {}", name, p.term));
377+
}
378+
}
379+
let bound_def_path = tcx.def_path_str(bound);
380+
if projections_str.is_empty() {
381+
where_clauses.push(format!("{}: {}", ty, bound_def_path));
382+
} else {
383+
bounds_str.push(format!(
384+
"{}<{}>",
385+
bound_def_path,
386+
projections_str.join(", ")
387+
));
369388
}
370389
}
371-
let bound_def_path = tcx.def_path_str(bound);
372-
if projections_str.is_empty() {
373-
where_clauses.push(format!("{}: {}", ty, bound_def_path));
390+
if bounds_str.is_empty() {
391+
ty.to_string()
374392
} else {
375-
bounds_str.push(format!("{}<{}>", bound_def_path, projections_str.join(", ")));
393+
format!("{}: {}", ty, bounds_str.join(" + "))
376394
}
377-
}
378-
if bounds_str.is_empty() {
379-
types_str.push(ty.to_string());
380395
} else {
381-
types_str.push(format!("{}: {}", ty, bounds_str.join(" + ")));
396+
ty.to_string()
382397
}
383-
} else {
398+
})
399+
.collect::<Vec<_>>();
400+
for (ty, bounds) in types.into_iter() {
401+
if !matches!(ty.kind(), ty::Param(_)) {
384402
// Avoid suggesting the following:
385403
// fn foo<T, <T as Trait>::Bar>(_: T) where T: Trait, <T as Trait>::Bar: Other {}
386404
where_clauses.extend(
@@ -472,10 +490,10 @@ fn fn_sig_suggestion<'tcx>(
472490
let output = if !output.is_unit() { format!(" -> {output}") } else { String::new() };
473491

474492
let safety = sig.safety.prefix_str();
475-
let (generics, where_clauses) = bounds_from_generic_predicates(tcx, predicates);
493+
let (generics, where_clauses) = bounds_from_generic_predicates(tcx, predicates, assoc);
476494

477495
// 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
496+
// not be present in the `fn` definition, nor will we account for renamed
479497
// lifetimes between the `impl` and the `trait`, but this should be good enough to
480498
// fill in a significant portion of the missing code, and other subsequent
481499
// suggestions can help the user fix the code.
@@ -511,6 +529,7 @@ fn suggestion_signature<'tcx>(
511529
let (generics, where_clauses) = bounds_from_generic_predicates(
512530
tcx,
513531
tcx.predicates_of(assoc.def_id).instantiate_own(tcx, args),
532+
assoc,
514533
);
515534
format!("type {}{generics} = /* Type */{where_clauses};", assoc.name())
516535
}
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)