Skip to content

Commit b01c1e2

Browse files
committed
parser: tweak item kind wording
1 parent ab84914 commit b01c1e2

12 files changed

+115
-112
lines changed

src/librustc_expand/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
378378
self.cx.span_err(
379379
span,
380380
&format!(
381-
"expected crate top-level item to be a module after macro expansion, found a {}",
382-
kind.descriptive_variant()
381+
"expected crate top-level item to be a module after macro expansion, found {} {}",
382+
kind.article(), kind.descr()
383383
),
384384
);
385385
}

src/librustc_hir/hir.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2500,24 +2500,24 @@ pub enum ItemKind<'hir> {
25002500
}
25012501

25022502
impl ItemKind<'_> {
2503-
pub fn descriptive_variant(&self) -> &str {
2503+
pub fn descr(&self) -> &str {
25042504
match *self {
25052505
ItemKind::ExternCrate(..) => "extern crate",
2506-
ItemKind::Use(..) => "use",
2506+
ItemKind::Use(..) => "`use` import",
25072507
ItemKind::Static(..) => "static item",
25082508
ItemKind::Const(..) => "constant item",
25092509
ItemKind::Fn(..) => "function",
25102510
ItemKind::Mod(..) => "module",
2511-
ItemKind::ForeignMod(..) => "foreign module",
2512-
ItemKind::GlobalAsm(..) => "global asm",
2511+
ItemKind::ForeignMod(..) => "extern block",
2512+
ItemKind::GlobalAsm(..) => "global asm item",
25132513
ItemKind::TyAlias(..) => "type alias",
25142514
ItemKind::OpaqueTy(..) => "opaque type",
25152515
ItemKind::Enum(..) => "enum",
25162516
ItemKind::Struct(..) => "struct",
25172517
ItemKind::Union(..) => "union",
25182518
ItemKind::Trait(..) => "trait",
25192519
ItemKind::TraitAlias(..) => "trait alias",
2520-
ItemKind::Impl { .. } => "impl",
2520+
ItemKind::Impl { .. } => "implementation",
25212521
}
25222522
}
25232523

src/librustc_parse/parser/item.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -661,12 +661,7 @@ impl<'a> Parser<'a> {
661661
self.struct_span_err(span, "associated `static` items are not allowed").emit();
662662
AssocItemKind::Const(a, b)
663663
}
664-
_ => {
665-
let span = self.sess.source_map().def_span(span);
666-
self.struct_span_err(span, "item kind not supported in `trait` or `impl`")
667-
.emit();
668-
return None;
669-
}
664+
_ => return self.error_bad_item_kind(span, &kind, "`trait` or `impl`"),
670665
};
671666
Some(P(Item { attrs, id, span, vis, ident, defaultness, kind, tokens }))
672667
}))
@@ -858,16 +853,19 @@ impl<'a> Parser<'a> {
858853
self.error_on_foreign_const(span, ident);
859854
ForeignItemKind::Static(a, Mutability::Not, b)
860855
}
861-
_ => {
862-
let span = self.sess.source_map().def_span(span);
863-
self.struct_span_err(span, "item kind not supported in `extern` block").emit();
864-
return None;
865-
}
856+
_ => return self.error_bad_item_kind(span, &kind, "`extern` block"),
866857
};
867858
Some(P(Item { attrs, id, span, vis, ident, defaultness, kind, tokens }))
868859
}))
869860
}
870861

862+
fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &str) -> Option<T> {
863+
let span = self.sess.source_map().def_span(span);
864+
let msg = format!("{} not supported in {}", kind.descr(), ctx);
865+
self.struct_span_err(span, &msg).emit();
866+
return None;
867+
}
868+
871869
fn error_on_foreign_const(&self, span: Span, ident: Ident) {
872870
self.struct_span_err(ident.span, "extern items cannot be `const`")
873871
.span_suggestion(

src/librustc_passes/dead.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
601601
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
602602
_ => "used",
603603
};
604-
self.warn_dead_code(
605-
item.hir_id,
606-
span,
607-
item.ident.name,
608-
item.kind.descriptive_variant(),
609-
participle,
610-
);
604+
self.warn_dead_code(item.hir_id, span, item.ident.name, item.kind.descr(), participle);
611605
} else {
612606
// Only continue if we didn't warn
613607
intravisit::walk_item(self, item);

src/librustc_passes/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'a, 'tcx> {
362362
// optional. They inherit stability from their parents when unannotated.
363363
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..) => {}
364364

365-
_ => self.check_missing_stability(i.hir_id, i.span, i.kind.descriptive_variant()),
365+
_ => self.check_missing_stability(i.hir_id, i.span, i.kind.descr()),
366366
}
367367

368368
intravisit::walk_item(self, i)

src/libsyntax/ast.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -2574,23 +2574,34 @@ pub enum ItemKind {
25742574
}
25752575

25762576
impl ItemKind {
2577-
pub fn descriptive_variant(&self) -> &str {
2578-
match *self {
2577+
pub fn article(&self) -> &str {
2578+
use ItemKind::*;
2579+
match self {
2580+
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
2581+
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a",
2582+
ExternCrate(..) | ForeignMod(..) | Mac(..) | Enum(..) | Impl { .. } => "an",
2583+
}
2584+
}
2585+
2586+
pub fn descr(&self) -> &str {
2587+
match self {
25792588
ItemKind::ExternCrate(..) => "extern crate",
2580-
ItemKind::Use(..) => "use",
2589+
ItemKind::Use(..) => "`use` import",
25812590
ItemKind::Static(..) => "static item",
25822591
ItemKind::Const(..) => "constant item",
25832592
ItemKind::Fn(..) => "function",
25842593
ItemKind::Mod(..) => "module",
2585-
ItemKind::ForeignMod(..) => "foreign module",
2586-
ItemKind::GlobalAsm(..) => "global asm",
2594+
ItemKind::ForeignMod(..) => "extern block",
2595+
ItemKind::GlobalAsm(..) => "global asm item",
25872596
ItemKind::TyAlias(..) => "type alias",
25882597
ItemKind::Enum(..) => "enum",
25892598
ItemKind::Struct(..) => "struct",
25902599
ItemKind::Union(..) => "union",
25912600
ItemKind::Trait(..) => "trait",
25922601
ItemKind::TraitAlias(..) => "trait alias",
2593-
ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl { .. } => "item",
2602+
ItemKind::Mac(..) => "item macro invocation",
2603+
ItemKind::MacroDef(..) => "macro definition",
2604+
ItemKind::Impl { .. } => "implementation",
25942605
}
25952606
}
25962607

src/test/ui/parser/default-on-wrong-item-kind.rs

+36-36
Original file line numberDiff line numberDiff line change
@@ -31,110 +31,110 @@ mod free_items {
3131
#[cfg(FALSE)]
3232
extern "C" {
3333
default extern crate foo; //~ ERROR item cannot be `default`
34-
//~^ ERROR item kind not supported in `extern` block
34+
//~^ ERROR extern crate not supported in `extern` block
3535
default use foo; //~ ERROR item cannot be `default`
36-
//~^ ERROR item kind not supported in `extern` block
36+
//~^ ERROR `use` import not supported in `extern` block
3737
default static foo: u8; //~ ERROR item cannot be `default`
3838
default const foo: u8; //~ ERROR item cannot be `default`
3939
//~^ ERROR extern items cannot be `const`
4040
default fn foo(); //~ ERROR item cannot be `default`
4141
default mod foo {} //~ ERROR item cannot be `default`
42-
//~^ ERROR item kind not supported in `extern` block
42+
//~^ ERROR module not supported in `extern` block
4343
default extern "C" {} //~ ERROR item cannot be `default`
44-
//~^ ERROR item kind not supported in `extern` block
44+
//~^ ERROR extern block not supported in `extern` block
4545
default type foo = u8; //~ ERROR item cannot be `default`
4646
default enum foo {} //~ ERROR item cannot be `default`
47-
//~^ ERROR item kind not supported in `extern` block
47+
//~^ ERROR enum not supported in `extern` block
4848
default struct foo {} //~ ERROR item cannot be `default`
49-
//~^ ERROR item kind not supported in `extern` block
49+
//~^ ERROR struct not supported in `extern` block
5050
default union foo {} //~ ERROR item cannot be `default`
51-
//~^ ERROR item kind not supported in `extern` block
51+
//~^ ERROR union not supported in `extern` block
5252
default trait foo {} //~ ERROR item cannot be `default`
53-
//~^ ERROR item kind not supported in `extern` block
53+
//~^ ERROR trait not supported in `extern` block
5454
default trait foo = Ord; //~ ERROR item cannot be `default`
55-
//~^ ERROR item kind not supported in `extern` block
55+
//~^ ERROR trait alias not supported in `extern` block
5656
default impl foo {}
57-
//~^ ERROR item kind not supported in `extern` block
57+
//~^ ERROR implementation not supported in `extern` block
5858
default!();
5959
default::foo::bar!();
6060
default default!(); //~ ERROR item cannot be `default`
6161
default default::foo::bar!(); //~ ERROR item cannot be `default`
6262
default macro foo {} //~ ERROR item cannot be `default`
63-
//~^ ERROR item kind not supported in `extern` block
63+
//~^ ERROR macro definition not supported in `extern` block
6464
default macro_rules! foo {} //~ ERROR item cannot be `default`
65-
//~^ ERROR item kind not supported in `extern` block
65+
//~^ ERROR macro definition not supported in `extern` block
6666
}
6767

6868
#[cfg(FALSE)]
6969
impl S {
7070
default extern crate foo;
71-
//~^ ERROR item kind not supported in `trait` or `impl`
71+
//~^ ERROR extern crate not supported in `trait` or `impl`
7272
default use foo;
73-
//~^ ERROR item kind not supported in `trait` or `impl`
73+
//~^ ERROR `use` import not supported in `trait` or `impl`
7474
default static foo: u8;
7575
//~^ ERROR associated `static` items are not allowed
7676
default const foo: u8;
7777
default fn foo();
7878
default mod foo {}
79-
//~^ ERROR item kind not supported in `trait` or `impl`
79+
//~^ ERROR module not supported in `trait` or `impl`
8080
default extern "C" {}
81-
//~^ ERROR item kind not supported in `trait` or `impl`
81+
//~^ ERROR extern block not supported in `trait` or `impl`
8282
default type foo = u8;
8383
default enum foo {}
84-
//~^ ERROR item kind not supported in `trait` or `impl`
84+
//~^ ERROR enum not supported in `trait` or `impl`
8585
default struct foo {}
86-
//~^ ERROR item kind not supported in `trait` or `impl`
86+
//~^ ERROR struct not supported in `trait` or `impl`
8787
default union foo {}
88-
//~^ ERROR item kind not supported in `trait` or `impl`
88+
//~^ ERROR union not supported in `trait` or `impl`
8989
default trait foo {}
90-
//~^ ERROR item kind not supported in `trait` or `impl`
90+
//~^ ERROR trait not supported in `trait` or `impl`
9191
default trait foo = Ord;
92-
//~^ ERROR item kind not supported in `trait` or `impl`
92+
//~^ ERROR trait alias not supported in `trait` or `impl`
9393
default impl foo {}
94-
//~^ ERROR item kind not supported in `trait` or `impl`
94+
//~^ ERROR implementation not supported in `trait` or `impl`
9595
default!();
9696
default::foo::bar!();
9797
default default!();
9898
default default::foo::bar!();
9999
default macro foo {}
100-
//~^ ERROR item kind not supported in `trait` or `impl`
100+
//~^ ERROR macro definition not supported in `trait` or `impl`
101101
default macro_rules! foo {}
102-
//~^ ERROR item kind not supported in `trait` or `impl`
102+
//~^ ERROR macro definition not supported in `trait` or `impl`
103103
}
104104

105105
#[cfg(FALSE)]
106106
trait T {
107107
default extern crate foo;
108-
//~^ ERROR item kind not supported in `trait` or `impl`
108+
//~^ ERROR extern crate not supported in `trait` or `impl`
109109
default use foo;
110-
//~^ ERROR item kind not supported in `trait` or `impl`
110+
//~^ ERROR `use` import not supported in `trait` or `impl`
111111
default static foo: u8;
112112
//~^ ERROR associated `static` items are not allowed
113113
default const foo: u8;
114114
default fn foo();
115115
default mod foo {}
116-
//~^ ERROR item kind not supported in `trait` or `impl`
116+
//~^ ERROR module not supported in `trait` or `impl`
117117
default extern "C" {}
118-
//~^ ERROR item kind not supported in `trait` or `impl`
118+
//~^ ERROR extern block not supported in `trait` or `impl`
119119
default type foo = u8;
120120
default enum foo {}
121-
//~^ ERROR item kind not supported in `trait` or `impl`
121+
//~^ ERROR enum not supported in `trait` or `impl`
122122
default struct foo {}
123-
//~^ ERROR item kind not supported in `trait` or `impl`
123+
//~^ ERROR struct not supported in `trait` or `impl`
124124
default union foo {}
125-
//~^ ERROR item kind not supported in `trait` or `impl`
125+
//~^ ERROR union not supported in `trait` or `impl`
126126
default trait foo {}
127-
//~^ ERROR item kind not supported in `trait` or `impl`
127+
//~^ ERROR trait not supported in `trait` or `impl`
128128
default trait foo = Ord;
129-
//~^ ERROR item kind not supported in `trait` or `impl`
129+
//~^ ERROR trait alias not supported in `trait` or `impl`
130130
default impl foo {}
131-
//~^ ERROR item kind not supported in `trait` or `impl`
131+
//~^ ERROR implementation not supported in `trait` or `impl`
132132
default!();
133133
default::foo::bar!();
134134
default default!();
135135
default default::foo::bar!();
136136
default macro foo {}
137-
//~^ ERROR item kind not supported in `trait` or `impl`
137+
//~^ ERROR macro definition not supported in `trait` or `impl`
138138
default macro_rules! foo {}
139-
//~^ ERROR item kind not supported in `trait` or `impl`
139+
//~^ ERROR macro definition not supported in `trait` or `impl`
140140
}

0 commit comments

Comments
 (0)