]> git.lizzy.rs Git - rust.git/commitdiff
feat: Honor recursion limit configuration
authorMaybe Waffle <waffle.lapkin@gmail.com>
Thu, 27 Jan 2022 22:23:09 +0000 (01:23 +0300)
committerMaybe Waffle <waffle.lapkin@gmail.com>
Fri, 28 Jan 2022 08:31:59 +0000 (11:31 +0300)
This patch makes RA understand `#![recursion_limit = "N"]` annotations.

- `crate_limits` query is moved to `DefDatabase`
- `DefMap` now has `recursion_limit: Option<u32>` field

crates/base_db/src/lib.rs
crates/hir_def/src/body/tests.rs
crates/hir_def/src/db.rs
crates/hir_def/src/nameres.rs
crates/hir_def/src/nameres/collector.rs
crates/hir_expand/src/name.rs

index a4c590cf6d918435c43967d63b6058c6cff4361e..d80660f7c9a36553775b52a5ac483e602b4ee651 100644 (file)
@@ -69,21 +69,6 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
     /// The crate graph.
     #[salsa::input]
     fn crate_graph(&self) -> Arc<CrateGraph>;
-
-    #[salsa::transparent]
-    fn crate_limits(&self, crate_id: CrateId) -> CrateLimits;
-}
-
-pub struct CrateLimits {
-    /// The maximum depth for potentially infinitely-recursive compile-time operations like macro expansion or auto-dereference.
-    pub recursion_limit: u32,
-}
-
-fn crate_limits(_db: &dyn SourceDatabase, _crate_id: CrateId) -> CrateLimits {
-    CrateLimits {
-        // 128 is the default in rustc.
-        recursion_limit: 128,
-    }
 }
 
 fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
index 673a75386167685f971e80ecf01bf3618e5d155c..1d84da48fb9bcbd2ed5471be4456ae1b8623ff17 100644 (file)
@@ -61,6 +61,24 @@ macro_rules! n_nuple {
     );
 }
 
+#[test]
+fn recursion_limit() {
+    cov_mark::check!(your_stack_belongs_to_me);
+
+    lower(
+        r#"
+#![recursion_limit = "2"]
+macro_rules! n_nuple {
+    ($e:tt) => ();
+    ($first:tt $($rest:tt)*) => {{
+        n_nuple!($($rest)*)
+    }};
+}
+fn main() { n_nuple!(1,2,3); }
+"#,
+    );
+}
+
 #[test]
 fn macro_resolve() {
     // Regression test for a path resolution bug introduced with inner item handling.
index 7946558311a27f4fcc0090eca2fdee8fc0066444..f9dd935c4b0c994e2cda76947b69852f7a04d02a 100644 (file)
@@ -157,9 +157,26 @@ fn fields_attrs_source_map(
 
     #[salsa::invoke(visibility::function_visibility_query)]
     fn function_visibility(&self, def: FunctionId) -> Visibility;
+
+    #[salsa::transparent]
+    fn crate_limits(&self, crate_id: CrateId) -> CrateLimits;
 }
 
 fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
     let _p = profile::span("crate_def_map:wait");
     db.crate_def_map_query(krate)
 }
+
+pub struct CrateLimits {
+    /// The maximum depth for potentially infinitely-recursive compile-time operations like macro expansion or auto-dereference.
+    pub recursion_limit: u32,
+}
+
+fn crate_limits(db: &dyn DefDatabase, crate_id: CrateId) -> CrateLimits {
+    let def_map = db.crate_def_map(crate_id);
+
+    CrateLimits {
+        // 128 is the default in rustc.
+        recursion_limit: def_map.recursion_limit().unwrap_or(128),
+    }
+}
index 64929c02b407177d28dc2233493ecf79d7421df6..ca4255c58193f9b407b543448107789a906b3ccd 100644 (file)
@@ -114,6 +114,7 @@ pub struct DefMap {
     registered_tools: Vec<SmolStr>,
 
     edition: Edition,
+    recursion_limit: Option<u32>,
     diagnostics: Vec<DefDiagnostic>,
 }
 
@@ -272,6 +273,7 @@ fn empty(krate: CrateId, edition: Edition, root_module_origin: ModuleOrigin) ->
             block: None,
             krate,
             edition,
+            recursion_limit: None,
             extern_prelude: FxHashMap::default(),
             exported_proc_macros: FxHashMap::default(),
             prelude: None,
@@ -461,6 +463,7 @@ fn shrink_to_fit(&mut self) {
             registered_tools,
             block: _,
             edition: _,
+            recursion_limit: _,
             krate: _,
             prelude: _,
             root: _,
@@ -482,6 +485,10 @@ fn shrink_to_fit(&mut self) {
     pub fn diagnostics(&self) -> &[DefDiagnostic] {
         self.diagnostics.as_slice()
     }
+
+    pub fn recursion_limit(&self) -> Option<u32> {
+        self.recursion_limit
+    }
 }
 
 impl ModuleData {
index dc75dc4d91a7e3114954f84b2d130727c16ebb96..7f7213f4c3e6946a42c7d6b68572cd44be147031 100644 (file)
@@ -271,6 +271,17 @@ fn seed_with_top_level(&mut self) {
                     None => continue,
                 };
 
+                if *attr_name == hir_expand::name![recursion_limit] {
+                    if let Some(input) = &attr.input {
+                        if let AttrInput::Literal(limit) = &**input {
+                            if let Ok(limit) = limit.parse() {
+                                self.def_map.recursion_limit = Some(limit);
+                            }
+                        }
+                    }
+                    continue;
+                }
+
                 let attr_is_register_like = *attr_name == hir_expand::name![register_attr]
                     || *attr_name == hir_expand::name![register_tool];
                 if !attr_is_register_like {
index 4dcda0fcdc88779fffa994f204687401c1734e7a..c36bd09e2b3b780e90b60291fee178431c08c327 100644 (file)
@@ -270,6 +270,7 @@ macro_rules! known_names {
         global_allocator,
         test,
         test_case,
+        recursion_limit,
         // Safe intrinsics
         abort,
         add_with_overflow,