@@ -17,7 +17,7 @@ use rustc_hir::def_id::DefId;
1717use rustc_hir:: intravisit:: Visitor ;
1818use rustc_hir:: Node ;
1919use rustc_span:: source_map:: SourceMap ;
20- use rustc_span:: symbol:: { kw, sym} ;
20+ use rustc_span:: symbol:: { kw, sym, Symbol } ;
2121use rustc_span:: { MultiSpan , Span , DUMMY_SP } ;
2222use std:: fmt;
2323
@@ -143,12 +143,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
143143 {
144144 // Missing generic type parameter bound.
145145 let param_name = self_ty. to_string ( ) ;
146+ let param = Symbol :: intern ( & param_name) ;
146147 let constraint = trait_ref. print_only_trait_path ( ) . to_string ( ) ;
147148 if suggest_constraining_type_param (
148149 self . tcx ,
149150 generics,
150151 & mut err,
151- & param_name ,
152+ param ,
152153 & constraint,
153154 self . tcx . sess . source_map ( ) ,
154155 * span,
@@ -1657,30 +1658,30 @@ pub fn suggest_constraining_type_param(
16571658 tcx : TyCtxt < ' _ > ,
16581659 generics : & hir:: Generics < ' _ > ,
16591660 err : & mut DiagnosticBuilder < ' _ > ,
1660- param_name : & str ,
1661+ param_name : Symbol ,
16611662 constraint : & str ,
16621663 source_map : & SourceMap ,
16631664 span : Span ,
16641665 def_id : Option < DefId > ,
16651666) -> bool {
16661667 let restrict_msg = "consider further restricting this bound" ;
16671668 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 ( )
16691670 {
16701671 if def_id == tcx. lang_items ( ) . sized_trait ( ) {
16711672 // Type parameters are already `Sized` by default.
16721673 err. span_label (
16731674 param. span ,
16741675 & format ! ( "this type parameter needs to be `{}`" , constraint) ,
16751676 ) ;
1676- } else if param_name. starts_with ( "impl " ) {
1677+ } else if param_name. as_str ( ) . starts_with ( "impl " ) {
16771678 // `impl Trait` in argument:
16781679 // `fn foo(x: impl Trait) {}` → `fn foo(t: impl Trait + Trait2) {}`
16791680 err. span_suggestion (
16801681 param. span ,
16811682 restrict_msg,
16821683 // `impl CurrentTrait + MissingTrait`
1683- format ! ( "{} + {}" , param_name, constraint) ,
1684+ format ! ( "{} + {}" , param_name. to_stringified_ident_guess ( ) , constraint) ,
16841685 Applicability :: MachineApplicable ,
16851686 ) ;
16861687 } else if generics. where_clause . predicates . is_empty ( ) && param. bounds . is_empty ( ) {
@@ -1690,7 +1691,7 @@ pub fn suggest_constraining_type_param(
16901691 err. span_suggestion (
16911692 param. span ,
16921693 "consider restricting this bound" ,
1693- format ! ( "{}: {}" , param_name, constraint) ,
1694+ format ! ( "{}: {}" , param_name. to_stringified_ident_guess ( ) , constraint) ,
16941695 Applicability :: MachineApplicable ,
16951696 ) ;
16961697 } else if !generics. where_clause . predicates . is_empty ( ) {
@@ -1699,8 +1700,11 @@ pub fn suggest_constraining_type_param(
16991700 // `fn foo<T>(t: T) where T: Debug, T: Trait {}`
17001701 err. span_suggestion (
17011702 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) ,
17041708 Applicability :: MachineApplicable ,
17051709 ) ;
17061710 } else {
@@ -1716,13 +1720,17 @@ pub fn suggest_constraining_type_param(
17161720 err. span_suggestion (
17171721 span,
17181722 restrict_msg,
1719- format ! ( "{}: {} + " , param_name, constraint) ,
1723+ format ! ( "{}: {} + " , param_name. to_stringified_ident_guess ( ) , constraint) ,
17201724 Applicability :: MachineApplicable ,
17211725 ) ;
17221726 } else {
17231727 err. span_label (
17241728 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+ ) ,
17261734 ) ;
17271735 }
17281736 }
0 commit comments