@@ -5134,62 +5134,59 @@ impl<'a> Resolver<'a> {
51345134 ) ;
51355135
51365136 // See https://github.com/rust-lang/rust/issues/32354
5137- if old_binding. is_import ( ) || new_binding. is_import ( ) {
5138- let binding = if new_binding. is_import ( ) && !new_binding. span . is_dummy ( ) {
5139- new_binding
5137+ let directive = match ( & new_binding. kind , & old_binding. kind ) {
5138+ ( NameBindingKind :: Import { directive, .. } , _) if !new_binding. span . is_dummy ( ) =>
5139+ Some ( ( directive, new_binding. span ) ) ,
5140+ ( _, NameBindingKind :: Import { directive, .. } ) if !old_binding. span . is_dummy ( ) =>
5141+ Some ( ( directive, old_binding. span ) ) ,
5142+ _ => None ,
5143+ } ;
5144+ if let Some ( ( directive, binding_span) ) = directive {
5145+ let suggested_name = if name. as_str ( ) . chars ( ) . next ( ) . unwrap ( ) . is_uppercase ( ) {
5146+ format ! ( "Other{}" , name)
51405147 } else {
5141- old_binding
5148+ format ! ( "other_{}" , name )
51425149 } ;
51435150
5144- let cm = self . session . source_map ( ) ;
5145- let rename_msg = "you can use `as` to change the binding name of the import" ;
5146-
5147- if let (
5148- Ok ( snippet) ,
5149- NameBindingKind :: Import { directive, ..} ,
5150- false ,
5151- false ,
5152- ) = (
5153- cm. span_to_snippet ( binding. span ) ,
5154- binding. kind . clone ( ) ,
5155- binding. span . is_dummy ( ) ,
5156- binding. span . ctxt ( ) . outer ( ) . expn_info ( ) . is_some ( ) ,
5157- ) {
5158- let suggested_name = if name. as_str ( ) . chars ( ) . next ( ) . unwrap ( ) . is_uppercase ( ) {
5159- format ! ( "Other{}" , name)
5160- } else {
5161- format ! ( "other_{}" , name)
5162- } ;
5151+ let mut suggestion = None ;
5152+ match directive. subclass {
5153+ ImportDirectiveSubclass :: SingleImport { type_ns_only : true , .. } =>
5154+ suggestion = Some ( format ! ( "self as {}" , suggested_name) ) ,
5155+ ImportDirectiveSubclass :: SingleImport { source, .. } => {
5156+ if let Some ( pos) = source. span . hi ( ) . 0 . checked_sub ( binding_span. lo ( ) . 0 )
5157+ . map ( |pos| pos as usize ) {
5158+ if let Ok ( snippet) = self . session . source_map ( )
5159+ . span_to_snippet ( binding_span) {
5160+ if pos <= snippet. len ( ) {
5161+ suggestion = Some ( format ! (
5162+ "{} as {}{}" ,
5163+ & snippet[ ..pos] ,
5164+ suggested_name,
5165+ if snippet. ends_with( ";" ) { ";" } else { "" }
5166+ ) )
5167+ }
5168+ }
5169+ }
5170+ }
5171+ ImportDirectiveSubclass :: ExternCrate { source, target, .. } =>
5172+ suggestion = Some ( format ! (
5173+ "extern crate {} as {};" ,
5174+ source. unwrap_or( target. name) ,
5175+ suggested_name,
5176+ ) ) ,
5177+ _ => unreachable ! ( ) ,
5178+ }
51635179
5180+ let rename_msg = "you can use `as` to change the binding name of the import" ;
5181+ if let Some ( suggestion) = suggestion {
51645182 err. span_suggestion_with_applicability (
5165- binding. span ,
5166- & rename_msg,
5167- match directive. subclass {
5168- ImportDirectiveSubclass :: SingleImport { type_ns_only : true , .. } =>
5169- format ! ( "self as {}" , suggested_name) ,
5170- ImportDirectiveSubclass :: SingleImport { source, .. } =>
5171- format ! (
5172- "{} as {}{}" ,
5173- & snippet[ ..( ( source. span. hi( ) . 0 - binding. span. lo( ) . 0 ) as usize ) ] ,
5174- suggested_name,
5175- if snippet. ends_with( ";" ) {
5176- ";"
5177- } else {
5178- ""
5179- }
5180- ) ,
5181- ImportDirectiveSubclass :: ExternCrate { source, target, .. } =>
5182- format ! (
5183- "extern crate {} as {};" ,
5184- source. unwrap_or( target. name) ,
5185- suggested_name,
5186- ) ,
5187- _ => unreachable ! ( ) ,
5188- } ,
5183+ binding_span,
5184+ rename_msg,
5185+ suggestion,
51895186 Applicability :: MaybeIncorrect ,
51905187 ) ;
51915188 } else {
5192- err. span_label ( binding . span , rename_msg) ;
5189+ err. span_label ( binding_span , rename_msg) ;
51935190 }
51945191 }
51955192
0 commit comments