Skip to content

Commit 0e022f8

Browse files
Remove needless indirection through Rc
NamedMatch is already cheap to clone due to Lrc's inside.
1 parent eedf6ce commit 0e022f8

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ use rustc_data_structures::sync::Lrc;
9292
use std::collections::hash_map::Entry::{Occupied, Vacant};
9393
use std::mem;
9494
use std::ops::{Deref, DerefMut};
95-
use std::rc::Rc;
9695

9796
// To avoid costly uniqueness checks, we require that `MatchSeq` always has a nonempty body.
9897

@@ -280,7 +279,7 @@ pub enum ParseResult<T> {
280279

281280
/// A `ParseResult` where the `Success` variant contains a mapping of `Ident`s to `NamedMatch`es.
282281
/// This represents the mapping of metavars to the token trees they bind to.
283-
pub type NamedParseResult = ParseResult<FxHashMap<Ident, Rc<NamedMatch>>>;
282+
pub type NamedParseResult = ParseResult<FxHashMap<Ident, NamedMatch>>;
284283

285284
/// Count how many metavars are named in the given matcher `ms`.
286285
pub fn count_names(ms: &[TokenTree]) -> usize {
@@ -373,7 +372,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
373372
sess: &ParseSess,
374373
m: &TokenTree,
375374
res: &mut I,
376-
ret_val: &mut FxHashMap<Ident, Rc<NamedMatch>>,
375+
ret_val: &mut FxHashMap<Ident, NamedMatch>,
377376
) -> Result<(), (syntax_pos::Span, String)> {
378377
match *m {
379378
TokenTree::Sequence(_, ref seq) => for next_m in &seq.tts {
@@ -390,8 +389,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
390389
TokenTree::MetaVarDecl(sp, bind_name, _) => {
391390
match ret_val.entry(bind_name) {
392391
Vacant(spot) => {
393-
// FIXME(simulacrum): Don't construct Rc here
394-
spot.insert(Rc::new(res.next().unwrap()));
392+
spot.insert(res.next().unwrap());
395393
}
396394
Occupied(..) => {
397395
return Err((sp, format!("duplicated bind name: {}", bind_name)))

src/libsyntax/ext/tt/macro_rules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ pub fn compile(
308308
let mut valid = true;
309309

310310
// Extract the arguments:
311-
let lhses = match *argument_map[&lhs_nm] {
311+
let lhses = match argument_map[&lhs_nm] {
312312
MatchedSeq(ref s, _) => s
313313
.iter()
314314
.map(|m| {
@@ -335,7 +335,7 @@ pub fn compile(
335335
_ => sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs"),
336336
};
337337

338-
let rhses = match *argument_map[&rhs_nm] {
338+
let rhses = match argument_map[&rhs_nm] {
339339
MatchedSeq(ref s, _) => s
340340
.iter()
341341
.map(|m| {

src/libsyntax/ext/tt/transcribe.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use smallvec::{smallvec, SmallVec};
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::sync::Lrc;
1414
use std::mem;
15-
use std::rc::Rc;
1615

1716
/// An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`).
1817
enum Frame {
@@ -65,9 +64,9 @@ impl Iterator for Frame {
6564
/// `transcribe` would return a `TokenStream` containing `println!("{}", stringify!(bar));`.
6665
///
6766
/// Along the way, we do some additional error checking.
68-
pub fn transcribe(
67+
pub(super) fn transcribe(
6968
cx: &ExtCtxt<'_>,
70-
interp: &FxHashMap<Ident, Rc<NamedMatch>>,
69+
interp: &FxHashMap<Ident, NamedMatch>,
7170
src: Vec<quoted::TokenTree>,
7271
) -> TokenStream {
7372
// Nothing for us to transcribe...
@@ -212,7 +211,7 @@ pub fn transcribe(
212211
// Find the matched nonterminal from the macro invocation, and use it to replace
213212
// the meta-var.
214213
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
215-
if let MatchedNonterminal(ref nt) = *cur_matched {
214+
if let MatchedNonterminal(ref nt) = cur_matched {
216215
// FIXME #2887: why do we apply a mark when matching a token tree meta-var
217216
// (e.g. `$x:tt`), but not when we are matching any other type of token
218217
// tree?
@@ -273,18 +272,17 @@ pub fn transcribe(
273272
/// See the definition of `repeats` in the `transcribe` function. `repeats` is used to descend
274273
/// into the right place in nested matchers. If we attempt to descend too far, the macro writer has
275274
/// made a mistake, and we return `None`.
276-
fn lookup_cur_matched(
275+
fn lookup_cur_matched<'a>(
277276
ident: Ident,
278-
interpolations: &FxHashMap<Ident, Rc<NamedMatch>>,
277+
interpolations: &'a FxHashMap<Ident, NamedMatch>,
279278
repeats: &[(usize, usize)],
280-
) -> Option<Rc<NamedMatch>> {
279+
) -> Option<&'a NamedMatch> {
281280
interpolations.get(&ident).map(|matched| {
282-
let mut matched = matched.clone();
281+
let mut matched = matched;
283282
for &(idx, _) in repeats {
284-
let m = matched.clone();
285-
match *m {
283+
match matched {
286284
MatchedNonterminal(_) => break,
287-
MatchedSeq(ref ads, _) => matched = Rc::new(ads[idx].clone()),
285+
MatchedSeq(ref ads, _) => matched = ads.get(idx).unwrap(),
288286
}
289287
}
290288

@@ -343,7 +341,7 @@ impl LockstepIterSize {
343341
/// multiple nested matcher sequences.
344342
fn lockstep_iter_size(
345343
tree: &quoted::TokenTree,
346-
interpolations: &FxHashMap<Ident, Rc<NamedMatch>>,
344+
interpolations: &FxHashMap<Ident, NamedMatch>,
347345
repeats: &[(usize, usize)],
348346
) -> LockstepIterSize {
349347
use quoted::TokenTree;
@@ -360,7 +358,7 @@ fn lockstep_iter_size(
360358
}
361359
TokenTree::MetaVar(_, name) | TokenTree::MetaVarDecl(_, name, _) => {
362360
match lookup_cur_matched(name, interpolations, repeats) {
363-
Some(matched) => match *matched {
361+
Some(matched) => match matched {
364362
MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
365363
MatchedSeq(ref ads, _) => LockstepIterSize::Constraint(ads.len(), name),
366364
},

0 commit comments

Comments
 (0)