Skip to content

Commit

Permalink
Allow raw identifiers e.g. r#mod (#149)
Browse files Browse the repository at this point in the history
* Allow raw identifiers

* Fix UI tests

* UI tests with latest nightly

* Add UI test
  • Loading branch information
ascjones authored Apr 11, 2022
1 parent 0c4ff0b commit e48f1f9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
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>();
}

0 comments on commit e48f1f9

Please sign in to comment.