@@ -16,9 +16,9 @@ use crate::ty::AdtKind;
1616use crate :: ty:: query:: Providers ;
1717use crate :: util:: nodemap:: { NodeMap , FxHashSet } ;
1818
19- use errors:: { Applicability , DiagnosticBuilder , FatalError } ;
19+ use errors:: FatalError ;
2020use syntax_pos:: { Span , DUMMY_SP , MultiSpan } ;
21- use syntax:: source_map:: { Spanned , SourceMap } ;
21+ use syntax:: source_map:: Spanned ;
2222use syntax:: ast:: { self , CrateSugar , Ident , Name , NodeId , AsmDialect } ;
2323use syntax:: ast:: { Attribute , Label , LitKind , StrStyle , FloatTy , IntTy , UintTy } ;
2424pub use syntax:: ast:: { Mutability , Constness , Unsafety , Movability , CaptureBy } ;
@@ -644,79 +644,6 @@ impl Generics {
644644 self . params . iter ( ) . map ( |p| p. span ) . collect :: < Vec < Span > > ( ) . into ( )
645645 }
646646 }
647-
648- /// Suggest restricting a type param with a new bound.
649- pub fn suggest_constraining_type_param (
650- & self ,
651- err : & mut DiagnosticBuilder < ' _ > ,
652- param_name : & str ,
653- constraint : & str ,
654- source_map : & SourceMap ,
655- span : Span ,
656- ) -> bool {
657- let restrict_msg = "consider further restricting this bound" ;
658- if let Some ( param) = self . params . iter ( ) . filter ( |p| {
659- p. name . ident ( ) . as_str ( ) == param_name
660- } ) . next ( ) {
661- if param_name. starts_with ( "impl " ) {
662- // `impl Trait` in argument:
663- // `fn foo(x: impl Trait) {}` → `fn foo(t: impl Trait + Trait2) {}`
664- err. span_suggestion (
665- param. span ,
666- restrict_msg,
667- // `impl CurrentTrait + MissingTrait`
668- format ! ( "{} + {}" , param_name, constraint) ,
669- Applicability :: MachineApplicable ,
670- ) ;
671- } else if self . where_clause . predicates . is_empty ( ) &&
672- param. bounds . is_empty ( )
673- {
674- // If there are no bounds whatsoever, suggest adding a constraint
675- // to the type parameter:
676- // `fn foo<T>(t: T) {}` → `fn foo<T: Trait>(t: T) {}`
677- err. span_suggestion (
678- param. span ,
679- "consider restricting this bound" ,
680- format ! ( "{}: {}" , param_name, constraint) ,
681- Applicability :: MachineApplicable ,
682- ) ;
683- } else if !self . where_clause . predicates . is_empty ( ) {
684- // There is a `where` clause, so suggest expanding it:
685- // `fn foo<T>(t: T) where T: Debug {}` →
686- // `fn foo<T>(t: T) where T: Debug, T: Trait {}`
687- err. span_suggestion (
688- self . where_clause . span ( ) . unwrap ( ) . shrink_to_hi ( ) ,
689- & format ! ( "consider further restricting type parameter `{}`" , param_name) ,
690- format ! ( ", {}: {}" , param_name, constraint) ,
691- Applicability :: MachineApplicable ,
692- ) ;
693- } else {
694- // If there is no `where` clause lean towards constraining to the
695- // type parameter:
696- // `fn foo<X: Bar, T>(t: T, x: X) {}` → `fn foo<T: Trait>(t: T) {}`
697- // `fn foo<T: Bar>(t: T) {}` → `fn foo<T: Bar + Trait>(t: T) {}`
698- let sp = param. span . with_hi ( span. hi ( ) ) ;
699- let span = source_map. span_through_char ( sp, ':' ) ;
700- if sp != param. span && sp != span {
701- // Only suggest if we have high certainty that the span
702- // covers the colon in `foo<T: Trait>`.
703- err. span_suggestion (
704- span,
705- restrict_msg,
706- format ! ( "{}: {} + " , param_name, constraint) ,
707- Applicability :: MachineApplicable ,
708- ) ;
709- } else {
710- err. span_label (
711- param. span ,
712- & format ! ( "consider adding a `where {}: {}` bound" , param_name, constraint) ,
713- ) ;
714- }
715- }
716- return true ;
717- }
718- false
719- }
720647}
721648
722649/// Synthetic type parameters are converted to another form during lowering; this allows
0 commit comments