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

Allow raw identifiers e.g. r#mod #149

Merged
merged 4 commits into from
Apr 11, 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
10 changes: 10 additions & 0 deletions src/ty/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test_suite/tests/ui/fail_missing_derive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ error[E0277]: the trait bound `PawType<u16>: TypeInfo` is not satisfied
19 | assert_type_info::<Cat<bool, u8, u16>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TypeInfo` is not implemented for `PawType<u16>`
|
= 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<bool, u8, u16>`
--> tests/ui/fail_missing_derive.rs:8:10
|
Expand Down
10 changes: 10 additions & 0 deletions test_suite/tests/ui/fail_unions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ error[E0277]: the trait bound `Commonwealth: TypeInfo` is not satisfied
14 | assert_type_info::<Commonwealth>();
| ^^^^^^^^^^^^ 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
|
Expand Down
21 changes: 21 additions & 0 deletions test_suite/tests/ui/pass_raw_identifers.rs
Original file line number Diff line number Diff line change
@@ -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<T: TypeInfo + 'static>() {}

fn main() {
assert_type_info::<r#mod::r#struct>();
}