Skip to content

Commit

Permalink
Auto merge of rust-lang#92034 - petrochenkov:nolinknores, r=joshtriplett
Browse files Browse the repository at this point in the history
Remove effect of `#[no_link]` attribute on name resolution

Previously it hid all non-macro names from other crates.
This has no relation to linking and can change name resolution behavior in some cases (e.g. glob conflicts), in addition to just producing the "unresolved name" errors.

I can kind of understand the possible reasoning behind the current behavior - if you can use names from a `no_link` crates then you can use, for example, functions too, but whether it will actually work or produce link-time errors will depend on random factors like inliner behavior.
(^^^ This is not the actual reason why the current behavior exist, I've looked through git history and it's mostly accidental.)

I think this risk is ok for such an obscure attribute, and we don't need to specifically prevent use of non-macro items from such crates.
(I'm not actually sure why would anyone use `#[no_link]` on a crate, even if it's macro only, if you aware of any use cases, please share. IIRC, at some point it was used for crates implementing custom derives - the now removed legacy ones, not the current proc macros.)

Extracted from rust-lang#91795.
  • Loading branch information
bors committed Jan 2, 2022
2 parents 7b13c62 + 54cd824 commit f7934f6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 19 deletions.
10 changes: 1 addition & 9 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,10 +1100,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
};

// Iterate over all children.
let macros_only = self.dep_kind.lock().macros_only();
if !macros_only {
let children = self.root.tables.children.get(self, id).unwrap_or_else(Lazy::empty);

if let Some(children) = self.root.tables.children.get(self, id) {
for child_index in children.decode((self, sess)) {
// FIXME: Merge with the logic below.
if let None | Some(EntryKind::ForeignMod | EntryKind::Impl(_)) =
Expand Down Expand Up @@ -1172,11 +1169,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

if let EntryKind::Mod(exports) = kind {
for exp in exports.decode((self, sess)) {
match exp.res {
Res::Def(DefKind::Macro(..), _) => {}
_ if macros_only => continue,
_ => {}
}
callback(exp);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/no-link.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// check-pass
// aux-build:empty-struct.rs

#[no_link]
extern crate empty_struct;

fn main() {
empty_struct::XEmpty1; //~ ERROR cannot find value `XEmpty1` in crate `empty_struct`
empty_struct::XEmpty1 {};
}
9 changes: 0 additions & 9 deletions src/test/ui/no-link.stderr

This file was deleted.

0 comments on commit f7934f6

Please sign in to comment.