@@ -23,7 +23,7 @@ use rustc_span::symbol::{kw, sym, Ident};
2323use rustc_span:: Span ;
2424use rustc_target:: spec:: abi;
2525use std:: mem;
26- use std:: ops:: DerefMut ;
26+ use std:: ops:: { Deref , DerefMut } ;
2727
2828const MORE_EXTERN : & str =
2929 "for more information, visit https://doc.rust-lang.org/std/keyword.extern.html" ;
@@ -1714,6 +1714,53 @@ fn deny_equality_constraints(
17141714 }
17151715 }
17161716 }
1717+ // Given `A: Foo, A::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
1718+ if let TyKind :: Path ( None , full_path) = & predicate. lhs_ty . kind {
1719+ if let [ potential_param, potential_assoc] = & full_path. segments [ ..] {
1720+ for param in & generics. params {
1721+ if param. ident == potential_param. ident {
1722+ for bound in & param. bounds {
1723+ if let ast:: GenericBound :: Trait ( trait_ref, TraitBoundModifier :: None ) = bound
1724+ {
1725+ if let [ trait_segment] = & trait_ref. trait_ref . path . segments [ ..] {
1726+ let assoc = pprust:: path_to_string ( & ast:: Path :: from_ident (
1727+ potential_assoc. ident ,
1728+ ) ) ;
1729+ let ty = pprust:: ty_to_string ( & predicate. rhs_ty ) ;
1730+ let ( args, span) = match & trait_segment. args {
1731+ Some ( args) => match args. deref ( ) {
1732+ ast:: GenericArgs :: AngleBracketed ( args) => {
1733+ let Some ( arg) = args. args . last ( ) else {
1734+ continue ;
1735+ } ;
1736+ (
1737+ format ! ( ", {} = {}" , assoc, ty) ,
1738+ arg. span ( ) . shrink_to_hi ( ) ,
1739+ )
1740+ }
1741+ _ => continue ,
1742+ } ,
1743+ None => (
1744+ format ! ( "<{} = {}>" , assoc, ty) ,
1745+ trait_segment. span ( ) . shrink_to_hi ( ) ,
1746+ ) ,
1747+ } ;
1748+ err. multipart_suggestion (
1749+ & format ! (
1750+ "if `{}::{}` is an associated type you're trying to set, \
1751+ use the associated type binding syntax",
1752+ trait_segment. ident, potential_assoc. ident,
1753+ ) ,
1754+ vec ! [ ( span, args) , ( predicate. span, String :: new( ) ) ] ,
1755+ Applicability :: MaybeIncorrect ,
1756+ ) ;
1757+ }
1758+ }
1759+ }
1760+ }
1761+ }
1762+ }
1763+ }
17171764 err. note (
17181765 "see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information" ,
17191766 ) ;
0 commit comments