@@ -23,7 +23,7 @@ use rustc_span::symbol::{kw, sym, Ident};
23
23
use rustc_span:: Span ;
24
24
use rustc_target:: spec:: abi;
25
25
use std:: mem;
26
- use std:: ops:: DerefMut ;
26
+ use std:: ops:: { Deref , DerefMut } ;
27
27
28
28
const MORE_EXTERN : & str =
29
29
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html" ;
@@ -1714,6 +1714,53 @@ fn deny_equality_constraints(
1714
1714
}
1715
1715
}
1716
1716
}
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
+ }
1717
1764
err. note (
1718
1765
"see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information" ,
1719
1766
) ;
0 commit comments