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

Span::source_text panics with multibyte source #408

Closed
aatifsyed opened this issue Oct 5, 2023 · 1 comment · Fixed by #409
Closed

Span::source_text panics with multibyte source #408

aatifsyed opened this issue Oct 5, 2023 · 1 comment · Fixed by #409

Comments

@aatifsyed
Copy link

    #[test]
    fn proc_macro2_source_text_is_correct_for_single_byte() {
        let source_code = "const FOO: () = ();";
        let ast = syn::parse_str::<syn::ItemConst>(source_code).unwrap();
        assert_eq!("FOO", ast.ident.span().source_text().unwrap())
    }

    #[test]
    #[should_panic = "is not a char boundary"]
    fn proc_macro2_source_text_is_incorrect_for_multibyte() {
        let source_code = "const 𓀕: () = ();";
        let ast = syn::parse_str::<syn::ItemConst>(source_code).unwrap();
        let reported = ast.ident.span().source_text(); // boom
        assert_eq!("𓀕", reported.unwrap())
    }

Note this is just calling the method, not unwrapping the Option:

thread 'tests::proc_macro2_source_text_is_incorrect_for_multibyte' panicked at 'byte index 7 is not a char boundary; it is inside '𓀕' (bytes 6..10) of `const 𓀕: () = ();`', /home/aatif/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/fallback.rs:369:9
...
   6: <alloc::string::String as core::ops::index::Index<core::ops::range::Range<usize>>>::index
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/alloc/src/string.rs:2350:10
   7: proc_macro2::fallback::FileInfo::source_text
             at /home/aatif/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/fallback.rs:369:9
   8: proc_macro2::fallback::Span::source_text::{{closure}}
             at /home/aatif/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/fallback.rs:569:43
   9: std::thread::local::LocalKey<T>::try_with
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/std/src/thread/local.rs:270:16
  10: std::thread::local::LocalKey<T>::with
             at /rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/std/src/thread/local.rs:246:9
  11: proc_macro2::fallback::Span::source_text
             at /home/aatif/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/fallback.rs:569:22
  12: proc_macro2::Span::source_text
             at /home/aatif/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/lib.rs:522:9
  13: synsert::tests::proc_macro2_source_text_is_incorrect_for_multibyte
             at ./src/lib.rs:165:24
  14: synsert::tests::proc_macro2_source_text_is_incorrect_for_multibyte::{{closure}}
             at ./src/lib.rs:162:61
...

proc-macro2 1.0.67 (latest)
Not called from a proc-macro context

@dtolnay
Copy link
Owner

dtolnay commented Oct 6, 2023

I have published a fix in proc-macro2 1.0.68.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants