From e163ab215105753fd4e9b60a3786ff4636a0c3ff Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 13 Apr 2014 11:26:43 -0700 Subject: [PATCH] rustc: Don't link in syntax extensions This bug was introduced in #13384 by accident, and this commit continues the work of #13384 by finishing support for loading a syntax extension crate without registering it with the local cstore. Closes #13495 --- src/librustc/driver/driver.rs | 6 +++--- src/librustc/driver/session.rs | 2 +- src/librustc/metadata/creader.rs | 14 +++++++------- src/librustc/metadata/csearch.rs | 2 +- src/librustc/metadata/decoder.rs | 4 ++-- src/librustc/metadata/encoder.rs | 8 ++------ src/libsyntax/ext/registrar.rs | 7 ++----- .../run-make/lto-syntax-extension/Makefile | 6 ++++++ src/test/run-make/lto-syntax-extension/lib.rs | 11 +++++++++++ src/test/run-make/lto-syntax-extension/main.rs | 18 ++++++++++++++++++ 10 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 src/test/run-make/lto-syntax-extension/Makefile create mode 100644 src/test/run-make/lto-syntax-extension/lib.rs create mode 100644 src/test/run-make/lto-syntax-extension/main.rs diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index 1b3653c6948a6..4fc7239e63b18 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -310,10 +310,10 @@ pub fn phase_3_run_analysis_passes(sess: Session, time(time_passes, "looking for entry point", (), |_| middle::entry::find_entry_point(&sess, krate, &ast_map)); - *sess.macro_registrar_fn.borrow_mut() = + sess.macro_registrar_fn.set( time(time_passes, "looking for macro registrar", (), |_| syntax::ext::registrar::find_macro_registrar( - sess.diagnostic(), krate)); + sess.diagnostic(), krate))); let freevars = time(time_passes, "freevar finding", (), |_| freevars::annotate_freevars(def_map, krate)); @@ -1054,7 +1054,7 @@ pub fn build_session_(sopts: session::Options, // For a library crate, this is always none entry_fn: RefCell::new(None), entry_type: Cell::new(None), - macro_registrar_fn: RefCell::new(None), + macro_registrar_fn: Cell::new(None), default_sysroot: default_sysroot, building_library: Cell::new(false), local_crate_source_file: local_crate_source_file, diff --git a/src/librustc/driver/session.rs b/src/librustc/driver/session.rs index a412742ab3a36..5d3069bbf3e2e 100644 --- a/src/librustc/driver/session.rs +++ b/src/librustc/driver/session.rs @@ -181,7 +181,7 @@ pub struct Session { // For a library crate, this is always none pub entry_fn: RefCell>, pub entry_type: Cell>, - pub macro_registrar_fn: RefCell>, + pub macro_registrar_fn: Cell>, pub default_sysroot: Option, pub building_library: Cell, // The name of the root source file of the crate, in the local file system. The path is always diff --git a/src/librustc/metadata/creader.rs b/src/librustc/metadata/creader.rs index b57ab8d0f2fc8..e6b7049f4f8fa 100644 --- a/src/librustc/metadata/creader.rs +++ b/src/librustc/metadata/creader.rs @@ -16,7 +16,6 @@ use back::link; use back::svh::Svh; use driver::{driver, session}; use driver::session::Session; -use metadata::csearch; use metadata::cstore; use metadata::cstore::CStore; use metadata::decoder; @@ -397,13 +396,14 @@ impl<'a> Loader<'a> { impl<'a> CrateLoader for Loader<'a> { fn load_crate(&mut self, krate: &ast::ViewItem) -> MacroCrate { let info = extract_crate_info(&self.env, krate).unwrap(); - let (cnum, data, library) = resolve_crate(&mut self.env, &None, - info.ident, &info.crate_id, - None, true, krate.span); + let (_, data, library) = resolve_crate(&mut self.env, &None, + info.ident, &info.crate_id, + None, info.should_link, + krate.span); let macros = decoder::get_exported_macros(data); - let cstore = &self.env.sess.cstore; - let registrar = csearch::get_macro_registrar_fn(cstore, cnum) - .map(|did| csearch::get_symbol(cstore, did)); + let registrar = decoder::get_macro_registrar_fn(data).map(|id| { + decoder::get_symbol(data.data.as_slice(), id) + }); MacroCrate { lib: library.dylib, macros: macros.move_iter().collect(), diff --git a/src/librustc/metadata/csearch.rs b/src/librustc/metadata/csearch.rs index 16068dd939b1d..f02f9c6158227 100644 --- a/src/librustc/metadata/csearch.rs +++ b/src/librustc/metadata/csearch.rs @@ -279,7 +279,7 @@ pub fn get_trait_of_method(cstore: &cstore::CStore, pub fn get_macro_registrar_fn(cstore: &cstore::CStore, crate_num: ast::CrateNum) - -> Option { + -> Option { let cdata = cstore.get_crate_data(crate_num); decoder::get_macro_registrar_fn(cdata) } diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 28968a6001616..5fe59a787737a 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -1235,9 +1235,9 @@ pub fn get_native_libraries(cdata: Cmd) -> Vec<(cstore::NativeLibaryKind, ~str)> return result; } -pub fn get_macro_registrar_fn(cdata: Cmd) -> Option { +pub fn get_macro_registrar_fn(cdata: Cmd) -> Option { reader::maybe_get_doc(reader::Doc(cdata.data()), tag_macro_registrar_fn) - .map(|doc| item_def_id(doc, cdata)) + .map(|doc| FromPrimitive::from_u32(reader::doc_as_u32(doc)).unwrap()) } pub fn get_exported_macros(cdata: Cmd) -> Vec<~str> { diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index e2fb4e26e5962..d5313eb257810 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -1565,12 +1565,8 @@ fn encode_native_libraries(ecx: &EncodeContext, ebml_w: &mut Encoder) { } fn encode_macro_registrar_fn(ecx: &EncodeContext, ebml_w: &mut Encoder) { - match *ecx.tcx.sess.macro_registrar_fn.borrow() { - Some(did) => { - ebml_w.start_tag(tag_macro_registrar_fn); - encode_def_id(ebml_w, did); - ebml_w.end_tag(); - } + match ecx.tcx.sess.macro_registrar_fn.get() { + Some(id) => { ebml_w.wr_tagged_u32(tag_macro_registrar_fn, id); } None => {} } } diff --git a/src/libsyntax/ext/registrar.rs b/src/libsyntax/ext/registrar.rs index ecd60fc9932d9..b76708147e1b2 100644 --- a/src/libsyntax/ext/registrar.rs +++ b/src/libsyntax/ext/registrar.rs @@ -36,7 +36,7 @@ impl Visitor<()> for MacroRegistrarContext { } pub fn find_macro_registrar(diagnostic: &diagnostic::SpanHandler, - krate: &ast::Crate) -> Option { + krate: &ast::Crate) -> Option { let mut ctx = MacroRegistrarContext { registrars: Vec::new() }; visit::walk_crate(&mut ctx, krate, ()); @@ -44,10 +44,7 @@ pub fn find_macro_registrar(diagnostic: &diagnostic::SpanHandler, 0 => None, 1 => { let (node_id, _) = ctx.registrars.pop().unwrap(); - Some(ast::DefId { - krate: ast::LOCAL_CRATE, - node: node_id - }) + Some(node_id) }, _ => { diagnostic.handler().err("multiple macro registration functions found"); diff --git a/src/test/run-make/lto-syntax-extension/Makefile b/src/test/run-make/lto-syntax-extension/Makefile new file mode 100644 index 0000000000000..4652556d34401 --- /dev/null +++ b/src/test/run-make/lto-syntax-extension/Makefile @@ -0,0 +1,6 @@ +-include ../tools.mk + +all: + $(RUSTC) lib.rs + $(RUSTC) main.rs -Z lto + $(call RUN,main) diff --git a/src/test/run-make/lto-syntax-extension/lib.rs b/src/test/run-make/lto-syntax-extension/lib.rs new file mode 100644 index 0000000000000..fbe967786e25e --- /dev/null +++ b/src/test/run-make/lto-syntax-extension/lib.rs @@ -0,0 +1,11 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[crate_type = "rlib"]; diff --git a/src/test/run-make/lto-syntax-extension/main.rs b/src/test/run-make/lto-syntax-extension/main.rs new file mode 100644 index 0000000000000..0c55d5fd3144d --- /dev/null +++ b/src/test/run-make/lto-syntax-extension/main.rs @@ -0,0 +1,18 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(phase)] + +extern crate lib; +#[phase(syntax)] extern crate fourcc; + +fn main() { + fourcc!("1234"); +}