Skip to content

Commit d7eab18

Browse files
authored
Rollup merge of rust-lang#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 0f47687 + 50b258b commit d7eab18

28 files changed

+62
-73
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
@@ -1127,7 +1127,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
11271127
hir::QPath::Resolved(_, ref path) => path.to_string(),
11281128
hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(),
11291129
};
1130-
let msg = format!("{} `{}` is private", kind.descr(), name);
1130+
let msg = format!("{} `{}` is private", kind.descr(def_id), name);
11311131
self.tcx.sess.span_err(span, &msg);
11321132
return;
11331133
}

src/librustc_resolve/diagnostics.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@ impl<'a> Resolver<'a> {
115115
let mod_prefix = match self.resolve_path_without_parent_scope(
116116
mod_path, Some(TypeNS), false, span, CrateLint::No
117117
) {
118-
PathResult::Module(ModuleOrUniformRoot::Module(module)) =>
119-
module.def_kind(),
118+
PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
120119
_ => None,
121-
}.map_or(String::new(), |kind| format!("{} ", kind.descr()));
120+
}.map_or(String::new(), |res| format!("{} ", res.descr()));
122121
(mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)))
123122
};
124123
(format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str),

src/librustc_resolve/lib.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,12 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
443443
err
444444
}
445445
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
446-
let shadows_what = binding.descr();
446+
let res = binding.res();
447+
let shadows_what = res.descr();
447448
let mut err = struct_span_err!(resolver.session, span, E0530, "{}s cannot shadow {}s",
448449
what_binding, shadows_what);
449450
err.span_label(span, format!("cannot be named the same as {} {}",
450-
binding.article(), shadows_what));
451+
res.article(), shadows_what));
451452
let participle = if binding.is_import() { "imported" } else { "defined" };
452453
let msg = format!("the {} `{}` is {} here", shadows_what, name, participle);
453454
err.span_label(binding.span, msg);
@@ -1242,13 +1243,6 @@ impl<'a> ModuleData<'a> {
12421243
}
12431244
}
12441245

1245-
fn def_kind(&self) -> Option<DefKind> {
1246-
match self.kind {
1247-
ModuleKind::Def(kind, ..) => Some(kind),
1248-
_ => None,
1249-
}
1250-
}
1251-
12521246
fn def_id(&self) -> Option<DefId> {
12531247
match self.kind {
12541248
ModuleKind::Def(_, def_id, _) => Some(def_id),
@@ -1493,14 +1487,6 @@ impl<'a> NameBinding<'a> {
14931487
self.res().macro_kind()
14941488
}
14951489

1496-
fn descr(&self) -> &'static str {
1497-
if self.is_extern_crate() { "extern crate" } else { self.res().descr() }
1498-
}
1499-
1500-
fn article(&self) -> &'static str {
1501-
if self.is_extern_crate() { "an" } else { self.res().article() }
1502-
}
1503-
15041490
// Suppose that we resolved macro invocation with `invoc_parent_expansion` to binding `binding`
15051491
// at some expansion round `max(invoc, binding)` when they both emerged from macros.
15061492
// Then this function returns `true` if `self` may emerge from a macro *after* that
@@ -4691,6 +4677,7 @@ impl<'a> Resolver<'a> {
46914677
}
46924678

46934679
fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
4680+
let res = b.res();
46944681
if b.span.is_dummy() {
46954682
let add_built_in = match b.res() {
46964683
// These already contain the "built-in" prefix or look bad with it.
@@ -4708,13 +4695,13 @@ impl<'a> Resolver<'a> {
47084695
("", "")
47094696
};
47104697

4711-
let article = if built_in.is_empty() { b.article() } else { "a" };
4698+
let article = if built_in.is_empty() { res.article() } else { "a" };
47124699
format!("{a}{built_in} {thing}{from}",
4713-
a = article, thing = b.descr(), built_in = built_in, from = from)
4700+
a = article, thing = res.descr(), built_in = built_in, from = from)
47144701
} else {
47154702
let introduced = if b.is_import() { "imported" } else { "defined" };
47164703
format!("the {thing} {introduced} here",
4717-
thing = b.descr(), introduced = introduced)
4704+
thing = res.descr(), introduced = introduced)
47184705
}
47194706
}
47204707

@@ -4737,6 +4724,7 @@ impl<'a> Resolver<'a> {
47374724
let note_msg = format!("`{ident}` could{also} refer to {what}",
47384725
ident = ident, also = also, what = what);
47394726

4727+
let thing = b.res().descr();
47404728
let mut help_msgs = Vec::new();
47414729
if b.is_glob_import() && (kind == AmbiguityKind::GlobVsGlob ||
47424730
kind == AmbiguityKind::GlobVsExpanded ||
@@ -4748,18 +4736,18 @@ impl<'a> Resolver<'a> {
47484736
if b.is_extern_crate() && ident.span.rust_2018() {
47494737
help_msgs.push(format!(
47504738
"use `::{ident}` to refer to this {thing} unambiguously",
4751-
ident = ident, thing = b.descr(),
4739+
ident = ident, thing = thing,
47524740
))
47534741
}
47544742
if misc == AmbiguityErrorMisc::SuggestCrate {
47554743
help_msgs.push(format!(
47564744
"use `crate::{ident}` to refer to this {thing} unambiguously",
4757-
ident = ident, thing = b.descr(),
4745+
ident = ident, thing = thing,
47584746
))
47594747
} else if misc == AmbiguityErrorMisc::SuggestSelf {
47604748
help_msgs.push(format!(
47614749
"use `self::{ident}` to refer to this {thing} unambiguously",
4762-
ident = ident, thing = b.descr(),
4750+
ident = ident, thing = thing,
47634751
))
47644752
}
47654753

@@ -4797,7 +4785,7 @@ impl<'a> Resolver<'a> {
47974785
for &PrivacyError(dedup_span, ident, binding) in &self.privacy_errors {
47984786
if reported_spans.insert(dedup_span) {
47994787
span_err!(self.session, ident.span, E0603, "{} `{}` is private",
4800-
binding.descr(), ident.name);
4788+
binding.res().descr(), ident.name);
48014789
}
48024790
}
48034791
}

src/librustc_typeck/astconv.rs

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

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

17041704
let mut could_refer_to = |kind: DefKind, def_id, also| {
17051705
let note_msg = format!("`{}` could{} refer to {} defined here",
1706-
assoc_ident, also, kind.descr());
1706+
assoc_ident, also, kind.descr(def_id));
17071707
err.span_note(tcx.def_span(def_id), &note_msg);
17081708
};
17091709
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;

src/test/ui/imports/glob-conflict-cross-crate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
extern crate glob_conflict;
44

55
fn main() {
6-
glob_conflict::f(); //~ ERROR cannot find function `f` in module `glob_conflict`
6+
glob_conflict::f(); //~ ERROR cannot find function `f` in crate `glob_conflict`
77
glob_conflict::glob::f(); //~ ERROR cannot find function `f` in module `glob_conflict::glob`
88
}

0 commit comments

Comments
 (0)