parser: tweak item kind wording

This commit is contained in:
Mazdak Farrokhzad 2020-02-23 06:04:37 +01:00
parent ab84914fe4
commit b01c1e2092
12 changed files with 115 additions and 112 deletions

View File

@ -378,8 +378,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
self.cx.span_err(
span,
&format!(
"expected crate top-level item to be a module after macro expansion, found a {}",
kind.descriptive_variant()
"expected crate top-level item to be a module after macro expansion, found {} {}",
kind.article(), kind.descr()
),
);
}

View File

@ -2500,16 +2500,16 @@ pub enum ItemKind<'hir> {
}
impl ItemKind<'_> {
pub fn descriptive_variant(&self) -> &str {
pub fn descr(&self) -> &str {
match *self {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "use",
ItemKind::Use(..) => "`use` import",
ItemKind::Static(..) => "static item",
ItemKind::Const(..) => "constant item",
ItemKind::Fn(..) => "function",
ItemKind::Mod(..) => "module",
ItemKind::ForeignMod(..) => "foreign module",
ItemKind::GlobalAsm(..) => "global asm",
ItemKind::ForeignMod(..) => "extern block",
ItemKind::GlobalAsm(..) => "global asm item",
ItemKind::TyAlias(..) => "type alias",
ItemKind::OpaqueTy(..) => "opaque type",
ItemKind::Enum(..) => "enum",
@ -2517,7 +2517,7 @@ impl ItemKind<'_> {
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Impl { .. } => "impl",
ItemKind::Impl { .. } => "implementation",
}
}

View File

@ -661,12 +661,7 @@ impl<'a> Parser<'a> {
self.struct_span_err(span, "associated `static` items are not allowed").emit();
AssocItemKind::Const(a, b)
}
_ => {
let span = self.sess.source_map().def_span(span);
self.struct_span_err(span, "item kind not supported in `trait` or `impl`")
.emit();
return None;
}
_ => return self.error_bad_item_kind(span, &kind, "`trait` or `impl`"),
};
Some(P(Item { attrs, id, span, vis, ident, defaultness, kind, tokens }))
}))
@ -858,16 +853,19 @@ impl<'a> Parser<'a> {
self.error_on_foreign_const(span, ident);
ForeignItemKind::Static(a, Mutability::Not, b)
}
_ => {
let span = self.sess.source_map().def_span(span);
self.struct_span_err(span, "item kind not supported in `extern` block").emit();
return None;
}
_ => return self.error_bad_item_kind(span, &kind, "`extern` block"),
};
Some(P(Item { attrs, id, span, vis, ident, defaultness, kind, tokens }))
}))
}
fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &str) -> Option<T> {
let span = self.sess.source_map().def_span(span);
let msg = format!("{} not supported in {}", kind.descr(), ctx);
self.struct_span_err(span, &msg).emit();
return None;
}
fn error_on_foreign_const(&self, span: Span, ident: Ident) {
self.struct_span_err(ident.span, "extern items cannot be `const`")
.span_suggestion(

View File

@ -601,13 +601,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
_ => "used",
};
self.warn_dead_code(
item.hir_id,
span,
item.ident.name,
item.kind.descriptive_variant(),
participle,
);
self.warn_dead_code(item.hir_id, span, item.ident.name, item.kind.descr(), participle);
} else {
// Only continue if we didn't warn
intravisit::walk_item(self, item);

View File

@ -362,7 +362,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'a, 'tcx> {
// optional. They inherit stability from their parents when unannotated.
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..) => {}
_ => self.check_missing_stability(i.hir_id, i.span, i.kind.descriptive_variant()),
_ => self.check_missing_stability(i.hir_id, i.span, i.kind.descr()),
}
intravisit::walk_item(self, i)

View File

@ -2574,23 +2574,34 @@ pub enum ItemKind {
}
impl ItemKind {
pub fn descriptive_variant(&self) -> &str {
match *self {
pub fn article(&self) -> &str {
use ItemKind::*;
match self {
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a",
ExternCrate(..) | ForeignMod(..) | Mac(..) | Enum(..) | Impl { .. } => "an",
}
}
pub fn descr(&self) -> &str {
match self {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "use",
ItemKind::Use(..) => "`use` import",
ItemKind::Static(..) => "static item",
ItemKind::Const(..) => "constant item",
ItemKind::Fn(..) => "function",
ItemKind::Mod(..) => "module",
ItemKind::ForeignMod(..) => "foreign module",
ItemKind::GlobalAsm(..) => "global asm",
ItemKind::ForeignMod(..) => "extern block",
ItemKind::GlobalAsm(..) => "global asm item",
ItemKind::TyAlias(..) => "type alias",
ItemKind::Enum(..) => "enum",
ItemKind::Struct(..) => "struct",
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl { .. } => "item",
ItemKind::Mac(..) => "item macro invocation",
ItemKind::MacroDef(..) => "macro definition",
ItemKind::Impl { .. } => "implementation",
}
}

View File

@ -31,110 +31,110 @@ mod free_items {
#[cfg(FALSE)]
extern "C" {
default extern crate foo; //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR extern crate not supported in `extern` block
default use foo; //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR `use` import not supported in `extern` block
default static foo: u8; //~ ERROR item cannot be `default`
default const foo: u8; //~ ERROR item cannot be `default`
//~^ ERROR extern items cannot be `const`
default fn foo(); //~ ERROR item cannot be `default`
default mod foo {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR module not supported in `extern` block
default extern "C" {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR extern block not supported in `extern` block
default type foo = u8; //~ ERROR item cannot be `default`
default enum foo {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR enum not supported in `extern` block
default struct foo {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR struct not supported in `extern` block
default union foo {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR union not supported in `extern` block
default trait foo {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR trait not supported in `extern` block
default trait foo = Ord; //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR trait alias not supported in `extern` block
default impl foo {}
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR implementation not supported in `extern` block
default!();
default::foo::bar!();
default default!(); //~ ERROR item cannot be `default`
default default::foo::bar!(); //~ ERROR item cannot be `default`
default macro foo {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR macro definition not supported in `extern` block
default macro_rules! foo {} //~ ERROR item cannot be `default`
//~^ ERROR item kind not supported in `extern` block
//~^ ERROR macro definition not supported in `extern` block
}
#[cfg(FALSE)]
impl S {
default extern crate foo;
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR extern crate not supported in `trait` or `impl`
default use foo;
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR `use` import not supported in `trait` or `impl`
default static foo: u8;
//~^ ERROR associated `static` items are not allowed
default const foo: u8;
default fn foo();
default mod foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR module not supported in `trait` or `impl`
default extern "C" {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR extern block not supported in `trait` or `impl`
default type foo = u8;
default enum foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR enum not supported in `trait` or `impl`
default struct foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR struct not supported in `trait` or `impl`
default union foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR union not supported in `trait` or `impl`
default trait foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR trait not supported in `trait` or `impl`
default trait foo = Ord;
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR trait alias not supported in `trait` or `impl`
default impl foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR implementation not supported in `trait` or `impl`
default!();
default::foo::bar!();
default default!();
default default::foo::bar!();
default macro foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR macro definition not supported in `trait` or `impl`
default macro_rules! foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR macro definition not supported in `trait` or `impl`
}
#[cfg(FALSE)]
trait T {
default extern crate foo;
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR extern crate not supported in `trait` or `impl`
default use foo;
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR `use` import not supported in `trait` or `impl`
default static foo: u8;
//~^ ERROR associated `static` items are not allowed
default const foo: u8;
default fn foo();
default mod foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR module not supported in `trait` or `impl`
default extern "C" {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR extern block not supported in `trait` or `impl`
default type foo = u8;
default enum foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR enum not supported in `trait` or `impl`
default struct foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR struct not supported in `trait` or `impl`
default union foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR union not supported in `trait` or `impl`
default trait foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR trait not supported in `trait` or `impl`
default trait foo = Ord;
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR trait alias not supported in `trait` or `impl`
default impl foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR implementation not supported in `trait` or `impl`
default!();
default::foo::bar!();
default default!();
default default::foo::bar!();
default macro foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR macro definition not supported in `trait` or `impl`
default macro_rules! foo {}
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR macro definition not supported in `trait` or `impl`
}

View File

@ -142,7 +142,7 @@ LL | default extern crate foo;
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: extern crate not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:33:5
|
LL | default extern crate foo;
@ -156,7 +156,7 @@ LL | default use foo;
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: `use` import not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:35:5
|
LL | default use foo;
@ -204,7 +204,7 @@ LL | default mod foo {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: module not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:41:5
|
LL | default mod foo {}
@ -218,7 +218,7 @@ LL | default extern "C" {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: extern block not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:43:5
|
LL | default extern "C" {}
@ -240,7 +240,7 @@ LL | default enum foo {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: enum not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:46:5
|
LL | default enum foo {}
@ -254,7 +254,7 @@ LL | default struct foo {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: struct not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:48:5
|
LL | default struct foo {}
@ -268,7 +268,7 @@ LL | default union foo {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: union not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:50:5
|
LL | default union foo {}
@ -282,7 +282,7 @@ LL | default trait foo {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: trait not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:52:5
|
LL | default trait foo {}
@ -296,13 +296,13 @@ LL | default trait foo = Ord;
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: trait alias not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:54:5
|
LL | default trait foo = Ord;
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `extern` block
error: implementation not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:56:5
|
LL | default impl foo {}
@ -332,7 +332,7 @@ LL | default macro foo {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: macro definition not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:62:5
|
LL | default macro foo {}
@ -346,19 +346,19 @@ LL | default macro_rules! foo {}
|
= note: only associated `fn`, `const`, and `type` items can be `default`
error: item kind not supported in `extern` block
error: macro definition not supported in `extern` block
--> $DIR/default-on-wrong-item-kind.rs:64:5
|
LL | default macro_rules! foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: extern crate not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:70:5
|
LL | default extern crate foo;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: `use` import not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:72:5
|
LL | default use foo;
@ -370,73 +370,73 @@ error: associated `static` items are not allowed
LL | default static foo: u8;
| ^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: module not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:78:5
|
LL | default mod foo {}
| ^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: extern block not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:80:5
|
LL | default extern "C" {}
| ^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: enum not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:83:5
|
LL | default enum foo {}
| ^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: struct not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:85:5
|
LL | default struct foo {}
| ^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: union not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:87:5
|
LL | default union foo {}
| ^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: trait not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:89:5
|
LL | default trait foo {}
| ^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: trait alias not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:91:5
|
LL | default trait foo = Ord;
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: implementation not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:93:5
|
LL | default impl foo {}
| ^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: macro definition not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:99:5
|
LL | default macro foo {}
| ^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: macro definition not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:101:5
|
LL | default macro_rules! foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: extern crate not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:107:5
|
LL | default extern crate foo;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: `use` import not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:109:5
|
LL | default use foo;
@ -448,61 +448,61 @@ error: associated `static` items are not allowed
LL | default static foo: u8;
| ^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: module not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:115:5
|
LL | default mod foo {}
| ^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: extern block not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:117:5
|
LL | default extern "C" {}
| ^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: enum not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:120:5
|
LL | default enum foo {}
| ^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: struct not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:122:5
|
LL | default struct foo {}
| ^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: union not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:124:5
|
LL | default union foo {}
| ^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: trait not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:126:5
|
LL | default trait foo {}
| ^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: trait alias not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:128:5
|
LL | default trait foo = Ord;
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: implementation not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:130:5
|
LL | default impl foo {}
| ^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: macro definition not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:136:5
|
LL | default macro foo {}
| ^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: macro definition not supported in `trait` or `impl`
--> $DIR/default-on-wrong-item-kind.rs:138:5
|
LL | default macro_rules! foo {}

View File

@ -4,10 +4,10 @@ impl T for () { //~ ERROR cannot find trait `T` in this scope
fn foo(&self) {}
trait T { //~ ERROR item kind not supported in `trait` or `impl`
trait T { //~ ERROR trait not supported in `trait` or `impl`
fn foo(&self);
}
pub(crate) struct Bar<T>(); //~ ERROR item kind not supported in `trait` or `impl`
pub(crate) struct Bar<T>(); //~ ERROR struct not supported in `trait` or `impl`
//~ ERROR this file contains an unclosed delimiter

View File

@ -7,13 +7,13 @@ LL | impl T for () {
LL |
| ^
error: item kind not supported in `trait` or `impl`
error: trait not supported in `trait` or `impl`
--> $DIR/missing-close-brace-in-impl-trait.rs:7:1
|
LL | trait T {
| ^^^^^^^
error: item kind not supported in `trait` or `impl`
error: struct not supported in `trait` or `impl`
--> $DIR/missing-close-brace-in-impl-trait.rs:11:1
|
LL | pub(crate) struct Bar<T>();

View File

@ -2,10 +2,10 @@ trait T {
fn foo(&self);
pub(crate) struct Bar<T>();
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR struct not supported in `trait` or `impl`
impl T for Bar<usize> {
//~^ ERROR item kind not supported in `trait` or `impl`
//~^ ERROR implementation not supported in `trait` or `impl`
fn foo(&self) {}
}

View File

@ -7,13 +7,13 @@ LL | trait T {
LL | fn main() {}
| ^
error: item kind not supported in `trait` or `impl`
error: struct not supported in `trait` or `impl`
--> $DIR/missing-close-brace-in-trait.rs:4:1
|
LL | pub(crate) struct Bar<T>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: item kind not supported in `trait` or `impl`
error: implementation not supported in `trait` or `impl`
--> $DIR/missing-close-brace-in-trait.rs:7:1
|
LL | impl T for Bar<usize> {