Skip to content

Commit

Permalink
Remove span generics from most of the mbe crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Mar 19, 2024
1 parent de6aedd commit 8372b78
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 228 deletions.
2 changes: 1 addition & 1 deletion crates/hir-expand/src/declarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
/// Old-style `macro_rules` or the new macros 2.0
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DeclarativeMacroExpander {
pub mac: mbe::DeclarativeMacro<span::Span>,
pub mac: mbe::DeclarativeMacro,
pub transparency: Transparency,
}

Expand Down
4 changes: 1 addition & 3 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ use crate::{

pub use crate::files::{AstId, ErasedAstId, InFile, InMacroFile, InRealFile};

pub use mbe::ValueResult;
pub use mbe::{DeclarativeMacro, ValueResult};
pub use span::{HirFileId, MacroCallId, MacroFileId};

pub type DeclarativeMacro = ::mbe::DeclarativeMacro<tt::Span>;

pub mod tt {
pub use span::Span;
pub use tt::{DelimiterKind, Spacing};
Expand Down
10 changes: 3 additions & 7 deletions crates/mbe/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn benchmark_expand_macro_rules() {
assert_eq!(hash, 69413);
}

fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<Span>> {
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro> {
macro_rules_fixtures_tt()
.into_iter()
.map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt, true, true)))
Expand Down Expand Up @@ -80,7 +80,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<Span>> {

/// Generate random invocation fixtures from rules
fn invocation_fixtures(
rules: &FxHashMap<String, DeclarativeMacro<Span>>,
rules: &FxHashMap<String, DeclarativeMacro>,
) -> Vec<(String, tt::Subtree<Span>)> {
let mut seed = 123456789;
let mut res = Vec::new();
Expand Down Expand Up @@ -128,11 +128,7 @@ fn invocation_fixtures(
}
return res;

fn collect_from_op(
op: &Op<Span>,
token_trees: &mut Vec<tt::TokenTree<Span>>,
seed: &mut usize,
) {
fn collect_from_op(op: &Op, token_trees: &mut Vec<tt::TokenTree<Span>>, seed: &mut usize) {
return match op {
Op::Var { kind, .. } => match kind.as_ref() {
Some(MetaVarKind::Ident) => token_trees.push(make_ident("foo")),
Expand Down
42 changes: 18 additions & 24 deletions crates/mbe/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ mod matcher;
mod transcriber;

use rustc_hash::FxHashMap;
use span::Span;
use syntax::SmolStr;
use tt::Span;

use crate::{parser::MetaVarKind, ExpandError, ExpandResult};

pub(crate) fn expand_rules<S: Span>(
rules: &[crate::Rule<S>],
input: &tt::Subtree<S>,
marker: impl Fn(&mut S) + Copy,
pub(crate) fn expand_rules(
rules: &[crate::Rule],
input: &tt::Subtree<Span>,
marker: impl Fn(&mut Span) + Copy,
is_2021: bool,
new_meta_vars: bool,
call_site: S,
) -> ExpandResult<tt::Subtree<S>> {
let mut match_: Option<(matcher::Match<S>, &crate::Rule<S>)> = None;
call_site: Span,
) -> ExpandResult<tt::Subtree<Span>> {
let mut match_: Option<(matcher::Match, &crate::Rule)> = None;
for rule in rules {
let new_match = matcher::match_(&rule.lhs, input, is_2021);

Expand Down Expand Up @@ -110,38 +110,32 @@ pub(crate) fn expand_rules<S: Span>(
/// In other words, `Bindings` is a *multi* mapping from `SmolStr` to
/// `tt::TokenTree`, where the index to select a particular `TokenTree` among
/// many is not a plain `usize`, but a `&[usize]`.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Bindings<S> {
inner: FxHashMap<SmolStr, Binding<S>>,
}

impl<S> Default for Bindings<S> {
fn default() -> Self {
Self { inner: Default::default() }
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct Bindings {
inner: FxHashMap<SmolStr, Binding>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Binding<S> {
Fragment(Fragment<S>),
Nested(Vec<Binding<S>>),
enum Binding {
Fragment(Fragment),
Nested(Vec<Binding>),
Empty,
Missing(MetaVarKind),
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Fragment<S> {
enum Fragment {
Empty,
/// token fragments are just copy-pasted into the output
Tokens(tt::TokenTree<S>),
Tokens(tt::TokenTree<Span>),
/// Expr ast fragments are surrounded with `()` on insertion to preserve
/// precedence. Note that this impl is different from the one currently in
/// `rustc` -- `rustc` doesn't translate fragments into token trees at all.
///
/// At one point in time, we tried to use "fake" delimiters here à la
/// proc-macro delimiter=none. As we later discovered, "none" delimiters are
/// tricky to handle in the parser, and rustc doesn't handle those either.
Expr(tt::Subtree<S>),
Expr(tt::Subtree<Span>),
/// There are roughly two types of paths: paths in expression context, where a
/// separator `::` between an identifier and its following generic argument list
/// is mandatory, and paths in type context, where `::` can be omitted.
Expand All @@ -151,5 +145,5 @@ enum Fragment<S> {
/// and is trasncribed as an expression-context path, verbatim transcription
/// would cause a syntax error. We need to fix it up just before transcribing;
/// see `transcriber::fix_up_and_push_path_tt()`.
Path(tt::Subtree<S>),
Path(tt::Subtree<Span>),
}
Loading

0 comments on commit 8372b78

Please sign in to comment.