@@ -856,36 +856,108 @@ fn elision_suggestions(
856856 . filter ( |param| !param. is_elided_lifetime ( ) && !param. is_impl_trait ( ) )
857857 . collect :: < Vec < _ > > ( ) ;
858858
859- let mut suggestions = if elidable_lts. len ( ) == explicit_params. len ( ) {
859+ if !elidable_lts
860+ . iter ( )
861+ . all ( |lt| explicit_params. iter ( ) . any ( |param| param. def_id == * lt) )
862+ {
863+ return None ;
864+ }
865+
866+ let mut suggestions = if elidable_lts. is_empty ( ) {
867+ vec ! [ ]
868+ } else if elidable_lts. len ( ) == explicit_params. len ( ) {
860869 // if all the params are elided remove the whole generic block
861870 //
862871 // fn x<'a>() {}
863872 // ^^^^
864873 vec ! [ ( generics. span, String :: new( ) ) ]
865874 } else {
866- elidable_lts
867- . iter ( )
868- . map ( |& id| {
869- let pos = explicit_params. iter ( ) . position ( |param| param. def_id == id) ?;
870- let param = explicit_params. get ( pos) ?;
871-
872- let span = if let Some ( next) = explicit_params. get ( pos + 1 ) {
873- // fn x<'prev, 'a, 'next>() {}
874- // ^^^^
875- param. span . until ( next. span )
875+ match & explicit_params[ ..] {
876+ // no params, nothing to elide
877+ [ ] => unreachable ! ( "handled by `elidable_lts.is_empty()`" ) ,
878+ [ param] => {
879+ if elidable_lts. contains ( & param. def_id ) {
880+ unreachable ! ( "handled by `elidable_lts.len() == explicit_params.len()`" )
876881 } else {
877- // `pos` should be at least 1 here, because the param in position 0 would either have a `next`
878- // param or would have taken the `elidable_lts.len() == explicit_params.len()` branch.
879- let prev = explicit_params. get ( pos - 1 ) ?;
880-
881- // fn x<'prev, 'a>() {}
882- // ^^^^
883- param. span . with_lo ( prev. span . hi ( ) )
882+ unreachable ! ( "handled by `elidable_lts.is_empty()`" )
883+ }
884+ } ,
885+ [ first, second] => {
886+ // NOTE: we could theoretically remove this case for the next one,
887+ // but the latter likely has quite a lot of overhead, so don't
888+ match (
889+ elidable_lts. contains ( & first. def_id ) ,
890+ elidable_lts. contains ( & second. def_id ) ,
891+ ) {
892+ ( false , false ) => unreachable ! ( "handled by `elidable_lts.is_empty()`" ) ,
893+ ( true , true ) => unreachable ! ( "handled by `elidable_lts.len() == explicit_params.len()`" ) ,
894+
895+ // <'a, 'b> -> <'a, 'b>
896+ // ^^ ^^^^
897+ ( false , true ) => vec ! [ ( second. span. with_lo( first. span. hi( ) ) , String :: new( ) ) ] ,
898+ // <'a, 'b> -> <'a, 'b>
899+ // ^^ ^^^^
900+ ( true , false ) => vec ! [ ( first. span. until( second. span) , String :: new( ) ) ] ,
901+ }
902+ } ,
903+ // Time for the big guns
904+ [ _, _, ..] => {
905+ // Given a list like `<'a, 'b, 'c, 'd, ..>`,
906+ //
907+ // If there is a cluster of elidable lifetimes at the beginning, say `'a` and `'b`, we should
908+ // suggest removing them _and_ the trailing comma. The span for that is `a.span.until(c.span)`:
909+ // <'a, 'b, 'c, 'd, ..> => <'a, 'b, 'c, 'd, ..>
910+ // ^^ ^^ ^^^^^^^^
911+ //
912+ // And since we know that `'c` isn't elidable--otherwise it would've been in the cluster--we can go
913+ // over all the lifetimes after it, and for each elidable one, add a suggestion spanning the
914+ // lifetime itself and the comma before, because each individual suggestion is guaranteed to leave
915+ // the list valid:
916+ // <.., 'c, 'd, 'e, 'f, 'g, ..> => <.., 'c, 'd, 'e, 'f, 'g, ..>
917+ // ^^ ^^ ^^ ^^^^ ^^^^^^^^
918+ //
919+ // In case there is no such starting cluster, we only need to do the second part of the algorithm:
920+ // <'a, 'b, 'c, 'd, 'e, 'f, 'g, ..> => <'a, 'b , 'c, 'd, 'e, 'f, 'g, ..>
921+ // ^^ ^^ ^^ ^^ ^^^^^^^^^ ^^^^^^^^
922+
923+ // Split off the starting cluster
924+ // TODO: use `slice::split_once` once stabilized (github.com/rust-lang/rust/issues/112811):
925+ // ```
926+ // let Some(split) = explicit_params.split_once(|param| !elidable_lts.contains(¶m.def_id)) else {
927+ // // there were no lifetime param that couldn't be elided
928+ // unreachable!("handled by `elidable_lts.len() == explicit_params.len()`")
929+ // };
930+ // match split { /* .. */ }
931+ // ```
932+ let Some ( split_pos) = explicit_params
933+ . iter ( )
934+ . position ( |param| !elidable_lts. contains ( & param. def_id ) )
935+ else {
936+ // there were no lifetime param that couldn't be elided
937+ unreachable ! ( "handled by `elidable_lts.len() == explicit_params.len()`" )
884938 } ;
885-
886- Some ( ( span, String :: new ( ) ) )
887- } )
888- . collect :: < Option < Vec < _ > > > ( ) ?
939+ let split = explicit_params
940+ . split_at_checked ( split_pos)
941+ . expect ( "got `split_pos` from `position` on the same Vec" ) ;
942+
943+ match split {
944+ ( [ ..] , [ ] ) => unreachable ! ( "handled by `elidable_lts.len() == explicit_params.len()`" ) ,
945+ ( [ ] , [ _] ) => unreachable ! ( "handled by `explicit_params.len() == 1`" ) ,
946+ ( cluster, rest @ [ rest_first, ..] ) => {
947+ // the span for the cluster
948+ ( cluster. first ( ) . map ( |fw| fw. span . until ( rest_first. span ) ) . into_iter ( ) )
949+ // the span for the remaining lifetimes (calculations independent of the cluster)
950+ . chain (
951+ rest. array_windows ( )
952+ . filter ( |[ _, curr] | elidable_lts. contains ( & curr. def_id ) )
953+ . map ( |[ prev, curr] | curr. span . with_lo ( prev. span . hi ( ) ) ) ,
954+ )
955+ . map ( |sp| ( sp, String :: new ( ) ) )
956+ . collect ( )
957+ } ,
958+ }
959+ } ,
960+ }
889961 } ;
890962
891963 suggestions. extend ( usages. iter ( ) . map ( |& usage| {
0 commit comments