Skip to content

Commit da5b646

Browse files
authored
Auto merge of #37247 - jseyfried:future_proof_no_link, r=nrc
macros: Future proof `#[no_link]` This PR future proofs `#[no_link]` for macro modularization (cc #35896). First, we resolve all `#[no_link] extern crate`s. `#[no_link]` crates without `#[macro_use]` or `#[macro_reexport]` are not resolved today, this is a [breaking-change]. For example, ```rust ``` Any breakage can be fixed by simply removing the `#[no_link] extern crate`. Second, `#[no_link] extern crate`s will define an empty module in type namespace to eventually allow importing the crate's macros with `use`. This is a [breaking-change], for example: ```rust mod syntax {} //< This becomes a duplicate error. ``` r? @nrc
2 parents e470827 + b283aaf commit da5b646

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

src/librustc_metadata/macro_import.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<'a> CrateLoader<'a> {
5252
// Parse the attributes relating to macros.
5353
let mut import = ImportSelection::Some(FnvHashMap());
5454
let mut reexport = FnvHashMap();
55+
let mut no_link = false;
5556

5657
for attr in &extern_crate.attrs {
5758
let mut used = true;
@@ -87,24 +88,30 @@ impl<'a> CrateLoader<'a> {
8788
}
8889
}
8990
}
91+
"no_link" => no_link = true,
9092
_ => used = false,
9193
}
9294
if used {
9395
attr::mark_used(attr);
9496
}
9597
}
9698

97-
self.load_macros(extern_crate, allows_macros, import, reexport)
99+
self.load_macros(extern_crate, allows_macros, import, reexport, no_link)
98100
}
99101

100102
fn load_macros<'b>(&mut self,
101103
vi: &ast::Item,
102104
allows_macros: bool,
103105
import: ImportSelection,
104-
reexport: MacroSelection)
106+
reexport: MacroSelection,
107+
no_link: bool)
105108
-> Vec<LoadedMacro> {
106109
if let ImportSelection::Some(ref sel) = import {
107110
if sel.is_empty() && reexport.is_empty() {
111+
// Make sure we can read macros from `#[no_link]` crates.
112+
if no_link {
113+
self.creader.read_macros(vi);
114+
}
108115
return Vec::new();
109116
}
110117
}

src/librustc_resolve/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl<'b> Resolver<'b> {
267267
}
268268

269269
self.populate_module_if_necessary(module);
270-
} else if custom_derive_crate {
270+
} else {
271271
// Define an empty module
272272
let def = Def::Mod(self.definitions.local_def_id(item.id));
273273
let module = ModuleS::new(Some(parent), ModuleKind::Def(def, name));

src/test/compile-fail-fulldeps/macro-crate-doesnt-resolve.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@
1414
extern crate macro_crate_test;
1515

1616
fn main() {
17-
macro_crate_test::foo();
18-
//~^ ERROR failed to resolve. Use of undeclared type or module `macro_crate_test`
17+
macro_crate_test::foo(); //~ ERROR unresolved name
1918
}

src/test/compile-fail-fulldeps/macro-crate-unknown-crate.rs src/test/compile-fail-fulldeps/no-link-unknown-crate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[macro_use] #[no_link]
11+
#[no_link]
1212
extern crate doesnt_exist; //~ ERROR can't find crate
1313

1414
fn main() {}

src/test/compile-fail/no-link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ extern crate libc;
1313

1414
fn main() {
1515
unsafe {
16-
libc::abs(0); //~ ERROR Use of undeclared type or module `libc`
16+
libc::abs(0); //~ ERROR unresolved name
1717
}
1818
}

0 commit comments

Comments
 (0)