Auto merge of #33878 - GuillaumeGomez:improve_helps, r=jonathandturner
Improve help messages for E0425 Fixes #33876. r? @Manishearth cc @steveklabnik cc @jonathandturner
This commit is contained in:
commit
0646e8ae6e
@ -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),
|
||||
@ -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,
|
||||
@ -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)
|
||||
}
|
||||
});
|
||||
@ -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
|
||||
@ -3063,6 +3066,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 {
|
||||
@ -3075,7 +3079,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);
|
||||
},
|
||||
_ => {},
|
||||
@ -3090,6 +3097,7 @@ impl<'a> Resolver<'a> {
|
||||
context: context,
|
||||
is_static_method: method_scope && is_static,
|
||||
is_field: is_field,
|
||||
def: def,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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
src/test/compile-fail/issue-33876.rs
Normal file
26
src/test/compile-fail/issue-33876.rs
Normal file
@ -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"); }
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user