Skip to content

Commit

Permalink
Stop unloading syntax libraries
Browse files Browse the repository at this point in the history
Externally loaded libraries are able to do things that cause references
to them to survive past the expansion phase (e.g. creating @-box cycles,
launching a task or storing something in task local data). As such, the
library has to stay loaded for the lifetime of the process.
  • Loading branch information
sfackler authored and alexcrichton committed Feb 13, 2014
1 parent cfb87f1 commit 6b429d0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
8 changes: 0 additions & 8 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use parse::token::{InternedString, intern, str_to_ident};
use util::small_vector::SmallVector;

use std::hashmap::HashMap;
use std::unstable::dynamic_lib::DynamicLibrary;

// new-style macro! tt code:
//
Expand Down Expand Up @@ -143,16 +142,13 @@ pub struct BlockInfo {
macros_escape : bool,
// what are the pending renames?
pending_renames : RenameList,
// references for crates loaded in this scope
macro_crates: ~[DynamicLibrary],
}

impl BlockInfo {
pub fn new() -> BlockInfo {
BlockInfo {
macros_escape: false,
pending_renames: ~[],
macro_crates: ~[],
}
}
}
Expand Down Expand Up @@ -551,10 +547,6 @@ impl SyntaxEnv {
self.find_escape_frame().map.insert(k, v);
}

pub fn insert_macro_crate(&mut self, lib: DynamicLibrary) {
self.find_escape_frame().info.macro_crates.push(lib);
}

pub fn info<'a>(&'a mut self) -> &'a mut BlockInfo {
&mut self.chain[self.chain.len()-1].info
}
Expand Down
10 changes: 7 additions & 3 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -28,6 +28,7 @@ use visit;
use visit::Visitor;
use util::small_vector::SmallVector;

use std::cast;
use std::vec;
use std::unstable::dynamic_lib::DynamicLibrary;
use std::os;
Expand Down Expand Up @@ -469,9 +470,12 @@ fn load_extern_macros(crate: &ast::ViewItem, fld: &mut MacroExpander) {
};
fld.extsbox.insert(name, extension);
});
}

fld.extsbox.insert_macro_crate(lib);
// Intentionally leak the dynamic library. We can't ever unload it
// since the library can do things that will outlive the expansion
// phase (e.g. make an @-box cycle or launch a task).
cast::forget(lib);
}
}

// expand a stmt
Expand Down
35 changes: 35 additions & 0 deletions src/test/auxiliary/macro_crate_outlive_expansion_phase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// force-host

#[feature(macro_registrar)];

extern mod syntax;

use std::any::Any;
use std::local_data;
use syntax::ast::Name;
use syntax::ext::base::SyntaxExtension;

struct Foo {
foo: int
}

impl Drop for Foo {
fn drop(&mut self) {}
}

#[macro_registrar]
pub fn registrar(_: |Name, SyntaxExtension|) {
local_data_key!(foo: ~Any);
local_data::set(foo, ~Foo { foo: 10 } as ~Any);
}

21 changes: 21 additions & 0 deletions src/test/run-pass-fulldeps/macro-crate-outlive-expansion-phase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:macro_crate_outlive_expansion_phase.rs
// ignore-stage1
// ignore-fast
// ignore-android

#[feature(phase)];

#[phase(syntax)]
extern mod macro_crate_outlive_expansion_phase;

pub fn main() {}

0 comments on commit 6b429d0

Please sign in to comment.