Skip to content

Commit

Permalink
Rollup merge of rust-lang#97640 - TaKO8Ki:fix-wrong-suggestion-for-ad…
Browse files Browse the repository at this point in the history
…ding-where-clauses, r=lcnr

Fix wrong suggestion for adding where clauses

closes rust-lang#97576
  • Loading branch information
matthiaskrgr authored Jun 2, 2022
2 parents bae8a57 + 2898890 commit 2058c37
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
use rustc_middle::hir::map;
use rustc_middle::ty::{
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree,
self,
subst::{GenericArgKind, SubstsRef},
suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree,
GeneratorDiagnosticData, GeneratorInteriorTypeCause, Infer, InferTy, ToPredicate, Ty, TyCtxt,
TypeFoldable,
};
Expand Down Expand Up @@ -458,6 +460,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
_ => (false, None),
};

let generic_args_have_impl_trait = |args: SubstsRef<'tcx>| -> bool {
args.iter().any(|arg| match arg.unpack() {
GenericArgKind::Type(ty) => match ty.kind() {
ty::Param(param) => param.name.as_str().starts_with("impl"),
_ => false,
},
_ => false,
})
};

// FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
// don't suggest `T: Sized + ?Sized`.
let mut hir_id = body_id;
Expand Down Expand Up @@ -588,7 +600,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
| hir::ItemKind::TraitAlias(generics, _)
| hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
..
}) if !param_ty => {
}) if !param_ty
&& !generic_args_have_impl_trait(trait_pred.skip_binder().trait_ref.substs) =>
{
// Missing generic type parameter bound.
let param_name = self_ty.to_string();
let constraint = trait_pred.print_modifiers_and_trait_path().to_string();
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/traits/issue-97576.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct Foo {
bar: String,
}

impl Foo {
pub fn new(bar: impl ToString) -> Self {
Self {
bar: bar.into(), //~ ERROR the trait bound `String: From<impl ToString>` is not satisfied
}
}
}

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/traits/issue-97576.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0277]: the trait bound `String: From<impl ToString>` is not satisfied
--> $DIR/issue-97576.rs:8:22
|
LL | bar: bar.into(),
| ^^^^ the trait `From<impl ToString>` is not implemented for `String`
|
= note: required because of the requirements on the impl of `Into<String>` for `impl ToString`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

0 comments on commit 2058c37

Please sign in to comment.