提交 86f5275e 编写于 作者: G Guillaume Gomez 提交者: Felix S. Klock II

Add note when item accessed from module via `m.i` rather than `m::i`.

上级 ce132752
......@@ -179,7 +179,7 @@ pub enum ResolutionError<'a> {
/// error E0424: `self` is not available in a static method
SelfNotAvailableInStaticMethod,
/// error E0425: unresolved name
UnresolvedName(&'a str, &'a str),
UnresolvedName(&'a str, &'a str, UnresolvedNameContext),
/// error E0426: use of undeclared label
UndeclaredLabel(&'a str),
/// error E0427: cannot use `ref` binding mode with ...
......@@ -202,6 +202,12 @@ pub enum ResolutionError<'a> {
AttemptToUseNonConstantValueInConstant,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum UnresolvedNameContext {
PathIsMod(ast::NodeId),
Other,
}
fn resolve_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
span: syntax::codemap::Span,
resolution_error: ResolutionError<'b>) {
......@@ -402,13 +408,46 @@ fn resolve_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
"`self` is not available in a static method. Maybe a `self` argument is \
missing?");
}
ResolutionError::UnresolvedName(path, name) => {
ResolutionError::UnresolvedName(path, msg, context) => {
span_err!(resolver.session,
span,
E0425,
"unresolved name `{}`{}",
path,
name);
msg);
match context {
UnresolvedNameContext::Other => {} // no help available
UnresolvedNameContext::PathIsMod(id) => {
let mut help_msg = String::new();
let parent_id = resolver.ast_map.get_parent_node(id);
if let Some(hir_map::Node::NodeExpr(e)) = resolver.ast_map.find(parent_id) {
match e.node {
ExprField(_, ident) => {
help_msg = format!("To reference an item from the \
`{module}` module, use \
`{module}::{ident}`",
module = &*path,
ident = ident.node);
}
ExprMethodCall(ident, _, _) => {
help_msg = format!("To call a function from the \
`{module}` module, use \
`{module}::{ident}(..)`",
module = &*path,
ident = ident.node);
}
_ => {} // no help available
}
}
if !help_msg.is_empty() {
resolver.session.fileline_help(span, &help_msg);
}
}
}
}
ResolutionError::UndeclaredLabel(name) => {
span_err!(resolver.session,
......@@ -3509,13 +3548,33 @@ fn resolve_expr(&mut self, expr: &Expr) {
format!("to call `{}::{}`", path_str, path_name),
};
let mut context = UnresolvedNameContext::Other;
if !msg.is_empty() {
msg = format!(". Did you mean {}?", msg)
msg = format!(". Did you mean {}?", msg);
} else {
// we check if this a module and if so, we display a help
// message
let name_path = path.segments.iter()
.map(|seg| seg.identifier.name)
.collect::<Vec<_>>();
let current_module = self.current_module.clone();
match self.resolve_module_path(current_module,
&name_path[..],
UseLexicalScope,
expr.span,
PathSearch) {
Success(_) => {
context = UnresolvedNameContext::PathIsMod(expr.id);
},
_ => {},
};
}
resolve_error(self,
expr.span,
ResolutionError::UnresolvedName(&*path_name, &*msg));
ResolutionError::UnresolvedName(
&*path_name, &*msg, context));
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册