From 614e5a2272a96eabe30acea2f7c797a305a4f2fd Mon Sep 17 00:00:00 2001
From: Jonas Schievink <jonasschievink@gmail.com>
Date: Thu, 10 Dec 2020 17:50:56 +0100
Subject: [PATCH 1/3] Improve macro limit error and move to const

---
 crates/hir_expand/src/db.rs | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 842a177db3f..3c0ee284ed2 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -13,6 +13,12 @@ use crate::{
     MacroFile, ProcMacroExpander,
 };
 
+/// Total limit on the number of tokens produced by any macro invocation.
+///
+/// If an invocation produces more tokens than this limit, it will not be stored in the database and
+/// an error will be emitted.
+const TOKEN_LIMIT: usize = 262144;
+
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub enum TokenExpander {
     MacroRules(mbe::MacroRules),
@@ -227,10 +233,10 @@ fn macro_expand_with_arg(
     let ExpandResult { value: tt, err } = macro_rules.0.expand(db, lazy_id, &macro_arg.0);
     // Set a hard limit for the expanded tt
     let count = tt.count();
-    if count > 262144 {
+    if count > TOKEN_LIMIT {
         return ExpandResult::str_err(format!(
-            "Total tokens count exceed limit : count = {}",
-            count
+            "macro invocation exceeds token limit: produced {} tokens, limit is {}",
+            count, TOKEN_LIMIT,
         ));
     }
 

From 19508b474f6024ac5e0a645af59a1eba1075a344 Mon Sep 17 00:00:00 2001
From: Jonas Schievink <jonasschievink@gmail.com>
Date: Thu, 10 Dec 2020 17:51:39 +0100
Subject: [PATCH 2/3] Double the macro token limit

---
 crates/hir_expand/src/db.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 3c0ee284ed2..245b4362d25 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -17,7 +17,7 @@ use crate::{
 ///
 /// If an invocation produces more tokens than this limit, it will not be stored in the database and
 /// an error will be emitted.
-const TOKEN_LIMIT: usize = 262144;
+const TOKEN_LIMIT: usize = 0x80000;
 
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub enum TokenExpander {

From 829d9d36ebd3b86f447b12ba573cf3e6cb5b0373 Mon Sep 17 00:00:00 2001
From: Jonas Schievink <jonasschievink@gmail.com>
Date: Thu, 10 Dec 2020 18:03:37 +0100
Subject: [PATCH 3/3] Use decimal notation

---
 crates/hir_expand/src/db.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 245b4362d25..ffb70f723c0 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -17,7 +17,7 @@ use crate::{
 ///
 /// If an invocation produces more tokens than this limit, it will not be stored in the database and
 /// an error will be emitted.
-const TOKEN_LIMIT: usize = 0x80000;
+const TOKEN_LIMIT: usize = 524288;
 
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub enum TokenExpander {