Skip to content

Commit

Permalink
Improve help messages for E0425
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 2, 2016
1 parent d5759a3 commit f4e6f3c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
22 changes: 15 additions & 7 deletions src/librustc_resolve/lib.rs
Expand Up @@ -153,7 +153,8 @@ enum ResolutionError<'a> {
message: &'a str,
context: UnresolvedNameContext<'a>,
is_static_method: bool,
is_field: bool
is_field: bool,
def: Def,
},
/// error E0426: use of undeclared label
UndeclaredLabel(&'a str),
Expand Down Expand Up @@ -413,7 +414,7 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
argument is missing?")
}
ResolutionError::UnresolvedName { path, message: msg, context, is_static_method,
is_field } => {
is_field, def } => {
let mut err = struct_span_err!(resolver.session,
span,
E0425,
Expand All @@ -430,19 +431,20 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
UnresolvedNameContext::PathIsMod(parent) => {
err.help(&match parent.map(|parent| &parent.node) {
Some(&ExprKind::Field(_, ident)) => {
format!("To reference an item from the `{module}` module, \
format!("to reference an item from the `{module}` module, \
use `{module}::{ident}`",
module = path,
ident = ident.node)
}
Some(&ExprKind::MethodCall(ident, _, _)) => {
format!("To call a function from the `{module}` module, \
format!("to call a function from the `{module}` module, \
use `{module}::{ident}(..)`",
module = path,
ident = ident.node)
}
_ => {
format!("Module `{module}` cannot be used as an expression",
format!("{def} `{module}` cannot be used as an expression",
def = def.kind_name(),
module = path)
}
});
Expand Down Expand Up @@ -1113,7 +1115,8 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
message: "",
context: UnresolvedNameContext::Other,
is_static_method: false,
is_field: false
is_field: false,
def: Def::Err,
};
resolve_error(self, path.span, error);
Def::Err
Expand Down Expand Up @@ -3064,6 +3067,7 @@ impl<'a> Resolver<'a> {
};

let mut context = UnresolvedNameContext::Other;
let mut def = Def::Err;
if !msg.is_empty() {
msg = format!(". Did you mean {}?", msg);
} else {
Expand All @@ -3076,7 +3080,10 @@ impl<'a> Resolver<'a> {
match self.resolve_module_path(&name_path[..],
UseLexicalScope,
expr.span) {
Success(_) => {
Success(e) => {
if let Some(def_type) = e.def {
def = def_type;
}
context = UnresolvedNameContext::PathIsMod(parent);
},
_ => {},
Expand All @@ -3091,6 +3098,7 @@ impl<'a> Resolver<'a> {
context: context,
is_static_method: method_scope && is_static,
is_field: is_field,
def: def,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-2356.rs
Expand Up @@ -88,6 +88,6 @@ impl cat {
fn main() {
self += 1;
//~^ ERROR: unresolved name `self`
//~| HELP: Module
//~| HELP: module `self`
// it's a bug if this suggests a missing `self` as we're not in a method
}
26 changes: 26 additions & 0 deletions src/test/compile-fail/issue-33876.rs
@@ -0,0 +1,26 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(reflect_marker)]

use std::marker::Reflect;
use std::any::Any;

struct Foo;

trait Bar {}

impl Bar for Foo {}

fn main() {
let any: &Any = &Bar; //~ ERROR E0425
//~| HELP trait `Bar`
if any.is::<u32>() { println!("u32"); }
}
18 changes: 9 additions & 9 deletions src/test/compile-fail/suggest-path-instead-of-mod-dot-item.rs
Expand Up @@ -26,51 +26,51 @@ pub mod a {
fn h1() -> i32 {
a.I
//~^ ERROR E0425
//~| HELP To reference an item from the `a` module, use `a::I`
//~| HELP to reference an item from the `a` module, use `a::I`
}

fn h2() -> i32 {
a.g()
//~^ ERROR E0425
//~| HELP To call a function from the `a` module, use `a::g(..)`
//~| HELP to call a function from the `a` module, use `a::g(..)`
}

fn h3() -> i32 {
a.b.J
//~^ ERROR E0425
//~| HELP To reference an item from the `a` module, use `a::b`
//~| HELP to reference an item from the `a` module, use `a::b`
}

fn h4() -> i32 {
a::b.J
//~^ ERROR E0425
//~| HELP To reference an item from the `a::b` module, use `a::b::J`
//~| HELP to reference an item from the `a::b` module, use `a::b::J`
}

fn h5() {
a.b.f();
//~^ ERROR E0425
//~| HELP To reference an item from the `a` module, use `a::b`
//~| HELP to reference an item from the `a` module, use `a::b`
let v = Vec::new();
v.push(a::b);
//~^ ERROR E0425
//~| HELP Module `a::b` cannot be used as an expression
//~| HELP module `a::b` cannot be used as an expression
}

fn h6() -> i32 {
a::b.f()
//~^ ERROR E0425
//~| HELP To call a function from the `a::b` module, use `a::b::f(..)`
//~| HELP to call a function from the `a::b` module, use `a::b::f(..)`
}

fn h7() {
a::b
//~^ ERROR E0425
//~| HELP Module `a::b` cannot be used as an expression
//~| HELP module `a::b` cannot be used as an expression
}

fn h8() -> i32 {
a::b()
//~^ ERROR E0425
//~| HELP Module `a::b` cannot be used as an expression
//~| HELP module `a::b` cannot be used as an expression
}

0 comments on commit f4e6f3c

Please sign in to comment.