aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-12-10 16:50:56 +0000
committerJonas Schievink <[email protected]>2020-12-10 16:50:56 +0000
commit614e5a2272a96eabe30acea2f7c797a305a4f2fd (patch)
tree168a2812a27572f5646bc861d48181abe1e33efc
parenta6c8098113505009453b12c8b461dd905f299c05 (diff)
Improve macro limit error and move to const
-rw-r--r--crates/hir_expand/src/db.rs12
1 files changed, 9 insertions, 3 deletions
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 842a177db..3c0ee284e 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -13,6 +13,12 @@ use crate::{
13 MacroFile, ProcMacroExpander, 13 MacroFile, ProcMacroExpander,
14}; 14};
15 15
16/// Total limit on the number of tokens produced by any macro invocation.
17///
18/// If an invocation produces more tokens than this limit, it will not be stored in the database and
19/// an error will be emitted.
20const TOKEN_LIMIT: usize = 262144;
21
16#[derive(Debug, Clone, Eq, PartialEq)] 22#[derive(Debug, Clone, Eq, PartialEq)]
17pub enum TokenExpander { 23pub enum TokenExpander {
18 MacroRules(mbe::MacroRules), 24 MacroRules(mbe::MacroRules),
@@ -227,10 +233,10 @@ fn macro_expand_with_arg(
227 let ExpandResult { value: tt, err } = macro_rules.0.expand(db, lazy_id, &macro_arg.0); 233 let ExpandResult { value: tt, err } = macro_rules.0.expand(db, lazy_id, &macro_arg.0);
228 // Set a hard limit for the expanded tt 234 // Set a hard limit for the expanded tt
229 let count = tt.count(); 235 let count = tt.count();
230 if count > 262144 { 236 if count > TOKEN_LIMIT {
231 return ExpandResult::str_err(format!( 237 return ExpandResult::str_err(format!(
232 "Total tokens count exceed limit : count = {}", 238 "macro invocation exceeds token limit: produced {} tokens, limit is {}",
233 count 239 count, TOKEN_LIMIT,
234 )); 240 ));
235 } 241 }
236 242