diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 41cc546281649..14930a150ed69 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -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 @@ -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 { @@ -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); }, _ => {}, @@ -3091,6 +3098,7 @@ impl<'a> Resolver<'a> { context: context, is_static_method: method_scope && is_static, is_field: is_field, + def: def, }); } } diff --git a/src/test/compile-fail/issue-2356.rs b/src/test/compile-fail/issue-2356.rs index 5e816bcfa61e4..d7ec1ed67397f 100644 --- a/src/test/compile-fail/issue-2356.rs +++ b/src/test/compile-fail/issue-2356.rs @@ -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 } diff --git a/src/test/compile-fail/issue-33876.rs b/src/test/compile-fail/issue-33876.rs new file mode 100644 index 0000000000000..d95890730a0f2 --- /dev/null +++ b/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 or the MIT license +// , 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::() { println!("u32"); } +} diff --git a/src/test/compile-fail/suggest-path-instead-of-mod-dot-item.rs b/src/test/compile-fail/suggest-path-instead-of-mod-dot-item.rs index 412c90fd214c1..4a816ea757276 100644 --- a/src/test/compile-fail/suggest-path-instead-of-mod-dot-item.rs +++ b/src/test/compile-fail/suggest-path-instead-of-mod-dot-item.rs @@ -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 }