Stream: t-lang

Topic: Can #[no_link] be extended to pull in types?


Lokathor (Jan 19 2020 at 03:25, on Zulip):

There's a #[no_link] attribute that you can put on an extern crate foo; statement to declare that you don't want to link against an extern crate.

This is, according to the notes in the reference, intended so that you can use macros from that crate. However, I wonder if we can expand the attribute so that it also pulls in types from that crate? They're just aliases after all, you're not linking in any actual code by using them.

Playground example of the desired possible usage: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bc74ec6092c6ab838922555a773044d9

simulacrum (Jan 19 2020 at 03:35, on Zulip):

(Out of interest, is there a reason this is an attribute, and not default behavior?)

rkruppe (Jan 19 2020 at 09:34, on Zulip):

other than backward compatibility, you mean? for example, linking a crate can pull in #[no_mangle] symbols or native libraries that other parts of the application need, without that connection being apparent to rustc

simulacrum (Jan 19 2020 at 12:55, on Zulip):

Hm, I guess it does depend, sometimes you want linkage and sometimes you don't.

rkruppe (Jan 19 2020 at 14:54, on Zulip):

Yeah. Arguably the default should have been different but, you know, backwards compatibility

gnzlbg (Jan 20 2020 at 12:44, on Zulip):

Notice that extern crate foo; does not link anything

gnzlbg (Jan 20 2020 at 12:45, on Zulip):

You have to actually go and use something from the foo crate

gnzlbg (Jan 20 2020 at 12:45, on Zulip):

since otherwise the crate is marked as "unused" and it will be removed early on

gnzlbg (Jan 20 2020 at 12:46, on Zulip):

(e.g. extern crate jemallocator_global; does not override the #[global_allocator] for this reason)

gnzlbg (Jan 20 2020 at 12:46, on Zulip):

(that crate only contains a #[global_allocator] declaration)

simulacrum (Jan 21 2020 at 02:37, on Zulip):

Hm, I guess it does depend, sometimes you want linkage and sometimes you don't.

gnzlbg (Jan 22 2020 at 09:43, on Zulip):

usually, when the crate you have as a dependency contains something "global", like builtins, #[global_allocator], panic-related functionality, etc. you want it linked

Last update: Jun 07 2020 at 08:50UTC