|
| 1 | +// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use {CrateLint, PathResult}; |
| 12 | + |
| 13 | +use syntax::ast::Ident; |
| 14 | +use syntax::symbol::keywords; |
| 15 | +use syntax_pos::Span; |
| 16 | + |
| 17 | +use resolve_imports::ImportResolver; |
| 18 | + |
| 19 | +impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> { |
| 20 | + /// Add suggestions for a path that cannot be resolved. |
| 21 | + pub(crate) fn make_path_suggestion( |
| 22 | + &mut self, |
| 23 | + span: Span, |
| 24 | + path: Vec<Ident> |
| 25 | + ) -> Option<Vec<Ident>> { |
| 26 | + debug!("make_path_suggestion: span={:?} path={:?}", span, path); |
| 27 | + // If we don't have a path to suggest changes to, then return. |
| 28 | + if path.is_empty() { |
| 29 | + return None; |
| 30 | + } |
| 31 | + |
| 32 | + // Check whether a ident is a path segment that is not root. |
| 33 | + let is_special = |ident: Ident| ident.is_path_segment_keyword() && |
| 34 | + ident.name != keywords::CrateRoot.name(); |
| 35 | + |
| 36 | + match (path.get(0), path.get(1)) { |
| 37 | + // Make suggestions that require at least two non-special path segments. |
| 38 | + (Some(fst), Some(snd)) if !is_special(*fst) && !is_special(*snd) => { |
| 39 | + debug!("make_path_suggestion: fst={:?} snd={:?}", fst, snd); |
| 40 | + |
| 41 | + self.make_missing_self_suggestion(span, path.clone()) |
| 42 | + .or_else(|| self.make_missing_crate_suggestion(span, path.clone())) |
| 43 | + .or_else(|| self.make_missing_super_suggestion(span, path.clone())) |
| 44 | + .or_else(|| self.make_external_crate_suggestion(span, path.clone())) |
| 45 | + }, |
| 46 | + _ => None, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Suggest a missing `self::` if that resolves to an correct module. |
| 51 | + /// |
| 52 | + /// ``` |
| 53 | + /// | |
| 54 | + /// LL | use foo::Bar; |
| 55 | + /// | ^^^ Did you mean `self::foo`? |
| 56 | + /// ``` |
| 57 | + fn make_missing_self_suggestion( |
| 58 | + &mut self, |
| 59 | + span: Span, |
| 60 | + mut path: Vec<Ident> |
| 61 | + ) -> Option<Vec<Ident>> { |
| 62 | + // Replace first ident with `self` and check if that is valid. |
| 63 | + path[0].name = keywords::SelfValue.name(); |
| 64 | + let result = self.resolve_path(None, &path, None, false, span, CrateLint::No); |
| 65 | + debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result); |
| 66 | + if let PathResult::Module(..) = result { |
| 67 | + Some(path) |
| 68 | + } else { |
| 69 | + None |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// Suggest a missing `crate::` if that resolves to an correct module. |
| 74 | + /// |
| 75 | + /// ``` |
| 76 | + /// | |
| 77 | + /// LL | use foo::Bar; |
| 78 | + /// | ^^^ Did you mean `crate::foo`? |
| 79 | + /// ``` |
| 80 | + fn make_missing_crate_suggestion( |
| 81 | + &mut self, |
| 82 | + span: Span, |
| 83 | + mut path: Vec<Ident> |
| 84 | + ) -> Option<Vec<Ident>> { |
| 85 | + // Replace first ident with `crate` and check if that is valid. |
| 86 | + path[0].name = keywords::Crate.name(); |
| 87 | + let result = self.resolve_path(None, &path, None, false, span, CrateLint::No); |
| 88 | + debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result); |
| 89 | + if let PathResult::Module(..) = result { |
| 90 | + Some(path) |
| 91 | + } else { |
| 92 | + None |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Suggest a missing `super::` if that resolves to an correct module. |
| 97 | + /// |
| 98 | + /// ``` |
| 99 | + /// | |
| 100 | + /// LL | use foo::Bar; |
| 101 | + /// | ^^^ Did you mean `super::foo`? |
| 102 | + /// ``` |
| 103 | + fn make_missing_super_suggestion( |
| 104 | + &mut self, |
| 105 | + span: Span, |
| 106 | + mut path: Vec<Ident> |
| 107 | + ) -> Option<Vec<Ident>> { |
| 108 | + // Replace first ident with `crate` and check if that is valid. |
| 109 | + path[0].name = keywords::Super.name(); |
| 110 | + let result = self.resolve_path(None, &path, None, false, span, CrateLint::No); |
| 111 | + debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result); |
| 112 | + if let PathResult::Module(..) = result { |
| 113 | + Some(path) |
| 114 | + } else { |
| 115 | + None |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// Suggest a missing external crate name if that resolves to an correct module. |
| 120 | + /// |
| 121 | + /// ``` |
| 122 | + /// | |
| 123 | + /// LL | use foobar::Baz; |
| 124 | + /// | ^^^^^^ Did you mean `baz::foobar`? |
| 125 | + /// ``` |
| 126 | + /// |
| 127 | + /// Used when importing a submodule of an external crate but missing that crate's |
| 128 | + /// name as the first part of path. |
| 129 | + fn make_external_crate_suggestion( |
| 130 | + &mut self, |
| 131 | + span: Span, |
| 132 | + mut path: Vec<Ident> |
| 133 | + ) -> Option<Vec<Ident>> { |
| 134 | + // Need to clone else we can't call `resolve_path` without a borrow error. |
| 135 | + let external_crate_names = self.extern_prelude.clone(); |
| 136 | + |
| 137 | + // Insert a new path segment that we can replace. |
| 138 | + let new_path_segment = path[0].clone(); |
| 139 | + path.insert(1, new_path_segment); |
| 140 | + |
| 141 | + for name in &external_crate_names { |
| 142 | + let ident = Ident::with_empty_ctxt(*name); |
| 143 | + // Calling `maybe_process_path_extern` ensures that we're only running `resolve_path` |
| 144 | + // on a crate name that won't ICE. |
| 145 | + if let Some(_) = self.crate_loader.maybe_process_path_extern(*name, ident.span) { |
| 146 | + // Replace the first after root (a placeholder we inserted) with a crate name |
| 147 | + // and check if that is valid. |
| 148 | + path[1].name = *name; |
| 149 | + let result = self.resolve_path(None, &path, None, false, span, CrateLint::No); |
| 150 | + debug!("make_external_crate_suggestion: name={:?} path={:?} result={:?}", |
| 151 | + name, path, result); |
| 152 | + if let PathResult::Module(..) = result { |
| 153 | + return Some(path) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Remove our placeholder segment. |
| 159 | + path.remove(1); |
| 160 | + None |
| 161 | + } |
| 162 | +} |
0 commit comments