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

+5-3
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

+1-1
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

+3-2
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

+2-3
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

+9-22
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

+2-2
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

+3-3
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

+2-2
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

+2-2
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

+1-1
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

+1-1
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
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0425]: cannot find function `f` in module `glob_conflict`
1+
error[E0425]: cannot find function `f` in crate `glob_conflict`
22
--> $DIR/glob-conflict-cross-crate.rs:6:20
33
|
44
LL | glob_conflict::f();

src/test/ui/imports/issue-56125.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ error[E0659]: `issue_56125` is ambiguous (name vs any other name during import r
1010
LL | use issue_56125::last_segment::*;
1111
| ^^^^^^^^^^^ ambiguous name
1212
|
13-
= note: `issue_56125` could refer to an extern crate passed with `--extern`
14-
= help: use `::issue_56125` to refer to this extern crate unambiguously
13+
= note: `issue_56125` could refer to a crate passed with `--extern`
14+
= help: use `::issue_56125` to refer to this crate unambiguously
1515
note: `issue_56125` could also refer to the module imported here
1616
--> $DIR/issue-56125.rs:6:9
1717
|
@@ -25,8 +25,8 @@ error[E0659]: `issue_56125` is ambiguous (name vs any other name during import r
2525
LL | use issue_56125::non_last_segment::non_last_segment::*;
2626
| ^^^^^^^^^^^ ambiguous name
2727
|
28-
= note: `issue_56125` could refer to an extern crate passed with `--extern`
29-
= help: use `::issue_56125` to refer to this extern crate unambiguously
28+
= note: `issue_56125` could refer to a crate passed with `--extern`
29+
= help: use `::issue_56125` to refer to this crate unambiguously
3030
note: `issue_56125` could also refer to the module imported here
3131
--> $DIR/issue-56125.rs:11:9
3232
|
@@ -40,8 +40,8 @@ error[E0659]: `issue_56125` is ambiguous (name vs any other name during import r
4040
LL | use issue_56125::*;
4141
| ^^^^^^^^^^^ ambiguous name
4242
|
43-
= note: `issue_56125` could refer to an extern crate passed with `--extern`
44-
= help: use `::issue_56125` to refer to this extern crate unambiguously
43+
= note: `issue_56125` could refer to a crate passed with `--extern`
44+
= help: use `::issue_56125` to refer to this crate unambiguously
4545
note: `issue_56125` could also refer to the module imported here
4646
--> $DIR/issue-56125.rs:18:9
4747
|

src/test/ui/imports/issue-57539.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0659]: `core` is ambiguous (name vs any other name during import resoluti
44
LL | use core;
55
| ^^^^ ambiguous name
66
|
7-
= note: `core` could refer to a built-in extern crate
8-
= help: use `::core` to refer to this extern crate unambiguously
7+
= note: `core` could refer to a built-in crate
8+
= help: use `::core` to refer to this crate unambiguously
99
note: `core` could also refer to the module imported here
1010
--> $DIR/issue-57539.rs:5:9
1111
|

src/test/ui/macros/macro-path-prelude-shadowing.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0659]: `std` is ambiguous (glob import vs any other name from outer scope
44
LL | std::panic!();
55
| ^^^ ambiguous name
66
|
7-
= note: `std` could refer to a built-in extern crate
7+
= note: `std` could refer to a built-in crate
88
note: `std` could also refer to the module imported here
99
--> $DIR/macro-path-prelude-shadowing.rs:27:9
1010
|

src/test/ui/no-link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
extern crate empty_struct;
55

66
fn main() {
7-
empty_struct::XEmpty1; //~ ERROR cannot find value `XEmpty1` in module `empty_struct`
7+
empty_struct::XEmpty1; //~ ERROR cannot find value `XEmpty1` in crate `empty_struct`
88
}

src/test/ui/no-link.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0425]: cannot find value `XEmpty1` in module `empty_struct`
1+
error[E0425]: cannot find value `XEmpty1` in crate `empty_struct`
22
--> $DIR/no-link.rs:7:19
33
|
44
LL | empty_struct::XEmpty1;

src/test/ui/recursion/recursive-reexports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
extern crate recursive_reexports;
44

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

77
fn main() {}

src/test/ui/recursion/recursive-reexports.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0412]: cannot find type `S` in module `recursive_reexports`
1+
error[E0412]: cannot find type `S` in crate `recursive_reexports`
22
--> $DIR/recursive-reexports.rs:5:32
33
|
44
LL | fn f() -> recursive_reexports::S {}

src/test/ui/resolve/enums-are-namespaced-xc.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0425]: cannot find value `A` in module `namespaced_enums`
1+
error[E0425]: cannot find value `A` in crate `namespaced_enums`
22
--> $DIR/enums-are-namespaced-xc.rs:5:31
33
|
44
LL | let _ = namespaced_enums::A;
@@ -8,7 +8,7 @@ help: possible candidate is found in another module, you can import it into scop
88
LL | use namespaced_enums::Foo::A;
99
|
1010

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

21-
error[E0422]: cannot find struct, variant or union type `C` in module `namespaced_enums`
21+
error[E0422]: cannot find struct, variant or union type `C` in crate `namespaced_enums`
2222
--> $DIR/enums-are-namespaced-xc.rs:9:31
2323
|
2424
LL | let _ = namespaced_enums::C { a: 10 };

src/test/ui/rfc-2126-extern-absolute-paths/single-segment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ use crate; //~ ERROR crate root imports need to be explicitly named: `use crate
66
use *; //~ ERROR cannot glob-import all possible crates
77

88
fn main() {
9-
let s = ::xcrate; //~ ERROR expected value, found module `xcrate`
9+
let s = ::xcrate; //~ ERROR expected value, found crate `xcrate`
1010
//~^ NOTE not a value
1111
}

src/test/ui/rfc-2126-extern-absolute-paths/single-segment.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: cannot glob-import all possible crates
1010
LL | use *;
1111
| ^
1212

13-
error[E0423]: expected value, found module `xcrate`
13+
error[E0423]: expected value, found crate `xcrate`
1414
--> $DIR/single-segment.rs:9:13
1515
|
1616
LL | let s = ::xcrate;

src/test/ui/rust-2018/uniform-paths/ambiguity-macros-nested.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0659]: `std` is ambiguous (name vs any other name during import resolutio
44
LL | pub use std::io;
55
| ^^^ ambiguous name
66
|
7-
= note: `std` could refer to a built-in extern crate
8-
= help: use `::std` to refer to this extern crate unambiguously
7+
= note: `std` could refer to a built-in crate
8+
= help: use `::std` to refer to this crate unambiguously
99
note: `std` could also refer to the module defined here
1010
--> $DIR/ambiguity-macros-nested.rs:13:13
1111
|

src/test/ui/rust-2018/uniform-paths/ambiguity-macros.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0659]: `std` is ambiguous (name vs any other name during import resolutio
44
LL | use std::io;
55
| ^^^ ambiguous name
66
|
7-
= note: `std` could refer to a built-in extern crate
8-
= help: use `::std` to refer to this extern crate unambiguously
7+
= note: `std` could refer to a built-in crate
8+
= help: use `::std` to refer to this crate unambiguously
99
note: `std` could also refer to the module defined here
1010
--> $DIR/ambiguity-macros.rs:12:9
1111
|

src/test/ui/rust-2018/uniform-paths/ambiguity-nested.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0659]: `std` is ambiguous (name vs any other name during import resolutio
44
LL | pub use std::io;
55
| ^^^ ambiguous name
66
|
7-
= note: `std` could refer to a built-in extern crate
8-
= help: use `::std` to refer to this extern crate unambiguously
7+
= note: `std` could refer to a built-in crate
8+
= help: use `::std` to refer to this crate unambiguously
99
note: `std` could also refer to the module defined here
1010
--> $DIR/ambiguity-nested.rs:11:5
1111
|

0 commit comments

Comments
 (0)