about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2023-11-15 20:22:54 +0000
committerEsteban Küber <esteban@kuber.com.ar>2023-11-20 23:44:37 +0000
commit02bea16c08d6c0202f7cb1658280bd7cf97560d0 (patch)
tree1175d74d6879dfbb62b7c4ce09a81a5fd7e697a9
parentbb9d720a161221dac6ad7f0fb1cb5e94ab157ab2 (diff)
Rely in resolve and not on path name for `&str` -> `String` suggestion
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs103
1 files changed, 54 insertions, 49 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index 86846aa214a..1ffe8a9a26e 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -3105,60 +3105,65 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
                                 owned_sugg = true;
                             }
                             if let Some(ty) = lt_finder.found {
-                                if let TyKind::Path(None, path @ Path { segments, .. }) = &ty.kind
-                                    && segments.len() == 1
-                                {
-                                    if segments[0].ident.name == sym::str {
-                                        // Don't suggest `-> str`, suggest `-> String`.
-                                        sugg = vec![
-                                            (lt.span.with_hi(ty.span.hi()), "String".to_string()),
-                                        ];
-                                    } else {
-                                        // Check if the path being borrowed is likely to be owned.
-                                        let path: Vec<_> = Segment::from_path(path);
-                                        match self.resolve_path(&path, Some(TypeNS), None) {
-                                            PathResult::Module(
-                                                ModuleOrUniformRoot::Module(module),
-                                            ) => {
-                                                match module.res() {
-                                                    Some(Res::PrimTy(..)) => {}
-                                                    Some(Res::Def(
-                                                        DefKind::Struct
-                                                        | DefKind::Union
-                                                        | DefKind::Enum
-                                                        | DefKind::ForeignTy
-                                                        | DefKind::AssocTy
-                                                        | DefKind::OpaqueTy
-                                                        | DefKind::TyParam,
-                                                        _,
-                                                    )) => {}
-                                                    _ => { // Do not suggest in all other cases.
-                                                        owned_sugg = false;
-                                                    }
+                                if let TyKind::Path(None, path) = &ty.kind {
+                                    // Check if the path being borrowed is likely to be owned.
+                                    let path: Vec<_> = Segment::from_path(path);
+                                    match self.resolve_path(&path, Some(TypeNS), None) {
+                                        PathResult::Module(
+                                            ModuleOrUniformRoot::Module(module),
+                                        ) => {
+                                            match module.res() {
+                                                Some(Res::PrimTy(PrimTy::Str)) => {
+                                                    // Don't suggest `-> str`, suggest `-> String`.
+                                                    sugg = vec![(
+                                                        lt.span.with_hi(ty.span.hi()),
+                                                        "String".to_string(),
+                                                    )];
                                                 }
-                                            }
-                                            PathResult::NonModule(res) => {
-                                                match res.base_res() {
-                                                    Res::PrimTy(..) => {}
-                                                    Res::Def(
-                                                        DefKind::Struct
-                                                        | DefKind::Union
-                                                        | DefKind::Enum
-                                                        | DefKind::ForeignTy
-                                                        | DefKind::AssocTy
-                                                        | DefKind::OpaqueTy
-                                                        | DefKind::TyParam,
-                                                        _,
-                                                    ) => {}
-                                                    _ => { // Do not suggest in all other cases.
-                                                        owned_sugg = false;
-                                                    }
+                                                Some(Res::PrimTy(..)) => {}
+                                                Some(Res::Def(
+                                                    DefKind::Struct
+                                                    | DefKind::Union
+                                                    | DefKind::Enum
+                                                    | DefKind::ForeignTy
+                                                    | DefKind::AssocTy
+                                                    | DefKind::OpaqueTy
+                                                    | DefKind::TyParam,
+                                                    _,
+                                                )) => {}
+                                                _ => { // Do not suggest in all other cases.
+                                                    owned_sugg = false;
                                                 }
                                             }
-                                            _ => { // Do not suggest in all other cases.
-                                                owned_sugg = false;
+                                        }
+                                        PathResult::NonModule(res) => {
+                                            match res.base_res() {
+                                                Res::PrimTy(PrimTy::Str) => {
+                                                    // Don't suggest `-> str`, suggest `-> String`.
+                                                    sugg = vec![(
+                                                        lt.span.with_hi(ty.span.hi()),
+                                                        "String".to_string(),
+                                                    )];
+                                                }
+                                                Res::PrimTy(..) => {}
+                                                Res::Def(
+                                                    DefKind::Struct
+                                                    | DefKind::Union
+                                                    | DefKind::Enum
+                                                    | DefKind::ForeignTy
+                                                    | DefKind::AssocTy
+                                                    | DefKind::OpaqueTy
+                                                    | DefKind::TyParam,
+                                                    _,
+                                                ) => {}
+                                                _ => { // Do not suggest in all other cases.
+                                                    owned_sugg = false;
+                                                }
                                             }
                                         }
+                                        _ => { // Do not suggest in all other cases.
+                                            owned_sugg = false;
+                                        }
                                     }
                                 }
                                 if let TyKind::Slice(inner_ty) = &ty.kind {