Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: Describe crate root modules in DefKind::Mod as "crate" #63250

Merged
merged 2 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/librustc/hir/def.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hir::def_id::DefId;
use crate::hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::util::nodemap::DefIdMap;
use syntax::ast;
use syntax::ext::base::MacroKind;
Expand Down Expand Up @@ -81,9 +81,11 @@ pub enum DefKind {
}

impl DefKind {
pub fn descr(self) -> &'static str {
pub fn descr(self, def_id: DefId) -> &'static str {
match self {
DefKind::Fn => "function",
DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE =>
"crate",
DefKind::Mod => "module",
DefKind::Static => "static",
DefKind::Enum => "enum",
Expand Down Expand Up @@ -366,7 +368,7 @@ impl<Id> Res<Id> {
/// A human readable name for the res kind ("function", "module", etc.).
pub fn descr(&self) -> &'static str {
match *self {
Res::Def(kind, _) => kind.descr(),
Res::Def(kind, def_id) => kind.descr(def_id),
Res::SelfCtor(..) => "self constructor",
Res::PrimTy(..) => "builtin type",
Res::Local(..) => "local variable",
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
hir::QPath::Resolved(_, ref path) => path.to_string(),
hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(),
};
let msg = format!("{} `{}` is private", kind.descr(), name);
let msg = format!("{} `{}` is private", kind.descr(def_id), name);
self.tcx.sess.span_err(span, &msg);
return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,12 @@ impl<'a> Resolver<'a> {
err
}
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
let shadows_what = binding.descr();
let res = binding.res();
let shadows_what = res.descr();
let mut err = struct_span_err!(self.session, span, E0530, "{}s cannot shadow {}s",
what_binding, shadows_what);
err.span_label(span, format!("cannot be named the same as {} {}",
binding.article(), shadows_what));
res.article(), shadows_what));
let participle = if binding.is_import() { "imported" } else { "defined" };
let msg = format!("the {} `{}` is {} here", shadows_what, name, participle);
err.span_label(binding.span, msg);
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ impl<'a> LateResolutionVisitor<'a, '_> {
let mod_prefix = match self.resolve_path(
mod_path, Some(TypeNS), false, span, CrateLint::No
) {
PathResult::Module(ModuleOrUniformRoot::Module(module)) =>
module.def_kind(),
PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
_ => None,
}.map_or(String::new(), |kind| format!("{} ", kind.descr()));
}.map_or(String::new(), |res| format!("{} ", res.descr()));
(mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)))
};
(format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str),
Expand Down
31 changes: 9 additions & 22 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,6 @@ impl<'a> ModuleData<'a> {
}
}

fn def_kind(&self) -> Option<DefKind> {
match self.kind {
ModuleKind::Def(kind, ..) => Some(kind),
_ => None,
}
}

fn def_id(&self) -> Option<DefId> {
match self.kind {
ModuleKind::Def(_, def_id, _) => Some(def_id),
Expand Down Expand Up @@ -745,14 +738,6 @@ impl<'a> NameBinding<'a> {
self.res().macro_kind()
}

fn descr(&self) -> &'static str {
if self.is_extern_crate() { "extern crate" } else { self.res().descr() }
}

fn article(&self) -> &'static str {
if self.is_extern_crate() { "an" } else { self.res().article() }
}

// Suppose that we resolved macro invocation with `invoc_parent_expansion` to binding `binding`
// at some expansion round `max(invoc, binding)` when they both emerged from macros.
// Then this function returns `true` if `self` may emerge from a macro *after* that
Expand Down Expand Up @@ -2200,6 +2185,7 @@ impl<'a> Resolver<'a> {
}

fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
let res = b.res();
if b.span.is_dummy() {
let add_built_in = match b.res() {
// These already contain the "built-in" prefix or look bad with it.
Expand All @@ -2217,13 +2203,13 @@ impl<'a> Resolver<'a> {
("", "")
};

let article = if built_in.is_empty() { b.article() } else { "a" };
let article = if built_in.is_empty() { res.article() } else { "a" };
format!("{a}{built_in} {thing}{from}",
a = article, thing = b.descr(), built_in = built_in, from = from)
a = article, thing = res.descr(), built_in = built_in, from = from)
} else {
let introduced = if b.is_import() { "imported" } else { "defined" };
format!("the {thing} {introduced} here",
thing = b.descr(), introduced = introduced)
thing = res.descr(), introduced = introduced)
}
}

Expand All @@ -2246,6 +2232,7 @@ impl<'a> Resolver<'a> {
let note_msg = format!("`{ident}` could{also} refer to {what}",
ident = ident, also = also, what = what);

let thing = b.res().descr();
let mut help_msgs = Vec::new();
if b.is_glob_import() && (kind == AmbiguityKind::GlobVsGlob ||
kind == AmbiguityKind::GlobVsExpanded ||
Expand All @@ -2257,18 +2244,18 @@ impl<'a> Resolver<'a> {
if b.is_extern_crate() && ident.span.rust_2018() {
help_msgs.push(format!(
"use `::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr(),
ident = ident, thing = thing,
))
}
if misc == AmbiguityErrorMisc::SuggestCrate {
help_msgs.push(format!(
"use `crate::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr(),
ident = ident, thing = thing,
))
} else if misc == AmbiguityErrorMisc::SuggestSelf {
help_msgs.push(format!(
"use `self::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr(),
ident = ident, thing = thing,
))
}

Expand Down Expand Up @@ -2310,7 +2297,7 @@ impl<'a> Resolver<'a> {
ident.span,
E0603,
"{} `{}` is private",
binding.descr(),
binding.res().descr(),
ident.name,
);
// FIXME: use the ctor's `def_id` to check wether any of the fields is not visible
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {

let kind = DefKind::AssocTy;
if !item.vis.is_accessible_from(def_scope, tcx) {
let msg = format!("{} `{}` is private", kind.descr(), assoc_ident);
let msg = format!("{} `{}` is private", kind.descr(item.def_id), assoc_ident);
tcx.sess.span_err(span, &msg);
}
tcx.check_stability(item.def_id, Some(hir_ref_id), span);
Expand All @@ -1722,7 +1722,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {

let mut could_refer_to = |kind: DefKind, def_id, also| {
let note_msg = format!("`{}` could{} refer to {} defined here",
assoc_ident, also, kind.descr());
assoc_ident, also, kind.descr(def_id));
err.span_note(tcx.def_span(def_id), &note_msg);
};
could_refer_to(DefKind::Variant, variant_def_id, "");
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&format!(
"there is {} {} with a similar name",
def_kind.article(),
def_kind.descr(),
def_kind.descr(lev_candidate.def_id),
),
lev_candidate.ident.to_string(),
Applicability::MaybeIncorrect,
Expand All @@ -543,9 +543,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.emit();
}

MethodError::PrivateMatch(kind, _, out_of_scope_traits) => {
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
let mut err = struct_span_err!(self.tcx.sess, span, E0624,
"{} `{}` is private", kind.descr(), item_name);
"{} `{}` is private", kind.descr(def_id), item_name);
self.suggest_valid_traits(&mut err, out_of_scope_traits);
err.emit();
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/extern/extern-crate-visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ mod foo {
}

// Check that private crates can be used from outside their modules, albeit with warnings
use foo::core::cell; //~ ERROR extern crate `core` is private
use foo::core::cell; //~ ERROR crate `core` is private

fn f() {
foo::core::cell::Cell::new(0); //~ ERROR extern crate `core` is private
foo::core::cell::Cell::new(0); //~ ERROR crate `core` is private

use foo::*;
mod core {} // Check that private crates are not glob imported
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/extern/extern-crate-visibility.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0603]: extern crate `core` is private
error[E0603]: crate `core` is private
--> $DIR/extern-crate-visibility.rs:6:10
|
LL | use foo::core::cell;
| ^^^^

error[E0603]: extern crate `core` is private
error[E0603]: crate `core` is private
--> $DIR/extern-crate-visibility.rs:9:10
|
LL | foo::core::cell::Cell::new(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LL | Vec::panic!();
| ^^^ ambiguous name
|
= note: `Vec` could refer to a struct from prelude
note: `Vec` could also refer to the extern crate imported here
note: `Vec` could also refer to the crate imported here
--> $DIR/extern-prelude-extern-crate-restricted-shadowing.rs:5:9
|
LL | extern crate std as Vec;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/imports/glob-conflict-cross-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
extern crate glob_conflict;

fn main() {
glob_conflict::f(); //~ ERROR cannot find function `f` in module `glob_conflict`
glob_conflict::f(); //~ ERROR cannot find function `f` in crate `glob_conflict`
glob_conflict::glob::f(); //~ ERROR cannot find function `f` in module `glob_conflict::glob`
}
2 changes: 1 addition & 1 deletion src/test/ui/imports/glob-conflict-cross-crate.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0425]: cannot find function `f` in module `glob_conflict`
error[E0425]: cannot find function `f` in crate `glob_conflict`
--> $DIR/glob-conflict-cross-crate.rs:6:20
|
LL | glob_conflict::f();
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/imports/issue-56125.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ error[E0659]: `issue_56125` is ambiguous (name vs any other name during import r
LL | use issue_56125::last_segment::*;
| ^^^^^^^^^^^ ambiguous name
|
= note: `issue_56125` could refer to an extern crate passed with `--extern`
= help: use `::issue_56125` to refer to this extern crate unambiguously
= note: `issue_56125` could refer to a crate passed with `--extern`
= help: use `::issue_56125` to refer to this crate unambiguously
note: `issue_56125` could also refer to the module imported here
--> $DIR/issue-56125.rs:6:9
|
Expand All @@ -25,8 +25,8 @@ error[E0659]: `issue_56125` is ambiguous (name vs any other name during import r
LL | use issue_56125::non_last_segment::non_last_segment::*;
| ^^^^^^^^^^^ ambiguous name
|
= note: `issue_56125` could refer to an extern crate passed with `--extern`
= help: use `::issue_56125` to refer to this extern crate unambiguously
= note: `issue_56125` could refer to a crate passed with `--extern`
= help: use `::issue_56125` to refer to this crate unambiguously
note: `issue_56125` could also refer to the module imported here
--> $DIR/issue-56125.rs:11:9
|
Expand All @@ -40,8 +40,8 @@ error[E0659]: `issue_56125` is ambiguous (name vs any other name during import r
LL | use issue_56125::*;
| ^^^^^^^^^^^ ambiguous name
|
= note: `issue_56125` could refer to an extern crate passed with `--extern`
= help: use `::issue_56125` to refer to this extern crate unambiguously
= note: `issue_56125` could refer to a crate passed with `--extern`
= help: use `::issue_56125` to refer to this crate unambiguously
note: `issue_56125` could also refer to the module imported here
--> $DIR/issue-56125.rs:18:9
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/imports/issue-57539.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0659]: `core` is ambiguous (name vs any other name during import resoluti
LL | use core;
| ^^^^ ambiguous name
|
= note: `core` could refer to a built-in extern crate
= help: use `::core` to refer to this extern crate unambiguously
= note: `core` could refer to a built-in crate
= help: use `::core` to refer to this crate unambiguously
note: `core` could also refer to the module imported here
--> $DIR/issue-57539.rs:5:9
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macros/macro-path-prelude-shadowing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0659]: `std` is ambiguous (glob import vs any other name from outer scope
LL | std::panic!();
| ^^^ ambiguous name
|
= note: `std` could refer to a built-in extern crate
= note: `std` could refer to a built-in crate
note: `std` could also refer to the module imported here
--> $DIR/macro-path-prelude-shadowing.rs:27:9
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/no-link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
extern crate empty_struct;

fn main() {
empty_struct::XEmpty1; //~ ERROR cannot find value `XEmpty1` in module `empty_struct`
empty_struct::XEmpty1; //~ ERROR cannot find value `XEmpty1` in crate `empty_struct`
}
2 changes: 1 addition & 1 deletion src/test/ui/no-link.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0425]: cannot find value `XEmpty1` in module `empty_struct`
error[E0425]: cannot find value `XEmpty1` in crate `empty_struct`
--> $DIR/no-link.rs:7:19
|
LL | empty_struct::XEmpty1;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/recursion/recursive-reexports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

extern crate recursive_reexports;

fn f() -> recursive_reexports::S {} //~ ERROR cannot find type `S` in module `recursive_reexports`
fn f() -> recursive_reexports::S {} //~ ERROR cannot find type `S` in crate `recursive_reexports`

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/recursion/recursive-reexports.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0412]: cannot find type `S` in module `recursive_reexports`
error[E0412]: cannot find type `S` in crate `recursive_reexports`
--> $DIR/recursive-reexports.rs:5:32
|
LL | fn f() -> recursive_reexports::S {}
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/resolve/enums-are-namespaced-xc.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0425]: cannot find value `A` in module `namespaced_enums`
error[E0425]: cannot find value `A` in crate `namespaced_enums`
--> $DIR/enums-are-namespaced-xc.rs:5:31
|
LL | let _ = namespaced_enums::A;
Expand All @@ -8,7 +8,7 @@ help: possible candidate is found in another module, you can import it into scop
LL | use namespaced_enums::Foo::A;
|

error[E0425]: cannot find function `B` in module `namespaced_enums`
error[E0425]: cannot find function `B` in crate `namespaced_enums`
--> $DIR/enums-are-namespaced-xc.rs:7:31
|
LL | let _ = namespaced_enums::B(10);
Expand All @@ -18,7 +18,7 @@ help: possible candidate is found in another module, you can import it into scop
LL | use namespaced_enums::Foo::B;
|

error[E0422]: cannot find struct, variant or union type `C` in module `namespaced_enums`
error[E0422]: cannot find struct, variant or union type `C` in crate `namespaced_enums`
--> $DIR/enums-are-namespaced-xc.rs:9:31
|
LL | let _ = namespaced_enums::C { a: 10 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use crate; //~ ERROR crate root imports need to be explicitly named: `use crate
use *; //~ ERROR cannot glob-import all possible crates

fn main() {
let s = ::xcrate; //~ ERROR expected value, found module `xcrate`
let s = ::xcrate; //~ ERROR expected value, found crate `xcrate`
//~^ NOTE not a value
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error: cannot glob-import all possible crates
LL | use *;
| ^

error[E0423]: expected value, found module `xcrate`
error[E0423]: expected value, found crate `xcrate`
--> $DIR/single-segment.rs:9:13
|
LL | let s = ::xcrate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0659]: `std` is ambiguous (name vs any other name during import resolutio
LL | pub use std::io;
| ^^^ ambiguous name
|
= note: `std` could refer to a built-in extern crate
= help: use `::std` to refer to this extern crate unambiguously
= note: `std` could refer to a built-in crate
= help: use `::std` to refer to this crate unambiguously
note: `std` could also refer to the module defined here
--> $DIR/ambiguity-macros-nested.rs:13:13
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/rust-2018/uniform-paths/ambiguity-macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0659]: `std` is ambiguous (name vs any other name during import resolutio
LL | use std::io;
| ^^^ ambiguous name
|
= note: `std` could refer to a built-in extern crate
= help: use `::std` to refer to this extern crate unambiguously
= note: `std` could refer to a built-in crate
= help: use `::std` to refer to this crate unambiguously
note: `std` could also refer to the module defined here
--> $DIR/ambiguity-macros.rs:12:9
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/rust-2018/uniform-paths/ambiguity-nested.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0659]: `std` is ambiguous (name vs any other name during import resolutio
LL | pub use std::io;
| ^^^ ambiguous name
|
= note: `std` could refer to a built-in extern crate
= help: use `::std` to refer to this extern crate unambiguously
= note: `std` could refer to a built-in crate
= help: use `::std` to refer to this crate unambiguously
note: `std` could also refer to the module defined here
--> $DIR/ambiguity-nested.rs:11:5
|
Expand Down
Loading