@@ -2307,7 +2307,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23072307
23082308 self . mention_default_field_values ( source, ident, & mut err) ;
23092309
2310- let mut not_publicly_reexported = false ;
23112310 if let Some ( ( this_res, outer_ident) ) = outermost_res {
23122311 let mut import_suggestions = self . lookup_import_candidates (
23132312 outer_ident,
@@ -2332,7 +2331,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23322331 ) ;
23332332 // If we suggest importing a public re-export, don't point at the definition.
23342333 if point_to_def && ident. span != outer_ident. span {
2335- not_publicly_reexported = true ;
23362334 let label = errors:: OuterIdentIsNotPubliclyReexported {
23372335 span : outer_ident. span ,
23382336 outer_ident_descr : this_res. descr ( ) ,
@@ -2408,7 +2406,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24082406 let first_binding = decl;
24092407 let mut next_binding = Some ( decl) ;
24102408 let mut next_ident = ident;
2411- let mut path = vec ! [ ] ;
24122409 while let Some ( binding) = next_binding {
24132410 let name = next_ident;
24142411 next_binding = match binding. kind {
@@ -2428,18 +2425,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24282425 } ;
24292426
24302427 match binding. kind {
2431- DeclKind :: Import { import, .. } => {
2432- for segment in import . module_path . iter ( ) . skip ( 1 ) {
2433- // Don't include `{{root}}` in suggestions - it's an internal symbol
2434- // that should never be shown to users.
2435- if segment . ident . name != kw :: PathRoot {
2436- path . push ( segment . ident ) ;
2437- }
2438- }
2439- sugg_paths . push ( (
2440- path . iter ( ) . cloned ( ) . chain ( std :: iter :: once ( ident ) ) . collect :: < Vec < _ > > ( ) ,
2441- true , // re-export
2442- ) ) ;
2428+ DeclKind :: Import { source_decl , import, .. } => {
2429+ // Don't include `{{root}}` in suggestions - it's an internal symbol
2430+ // that should never be shown to users.
2431+ let path = import
2432+ . module_path
2433+ . iter ( )
2434+ . filter ( |seg| seg . ident . name != kw :: PathRoot )
2435+ . map ( |seg| seg . ident . clone ( ) )
2436+ . chain ( std :: iter :: once ( ident ) )
2437+ . collect :: < Vec < _ > > ( ) ;
2438+ let through_reexport = ! matches ! ( source_decl . kind , DeclKind :: Def ( _ ) ) ;
2439+ sugg_paths . push ( ( path , through_reexport ) ) ;
24432440 }
24442441 DeclKind :: Def ( _) => { }
24452442 }
@@ -2472,25 +2469,34 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24722469 } ;
24732470 err. subdiagnostic ( note) ;
24742471 }
2475- // We prioritize shorter paths, non-core imports and direct imports over the alternatives.
2476- sugg_paths. sort_by_key ( |( p, reexport) | ( p. len ( ) , p[ 0 ] . name == sym:: core, * reexport) ) ;
2477- for ( sugg, reexport) in sugg_paths {
2478- if not_publicly_reexported {
2472+ // The suggestion replaces `dedup_span` with a path reaching the failing ident.
2473+ // That's valid only when
2474+ // 1) the failing ident is the imported leaf, otherwise `as` renames and trailing segments
2475+ // get dropped, and
2476+ // 2) the use isn't nested, otherwise `dedup_span` is one ident in `{...}`.
2477+ //
2478+ // See issue #156060.
2479+ let can_replace_use =
2480+ !single_nested && !outermost_res. is_some_and ( |( _, outer) | outer. span != ident. span ) ;
2481+ if can_replace_use {
2482+ // We prioritize shorter paths, non-core imports and direct imports over the
2483+ // alternatives.
2484+ sugg_paths. sort_by_key ( |( p, reexport) | ( p. len ( ) , p[ 0 ] . name == sym:: core, * reexport) ) ;
2485+ for ( sugg, reexport) in sugg_paths {
2486+ if sugg. len ( ) <= 1 {
2487+ // A single path segment suggestion is wrong. This happens on circular
2488+ // imports. `tests/ui/imports/issue-55884-2.rs`
2489+ continue ;
2490+ }
2491+ let path = join_path_idents ( sugg) ;
2492+ let sugg = if reexport {
2493+ errors:: ImportIdent :: ThroughReExport { span : dedup_span, ident, path }
2494+ } else {
2495+ errors:: ImportIdent :: Directly { span : dedup_span, ident, path }
2496+ } ;
2497+ err. subdiagnostic ( sugg) ;
24792498 break ;
24802499 }
2481- if sugg. len ( ) <= 1 {
2482- // A single path segment suggestion is wrong. This happens on circular imports.
2483- // `tests/ui/imports/issue-55884-2.rs`
2484- continue ;
2485- }
2486- let path = join_path_idents ( sugg) ;
2487- let sugg = if reexport {
2488- errors:: ImportIdent :: ThroughReExport { span : dedup_span, ident, path }
2489- } else {
2490- errors:: ImportIdent :: Directly { span : dedup_span, ident, path }
2491- } ;
2492- err. subdiagnostic ( sugg) ;
2493- break ;
24942500 }
24952501
24962502 err. emit ( ) ;
0 commit comments