Skip to content
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

Validate absolute path for substitute_type #577

Merged
merged 4 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 20 additions & 1 deletion codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use syn::{

#[derive(Debug, PartialEq, Eq)]
pub struct ItemMod {
// attrs: Vec<syn::Attribute>,
vis: syn::Visibility,
mod_token: token::Mod,
pub ident: syn::Ident,
Expand Down Expand Up @@ -103,6 +102,26 @@ impl From<syn::Item> for Item {
if let Some(attr) = substitute_attrs.get(0) {
let use_path = &use_.tree;
let substitute_with: syn::TypePath = syn::parse_quote!( #use_path );

let is_crate = substitute_with
.path
.segments
.first()
.map(|segment| segment.ident == "crate")
.unwrap_or(false);

// Check if the substitute path is a global absolute path, meaning it
// is prefixed with `::` or `crate`.
//
// Note: the leading colon is lost when parsing to `syn::TypePath` via
// `syn::parse_quote!`. Therefore, inspect `use_`'s leading colon.
if use_.leading_colon.is_none() && !is_crate {
abort!(
use_path.span(),
"The substitute path must be a global absolute path; try prefixing with `::` or `crate`"
)
}

let type_substitute = SubxtItem::TypeSubstitute {
generated_type_path: attr.substitute_type(),
substitute_with,
Expand Down
2 changes: 1 addition & 1 deletion testing/test-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async fn run() {
)]
pub mod node_runtime {{
#[subxt(substitute_type = "sp_arithmetic::per_things::Perbill")]
use sp_runtime::Perbill;
use ::sp_runtime::Perbill;
}}
"#,
metadata_path
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[subxt::subxt(runtime_metadata_path = "../../../artifacts/polkadot_metadata.scale")]
pub mod node_runtime {
#[subxt::subxt(substitute_type = "sp_arithmetic::per_things::Perbill")]
use sp_runtime::Perbill;
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: The substitute path must be a global absolute path; try prefixing with `::` or `crate`
--> src/incorrect/substitute_path_not_absolute.rs:4:9
|
4 | use sp_runtime::Perbill;
| ^^^^^^^^^^
14 changes: 14 additions & 0 deletions testing/ui-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
#![cfg(test)]

//! UI test set uses [`trybuild`](https://docs.rs/trybuild/latest/trybuild/index.html) to
//! check whether expected valid examples of code compile correctly, and for incorrect ones
//! errors are helpful and valid (e.g. have correct spans).
//!
//!
//! Use with `TRYBUILD=overwrite` after updating codebase (see `trybuild` docs for more details on that)
//! to automatically regenerate `stderr` files, but don't forget to check that new files make sense.

mod dispatch_errors;
mod storage;
mod utils;
Expand Down Expand Up @@ -49,3 +57,9 @@ fn ui_tests() {
dispatch_errors::metadata_array_dispatch_error(),
));
}

#[test]
fn ui_fail() {
let t = trybuild::TestCases::new();
t.compile_fail("src/incorrect/*.rs");
}