From f5fde1786aa3d8b4cfc7719755d0b3427944bd9c Mon Sep 17 00:00:00 2001 From: ascjones Date: Fri, 8 Apr 2022 17:11:08 +0100 Subject: [PATCH 1/4] Allow raw identifiers --- src/ty/path.rs | 10 ++++++++++ src/utils.rs | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ty/path.rs b/src/ty/path.rs index d18be62b..f19f57dc 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -205,6 +205,16 @@ mod tests { ); } + #[test] + fn path_with_raw_identifers_ok() { + assert_eq!( + Path::from_segments(vec!["r#mod", "r#Struct"]), + Ok(Path { + segments: vec!["r#mod", "r#Struct"] + }) + ); + } + #[test] fn path_err() { assert_eq!(Path::from_segments(vec![]), Err(PathError::MissingSegments)); diff --git a/src/utils.rs b/src/utils.rs index 63a93e23..88f76cf2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,7 +19,9 @@ pub fn is_rust_identifier(s: &str) -> bool { if !s.is_ascii() { return false } - if let Some((&head, tail)) = s.as_bytes().split_first() { + // Trim valid raw identifier prefix + let trimmed = s.trim_start_matches("r#"); + if let Some((&head, tail)) = trimmed.as_bytes().split_first() { // Check if head and tail make up a proper Rust identifier. let head_ok = head == b'_' || (b'a'..=b'z').contains(&head) From d9a2fae1048a35e2bfe873409352ba8d2c8520aa Mon Sep 17 00:00:00 2001 From: ascjones Date: Fri, 8 Apr 2022 17:12:28 +0100 Subject: [PATCH 2/4] Fix UI tests --- test_suite/tests/ui/fail_duplicate_bounds_params.stderr | 2 +- test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test_suite/tests/ui/fail_duplicate_bounds_params.stderr b/test_suite/tests/ui/fail_duplicate_bounds_params.stderr index cd76cddf..7c3d8be9 100644 --- a/test_suite/tests/ui/fail_duplicate_bounds_params.stderr +++ b/test_suite/tests/ui/fail_duplicate_bounds_params.stderr @@ -2,4 +2,4 @@ error: Duplicate `bounds` attributes --> tests/ui/fail_duplicate_bounds_params.rs:6:1 | 6 | #[scale_info(bounds(), bounds())] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^ diff --git a/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr b/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr index 90d52022..6246d1c9 100644 --- a/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr +++ b/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr @@ -2,13 +2,13 @@ error: Invalid attribute on field, only `#[codec(skip)]`, `#[codec(compact)]` an --> tests/ui/fail_with_invalid_codec_attrs.rs:8:7 | 8 | #[codec(skip, compact)] - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^^^ error: Invalid attribute on field, only `#[codec(skip)]`, `#[codec(compact)]` and `#[codec(encoded_as = "$EncodeAs")]` are accepted. --> tests/ui/fail_with_invalid_codec_attrs.rs:14:19 | 14 | Thing(#[codec(index = 3)] u32), - | ^^^^^^^^^ + | ^^^^^ error: expected literal --> tests/ui/fail_with_invalid_codec_attrs.rs:19:21 From f15f1660da0bdceed7e9bb76cbcb3e06736cafcb Mon Sep 17 00:00:00 2001 From: ascjones Date: Fri, 8 Apr 2022 17:26:57 +0100 Subject: [PATCH 3/4] UI tests with latest nightly --- .../tests/ui/fail_duplicate_bounds_params.stderr | 2 +- test_suite/tests/ui/fail_missing_derive.stderr | 10 ++++++++++ test_suite/tests/ui/fail_unions.stderr | 10 ++++++++++ .../tests/ui/fail_with_invalid_codec_attrs.stderr | 4 ++-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/test_suite/tests/ui/fail_duplicate_bounds_params.stderr b/test_suite/tests/ui/fail_duplicate_bounds_params.stderr index 7c3d8be9..cd76cddf 100644 --- a/test_suite/tests/ui/fail_duplicate_bounds_params.stderr +++ b/test_suite/tests/ui/fail_duplicate_bounds_params.stderr @@ -2,4 +2,4 @@ error: Duplicate `bounds` attributes --> tests/ui/fail_duplicate_bounds_params.rs:6:1 | 6 | #[scale_info(bounds(), bounds())] - | ^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/test_suite/tests/ui/fail_missing_derive.stderr b/test_suite/tests/ui/fail_missing_derive.stderr index 74ba2095..801764b4 100644 --- a/test_suite/tests/ui/fail_missing_derive.stderr +++ b/test_suite/tests/ui/fail_missing_derive.stderr @@ -4,6 +4,16 @@ error[E0277]: the trait bound `PawType: TypeInfo` is not satisfied 19 | assert_type_info::>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TypeInfo` is not implemented for `PawType` | + = help: the following other types implement trait `TypeInfo`: + &T + &mut T + () + (A, B) + (A, B, C) + (A, B, C, D) + (A, B, C, D, E) + (A, B, C, D, E, F) + and 54 others note: required because of the requirements on the impl of `TypeInfo` for `Cat` --> tests/ui/fail_missing_derive.rs:8:10 | diff --git a/test_suite/tests/ui/fail_unions.stderr b/test_suite/tests/ui/fail_unions.stderr index 609a3220..3b0248d1 100644 --- a/test_suite/tests/ui/fail_unions.stderr +++ b/test_suite/tests/ui/fail_unions.stderr @@ -14,6 +14,16 @@ error[E0277]: the trait bound `Commonwealth: TypeInfo` is not satisfied 14 | assert_type_info::(); | ^^^^^^^^^^^^ the trait `TypeInfo` is not implemented for `Commonwealth` | + = help: the following other types implement trait `TypeInfo`: + &T + &mut T + () + (A, B) + (A, B, C) + (A, B, C, D) + (A, B, C, D, E) + (A, B, C, D, E, F) + and 53 others note: required by a bound in `assert_type_info` --> tests/ui/fail_unions.rs:11:24 | diff --git a/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr b/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr index 6246d1c9..90d52022 100644 --- a/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr +++ b/test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr @@ -2,13 +2,13 @@ error: Invalid attribute on field, only `#[codec(skip)]`, `#[codec(compact)]` an --> tests/ui/fail_with_invalid_codec_attrs.rs:8:7 | 8 | #[codec(skip, compact)] - | ^^^^^ + | ^^^^^^^^^^^^^^^^^^^^ error: Invalid attribute on field, only `#[codec(skip)]`, `#[codec(compact)]` and `#[codec(encoded_as = "$EncodeAs")]` are accepted. --> tests/ui/fail_with_invalid_codec_attrs.rs:14:19 | 14 | Thing(#[codec(index = 3)] u32), - | ^^^^^ + | ^^^^^^^^^ error: expected literal --> tests/ui/fail_with_invalid_codec_attrs.rs:19:21 From f944f94ed6148798eff5c70cac7ae677e0fa39bb Mon Sep 17 00:00:00 2001 From: ascjones Date: Mon, 11 Apr 2022 09:39:10 +0100 Subject: [PATCH 4/4] Add UI test --- test_suite/tests/ui/pass_raw_identifers.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test_suite/tests/ui/pass_raw_identifers.rs diff --git a/test_suite/tests/ui/pass_raw_identifers.rs b/test_suite/tests/ui/pass_raw_identifers.rs new file mode 100644 index 00000000..9e28649c --- /dev/null +++ b/test_suite/tests/ui/pass_raw_identifers.rs @@ -0,0 +1,21 @@ +use info::{self as scale_info}; +use scale_info::TypeInfo; + +#[allow(dead_code, non_camel_case_types)] +mod r#mod { + use super::*; + #[derive(TypeInfo)] + pub enum r#enum { + r#true, + } + #[derive(TypeInfo)] + pub struct r#struct { + r#try: r#enum, + } +} + +fn assert_type_info() {} + +fn main() { + assert_type_info::(); +}