Skip to content

Commit e690f35

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 0599f20 + 50b258b commit e690f35

28 files changed

+62
-73
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
@@ -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

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

+12-24
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

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

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