Skip to content

Commit

Permalink
Rollup merge of rust-lang#62983 - Mark-Simulacrum:remove-needless-rc,…
Browse files Browse the repository at this point in the history
… r=petrochenkov

Remove needless indirection through Rc

NamedMatch is already cheap to clone due to Lrc's inside.
  • Loading branch information
Centril authored Jul 26, 2019
2 parents 15398b6 + 0e022f8 commit 0614a94
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/libsyntax/ext/tt/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ use rustc_data_structures::sync::Lrc;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::mem;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;

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

Expand Down Expand Up @@ -280,7 +279,7 @@ pub enum ParseResult<T> {

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

/// Count how many metavars are named in the given matcher `ms`.
pub fn count_names(ms: &[TokenTree]) -> usize {
Expand Down Expand Up @@ -373,7 +372,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
sess: &ParseSess,
m: &TokenTree,
res: &mut I,
ret_val: &mut FxHashMap<Ident, Rc<NamedMatch>>,
ret_val: &mut FxHashMap<Ident, NamedMatch>,
) -> Result<(), (syntax_pos::Span, String)> {
match *m {
TokenTree::Sequence(_, ref seq) => for next_m in &seq.tts {
Expand All @@ -390,8 +389,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
TokenTree::MetaVarDecl(sp, bind_name, _) => {
match ret_val.entry(bind_name) {
Vacant(spot) => {
// FIXME(simulacrum): Don't construct Rc here
spot.insert(Rc::new(res.next().unwrap()));
spot.insert(res.next().unwrap());
}
Occupied(..) => {
return Err((sp, format!("duplicated bind name: {}", bind_name)))
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ pub fn compile(
let mut valid = true;

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

let rhses = match *argument_map[&rhs_nm] {
let rhses = match argument_map[&rhs_nm] {
MatchedSeq(ref s, _) => s
.iter()
.map(|m| {
Expand Down
24 changes: 11 additions & 13 deletions src/libsyntax/ext/tt/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use smallvec::{smallvec, SmallVec};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lrc;
use std::mem;
use std::rc::Rc;

/// An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`).
enum Frame {
Expand Down Expand Up @@ -65,9 +64,9 @@ impl Iterator for Frame {
/// `transcribe` would return a `TokenStream` containing `println!("{}", stringify!(bar));`.
///
/// Along the way, we do some additional error checking.
pub fn transcribe(
pub(super) fn transcribe(
cx: &ExtCtxt<'_>,
interp: &FxHashMap<Ident, Rc<NamedMatch>>,
interp: &FxHashMap<Ident, NamedMatch>,
src: Vec<quoted::TokenTree>,
) -> TokenStream {
// Nothing for us to transcribe...
Expand Down Expand Up @@ -212,7 +211,7 @@ pub fn transcribe(
// Find the matched nonterminal from the macro invocation, and use it to replace
// the meta-var.
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
if let MatchedNonterminal(ref nt) = *cur_matched {
if let MatchedNonterminal(ref nt) = cur_matched {
// FIXME #2887: why do we apply a mark when matching a token tree meta-var
// (e.g. `$x:tt`), but not when we are matching any other type of token
// tree?
Expand Down Expand Up @@ -273,18 +272,17 @@ pub fn transcribe(
/// See the definition of `repeats` in the `transcribe` function. `repeats` is used to descend
/// into the right place in nested matchers. If we attempt to descend too far, the macro writer has
/// made a mistake, and we return `None`.
fn lookup_cur_matched(
fn lookup_cur_matched<'a>(
ident: Ident,
interpolations: &FxHashMap<Ident, Rc<NamedMatch>>,
interpolations: &'a FxHashMap<Ident, NamedMatch>,
repeats: &[(usize, usize)],
) -> Option<Rc<NamedMatch>> {
) -> Option<&'a NamedMatch> {
interpolations.get(&ident).map(|matched| {
let mut matched = matched.clone();
let mut matched = matched;
for &(idx, _) in repeats {
let m = matched.clone();
match *m {
match matched {
MatchedNonterminal(_) => break,
MatchedSeq(ref ads, _) => matched = Rc::new(ads[idx].clone()),
MatchedSeq(ref ads, _) => matched = ads.get(idx).unwrap(),
}
}

Expand Down Expand Up @@ -343,7 +341,7 @@ impl LockstepIterSize {
/// multiple nested matcher sequences.
fn lockstep_iter_size(
tree: &quoted::TokenTree,
interpolations: &FxHashMap<Ident, Rc<NamedMatch>>,
interpolations: &FxHashMap<Ident, NamedMatch>,
repeats: &[(usize, usize)],
) -> LockstepIterSize {
use quoted::TokenTree;
Expand All @@ -360,7 +358,7 @@ fn lockstep_iter_size(
}
TokenTree::MetaVar(_, name) | TokenTree::MetaVarDecl(_, name, _) => {
match lookup_cur_matched(name, interpolations, repeats) {
Some(matched) => match *matched {
Some(matched) => match matched {
MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
MatchedSeq(ref ads, _) => LockstepIterSize::Constraint(ads.len(), name),
},
Expand Down

0 comments on commit 0614a94

Please sign in to comment.