Skip to content

Commit 8fd7df6

Browse files
committed
Auto merge of #63250 - petrochenkov:descrate, r=davidtwco
diagnostics: Describe crate root modules in `DefKind::Mod` as "crate" Or we can use "extern crate" like resolve previously did sometimes, not sure. r? @davidtwco
2 parents 9703ef6 + 26d26eb commit 8fd7df6

30 files changed

+63
-74
lines changed

src/librustc/hir/def.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hir::def_id::DefId;
1+
use crate::hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
22
use crate::util::nodemap::DefIdMap;
33
use syntax::ast;
44
use syntax::ext::base::MacroKind;
@@ -81,9 +81,11 @@ pub enum DefKind {
8181
}
8282

8383
impl DefKind {
84-
pub fn descr(self) -> &'static str {
84+
pub fn descr(self, def_id: DefId) -> &'static str {
8585
match self {
8686
DefKind::Fn => "function",
87+
DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE =>
88+
"crate",
8789
DefKind::Mod => "module",
8890
DefKind::Static => "static",
8991
DefKind::Enum => "enum",
@@ -366,7 +368,7 @@ impl<Id> Res<Id> {
366368
/// A human readable name for the res kind ("function", "module", etc.).
367369
pub fn descr(&self) -> &'static str {
368370
match *self {
369-
Res::Def(kind, _) => kind.descr(),
371+
Res::Def(kind, def_id) => kind.descr(def_id),
370372
Res::SelfCtor(..) => "self constructor",
371373
Res::PrimTy(..) => "builtin type",
372374
Res::Local(..) => "local variable",

src/librustc_privacy/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
12591259
hir::QPath::Resolved(_, ref path) => path.to_string(),
12601260
hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(),
12611261
};
1262-
let msg = format!("{} `{}` is private", kind.descr(), name);
1262+
let msg = format!("{} `{}` is private", kind.descr(def_id), name);
12631263
self.tcx.sess.span_err(span, &msg);
12641264
return;
12651265
}

src/librustc_resolve/diagnostics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,12 @@ impl<'a> Resolver<'a> {
319319
err
320320
}
321321
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
322-
let shadows_what = binding.descr();
322+
let res = binding.res();
323+
let shadows_what = res.descr();
323324
let mut err = struct_span_err!(self.session, span, E0530, "{}s cannot shadow {}s",
324325
what_binding, shadows_what);
325326
err.span_label(span, format!("cannot be named the same as {} {}",
326-
binding.article(), shadows_what));
327+
res.article(), shadows_what));
327328
let participle = if binding.is_import() { "imported" } else { "defined" };
328329
let msg = format!("the {} `{}` is {} here", shadows_what, name, participle);
329330
err.span_label(binding.span, msg);

src/librustc_resolve/late/diagnostics.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ impl<'a> LateResolutionVisitor<'a, '_> {
8888
let mod_prefix = match self.resolve_path(
8989
mod_path, Some(TypeNS), false, span, CrateLint::No
9090
) {
91-
PathResult::Module(ModuleOrUniformRoot::Module(module)) =>
92-
module.def_kind(),
91+
PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
9392
_ => None,
94-
}.map_or(String::new(), |kind| format!("{} ", kind.descr()));
93+
}.map_or(String::new(), |res| format!("{} ", res.descr()));
9594
(mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)))
9695
};
9796
(format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str),

src/librustc_resolve/lib.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,6 @@ impl<'a> ModuleData<'a> {
494494
}
495495
}
496496

497-
fn def_kind(&self) -> Option<DefKind> {
498-
match self.kind {
499-
ModuleKind::Def(kind, ..) => Some(kind),
500-
_ => None,
501-
}
502-
}
503-
504497
fn def_id(&self) -> Option<DefId> {
505498
match self.kind {
506499
ModuleKind::Def(_, def_id, _) => Some(def_id),
@@ -745,14 +738,6 @@ impl<'a> NameBinding<'a> {
745738
self.res().macro_kind()
746739
}
747740

748-
fn descr(&self) -> &'static str {
749-
if self.is_extern_crate() { "extern crate" } else { self.res().descr() }
750-
}
751-
752-
fn article(&self) -> &'static str {
753-
if self.is_extern_crate() { "an" } else { self.res().article() }
754-
}
755-
756741
// Suppose that we resolved macro invocation with `invoc_parent_expansion` to binding `binding`
757742
// at some expansion round `max(invoc, binding)` when they both emerged from macros.
758743
// Then this function returns `true` if `self` may emerge from a macro *after* that
@@ -2200,6 +2185,7 @@ impl<'a> Resolver<'a> {
22002185
}
22012186

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

2220-
let article = if built_in.is_empty() { b.article() } else { "a" };
2206+
let article = if built_in.is_empty() { res.article() } else { "a" };
22212207
format!("{a}{built_in} {thing}{from}",
2222-
a = article, thing = b.descr(), built_in = built_in, from = from)
2208+
a = article, thing = res.descr(), built_in = built_in, from = from)
22232209
} else {
22242210
let introduced = if b.is_import() { "imported" } else { "defined" };
22252211
format!("the {thing} {introduced} here",
2226-
thing = b.descr(), introduced = introduced)
2212+
thing = res.descr(), introduced = introduced)
22272213
}
22282214
}
22292215

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

2235+
let thing = b.res().descr();
22492236
let mut help_msgs = Vec::new();
22502237
if b.is_glob_import() && (kind == AmbiguityKind::GlobVsGlob ||
22512238
kind == AmbiguityKind::GlobVsExpanded ||
@@ -2257,18 +2244,18 @@ impl<'a> Resolver<'a> {
22572244
if b.is_extern_crate() && ident.span.rust_2018() {
22582245
help_msgs.push(format!(
22592246
"use `::{ident}` to refer to this {thing} unambiguously",
2260-
ident = ident, thing = b.descr(),
2247+
ident = ident, thing = thing,
22612248
))
22622249
}
22632250
if misc == AmbiguityErrorMisc::SuggestCrate {
22642251
help_msgs.push(format!(
22652252
"use `crate::{ident}` to refer to this {thing} unambiguously",
2266-
ident = ident, thing = b.descr(),
2253+
ident = ident, thing = thing,
22672254
))
22682255
} else if misc == AmbiguityErrorMisc::SuggestSelf {
22692256
help_msgs.push(format!(
22702257
"use `self::{ident}` to refer to this {thing} unambiguously",
2271-
ident = ident, thing = b.descr(),
2258+
ident = ident, thing = thing,
22722259
))
22732260
}
22742261

@@ -2310,7 +2297,7 @@ impl<'a> Resolver<'a> {
23102297
ident.span,
23112298
E0603,
23122299
"{} `{}` is private",
2313-
binding.descr(),
2300+
binding.res().descr(),
23142301
ident.name,
23152302
);
23162303
// FIXME: use the ctor's `def_id` to check wether any of the fields is not visible

src/librustc_typeck/astconv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
17071707

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

17231723
let mut could_refer_to = |kind: DefKind, def_id, also| {
17241724
let note_msg = format!("`{}` could{} refer to {} defined here",
1725-
assoc_ident, also, kind.descr());
1725+
assoc_ident, also, kind.descr(def_id));
17261726
err.span_note(tcx.def_span(def_id), &note_msg);
17271727
};
17281728
could_refer_to(DefKind::Variant, variant_def_id, "");

src/librustc_typeck/check/method/suggest.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
522522
&format!(
523523
"there is {} {} with a similar name",
524524
def_kind.article(),
525-
def_kind.descr(),
525+
def_kind.descr(lev_candidate.def_id),
526526
),
527527
lev_candidate.ident.to_string(),
528528
Applicability::MaybeIncorrect,
@@ -543,9 +543,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
543543
err.emit();
544544
}
545545

546-
MethodError::PrivateMatch(kind, _, out_of_scope_traits) => {
546+
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
547547
let mut err = struct_span_err!(self.tcx.sess, span, E0624,
548-
"{} `{}` is private", kind.descr(), item_name);
548+
"{} `{}` is private", kind.descr(def_id), item_name);
549549
self.suggest_valid_traits(&mut err, out_of_scope_traits);
550550
err.emit();
551551
}

src/test/ui/extern/extern-crate-visibility.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ mod foo {
33
}
44

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

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

1111
use foo::*;
1212
mod core {} // Check that private crates are not glob imported

src/test/ui/extern/extern-crate-visibility.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0603]: extern crate `core` is private
1+
error[E0603]: crate `core` is private
22
--> $DIR/extern-crate-visibility.rs:6:10
33
|
44
LL | use foo::core::cell;
55
| ^^^^
66

7-
error[E0603]: extern crate `core` is private
7+
error[E0603]: crate `core` is private
88
--> $DIR/extern-crate-visibility.rs:9:10
99
|
1010
LL | foo::core::cell::Cell::new(0);

src/test/ui/imports/extern-prelude-extern-crate-restricted-shadowing.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | Vec::panic!();
1414
| ^^^ ambiguous name
1515
|
1616
= note: `Vec` could refer to a struct from prelude
17-
note: `Vec` could also refer to the extern crate imported here
17+
note: `Vec` could also refer to the crate imported here
1818
--> $DIR/extern-prelude-extern-crate-restricted-shadowing.rs:5:9
1919
|
2020
LL | extern crate std as Vec;

0 commit comments

Comments
 (0)