Don't require module name in inherents (#6576)

* Start

* Cleanup `construct_runtime!`

* Add tests

* Fix after merge

* Update the docs
This commit is contained in:
Bastian Köcher
2020-07-06 12:29:17 +02:00
committed by GitHub
parent 2019f70768
commit ad2e832289
15 changed files with 279 additions and 161 deletions
@@ -279,18 +279,6 @@ impl ModulePartKeyword {
Ident::new(self.name(), self.span())
}
/// Returns `true` if this module part allows to have an argument.
///
/// For example `Inherent(Timestamp)`.
fn allows_arg(&self) -> bool {
Self::all_allow_arg().iter().any(|n| *n == self.name())
}
/// Returns the names of all module parts that allow to have an argument.
fn all_allow_arg() -> &'static [&'static str] {
&["Inherent"]
}
/// Returns `true` if this module part is allowed to have generic arguments.
fn allows_generic(&self) -> bool {
Self::all_generic_arg().iter().any(|n| *n == self.name())
@@ -321,7 +309,6 @@ impl Spanned for ModulePartKeyword {
pub struct ModulePart {
pub keyword: ModulePartKeyword,
pub generics: syn::Generics,
pub args: Option<ext::Parens<ext::Punctuated<Ident, Token![,]>>>,
}
impl Parse for ModulePart {
@@ -339,27 +326,10 @@ impl Parse for ModulePart {
);
return Err(syn::Error::new(keyword.span(), msg));
}
let args = if input.peek(token::Paren) {
if !keyword.allows_arg() {
let syn::group::Parens { token: parens, .. } = syn::group::parse_parens(input)?;
let valid_names = ModulePart::format_names(ModulePartKeyword::all_allow_arg());
let msg = format!(
"`{}` is not allowed to have arguments in parens. \
Only the following modules are allowed to have arguments in parens: {}.",
keyword.name(),
valid_names,
);
return Err(syn::Error::new(parens.span, msg));
}
Some(input.parse()?)
} else {
None
};
Ok(Self {
keyword,
generics,
args,
})
}
}