From 1a183368082ad357b1fef0f55038becc9ac14b7b Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 30 Jan 2019 15:12:41 +0100 Subject: proc_macro: make `TokenStream::from_streams` pre-allocate its vector. This requires a pre-pass over the input streams. But that is cheap compared to the quadratic blowup associated with reallocating the accumulating vector on-the-fly. --- src/libsyntax/tokenstream.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index f5d2d6f18ee..7d6ffceb2c0 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -255,7 +255,13 @@ impl TokenStream { 0 => TokenStream::empty(), 1 => streams.pop().unwrap(), _ => { - let mut vec = vec![]; + // rust-lang/rust#57735: pre-allocate vector to avoid + // quadratic blow-up due to on-the-fly reallocations. + let tree_count = streams.iter() + .map(|ts| match &ts.0 { None => 0, Some(s) => s.len() }) + .sum(); + let mut vec = Vec::with_capacity(tree_count); + for stream in streams { match stream.0 { None => {}, -- cgit 1.4.1-3-g733a5 From 9851a296885cc745eccbc33858915d08e839eadc Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 4 Feb 2019 00:35:12 +0900 Subject: Add the span of attributes of the lhs to the span of the assignment expression --- src/libsyntax/parse/parser.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 514b2952c50..1c02a80df46 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3455,6 +3455,14 @@ impl<'a> Parser<'a> { }), }?; + // Make sure that the span of the parent node is larger than the span of lhs and rhs, + // including the attributes. + let lhs_span = lhs + .attrs + .iter() + .filter(|a| a.style == AttrStyle::Outer) + .next() + .map_or(lhs_span, |a| a.span); let span = lhs_span.to(rhs.span); lhs = match op { AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide | -- cgit 1.4.1-3-g733a5