From 3be86e6528db24fb055530ef93c93b2a9fc9ce90 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 28 Sep 2022 10:07:01 +1000 Subject: [PATCH] Clarify operator splitting. I found this code hard to read. --- compiler/rustc_expand/src/proc_macro_server.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 59a7b668a83..ff09a9fb87a 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -110,10 +110,14 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec (token, spacing == Joint), }; + // Split the operator into one or more `Punct`s, one per character. + // The final one inherits the jointness of the original token. Any + // before that get `joint = true`. let mut op = |s: &str| { assert!(s.is_ascii()); - trees.extend(s.as_bytes().iter().enumerate().map(|(idx, &ch)| { - TokenTree::Punct(Punct { ch, joint: joint || idx != s.len() - 1, span }) + trees.extend(s.bytes().enumerate().map(|(idx, ch)| { + let is_final = idx == s.len() - 1; + TokenTree::Punct(Punct { ch, joint: if is_final { joint } else { true }, span }) })); };