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

Commit 1ff1df5

Browse files
KiChjangchevdor
authored andcommitted
Improve MultiLocation conversion functions in xcm-procedural (#3690)
* Use fully-qualified paths to reference core traits in proc macro * Ensure a canonicalized v0 MultiLocation first before attempting to convert to v1 * Fix failing test
1 parent e956acf commit 1ff1df5

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

xcm/procedural/src/v0/multilocation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn generate_conversion_from_v1() -> TokenStream {
9696
.collect::<TokenStream>();
9797

9898
quote! {
99-
impl TryFrom<crate::v1::MultiLocation> for MultiLocation {
99+
impl core::convert::TryFrom<crate::v1::MultiLocation> for MultiLocation {
100100
type Error = ();
101101
fn try_from(v1: crate::v1::MultiLocation) -> core::result::Result<Self, ()> {
102102
let mut res = MultiLocation::Null;

xcm/procedural/src/v1/multilocation.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn generate_conversion_from_v0() -> TokenStream {
172172
if #( #parent_idents.is_parent() )&&* =>
173173
Ok(MultiLocation {
174174
parents: #parent_count,
175-
interior: #junction_variant( #( #junction_idents.try_into()? ),* ),
175+
interior: #junction_variant( #( core::convert::TryInto::try_into(#junction_idents)? ),* ),
176176
}),
177177
}
178178
})
@@ -184,16 +184,18 @@ fn generate_conversion_from_v0() -> TokenStream {
184184
Ok(MultiLocation::ancestor(#num_ancestors)),
185185
#intermediate_match_arms
186186
crate::v0::MultiLocation::#variant( #(#idents),* ) =>
187-
Ok( #variant( #( #idents.try_into()? ),* ).into() ),
187+
Ok( #variant( #( core::convert::TryInto::try_into(#idents)? ),* ).into() ),
188188
}
189189
})
190190
.collect::<TokenStream>();
191191

192192
quote! {
193-
impl TryFrom<crate::v0::MultiLocation> for MultiLocation {
193+
impl core::convert::TryFrom<crate::v0::MultiLocation> for MultiLocation {
194194
type Error = ();
195-
fn try_from(v0: crate::v0::MultiLocation) -> core::result::Result<Self, ()> {
195+
fn try_from(mut v0: crate::v0::MultiLocation) -> core::result::Result<Self, ()> {
196196
use Junctions::*;
197+
198+
v0.canonicalize();
197199
match v0 {
198200
crate::v0::MultiLocation::Null => Ok(Here.into()),
199201
#match_variants

xcm/src/v0/multi_location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! Cross-Consensus Message format data structures.
1818
1919
use super::Junction;
20-
use core::{convert::TryFrom, mem, result};
20+
use core::{mem, result};
2121
use parity_scale_codec::{self, Decode, Encode};
2222

2323
/// A relative path between state-bearing consensus systems.

xcm/src/v1/multilocation.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
//! Cross-Consensus Message format data structures.
1818
1919
use super::Junction;
20-
use core::{
21-
convert::{TryFrom, TryInto},
22-
mem, result,
23-
};
20+
use core::{convert::TryFrom, mem, result};
2421
use parity_scale_codec::{Decode, Encode};
2522

2623
/// A relative path between state-bearing consensus systems.
@@ -898,7 +895,7 @@ mod tests {
898895
);
899896
assert_eq!(
900897
v0::MultiLocation::X2(v0::Junction::Parachain(88), v0::Junction::Parent).try_into(),
901-
Err::<MultiLocation, ()>(()),
898+
Ok(MultiLocation::here()),
902899
);
903900
assert_eq!(
904901
v0::MultiLocation::X3(

0 commit comments

Comments
 (0)