Skip to content

Commit 0646e8a

Browse files
committed
Auto merge of #33878 - GuillaumeGomez:improve_helps, r=jonathandturner
Improve help messages for E0425 Fixes #33876. r? @Manishearth cc @steveklabnik cc @jonathandturner
2 parents 915b003 + f4e6f3c commit 0646e8a

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

src/librustc_resolve/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ enum ResolutionError<'a> {
153153
message: &'a str,
154154
context: UnresolvedNameContext<'a>,
155155
is_static_method: bool,
156-
is_field: bool
156+
is_field: bool,
157+
def: Def,
157158
},
158159
/// error E0426: use of undeclared label
159160
UndeclaredLabel(&'a str),
@@ -413,7 +414,7 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
413414
argument is missing?")
414415
}
415416
ResolutionError::UnresolvedName { path, message: msg, context, is_static_method,
416-
is_field } => {
417+
is_field, def } => {
417418
let mut err = struct_span_err!(resolver.session,
418419
span,
419420
E0425,
@@ -430,19 +431,20 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
430431
UnresolvedNameContext::PathIsMod(parent) => {
431432
err.help(&match parent.map(|parent| &parent.node) {
432433
Some(&ExprKind::Field(_, ident)) => {
433-
format!("To reference an item from the `{module}` module, \
434+
format!("to reference an item from the `{module}` module, \
434435
use `{module}::{ident}`",
435436
module = path,
436437
ident = ident.node)
437438
}
438439
Some(&ExprKind::MethodCall(ident, _, _)) => {
439-
format!("To call a function from the `{module}` module, \
440+
format!("to call a function from the `{module}` module, \
440441
use `{module}::{ident}(..)`",
441442
module = path,
442443
ident = ident.node)
443444
}
444445
_ => {
445-
format!("Module `{module}` cannot be used as an expression",
446+
format!("{def} `{module}` cannot be used as an expression",
447+
def = def.kind_name(),
446448
module = path)
447449
}
448450
});
@@ -1113,7 +1115,8 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
11131115
message: "",
11141116
context: UnresolvedNameContext::Other,
11151117
is_static_method: false,
1116-
is_field: false
1118+
is_field: false,
1119+
def: Def::Err,
11171120
};
11181121
resolve_error(self, path.span, error);
11191122
Def::Err
@@ -3063,6 +3066,7 @@ impl<'a> Resolver<'a> {
30633066
};
30643067

30653068
let mut context = UnresolvedNameContext::Other;
3069+
let mut def = Def::Err;
30663070
if !msg.is_empty() {
30673071
msg = format!(". Did you mean {}?", msg);
30683072
} else {
@@ -3075,7 +3079,10 @@ impl<'a> Resolver<'a> {
30753079
match self.resolve_module_path(&name_path[..],
30763080
UseLexicalScope,
30773081
expr.span) {
3078-
Success(_) => {
3082+
Success(e) => {
3083+
if let Some(def_type) = e.def {
3084+
def = def_type;
3085+
}
30793086
context = UnresolvedNameContext::PathIsMod(parent);
30803087
},
30813088
_ => {},
@@ -3090,6 +3097,7 @@ impl<'a> Resolver<'a> {
30903097
context: context,
30913098
is_static_method: method_scope && is_static,
30923099
is_field: is_field,
3100+
def: def,
30933101
});
30943102
}
30953103
}

src/test/compile-fail/issue-2356.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ impl cat {
8888
fn main() {
8989
self += 1;
9090
//~^ ERROR: unresolved name `self`
91-
//~| HELP: Module
91+
//~| HELP: module `self`
9292
// it's a bug if this suggests a missing `self` as we're not in a method
9393
}

src/test/compile-fail/issue-33876.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 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+
#![feature(reflect_marker)]
12+
13+
use std::marker::Reflect;
14+
use std::any::Any;
15+
16+
struct Foo;
17+
18+
trait Bar {}
19+
20+
impl Bar for Foo {}
21+
22+
fn main() {
23+
let any: &Any = &Bar; //~ ERROR E0425
24+
//~| HELP trait `Bar`
25+
if any.is::<u32>() { println!("u32"); }
26+
}

src/test/compile-fail/suggest-path-instead-of-mod-dot-item.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,51 @@ pub mod a {
2626
fn h1() -> i32 {
2727
a.I
2828
//~^ ERROR E0425
29-
//~| HELP To reference an item from the `a` module, use `a::I`
29+
//~| HELP to reference an item from the `a` module, use `a::I`
3030
}
3131

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

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

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

5050
fn h5() {
5151
a.b.f();
5252
//~^ ERROR E0425
53-
//~| HELP To reference an item from the `a` module, use `a::b`
53+
//~| HELP to reference an item from the `a` module, use `a::b`
5454
let v = Vec::new();
5555
v.push(a::b);
5656
//~^ ERROR E0425
57-
//~| HELP Module `a::b` cannot be used as an expression
57+
//~| HELP module `a::b` cannot be used as an expression
5858
}
5959

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

6666
fn h7() {
6767
a::b
6868
//~^ ERROR E0425
69-
//~| HELP Module `a::b` cannot be used as an expression
69+
//~| HELP module `a::b` cannot be used as an expression
7070
}
7171

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

0 commit comments

Comments
 (0)