FAIL_TO_FAIL sequencelengths 0 0 | created_at stringdate 2022-09-29 09:55:02 2023-10-09 08:56:29 | PASS_TO_FAIL sequencelengths 0 0 | patch stringclasses 4 values | FAIL_TO_PASS sequencelengths 1 2 | pull_number int64 354 411 | instance_id stringclasses 4 values | base_commit stringclasses 4 values | hints_text stringclasses 2 values | version stringclasses 1 value | repo stringclasses 1 value | PASS_TO_PASS sequencelengths 64 70 | problem_statement stringclasses 4 values | test_patch stringclasses 4 values | environment_setup_commit stringclasses 1 value | issue_numbers sequencelengths 1 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
[] | 2023-10-06T12:04:55Z | [] | diff --git a/src/fallback.rs b/src/fallback.rs
--- a/src/fallback.rs
+++ b/src/fallback.rs
@@ -364,8 +364,13 @@ impl FileInfo {
fn source_text(&self, span: Span) -> String {
let lo = (span.lo - self.span.lo) as usize;
- let hi = (span.hi - self.span.lo) as usize;
- self.source_text[lo..hi].to_owned()
+ let trunc_lo = &self.source_text[lo..];
+ let char_len = (span.hi - span.lo) as usize;
+ let source_text = match trunc_lo.char_indices().nth(char_len) {
+ Some((offset, _ch)) => &trunc_lo[..offset],
+ None => trunc_lo,
+ };
+ source_text.to_owned()
}
}
| [
"source_text"
] | 409 | dtolnay__proc-macro2-409 | 07bb590e9706c65f4c850f0163ec05333ab636c9 | 1.0 | dtolnay/proc-macro2 | [
"closed_immediately",
"carriage_return",
"incomplete",
"lit",
"Delimiter",
"Group",
"Ident",
"LexError",
"Literal",
"Spacing",
"Punct",
"Span",
"TokenStream",
"unwind_safe::Delimiter",
"TokenTree",
"unwind_safe::Ident",
"unwind_safe::Group",
"unwind_safe::LexError",
"unwind_safe:... | `Span::source_text` panics with multibyte source
```rust
#[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
| diff --git a/tests/test.rs b/tests/test.rs
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -325,6 +325,15 @@ fn literal_span() {
assert!(positive.subspan(1..4).is_none());
}
+#[cfg(span_locations)]
+#[test]
+fn source_text() {
+ let input = " π ";
+ let tokens = input.parse::<proc_macro2::TokenStream>().unwrap();
+ let ident = tokens.into_iter().next().unwrap();
+ assert_eq!("π", ident.span().source_text().unwrap());
+}
+
#[test]
fn roundtrip() {
fn roundtrip(p: &str) {
| 1edd1b993b79d16f60a85f32a320d9430dfde8a8 | [
"408"
] | |
[] | 2022-09-29T09:55:02Z | [] | diff --git a/src/fallback.rs b/src/fallback.rs
--- a/src/fallback.rs
+++ b/src/fallback.rs
@@ -182,7 +182,13 @@ impl FromStr for TokenStream {
fn from_str(src: &str) -> Result<TokenStream, LexError> {
// Create a dummy file & add it to the source map
- let cursor = get_cursor(src);
+ let mut cursor = get_cursor(src);
+
+ // Strip a byte order mark if present
+ const BYTE_ORDER_MARK: &str = "\u{feff}";
+ if cursor.starts_with(BYTE_ORDER_MARK) {
+ cursor = cursor.advance(BYTE_ORDER_MARK.len());
+ }
parse::token_stream(cursor)
}
diff --git a/src/parse.rs b/src/parse.rs
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -14,7 +14,7 @@ pub(crate) struct Cursor<'a> {
}
impl<'a> Cursor<'a> {
- fn advance(&self, bytes: usize) -> Cursor<'a> {
+ pub fn advance(&self, bytes: usize) -> Cursor<'a> {
let (_front, rest) = self.rest.split_at(bytes);
Cursor {
rest,
diff --git a/src/parse.rs b/src/parse.rs
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -23,7 +23,7 @@ impl<'a> Cursor<'a> {
}
}
- fn starts_with(&self, s: &str) -> bool {
+ pub fn starts_with(&self, s: &str) -> bool {
self.rest.starts_with(s)
}
| [
"byte_order_mark"
] | 354 | dtolnay__proc-macro2-354 | f26128d5d6c94c62ce70683f6f4363766963a256 | 1.0 | dtolnay/proc-macro2 | [
"closed_immediately",
"incomplete",
"carriage_return",
"lit",
"Delimiter",
"Group",
"Ident",
"LexError",
"Literal",
"Punct",
"Spacing",
"Span",
"TokenStream",
"TokenTree",
"unwind_safe::Delimiter",
"unwind_safe::Group",
"unwind_safe::Ident",
"unwind_safe::LexError",
"unwind_safe:... | Fails to parse string starting with byte order mark
```rust
let string = "\u{feff}foo";
eprintln!("{:?}", string.parse::<TokenStream>());
```
proc_macro::TokenStream:
```console
Ok(TokenStream [Ident { ident: "foo", span: #6 bytes(31..42) }])
```
proc_macro2::TokenStream:
```console
Err(LexError { span: Span })
```
| diff --git a/tests/test.rs b/tests/test.rs
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -630,3 +630,16 @@ fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usi
}
}
}
+
+#[test]
+fn byte_order_mark() {
+ let string = "\u{feff}foo";
+ let tokens = string.parse::<TokenStream>().unwrap();
+ match tokens.into_iter().next().unwrap() {
+ TokenTree::Ident(ident) => assert_eq!(ident, "foo"),
+ _ => unreachable!(),
+ }
+
+ let string = "foo\u{feff}";
+ string.parse::<TokenStream>().unwrap_err();
+}
| 1edd1b993b79d16f60a85f32a320d9430dfde8a8 | [
"353"
] | |
[] | 2023-10-09T08:56:29Z | [] | diff --git a/src/fallback.rs b/src/fallback.rs
--- a/src/fallback.rs
+++ b/src/fallback.rs
@@ -364,7 +364,10 @@ impl FileInfo {
fn source_text(&self, span: Span) -> String {
let lo = (span.lo - self.span.lo) as usize;
- let trunc_lo = &self.source_text[lo..];
+ let trunc_lo = match self.source_text.char_indices().nth(lo) {
+ Some((offset, _ch)) => &self.source_text[offset..],
+ None => return String::new(),
+ };
let char_len = (span.hi - span.lo) as usize;
let source_text = match trunc_lo.char_indices().nth(char_len) {
Some((offset, _ch)) => &trunc_lo[..offset],
| [
"source_text"
] | 411 | dtolnay__proc-macro2-411 | 12eddc03a40e8393e82f7ef1dadbaaabdcb00a08 | 1.0 | dtolnay/proc-macro2 | [
"closed_immediately",
"carriage_return",
"incomplete",
"lit",
"Delimiter",
"Group",
"Ident",
"Literal",
"LexError",
"Punct",
"Spacing",
"Span",
"TokenTree",
"TokenStream",
"unwind_safe::Group",
"unwind_safe::Delimiter",
"unwind_safe::Ident",
"unwind_safe::LexError",
"unwind_safe:... | Issue with multibyte chars in source_text() computation
It treats `lo` as a byte index, while it is actually a character index:
https://github.com/dtolnay/proc-macro2/blob/7f5533d6cc9ff783d174aec4e3be2caa202b62ca/src/fallback.rs#L367
I expect this test to pass, but it does not:
```rust
#[cfg(span_locations)]
#[test]
fn source_text() {
let input = " π c ";
let mut tokens = input
.parse::<proc_macro2::TokenStream>()
.unwrap()
.into_iter();
let ident1 = tokens.next().unwrap();
assert_eq!("π", ident1.span().source_text().unwrap());
let ident2 = tokens.next().unwrap();
assert_eq!("π", ident2.span().source_text().unwrap());
}
```
Panics with (as character `π` occupies byte 5 and 6)
```
---- source_text stdout ----
thread 'source_text' panicked at 'byte index 6 is not a char boundary; it is inside 'π' (bytes 4..8) of ` π c `', src/fallback.rs:367:25
```
| diff --git a/tests/test.rs b/tests/test.rs
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -328,10 +328,17 @@ fn literal_span() {
#[cfg(span_locations)]
#[test]
fn source_text() {
- let input = " π ";
- let tokens = input.parse::<proc_macro2::TokenStream>().unwrap();
- let ident = tokens.into_iter().next().unwrap();
+ let input = " π c ";
+ let mut tokens = input
+ .parse::<proc_macro2::TokenStream>()
+ .unwrap()
+ .into_iter();
+
+ let ident = tokens.next().unwrap();
assert_eq!("π", ident.span().source_text().unwrap());
+
+ let ident = tokens.next().unwrap();
+ assert_eq!("c", ident.span().source_text().unwrap());
}
#[test]
| 1edd1b993b79d16f60a85f32a320d9430dfde8a8 | [
"410"
] | |
[] | 2023-04-03T14:04:06Z | [] | diff --git a/src/fallback.rs b/src/fallback.rs
--- a/src/fallback.rs
+++ b/src/fallback.rs
@@ -958,12 +958,20 @@ impl Literal {
pub fn string(t: &str) -> Literal {
let mut repr = String::with_capacity(t.len() + 2);
repr.push('"');
- for c in t.chars() {
- if c == '\'' {
+ let mut chars = t.chars();
+ while let Some(ch) = chars.next() {
+ if ch == '\0'
+ && chars
+ .as_str()
+ .starts_with(|next| '0' <= next && next <= '7')
+ {
+ // circumvent clippy::octal_escapes lint
+ repr.push_str("\\x00");
+ } else if ch == '\'' {
// escape_debug turns this into "\'" which is unnecessary.
- repr.push(c);
+ repr.push(ch);
} else {
- repr.extend(c.escape_debug());
+ repr.extend(ch.escape_debug());
}
}
repr.push('"');
diff --git a/src/fallback.rs b/src/fallback.rs
--- a/src/fallback.rs
+++ b/src/fallback.rs
@@ -985,16 +993,21 @@ impl Literal {
pub fn byte_string(bytes: &[u8]) -> Literal {
let mut escaped = "b\"".to_string();
- for b in bytes {
+ let mut bytes = bytes.iter();
+ while let Some(&b) = bytes.next() {
#[allow(clippy::match_overlapping_arm)]
- match *b {
- b'\0' => escaped.push_str(r"\0"),
+ match b {
+ b'\0' => escaped.push_str(match bytes.as_slice().first() {
+ // circumvent clippy::octal_escapes lint
+ Some(b'0'..=b'7') => r"\x00",
+ _ => r"\0",
+ }),
b'\t' => escaped.push_str(r"\t"),
b'\n' => escaped.push_str(r"\n"),
b'\r' => escaped.push_str(r"\r"),
b'"' => escaped.push_str("\\\""),
b'\\' => escaped.push_str("\\\\"),
- b'\x20'..=b'\x7E' => escaped.push(*b as char),
+ b'\x20'..=b'\x7E' => escaped.push(b as char),
_ => {
let _ = write!(escaped, "\\x{:02X}", b);
}
| [
"literal_byte_string",
"literal_string"
] | 380 | dtolnay__proc-macro2-380 | c4a3e197f086b9f21880c979f7c87a04808660ac | https://github.com/dtolnay/proc-macro2/blob/master/src/fallback.rs#L932
Actually this only triggers if the `\0` is followed by a digit. | 1.0 | dtolnay/proc-macro2 | [
"closed_immediately",
"carriage_return",
"incomplete",
"lit",
"Delimiter",
"Group",
"Literal",
"Ident",
"Punct",
"LexError",
"Spacing",
"Span",
"unwind_safe::Delimiter",
"TokenStream",
"TokenTree",
"unwind_safe::Group",
"unwind_safe::Ident",
"unwind_safe::LexError",
"unwind_safe:... | `LitByteStr` produces tokens that trigger `clippy::octal-escapes`
Not sure whether to file this here or in `quote`.
`LitByteStr::new(&[65, 0], ...)` quotes as `b"a\0"`, which triggers [this default clippy lint](https://rust-lang.github.io/rust-clippy/master/#octal_escapes). Whether that lint should be default is a different question, but it'd be nice if syn output would be clippy clean. This would mean representing `0u8` by `\x00` instead of `\0`.
| diff --git a/tests/test.rs b/tests/test.rs
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -1,7 +1,8 @@
#![allow(
clippy::assertions_on_result_states,
clippy::items_after_statements,
- clippy::non_ascii_literal
+ clippy::non_ascii_literal,
+ clippy::octal_escapes
)]
use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
diff --git a/tests/test.rs b/tests/test.rs
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -114,6 +115,11 @@ fn literal_string() {
assert_eq!(Literal::string("foo").to_string(), "\"foo\"");
assert_eq!(Literal::string("\"").to_string(), "\"\\\"\"");
assert_eq!(Literal::string("didn't").to_string(), "\"didn't\"");
+
+ let repr = Literal::string("a\00b\07c\08d\0e\0").to_string();
+ if repr != "\"a\\x000b\\x007c\\u{0}8d\\u{0}e\\u{0}\"" {
+ assert_eq!(repr, "\"a\\x000b\\x007c\\08d\\0e\\0\"");
+ }
}
#[test]
diff --git a/tests/test.rs b/tests/test.rs
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -147,6 +153,10 @@ fn literal_byte_string() {
Literal::byte_string(b"\0\t\n\r\"\\2\x10").to_string(),
"b\"\\0\\t\\n\\r\\\"\\\\2\\x10\"",
);
+ assert_eq!(
+ Literal::byte_string(b"a\00b\07c\08d\0e\0").to_string(),
+ "b\"a\\x000b\\x007c\\08d\\0e\\0\"",
+ );
}
#[test]
| 1edd1b993b79d16f60a85f32a320d9430dfde8a8 | [
"363"
] |
README.md exists but content is empty.
- Downloads last month
- 5