Skip to content

Commit

Permalink
Better error for missing index in CRV2 (#4643)
Browse files Browse the repository at this point in the history
Fixes #4552

---------

Co-authored-by: command-bot <>
Co-authored-by: Bastian Köcher <info@kchr.de>
  • Loading branch information
codekitz and bkchr authored Jun 2, 2024
1 parent fc6c318 commit f81751e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
15 changes: 9 additions & 6 deletions substrate/frame/support/procedural/src/runtime/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ impl Def {
let mut pallets = vec![];

for item in items.iter_mut() {
let mut pallet_item = None;
let mut pallet_index = 0;
let mut pallet_index_and_item = None;

let mut disable_call = false;
let mut disable_unsigned = false;
Expand All @@ -170,9 +169,8 @@ impl Def {
runtime_types = Some(types);
},
RuntimeAttr::PalletIndex(span, index) => {
pallet_index = index;
pallet_item = if let syn::Item::Type(item) = item {
Some(item.clone())
pallet_index_and_item = if let syn::Item::Type(item) = item {
Some((index, item.clone()))
} else {
let msg = "Invalid runtime::pallet_index, expected type definition";
return Err(syn::Error::new(span, msg))
Expand All @@ -187,7 +185,7 @@ impl Def {
}
}

if let Some(pallet_item) = pallet_item {
if let Some((pallet_index, pallet_item)) = pallet_index_and_item {
match *pallet_item.ty.clone() {
syn::Type::Path(ref path) => {
let pallet_decl =
Expand Down Expand Up @@ -230,6 +228,11 @@ impl Def {
},
_ => continue,
}
} else {
if let syn::Item::Type(item) = item {
let msg = "Missing pallet index for pallet declaration. Please add `#[runtime::pallet_index(...)]`";
return Err(syn::Error::new(item.span(), &msg))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[frame_support::runtime]
mod runtime {
#[runtime::runtime]
#[runtime::derive(RuntimeCall)]
pub struct Runtime;

pub type System = frame_system;
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Missing pallet index for pallet declaration. Please add `#[runtime::pallet_index(...)]`
--> tests/runtime_ui/missing_pallet_index.rs:24:5
|
24 | pub type System = frame_system;
| ^^^

0 comments on commit f81751e

Please sign in to comment.