]> git.lizzy.rs Git - rust.git/commitdiff
Run lint passes using the Option dance instead of RefCells
authorKeegan McAllister <kmcallister@mozilla.com>
Fri, 13 Jun 2014 07:00:49 +0000 (00:00 -0700)
committerKeegan McAllister <kmcallister@mozilla.com>
Tue, 24 Jun 2014 18:36:27 +0000 (11:36 -0700)
src/librustc/lint/context.rs

index bcb0193f34446f39cb0d90a67b47ce4292658c73..bc92d69a747c4b3dbd7d4ecd35252ada5d045d5f 100644 (file)
@@ -56,7 +56,9 @@ pub struct LintStore {
     lints: Vec<(&'static Lint, bool)>,
 
     /// Trait objects for each lint pass.
-    passes: Vec<RefCell<LintPassObject>>,
+    /// This is only `None` while iterating over the objects. See the definition
+    /// of run_lints.
+    passes: Option<Vec<LintPassObject>>,
 
     /// Lints indexed by name.
     by_name: HashMap<&'static str, LintId>,
@@ -84,7 +86,7 @@ fn set_level(&mut self, lint: LintId, lvlsrc: LevelSource) {
     pub fn new() -> LintStore {
         LintStore {
             lints: vec!(),
-            passes: vec!(),
+            passes: Some(vec!()),
             by_name: HashMap::new(),
             levels: HashMap::new(),
         }
@@ -117,7 +119,7 @@ pub fn register_pass(&mut self, sess: Option<&Session>,
                 self.levels.insert(id, (lint.default_level, Default));
             }
         }
-        self.passes.push(RefCell::new(pass));
+        self.passes.get_mut_ref().push(pass);
     }
 
     pub fn register_builtin(&mut self, sess: Option<&Session>) {
@@ -181,11 +183,15 @@ pub struct Context<'a> {
 }
 
 /// Convenience macro for calling a `LintPass` method on every pass in the context.
-macro_rules! run_lints ( ($cx:expr, $f:ident, $($args:expr),*) => (
-    for obj in $cx.lints.passes.iter() {
-        obj.borrow_mut().$f($cx, $($args),*);
-    }
-))
+macro_rules! run_lints ( ($cx:expr, $f:ident, $($args:expr),*) => ({
+    // Move the vector of passes out of `$cx` so that we can
+    // iterate over it mutably while passing `$cx` to the methods.
+    let mut passes = $cx.lints.passes.take_unwrap();
+    for obj in passes.mut_iter() {
+        obj.$f($cx, $($args),*);
+    }
+    $cx.lints.passes = Some(passes);
+}))
 
 /// Emit a lint as a warning or an error (or not at all)
 /// according to `level`.