test.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #![allow(clippy::non_ascii_literal)]
  2. use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
  3. use std::panic;
  4. use std::str::{self, FromStr};
  5. #[test]
  6. fn idents() {
  7. assert_eq!(
  8. Ident::new("String", Span::call_site()).to_string(),
  9. "String"
  10. );
  11. assert_eq!(Ident::new("fn", Span::call_site()).to_string(), "fn");
  12. assert_eq!(Ident::new("_", Span::call_site()).to_string(), "_");
  13. }
  14. #[test]
  15. #[cfg(procmacro2_semver_exempt)]
  16. fn raw_idents() {
  17. assert_eq!(
  18. Ident::new_raw("String", Span::call_site()).to_string(),
  19. "r#String"
  20. );
  21. assert_eq!(Ident::new_raw("fn", Span::call_site()).to_string(), "r#fn");
  22. assert_eq!(Ident::new_raw("_", Span::call_site()).to_string(), "r#_");
  23. }
  24. #[test]
  25. #[should_panic(expected = "Ident is not allowed to be empty; use Option<Ident>")]
  26. fn ident_empty() {
  27. Ident::new("", Span::call_site());
  28. }
  29. #[test]
  30. #[should_panic(expected = "Ident cannot be a number; use Literal instead")]
  31. fn ident_number() {
  32. Ident::new("255", Span::call_site());
  33. }
  34. #[test]
  35. #[should_panic(expected = "\"a#\" is not a valid Ident")]
  36. fn ident_invalid() {
  37. Ident::new("a#", Span::call_site());
  38. }
  39. #[test]
  40. #[should_panic(expected = "not a valid Ident")]
  41. fn raw_ident_empty() {
  42. Ident::new("r#", Span::call_site());
  43. }
  44. #[test]
  45. #[should_panic(expected = "not a valid Ident")]
  46. fn raw_ident_number() {
  47. Ident::new("r#255", Span::call_site());
  48. }
  49. #[test]
  50. #[should_panic(expected = "\"r#a#\" is not a valid Ident")]
  51. fn raw_ident_invalid() {
  52. Ident::new("r#a#", Span::call_site());
  53. }
  54. #[test]
  55. #[should_panic(expected = "not a valid Ident")]
  56. fn lifetime_empty() {
  57. Ident::new("'", Span::call_site());
  58. }
  59. #[test]
  60. #[should_panic(expected = "not a valid Ident")]
  61. fn lifetime_number() {
  62. Ident::new("'255", Span::call_site());
  63. }
  64. #[test]
  65. fn lifetime_invalid() {
  66. let result = panic::catch_unwind(|| Ident::new("'a#", Span::call_site()));
  67. match result {
  68. Err(box_any) => {
  69. let message = box_any.downcast_ref::<String>().unwrap();
  70. let expected1 = r#""\'a#" is not a valid Ident"#; // 1.31.0 .. 1.53.0
  71. let expected2 = r#""'a#" is not a valid Ident"#; // 1.53.0 ..
  72. assert!(
  73. message == expected1 || message == expected2,
  74. "panic message does not match expected string\n\
  75. \x20 panic message: `{:?}`\n\
  76. \x20expected message: `{:?}`",
  77. message,
  78. expected2,
  79. );
  80. }
  81. Ok(_) => panic!("test did not panic as expected"),
  82. }
  83. }
  84. #[test]
  85. fn literal_string() {
  86. assert_eq!(Literal::string("foo").to_string(), "\"foo\"");
  87. assert_eq!(Literal::string("\"").to_string(), "\"\\\"\"");
  88. assert_eq!(Literal::string("didn't").to_string(), "\"didn't\"");
  89. }
  90. #[test]
  91. fn literal_raw_string() {
  92. "r\"\r\n\"".parse::<TokenStream>().unwrap();
  93. }
  94. #[test]
  95. fn literal_byte_string() {
  96. assert_eq!(Literal::byte_string(b"").to_string(), "b\"\"");
  97. assert_eq!(
  98. Literal::byte_string(b"\0\t\n\r\"\\2\x10").to_string(),
  99. "b\"\\0\\t\\n\\r\\\"\\\\2\\x10\"",
  100. );
  101. }
  102. #[test]
  103. fn literal_character() {
  104. assert_eq!(Literal::character('x').to_string(), "'x'");
  105. assert_eq!(Literal::character('\'').to_string(), "'\\''");
  106. assert_eq!(Literal::character('"').to_string(), "'\"'");
  107. }
  108. #[test]
  109. fn literal_integer() {
  110. assert_eq!(Literal::u8_suffixed(10).to_string(), "10u8");
  111. assert_eq!(Literal::u16_suffixed(10).to_string(), "10u16");
  112. assert_eq!(Literal::u32_suffixed(10).to_string(), "10u32");
  113. assert_eq!(Literal::u64_suffixed(10).to_string(), "10u64");
  114. assert_eq!(Literal::u128_suffixed(10).to_string(), "10u128");
  115. assert_eq!(Literal::usize_suffixed(10).to_string(), "10usize");
  116. assert_eq!(Literal::i8_suffixed(10).to_string(), "10i8");
  117. assert_eq!(Literal::i16_suffixed(10).to_string(), "10i16");
  118. assert_eq!(Literal::i32_suffixed(10).to_string(), "10i32");
  119. assert_eq!(Literal::i64_suffixed(10).to_string(), "10i64");
  120. assert_eq!(Literal::i128_suffixed(10).to_string(), "10i128");
  121. assert_eq!(Literal::isize_suffixed(10).to_string(), "10isize");
  122. assert_eq!(Literal::u8_unsuffixed(10).to_string(), "10");
  123. assert_eq!(Literal::u16_unsuffixed(10).to_string(), "10");
  124. assert_eq!(Literal::u32_unsuffixed(10).to_string(), "10");
  125. assert_eq!(Literal::u64_unsuffixed(10).to_string(), "10");
  126. assert_eq!(Literal::u128_unsuffixed(10).to_string(), "10");
  127. assert_eq!(Literal::usize_unsuffixed(10).to_string(), "10");
  128. assert_eq!(Literal::i8_unsuffixed(10).to_string(), "10");
  129. assert_eq!(Literal::i16_unsuffixed(10).to_string(), "10");
  130. assert_eq!(Literal::i32_unsuffixed(10).to_string(), "10");
  131. assert_eq!(Literal::i64_unsuffixed(10).to_string(), "10");
  132. assert_eq!(Literal::i128_unsuffixed(10).to_string(), "10");
  133. assert_eq!(Literal::isize_unsuffixed(10).to_string(), "10");
  134. }
  135. #[test]
  136. fn literal_float() {
  137. assert_eq!(Literal::f32_suffixed(10.0).to_string(), "10f32");
  138. assert_eq!(Literal::f64_suffixed(10.0).to_string(), "10f64");
  139. assert_eq!(Literal::f32_unsuffixed(10.0).to_string(), "10.0");
  140. assert_eq!(Literal::f64_unsuffixed(10.0).to_string(), "10.0");
  141. }
  142. #[test]
  143. fn literal_suffix() {
  144. fn token_count(p: &str) -> usize {
  145. p.parse::<TokenStream>().unwrap().into_iter().count()
  146. }
  147. assert_eq!(token_count("999u256"), 1);
  148. assert_eq!(token_count("999r#u256"), 3);
  149. assert_eq!(token_count("1."), 1);
  150. assert_eq!(token_count("1.f32"), 3);
  151. assert_eq!(token_count("1.0_0"), 1);
  152. assert_eq!(token_count("1._0"), 3);
  153. assert_eq!(token_count("1._m"), 3);
  154. assert_eq!(token_count("\"\"s"), 1);
  155. assert_eq!(token_count("r\"\"r"), 1);
  156. assert_eq!(token_count("b\"\"b"), 1);
  157. assert_eq!(token_count("br\"\"br"), 1);
  158. assert_eq!(token_count("r#\"\"#r"), 1);
  159. assert_eq!(token_count("'c'c"), 1);
  160. assert_eq!(token_count("b'b'b"), 1);
  161. assert_eq!(token_count("0E"), 1);
  162. assert_eq!(token_count("0o0A"), 1);
  163. assert_eq!(token_count("0E--0"), 4);
  164. assert_eq!(token_count("0.0ECMA"), 1);
  165. }
  166. #[test]
  167. fn literal_iter_negative() {
  168. let negative_literal = Literal::i32_suffixed(-3);
  169. let tokens = TokenStream::from(TokenTree::Literal(negative_literal));
  170. let mut iter = tokens.into_iter();
  171. match iter.next().unwrap() {
  172. TokenTree::Punct(punct) => {
  173. assert_eq!(punct.as_char(), '-');
  174. assert_eq!(punct.spacing(), Spacing::Alone);
  175. }
  176. unexpected => panic!("unexpected token {:?}", unexpected),
  177. }
  178. match iter.next().unwrap() {
  179. TokenTree::Literal(literal) => {
  180. assert_eq!(literal.to_string(), "3i32");
  181. }
  182. unexpected => panic!("unexpected token {:?}", unexpected),
  183. }
  184. assert!(iter.next().is_none());
  185. }
  186. #[test]
  187. fn literal_parse() {
  188. assert!("1".parse::<Literal>().is_ok());
  189. assert!("-1".parse::<Literal>().is_ok());
  190. assert!("-1u12".parse::<Literal>().is_ok());
  191. assert!("1.0".parse::<Literal>().is_ok());
  192. assert!("-1.0".parse::<Literal>().is_ok());
  193. assert!("-1.0f12".parse::<Literal>().is_ok());
  194. assert!("'a'".parse::<Literal>().is_ok());
  195. assert!("\"\n\"".parse::<Literal>().is_ok());
  196. assert!("0 1".parse::<Literal>().is_err());
  197. assert!(" 0".parse::<Literal>().is_err());
  198. assert!("0 ".parse::<Literal>().is_err());
  199. assert!("/* comment */0".parse::<Literal>().is_err());
  200. assert!("0/* comment */".parse::<Literal>().is_err());
  201. assert!("0// comment".parse::<Literal>().is_err());
  202. assert!("- 1".parse::<Literal>().is_err());
  203. assert!("- 1.0".parse::<Literal>().is_err());
  204. assert!("-\"\"".parse::<Literal>().is_err());
  205. }
  206. #[test]
  207. fn roundtrip() {
  208. fn roundtrip(p: &str) {
  209. println!("parse: {}", p);
  210. let s = p.parse::<TokenStream>().unwrap().to_string();
  211. println!("first: {}", s);
  212. let s2 = s.parse::<TokenStream>().unwrap().to_string();
  213. assert_eq!(s, s2);
  214. }
  215. roundtrip("a");
  216. roundtrip("<<");
  217. roundtrip("<<=");
  218. roundtrip(
  219. "
  220. 1
  221. 1.0
  222. 1f32
  223. 2f64
  224. 1usize
  225. 4isize
  226. 4e10
  227. 1_000
  228. 1_0i32
  229. 8u8
  230. 9
  231. 0
  232. 0xffffffffffffffffffffffffffffffff
  233. 1x
  234. 1u80
  235. 1f320
  236. ",
  237. );
  238. roundtrip("'a");
  239. roundtrip("'_");
  240. roundtrip("'static");
  241. roundtrip("'\\u{10__FFFF}'");
  242. roundtrip("\"\\u{10_F0FF__}foo\\u{1_0_0_0__}\"");
  243. }
  244. #[test]
  245. fn fail() {
  246. fn fail(p: &str) {
  247. if let Ok(s) = p.parse::<TokenStream>() {
  248. panic!("should have failed to parse: {}\n{:#?}", p, s);
  249. }
  250. }
  251. fail("' static");
  252. fail("r#1");
  253. fail("r#_");
  254. fail("\"\\u{0000000}\""); // overlong unicode escape (rust allows at most 6 hex digits)
  255. fail("\"\\u{999999}\""); // outside of valid range of char
  256. fail("\"\\u{_0}\""); // leading underscore
  257. fail("\"\\u{}\""); // empty
  258. fail("b\"\r\""); // bare carriage return in byte string
  259. fail("r\"\r\""); // bare carriage return in raw string
  260. fail("\"\\\r \""); // backslash carriage return
  261. fail("'aa'aa");
  262. fail("br##\"\"#");
  263. fail("\"\\\n\u{85}\r\"");
  264. }
  265. #[cfg(span_locations)]
  266. #[test]
  267. fn span_test() {
  268. check_spans(
  269. "\
  270. /// This is a document comment
  271. testing 123
  272. {
  273. testing 234
  274. }",
  275. &[
  276. (1, 0, 1, 30), // #
  277. (1, 0, 1, 30), // [ ... ]
  278. (1, 0, 1, 30), // doc
  279. (1, 0, 1, 30), // =
  280. (1, 0, 1, 30), // "This is..."
  281. (2, 0, 2, 7), // testing
  282. (2, 8, 2, 11), // 123
  283. (3, 0, 5, 1), // { ... }
  284. (4, 2, 4, 9), // testing
  285. (4, 10, 4, 13), // 234
  286. ],
  287. );
  288. }
  289. #[cfg(procmacro2_semver_exempt)]
  290. #[cfg(not(nightly))]
  291. #[test]
  292. fn default_span() {
  293. let start = Span::call_site().start();
  294. assert_eq!(start.line, 1);
  295. assert_eq!(start.column, 0);
  296. let end = Span::call_site().end();
  297. assert_eq!(end.line, 1);
  298. assert_eq!(end.column, 0);
  299. let source_file = Span::call_site().source_file();
  300. assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
  301. assert!(!source_file.is_real());
  302. }
  303. #[cfg(procmacro2_semver_exempt)]
  304. #[test]
  305. fn span_join() {
  306. let source1 = "aaa\nbbb"
  307. .parse::<TokenStream>()
  308. .unwrap()
  309. .into_iter()
  310. .collect::<Vec<_>>();
  311. let source2 = "ccc\nddd"
  312. .parse::<TokenStream>()
  313. .unwrap()
  314. .into_iter()
  315. .collect::<Vec<_>>();
  316. assert!(source1[0].span().source_file() != source2[0].span().source_file());
  317. assert_eq!(
  318. source1[0].span().source_file(),
  319. source1[1].span().source_file()
  320. );
  321. let joined1 = source1[0].span().join(source1[1].span());
  322. let joined2 = source1[0].span().join(source2[0].span());
  323. assert!(joined1.is_some());
  324. assert!(joined2.is_none());
  325. let start = joined1.unwrap().start();
  326. let end = joined1.unwrap().end();
  327. assert_eq!(start.line, 1);
  328. assert_eq!(start.column, 0);
  329. assert_eq!(end.line, 2);
  330. assert_eq!(end.column, 3);
  331. assert_eq!(
  332. joined1.unwrap().source_file(),
  333. source1[0].span().source_file()
  334. );
  335. }
  336. #[test]
  337. fn no_panic() {
  338. let s = str::from_utf8(b"b\'\xc2\x86 \x00\x00\x00^\"").unwrap();
  339. assert!(s.parse::<TokenStream>().is_err());
  340. }
  341. #[test]
  342. fn punct_before_comment() {
  343. let mut tts = TokenStream::from_str("~// comment").unwrap().into_iter();
  344. match tts.next().unwrap() {
  345. TokenTree::Punct(tt) => {
  346. assert_eq!(tt.as_char(), '~');
  347. assert_eq!(tt.spacing(), Spacing::Alone);
  348. }
  349. wrong => panic!("wrong token {:?}", wrong),
  350. }
  351. }
  352. #[test]
  353. fn joint_last_token() {
  354. // This test verifies that we match the behavior of libproc_macro *not* in
  355. // the range nightly-2020-09-06 through nightly-2020-09-10, in which this
  356. // behavior was temporarily broken.
  357. // See https://github.com/rust-lang/rust/issues/76399
  358. let joint_punct = Punct::new(':', Spacing::Joint);
  359. let stream = TokenStream::from(TokenTree::Punct(joint_punct));
  360. let punct = match stream.into_iter().next().unwrap() {
  361. TokenTree::Punct(punct) => punct,
  362. _ => unreachable!(),
  363. };
  364. assert_eq!(punct.spacing(), Spacing::Joint);
  365. }
  366. #[test]
  367. fn raw_identifier() {
  368. let mut tts = TokenStream::from_str("r#dyn").unwrap().into_iter();
  369. match tts.next().unwrap() {
  370. TokenTree::Ident(raw) => assert_eq!("r#dyn", raw.to_string()),
  371. wrong => panic!("wrong token {:?}", wrong),
  372. }
  373. assert!(tts.next().is_none());
  374. }
  375. #[test]
  376. fn test_debug_ident() {
  377. let ident = Ident::new("proc_macro", Span::call_site());
  378. #[cfg(not(span_locations))]
  379. let expected = "Ident(proc_macro)";
  380. #[cfg(span_locations)]
  381. let expected = "Ident { sym: proc_macro }";
  382. assert_eq!(expected, format!("{:?}", ident));
  383. }
  384. #[test]
  385. fn test_debug_tokenstream() {
  386. let tts = TokenStream::from_str("[a + 1]").unwrap();
  387. #[cfg(not(span_locations))]
  388. let expected = "\
  389. TokenStream [
  390. Group {
  391. delimiter: Bracket,
  392. stream: TokenStream [
  393. Ident {
  394. sym: a,
  395. },
  396. Punct {
  397. char: '+',
  398. spacing: Alone,
  399. },
  400. Literal {
  401. lit: 1,
  402. },
  403. ],
  404. },
  405. ]\
  406. ";
  407. #[cfg(not(span_locations))]
  408. let expected_before_trailing_commas = "\
  409. TokenStream [
  410. Group {
  411. delimiter: Bracket,
  412. stream: TokenStream [
  413. Ident {
  414. sym: a
  415. },
  416. Punct {
  417. char: '+',
  418. spacing: Alone
  419. },
  420. Literal {
  421. lit: 1
  422. }
  423. ]
  424. }
  425. ]\
  426. ";
  427. #[cfg(span_locations)]
  428. let expected = "\
  429. TokenStream [
  430. Group {
  431. delimiter: Bracket,
  432. stream: TokenStream [
  433. Ident {
  434. sym: a,
  435. span: bytes(2..3),
  436. },
  437. Punct {
  438. char: '+',
  439. spacing: Alone,
  440. span: bytes(4..5),
  441. },
  442. Literal {
  443. lit: 1,
  444. span: bytes(6..7),
  445. },
  446. ],
  447. span: bytes(1..8),
  448. },
  449. ]\
  450. ";
  451. #[cfg(span_locations)]
  452. let expected_before_trailing_commas = "\
  453. TokenStream [
  454. Group {
  455. delimiter: Bracket,
  456. stream: TokenStream [
  457. Ident {
  458. sym: a,
  459. span: bytes(2..3)
  460. },
  461. Punct {
  462. char: '+',
  463. spacing: Alone,
  464. span: bytes(4..5)
  465. },
  466. Literal {
  467. lit: 1,
  468. span: bytes(6..7)
  469. }
  470. ],
  471. span: bytes(1..8)
  472. }
  473. ]\
  474. ";
  475. let actual = format!("{:#?}", tts);
  476. if actual.ends_with(",\n]") {
  477. assert_eq!(expected, actual);
  478. } else {
  479. assert_eq!(expected_before_trailing_commas, actual);
  480. }
  481. }
  482. #[test]
  483. fn default_tokenstream_is_empty() {
  484. let default_token_stream = <TokenStream as Default>::default();
  485. assert!(default_token_stream.is_empty());
  486. }
  487. #[test]
  488. fn tuple_indexing() {
  489. // This behavior may change depending on https://github.com/rust-lang/rust/pull/71322
  490. let mut tokens = "tuple.0.0".parse::<TokenStream>().unwrap().into_iter();
  491. assert_eq!("tuple", tokens.next().unwrap().to_string());
  492. assert_eq!(".", tokens.next().unwrap().to_string());
  493. assert_eq!("0.0", tokens.next().unwrap().to_string());
  494. assert!(tokens.next().is_none());
  495. }
  496. #[cfg(span_locations)]
  497. #[test]
  498. fn non_ascii_tokens() {
  499. check_spans("// abc", &[]);
  500. check_spans("// ábc", &[]);
  501. check_spans("// abc x", &[]);
  502. check_spans("// ábc x", &[]);
  503. check_spans("/* abc */ x", &[(1, 10, 1, 11)]);
  504. check_spans("/* ábc */ x", &[(1, 10, 1, 11)]);
  505. check_spans("/* ab\nc */ x", &[(2, 5, 2, 6)]);
  506. check_spans("/* áb\nc */ x", &[(2, 5, 2, 6)]);
  507. check_spans("/*** abc */ x", &[(1, 12, 1, 13)]);
  508. check_spans("/*** ábc */ x", &[(1, 12, 1, 13)]);
  509. check_spans(r#""abc""#, &[(1, 0, 1, 5)]);
  510. check_spans(r#""ábc""#, &[(1, 0, 1, 5)]);
  511. check_spans(r###"r#"abc"#"###, &[(1, 0, 1, 8)]);
  512. check_spans(r###"r#"ábc"#"###, &[(1, 0, 1, 8)]);
  513. check_spans("r#\"a\nc\"#", &[(1, 0, 2, 3)]);
  514. check_spans("r#\"á\nc\"#", &[(1, 0, 2, 3)]);
  515. check_spans("'a'", &[(1, 0, 1, 3)]);
  516. check_spans("'á'", &[(1, 0, 1, 3)]);
  517. check_spans("//! abc", &[(1, 0, 1, 7), (1, 0, 1, 7), (1, 0, 1, 7)]);
  518. check_spans("//! ábc", &[(1, 0, 1, 7), (1, 0, 1, 7), (1, 0, 1, 7)]);
  519. check_spans("//! abc\n", &[(1, 0, 1, 7), (1, 0, 1, 7), (1, 0, 1, 7)]);
  520. check_spans("//! ábc\n", &[(1, 0, 1, 7), (1, 0, 1, 7), (1, 0, 1, 7)]);
  521. check_spans("/*! abc */", &[(1, 0, 1, 10), (1, 0, 1, 10), (1, 0, 1, 10)]);
  522. check_spans("/*! ábc */", &[(1, 0, 1, 10), (1, 0, 1, 10), (1, 0, 1, 10)]);
  523. check_spans("/*! a\nc */", &[(1, 0, 2, 4), (1, 0, 2, 4), (1, 0, 2, 4)]);
  524. check_spans("/*! á\nc */", &[(1, 0, 2, 4), (1, 0, 2, 4), (1, 0, 2, 4)]);
  525. check_spans("abc", &[(1, 0, 1, 3)]);
  526. check_spans("ábc", &[(1, 0, 1, 3)]);
  527. check_spans("ábć", &[(1, 0, 1, 3)]);
  528. check_spans("abc// foo", &[(1, 0, 1, 3)]);
  529. check_spans("ábc// foo", &[(1, 0, 1, 3)]);
  530. check_spans("ábć// foo", &[(1, 0, 1, 3)]);
  531. check_spans("b\"a\\\n c\"", &[(1, 0, 2, 3)]);
  532. check_spans("b\"a\\\n\u{00a0}c\"", &[(1, 0, 2, 3)]);
  533. }
  534. #[cfg(span_locations)]
  535. fn check_spans(p: &str, mut lines: &[(usize, usize, usize, usize)]) {
  536. let ts = p.parse::<TokenStream>().unwrap();
  537. check_spans_internal(ts, &mut lines);
  538. assert!(lines.is_empty(), "leftover ranges: {:?}", lines);
  539. }
  540. #[cfg(span_locations)]
  541. fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usize)]) {
  542. for i in ts {
  543. if let Some((&(sline, scol, eline, ecol), rest)) = lines.split_first() {
  544. *lines = rest;
  545. let start = i.span().start();
  546. assert_eq!(start.line, sline, "sline did not match for {}", i);
  547. assert_eq!(start.column, scol, "scol did not match for {}", i);
  548. let end = i.span().end();
  549. assert_eq!(end.line, eline, "eline did not match for {}", i);
  550. assert_eq!(end.column, ecol, "ecol did not match for {}", i);
  551. if let TokenTree::Group(g) = i {
  552. check_spans_internal(g.stream().clone(), lines);
  553. }
  554. }
  555. }
  556. }