Rollup merge of #62983 - Mark-Simulacrum:remove-needless-rc, r=petrochenkov

Remove needless indirection through Rc

NamedMatch is already cheap to clone due to Lrc's inside.
This commit is contained in:
Mazdak Farrokhzad 2019-07-26 18:56:59 +02:00 committed by GitHub
commit 0614a94d67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 20 deletions

View File

@ -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.
@ -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 {
@ -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 {
@ -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)))

View File

@ -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| {
@ -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| {

View File

@ -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 {
@ -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...
@ -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?
@ -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(),
}
}
@ -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;
@ -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),
},