Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fix Lifetime Warnings #11603

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl pallet_session::historical::Config for Runtime {
}

pallet_staking_reward_curve::build! {
const REWARD_CURVE: PiecewiseLinear<'static> = curve!(
const REWARD_CURVE: PiecewiseLinear = curve!(
min_inflation: 0_025_000,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand All @@ -524,7 +524,7 @@ parameter_types! {
pub const SessionsPerEra: sp_staking::SessionIndex = 6;
pub const BondingDuration: sp_staking::EraIndex = 24 * 28;
pub const SlashDeferDuration: sp_staking::EraIndex = 24 * 7; // 1/4 the bonding duration.
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const RewardCurve: &'static PiecewiseLinear = &REWARD_CURVE;
pub const MaxNominatorRewardedPerValidator: u32 = 256;
pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
pub OffchainRepeat: BlockNumber = 5;
Expand Down
4 changes: 2 additions & 2 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl pallet_balances::Config for Test {
}

pallet_staking_reward_curve::build! {
const REWARD_CURVE: PiecewiseLinear<'static> = curve!(
const REWARD_CURVE: PiecewiseLinear = curve!(
min_inflation: 0_025_000u64,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand All @@ -168,7 +168,7 @@ parameter_types! {
pub const SessionsPerEra: SessionIndex = 3;
pub const BondingDuration: EraIndex = 3;
pub const SlashDeferDuration: EraIndex = 0;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const RewardCurve: &'static PiecewiseLinear = &REWARD_CURVE;
pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(16);
}

Expand Down
4 changes: 2 additions & 2 deletions frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl pallet_timestamp::Config for Test {
}

pallet_staking_reward_curve::build! {
const REWARD_CURVE: PiecewiseLinear<'static> = curve!(
const REWARD_CURVE: PiecewiseLinear = curve!(
min_inflation: 0_025_000u64,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand All @@ -172,7 +172,7 @@ pallet_staking_reward_curve::build! {
parameter_types! {
pub const SessionsPerEra: SessionIndex = 3;
pub const BondingDuration: EraIndex = 3;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const RewardCurve: &'static PiecewiseLinear = &REWARD_CURVE;
pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
}

Expand Down
4 changes: 2 additions & 2 deletions frame/nomination-pools/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl pallet_balances::Config for Runtime {
}

pallet_staking_reward_curve::build! {
const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!(
const I_NPOS: sp_runtime::curve::PiecewiseLinear = curve!(
min_inflation: 0_025_000,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand All @@ -84,7 +84,7 @@ pallet_staking_reward_curve::build! {
);
}
parameter_types! {
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS;
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear = &I_NPOS;
}
impl pallet_staking::Config for Runtime {
type MaxNominations = ConstU32<16>;
Expand Down
4 changes: 2 additions & 2 deletions frame/offences/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl pallet_session::Config for Test {
}

pallet_staking_reward_curve::build! {
const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!(
const I_NPOS: sp_runtime::curve::PiecewiseLinear = curve!(
min_inflation: 0_025_000,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand All @@ -143,7 +143,7 @@ pallet_staking_reward_curve::build! {
);
}
parameter_types! {
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS;
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear = &I_NPOS;
}

pub type Extrinsic = sp_runtime::testing::TestXt<Call, ()>;
Expand Down
4 changes: 2 additions & 2 deletions frame/session/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl pallet_session::Config for Test {
type WeightInfo = ();
}
pallet_staking_reward_curve::build! {
const I_NPOS: sp_runtime::curve::PiecewiseLinear<'static> = curve!(
const I_NPOS: sp_runtime::curve::PiecewiseLinear = curve!(
min_inflation: 0_025_000,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand All @@ -141,7 +141,7 @@ pallet_staking_reward_curve::build! {
);
}
parameter_types! {
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS;
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear = &I_NPOS;
}

pub type Extrinsic = sp_runtime::testing::TestXt<Call, ()>;
Expand Down
2 changes: 1 addition & 1 deletion frame/staking/reward-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use syn::parse::{Parse, ParseStream};
/// use sp_runtime::curve::PiecewiseLinear;
///
/// pallet_staking_reward_curve::build! {
/// const I_NPOS: PiecewiseLinear<'static> = curve!(
/// const I_NPOS: PiecewiseLinear = curve!(
/// min_inflation: 0_025_000,
/// max_inflation: 0_100_000,
/// ideal_stake: 0_500_000,
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/reward-curve/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

mod test_small_falloff {
pallet_staking_reward_curve::build! {
const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear<'static> = curve!(
const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear = curve!(
min_inflation: 0_020_000,
max_inflation: 0_200_000,
ideal_stake: 0_600_000,
Expand All @@ -33,7 +33,7 @@ mod test_small_falloff {

mod test_big_falloff {
pallet_staking_reward_curve::build! {
const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear<'static> = curve!(
const REWARD_CURVE: sp_runtime::curve::PiecewiseLinear = curve!(
min_inflation: 0_100_000,
max_inflation: 0_400_000,
ideal_stake: 0_400_000,
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use sp_runtime::{curve::PiecewiseLinear, traits::AtLeast32BitUnsigned, Perbill};
///
/// `era_duration` is expressed in millisecond.
pub fn compute_total_payout<N>(
yearly_inflation: &PiecewiseLinear<'static>,
yearly_inflation: &PiecewiseLinear,
npos_token_staked: N,
total_tokens: N,
era_duration: u64,
Expand All @@ -54,7 +54,7 @@ mod test {
use sp_runtime::curve::PiecewiseLinear;

pallet_staking_reward_curve::build! {
const I_NPOS: PiecewiseLinear<'static> = curve!(
const I_NPOS: PiecewiseLinear = curve!(
min_inflation: 0_025_000,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,8 @@ impl<Balance: Default> EraPayout<Balance> for () {
/// Adaptor to turn a `PiecewiseLinear` curve definition into an `EraPayout` impl, used for
/// backwards compatibility.
pub struct ConvertCurve<T>(sp_std::marker::PhantomData<T>);
impl<Balance: AtLeast32BitUnsigned + Clone, T: Get<&'static PiecewiseLinear<'static>>>
EraPayout<Balance> for ConvertCurve<T>
impl<Balance: AtLeast32BitUnsigned + Clone, T: Get<&'static PiecewiseLinear>> EraPayout<Balance>
for ConvertCurve<T>
{
fn era_payout(
total_staked: Balance,
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl pallet_timestamp::Config for Test {
}

pallet_staking_reward_curve::build! {
const I_NPOS: PiecewiseLinear<'static> = curve!(
const I_NPOS: PiecewiseLinear = curve!(
min_inflation: 0_025_000,
max_inflation: 0_100_000,
ideal_stake: 0_500_000,
Expand All @@ -212,7 +212,7 @@ pallet_staking_reward_curve::build! {
}
parameter_types! {
pub const BondingDuration: EraIndex = 3;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &I_NPOS;
pub const RewardCurve: &'static PiecewiseLinear = &I_NPOS;
pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(75);
}

Expand Down
19 changes: 8 additions & 11 deletions frame/support/src/storage/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,13 @@ where
/// Similar to a `BoundedVec`, but not owned and cannot be decoded.
#[derive(Encode, scale_info::TypeInfo)]
#[scale_info(skip_type_params(S))]
pub struct BoundedSlice<'a, T, S>(&'a [T], PhantomData<S>);
pub struct BoundedSlice<T: 'static, S>(&'static [T], PhantomData<S>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't see why this can be 'static, we should be parameterizing over the lifetime of the reference to the slice, but I don't know why rustc or clippy is telling us to just use 'static.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, thinking a little more, I think you are right. So then just add a clippy silencer here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rust-lang/rust#96956

Seems it is a bug atm (either to silence it or the warning itself)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea why it started annoying us with this. Does not make sense IMO.
Otherwise it would not work outside of static context anymore

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah perhaps we silence this lint globally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can not silence it. However, I also don't think this here is the correct solution. This is a bug in rustc and I also already looked into it. It is coming from the scale_info::TypeInfo derive macro. We need to wait for rustc to fix this.


// `BoundedSlice`s encode to something which will always decode into a `BoundedVec`,
// `WeakBoundedVec`, or a `Vec`.
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<BoundedVec<T, S>> for BoundedSlice<'a, T, S> {}
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<WeakBoundedVec<T, S>>
for BoundedSlice<'a, T, S>
{
}
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<Vec<T>> for BoundedSlice<'a, T, S> {}
impl<T: Encode + Decode, S: Get<u32>> EncodeLike<BoundedVec<T, S>> for BoundedSlice<T, S> {}
impl<T: Encode + Decode, S: Get<u32>> EncodeLike<WeakBoundedVec<T, S>> for BoundedSlice<T, S> {}
impl<T: Encode + Decode, S: Get<u32>> EncodeLike<Vec<T>> for BoundedSlice<T, S> {}

impl<T: PartialOrd, Bound: Get<u32>> PartialOrd for BoundedVec<T, Bound> {
fn partial_cmp(&self, other: &Self) -> Option<sp_std::cmp::Ordering> {
Expand All @@ -132,9 +129,9 @@ impl<T: Ord, Bound: Get<u32>> Ord for BoundedVec<T, Bound> {
}
}

impl<'a, T, S: Get<u32>> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {
impl<T: 'static, S: Get<u32>> TryFrom<&'static [T]> for BoundedSlice<T, S> {
type Error = ();
fn try_from(t: &'a [T]) -> Result<Self, Self::Error> {
fn try_from(t: &'static [T]) -> Result<Self, Self::Error> {
if t.len() < S::get() as usize {
Ok(BoundedSlice(t, PhantomData))
} else {
Expand All @@ -143,8 +140,8 @@ impl<'a, T, S: Get<u32>> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {
}
}

impl<'a, T, S> From<BoundedSlice<'a, T, S>> for &'a [T] {
fn from(t: BoundedSlice<'a, T, S>) -> Self {
impl<T: 'static, S> From<BoundedSlice<T, S>> for &'static [T] {
fn from(t: BoundedSlice<T, S>) -> Self {
t.0
}
}
Expand Down
6 changes: 3 additions & 3 deletions primitives/runtime/src/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use core::ops::Sub;

/// Piecewise Linear function in [0, 1] -> [0, 1].
#[derive(PartialEq, Eq, sp_core::RuntimeDebug, scale_info::TypeInfo)]
pub struct PiecewiseLinear<'a> {
pub struct PiecewiseLinear {
/// Array of points. Must be in order from the lowest abscissas to the highest.
pub points: &'a [(Perbill, Perbill)],
pub points: &'static [(Perbill, Perbill)],
/// The maximum value that can be returned.
pub maximum: Perbill,
}
Expand All @@ -36,7 +36,7 @@ fn abs_sub<N: Ord + Sub<Output = N> + Clone>(a: N, b: N) -> N where {
a.clone().max(b.clone()) - a.min(b)
}

impl<'a> PiecewiseLinear<'a> {
impl PiecewiseLinear {
/// Compute `f(n/d)*d` with `n <= d`. This is useful to avoid loss of precision.
pub fn calculate_for_fraction_times_denominator<N>(&self, n: N, d: N) -> N
where
Expand Down