@@ -17,7 +17,7 @@ use rustc_hir::def_id::DefId;
17
17
use rustc_hir:: intravisit:: Visitor ;
18
18
use rustc_hir:: Node ;
19
19
use rustc_span:: source_map:: SourceMap ;
20
- use rustc_span:: symbol:: { kw, sym} ;
20
+ use rustc_span:: symbol:: { kw, sym, Symbol } ;
21
21
use rustc_span:: { MultiSpan , Span , DUMMY_SP } ;
22
22
use std:: fmt;
23
23
@@ -143,12 +143,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
143
143
{
144
144
// Missing generic type parameter bound.
145
145
let param_name = self_ty. to_string ( ) ;
146
+ let param = Symbol :: intern ( & param_name) ;
146
147
let constraint = trait_ref. print_only_trait_path ( ) . to_string ( ) ;
147
148
if suggest_constraining_type_param (
148
149
self . tcx ,
149
150
generics,
150
151
& mut err,
151
- & param_name ,
152
+ param ,
152
153
& constraint,
153
154
self . tcx . sess . source_map ( ) ,
154
155
* span,
@@ -1657,30 +1658,30 @@ pub fn suggest_constraining_type_param(
1657
1658
tcx : TyCtxt < ' _ > ,
1658
1659
generics : & hir:: Generics < ' _ > ,
1659
1660
err : & mut DiagnosticBuilder < ' _ > ,
1660
- param_name : & str ,
1661
+ param_name : Symbol ,
1661
1662
constraint : & str ,
1662
1663
source_map : & SourceMap ,
1663
1664
span : Span ,
1664
1665
def_id : Option < DefId > ,
1665
1666
) -> bool {
1666
1667
let restrict_msg = "consider further restricting this bound" ;
1667
1668
if let Some ( param) =
1668
- generics. params . iter ( ) . filter ( |p| p. name . ident ( ) . as_str ( ) == param_name) . next ( )
1669
+ generics. params . iter ( ) . filter ( |p| p. name . ident ( ) . as_str ( ) == param_name. as_str ( ) ) . next ( )
1669
1670
{
1670
1671
if def_id == tcx. lang_items ( ) . sized_trait ( ) {
1671
1672
// Type parameters are already `Sized` by default.
1672
1673
err. span_label (
1673
1674
param. span ,
1674
1675
& format ! ( "this type parameter needs to be `{}`" , constraint) ,
1675
1676
) ;
1676
- } else if param_name. starts_with ( "impl " ) {
1677
+ } else if param_name. as_str ( ) . starts_with ( "impl " ) {
1677
1678
// `impl Trait` in argument:
1678
1679
// `fn foo(x: impl Trait) {}` → `fn foo(t: impl Trait + Trait2) {}`
1679
1680
err. span_suggestion (
1680
1681
param. span ,
1681
1682
restrict_msg,
1682
1683
// `impl CurrentTrait + MissingTrait`
1683
- format ! ( "{} + {}" , param_name, constraint) ,
1684
+ format ! ( "{} + {}" , param_name. to_stringified_ident_guess ( ) , constraint) ,
1684
1685
Applicability :: MachineApplicable ,
1685
1686
) ;
1686
1687
} else if generics. where_clause . predicates . is_empty ( ) && param. bounds . is_empty ( ) {
@@ -1690,7 +1691,7 @@ pub fn suggest_constraining_type_param(
1690
1691
err. span_suggestion (
1691
1692
param. span ,
1692
1693
"consider restricting this bound" ,
1693
- format ! ( "{}: {}" , param_name, constraint) ,
1694
+ format ! ( "{}: {}" , param_name. to_stringified_ident_guess ( ) , constraint) ,
1694
1695
Applicability :: MachineApplicable ,
1695
1696
) ;
1696
1697
} else if !generics. where_clause . predicates . is_empty ( ) {
@@ -1699,8 +1700,11 @@ pub fn suggest_constraining_type_param(
1699
1700
// `fn foo<T>(t: T) where T: Debug, T: Trait {}`
1700
1701
err. span_suggestion (
1701
1702
generics. where_clause . span ( ) . unwrap ( ) . shrink_to_hi ( ) ,
1702
- & format ! ( "consider further restricting type parameter `{}`" , param_name) ,
1703
- format ! ( ", {}: {}" , param_name, constraint) ,
1703
+ & format ! (
1704
+ "consider further restricting type parameter `{}`" ,
1705
+ param_name. to_stringified_ident_guess( )
1706
+ ) ,
1707
+ format ! ( ", {}: {}" , param_name. to_stringified_ident_guess( ) , constraint) ,
1704
1708
Applicability :: MachineApplicable ,
1705
1709
) ;
1706
1710
} else {
@@ -1716,13 +1720,17 @@ pub fn suggest_constraining_type_param(
1716
1720
err. span_suggestion (
1717
1721
span,
1718
1722
restrict_msg,
1719
- format ! ( "{}: {} + " , param_name, constraint) ,
1723
+ format ! ( "{}: {} + " , param_name. to_stringified_ident_guess ( ) , constraint) ,
1720
1724
Applicability :: MachineApplicable ,
1721
1725
) ;
1722
1726
} else {
1723
1727
err. span_label (
1724
1728
param. span ,
1725
- & format ! ( "consider adding a `where {}: {}` bound" , param_name, constraint) ,
1729
+ & format ! (
1730
+ "consider adding a `where {}: {}` bound" ,
1731
+ param_name. to_stringified_ident_guess( ) ,
1732
+ constraint
1733
+ ) ,
1726
1734
) ;
1727
1735
}
1728
1736
}
0 commit comments