-
Notifications
You must be signed in to change notification settings - Fork 646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Metadata V16 (unstable): Enrich metadata with associated types of config traits #5274
Open
lexnv
wants to merge
24
commits into
master
Choose a base branch
from
lexnv/metadata-v16-associated-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7e8baee
metadata-ir: Introduce PalletAssociatedTypeMetadata
lexnv 1c12f15
frame/config: Add associated types to parsed config
lexnv 20b22d8
frame/expand: Implement associated types expansion
lexnv 350efce
frame/expand: Use provided cfgs for the associated types
lexnv 3e97971
frame/construct_runtime: Extract associated types from pallet config
lexnv 5d8021e
frame/pallet: Introduce `config(without_metadata)`
lexnv 5679244
frame/pallet: Introduce `#[pallet::include_metadata]` for associated
lexnv 958611e
frame/pallet: Include associated type iff bounds contain TypeInfo
lexnv 7f26b67
frame/pallet: Proper flag for metdata collection
lexnv cc001f3
frame/tests/ui: Fix type in test
lexnv b2803e2
frame/tests/ui: Check config without metadata
lexnv 94007d8
frame/tests/ui: Check config with multiple attributes
lexnv 75d2697
frame/tests/ui: Add negative test for duplicate attributes in config
lexnv ebcb4a0
frame/tests/ui: Add negative test for collecting metadata from constants
lexnv 5115021
frame/tests/ui: Add negative test for metadata collection on events
lexnv aba029a
frame/tests: Check PalletAssociatedTypeMetadataIR collection
lexnv 6502c83
frame/support: Add documentation
lexnv e9571cb
Add PRdoc
lexnv 31bf284
Merge remote-tracking branch 'origin/master' into lexnv/metadata-v16-…
lexnv 7429eb7
Update prdoc
lexnv 8a0c138
prdoc: Remove unneeded crate
lexnv c6c1800
prdoc: Include frame-support
lexnv 90c100c
Prupdate
lexnv 2982c59
Merge remote-tracking branch 'origin/master' into lexnv/metadata-v16-…
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
title: Enrich metadata IR with associated types of config traits | ||
|
||
doc: | ||
- audience: Runtime Dev | ||
description: | | ||
This feature is part of the upcoming metadata V16. The associated types of the `Config` trait that require the `TypeInfo` | ||
or `Parameter` bounds are included in the metadata of the pallet. The metadata is not yet exposed to the end-user, however | ||
the metadata intermediate representation (IR) contains these types. | ||
|
||
Developers can opt out of metadata collection of the associated types by specifying `without_metadata` optional attribute | ||
to the `#[pallet::config]`. | ||
|
||
Furthermore, the `without_metadata` argument can be used in combination with the newly added `#[pallet::include_metadata]` | ||
attribute to selectively include only certain associated types in the metadata collection. | ||
|
||
crates: | ||
- name: frame-support | ||
bump: patch | ||
- name: frame-support-procedural | ||
bump: patch | ||
- name: frame-support-procedural-tools | ||
bump: patch | ||
- name: sp-metadata-ir | ||
bump: major |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,9 @@ | |
// limitations under the License. | ||
|
||
use super::helper; | ||
use frame_support_procedural_tools::{get_doc_literals, is_using_frame_crate}; | ||
use frame_support_procedural_tools::{get_cfg_attributes, get_doc_literals, is_using_frame_crate}; | ||
use quote::ToTokens; | ||
use syn::{spanned::Spanned, token, Token}; | ||
use syn::{spanned::Spanned, token, Token, TraitItemType}; | ||
|
||
/// List of additional token to be used for parsing. | ||
mod keyword { | ||
|
@@ -36,6 +36,7 @@ mod keyword { | |
syn::custom_keyword!(no_default); | ||
syn::custom_keyword!(no_default_bounds); | ||
syn::custom_keyword!(constant); | ||
syn::custom_keyword!(include_metadata); | ||
} | ||
|
||
#[derive(Default)] | ||
|
@@ -55,6 +56,8 @@ pub struct ConfigDef { | |
pub has_instance: bool, | ||
/// Const associated type. | ||
pub consts_metadata: Vec<ConstMetadataDef>, | ||
/// Associated types metadata. | ||
pub associated_types_metadata: Vec<AssociatedTypeMetadataDef>, | ||
/// Whether the trait has the associated type `Event`, note that those bounds are | ||
/// checked: | ||
/// * `IsType<Self as frame_system::Config>::RuntimeEvent` | ||
|
@@ -70,6 +73,26 @@ pub struct ConfigDef { | |
pub default_sub_trait: Option<DefaultTrait>, | ||
} | ||
|
||
/// Input definition for an associated type in pallet config. | ||
pub struct AssociatedTypeMetadataDef { | ||
/// Name of the associated type. | ||
pub ident: syn::Ident, | ||
/// The doc associated. | ||
pub doc: Vec<syn::Expr>, | ||
/// The cfg associated. | ||
pub cfg: Vec<syn::Attribute>, | ||
} | ||
|
||
impl From<&syn::TraitItemType> for AssociatedTypeMetadataDef { | ||
fn from(trait_ty: &syn::TraitItemType) -> Self { | ||
let ident = trait_ty.ident.clone(); | ||
let doc = get_doc_literals(&trait_ty.attrs); | ||
let cfg = get_cfg_attributes(&trait_ty.attrs); | ||
|
||
Self { ident, doc, cfg } | ||
} | ||
} | ||
|
||
/// Input definition for a constant in pallet config. | ||
pub struct ConstMetadataDef { | ||
/// Name of the associated type. | ||
|
@@ -146,6 +169,8 @@ pub enum PalletAttrType { | |
NoBounds(keyword::no_default_bounds), | ||
#[peek(keyword::constant, name = "constant")] | ||
Constant(keyword::constant), | ||
#[peek(keyword::include_metadata, name = "include_metadata")] | ||
IncludeMetadata(keyword::include_metadata), | ||
} | ||
|
||
/// Parsing for `#[pallet::X]` | ||
|
@@ -322,12 +347,30 @@ pub fn replace_self_by_t(input: proc_macro2::TokenStream) -> proc_macro2::TokenS | |
.collect() | ||
} | ||
|
||
/// Check that the trait item requires the `TypeInfo` bound (or similar). | ||
fn contains_type_info_bound(ty: &TraitItemType) -> bool { | ||
const KNOWN_TYPE_INFO_BOUNDS: &[&str] = &[ | ||
// Explicit TypeInfo trait. | ||
"TypeInfo", | ||
// Implicit known substrate traits that implement type info. | ||
// Note: Aim to keep this list as small as possible. | ||
"Parameter", | ||
]; | ||
|
||
ty.bounds.iter().any(|bound| { | ||
let syn::TypeParamBound::Trait(bound) = bound else { return false }; | ||
|
||
KNOWN_TYPE_INFO_BOUNDS.iter().any(|known| bound.path.is_ident(known)) | ||
}) | ||
} | ||
|
||
impl ConfigDef { | ||
pub fn try_from( | ||
frame_system: &syn::Path, | ||
index: usize, | ||
item: &mut syn::Item, | ||
enable_default: bool, | ||
enable_associated_metadata: bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe |
||
) -> syn::Result<Self> { | ||
let syn::Item::Trait(item) = item else { | ||
let msg = "Invalid pallet::config, expected trait definition"; | ||
|
@@ -368,6 +411,7 @@ impl ConfigDef { | |
|
||
let mut has_event_type = false; | ||
let mut consts_metadata = vec![]; | ||
let mut associated_types_metadata = vec![]; | ||
let mut default_sub_trait = if enable_default { | ||
Some(DefaultTrait { | ||
items: Default::default(), | ||
|
@@ -383,6 +427,7 @@ impl ConfigDef { | |
let mut already_no_default = false; | ||
let mut already_constant = false; | ||
let mut already_no_default_bounds = false; | ||
let mut already_collected_associated_type = false; | ||
|
||
while let Ok(Some(pallet_attr)) = | ||
helper::take_first_item_pallet_attr::<PalletAttr>(trait_item) | ||
|
@@ -403,11 +448,39 @@ impl ConfigDef { | |
trait_item.span(), | ||
"Invalid #[pallet::constant] in #[pallet::config], expected type item", | ||
)), | ||
// Pallet developer has explicitly requested to include metadata for this associated type. | ||
// | ||
// They must provide a type item that implements `TypeInfo`. | ||
(PalletAttrType::IncludeMetadata(_), syn::TraitItem::Type(ref typ)) => { | ||
if is_event { | ||
return Err(syn::Error::new( | ||
pallet_attr._bracket.span.join(), | ||
"Invalid #[pallet::include_metadata] in `type RuntimeEvent`, \ | ||
expected type item. The associated type `RuntimeEvent` is already collected.", | ||
)) | ||
} | ||
|
||
if already_constant { | ||
return Err(syn::Error::new( | ||
pallet_attr._bracket.span.join(), | ||
"Invalid #[pallet::include_metadata] in #[pallet::constant], \ | ||
expected type item. Pallet constant's metadata is already collected.", | ||
)) | ||
} | ||
|
||
already_collected_associated_type = true; | ||
associated_types_metadata.push(AssociatedTypeMetadataDef::from(typ)); | ||
} | ||
(PalletAttrType::IncludeMetadata(_), _) => | ||
return Err(syn::Error::new( | ||
pallet_attr._bracket.span.join(), | ||
"Invalid #[pallet::include_metadata] in #[pallet::config], expected type item", | ||
)), | ||
(PalletAttrType::NoDefault(_), _) => { | ||
if !enable_default { | ||
return Err(syn::Error::new( | ||
pallet_attr._bracket.span.join(), | ||
"`#[pallet:no_default]` can only be used if `#[pallet::config(with_default)]` \ | ||
"`#[pallet::no_default]` can only be used if `#[pallet::config(with_default)]` \ | ||
has been specified" | ||
)); | ||
} | ||
|
@@ -439,6 +512,20 @@ impl ConfigDef { | |
} | ||
} | ||
|
||
// Metadata of associated types is collected by default, iff the associated type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// implements `TypeInfo`, or a similar trait that requires the `TypeInfo` bound. | ||
if !already_collected_associated_type && | ||
enable_associated_metadata && | ||
!is_event && !already_constant | ||
{ | ||
if let syn::TraitItem::Type(ref ty) = trait_item { | ||
// Collect the metadata of the associated type if it implements `TypeInfo`. | ||
if contains_type_info_bound(ty) { | ||
associated_types_metadata.push(AssociatedTypeMetadataDef::from(ty)); | ||
} | ||
} | ||
} | ||
|
||
if !already_no_default && enable_default { | ||
default_sub_trait | ||
.as_mut() | ||
|
@@ -481,6 +568,7 @@ impl ConfigDef { | |
index, | ||
has_instance, | ||
consts_metadata, | ||
associated_types_metadata, | ||
has_event_type, | ||
where_clause, | ||
default_sub_trait, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Why not a Set?