Skip to content

Commit 56a20c4

Browse files
committed
Auto merge of #63948 - crlf0710:path_to_raw_dylib, r=Centril
Add feature gate for raw_dylib. This PR adds the feature gate for RFC 2627 (#58713). It doesn't contain the actual functionality. Add I'm not sure whether i did it correctly, since this is the first time i did this. r? @Centril
2 parents 4ff32c0 + 9846af8 commit 56a20c4

19 files changed

+221
-4
lines changed

src/librustc/hir/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,11 @@ pub struct CodegenFnAttrs {
26262626
/// probably isn't set when this is set, this is for foreign items while
26272627
/// `#[export_name]` is for Rust-defined functions.
26282628
pub link_name: Option<Symbol>,
2629+
/// The `#[link_ordinal = "..."]` attribute, indicating an ordinal an
2630+
/// imported function has in the dynamic library. Note that this must not
2631+
/// be set when `link_name` is set. This is for foreign items with the
2632+
/// "raw-dylib" kind.
2633+
pub link_ordinal: Option<usize>,
26292634
/// The `#[target_feature(enable = "...")]` attribute and the enabled
26302635
/// features (only enabled features are supported right now).
26312636
pub target_features: Vec<Symbol>,
@@ -2685,6 +2690,7 @@ impl CodegenFnAttrs {
26852690
optimize: OptimizeAttr::None,
26862691
export_name: None,
26872692
link_name: None,
2693+
link_ordinal: None,
26882694
target_features: vec![],
26892695
linkage: None,
26902696
link_section: None,

src/librustc/middle/cstore.rs

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pub enum NativeLibraryKind {
9696
NativeStaticNobundle,
9797
/// macOS-specific
9898
NativeFramework,
99+
/// Windows dynamic library without import library.
100+
NativeRawDylib,
99101
/// default way to specify a dynamic library
100102
NativeUnknown,
101103
}

src/librustc_codegen_ssa/back/link.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
314314
NativeLibraryKind::NativeStatic => {}
315315
NativeLibraryKind::NativeStaticNobundle |
316316
NativeLibraryKind::NativeFramework |
317+
NativeLibraryKind::NativeRawDylib |
317318
NativeLibraryKind::NativeUnknown => continue,
318319
}
319320
if let Some(name) = lib.name {
@@ -874,7 +875,8 @@ pub fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary
874875
Some(format!("-framework {}", name))
875876
},
876877
// These are included, no need to print them
877-
NativeLibraryKind::NativeStatic => None,
878+
NativeLibraryKind::NativeStatic |
879+
NativeLibraryKind::NativeRawDylib => None,
878880
}
879881
})
880882
.collect();
@@ -1284,7 +1286,11 @@ pub fn add_local_native_libraries(cmd: &mut dyn Linker,
12841286
NativeLibraryKind::NativeUnknown => cmd.link_dylib(name),
12851287
NativeLibraryKind::NativeFramework => cmd.link_framework(name),
12861288
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(name),
1287-
NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(name, &search_path)
1289+
NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(name, &search_path),
1290+
NativeLibraryKind::NativeRawDylib => {
1291+
// FIXME(#58713): Proper handling for raw dylibs.
1292+
bug!("raw_dylib feature not yet implemented");
1293+
},
12881294
}
12891295
}
12901296
}
@@ -1661,7 +1667,11 @@ pub fn add_upstream_native_libraries(cmd: &mut dyn Linker,
16611667
// ignore statically included native libraries here as we've
16621668
// already included them when we included the rust library
16631669
// previously
1664-
NativeLibraryKind::NativeStatic => {}
1670+
NativeLibraryKind::NativeStatic => {},
1671+
NativeLibraryKind::NativeRawDylib => {
1672+
// FIXME(#58713): Proper handling for raw dylibs.
1673+
bug!("raw_dylib feature not yet implemented");
1674+
},
16651675
}
16661676
}
16671677
}

src/librustc_metadata/cstore_impl.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ pub fn provide(providers: &mut Providers<'_>) {
248248
// resolve! Does this work? Unsure! That's what the issue is about
249249
*providers = Providers {
250250
is_dllimport_foreign_item: |tcx, id| {
251-
tcx.native_library_kind(id) == Some(NativeLibraryKind::NativeUnknown)
251+
match tcx.native_library_kind(id) {
252+
Some(NativeLibraryKind::NativeUnknown) |
253+
Some(NativeLibraryKind::NativeRawDylib) => true,
254+
_ => false,
255+
}
252256
},
253257
is_statically_included_foreign_item: |tcx, id| {
254258
match tcx.native_library_kind(id) {

src/librustc_metadata/native_libs.rs

+9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
7373
"static-nobundle" => cstore::NativeStaticNobundle,
7474
"dylib" => cstore::NativeUnknown,
7575
"framework" => cstore::NativeFramework,
76+
"raw-dylib" => cstore::NativeRawDylib,
7677
k => {
7778
struct_span_err!(self.tcx.sess, item.span(), E0458,
7879
"unknown kind: `{}`", k)
@@ -169,6 +170,14 @@ impl Collector<'tcx> {
169170
GateIssue::Language,
170171
"kind=\"static-nobundle\" is unstable");
171172
}
173+
if lib.kind == cstore::NativeRawDylib &&
174+
!self.tcx.features().raw_dylib {
175+
feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
176+
sym::raw_dylib,
177+
span.unwrap_or_else(|| syntax_pos::DUMMY_SP),
178+
GateIssue::Language,
179+
"kind=\"raw-dylib\" is unstable");
180+
}
172181
self.libs.push(lib);
173182
}
174183

src/librustc_typeck/collect.rs

+53
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
25222522
let whitelist = tcx.target_features_whitelist(LOCAL_CRATE);
25232523

25242524
let mut inline_span = None;
2525+
let mut link_ordinal_span = None;
25252526
for attr in attrs.iter() {
25262527
if attr.check_name(sym::cold) {
25272528
codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
@@ -2603,6 +2604,11 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26032604
}
26042605
} else if attr.check_name(sym::link_name) {
26052606
codegen_fn_attrs.link_name = attr.value_str();
2607+
} else if attr.check_name(sym::link_ordinal) {
2608+
link_ordinal_span = Some(attr.span);
2609+
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
2610+
codegen_fn_attrs.link_ordinal = ordinal;
2611+
}
26062612
}
26072613
}
26082614

@@ -2680,6 +2686,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26802686
// purpose functions as they wouldn't have the right target features
26812687
// enabled. For that reason we also forbid #[inline(always)] as it can't be
26822688
// respected.
2689+
26832690
if codegen_fn_attrs.target_features.len() > 0 {
26842691
if codegen_fn_attrs.inline == InlineAttr::Always {
26852692
if let Some(span) = inline_span {
@@ -2704,6 +2711,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27042711
codegen_fn_attrs.export_name = Some(name);
27052712
codegen_fn_attrs.link_name = Some(name);
27062713
}
2714+
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
27072715

27082716
// Internal symbols to the standard library all have no_mangle semantics in
27092717
// that they have defined symbol names present in the function name. This
@@ -2714,3 +2722,48 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27142722

27152723
codegen_fn_attrs
27162724
}
2725+
2726+
fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<usize> {
2727+
use syntax::ast::{Lit, LitIntType, LitKind};
2728+
let meta_item_list = attr.meta_item_list();
2729+
let meta_item_list: Option<&[ast::NestedMetaItem]> = meta_item_list.as_ref().map(Vec::as_ref);
2730+
let sole_meta_list = match meta_item_list {
2731+
Some([item]) => item.literal(),
2732+
_ => None,
2733+
};
2734+
if let Some(Lit { node: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = sole_meta_list {
2735+
if *ordinal <= std::usize::MAX as u128 {
2736+
Some(*ordinal as usize)
2737+
} else {
2738+
let msg = format!(
2739+
"ordinal value in `link_ordinal` is too large: `{}`",
2740+
&ordinal
2741+
);
2742+
tcx.sess.struct_span_err(attr.span, &msg)
2743+
.note("the value may not exceed `std::usize::MAX`")
2744+
.emit();
2745+
None
2746+
}
2747+
} else {
2748+
tcx.sess.struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
2749+
.note("an unsuffixed integer value, e.g., `1`, is expected")
2750+
.emit();
2751+
None
2752+
}
2753+
}
2754+
2755+
fn check_link_name_xor_ordinal(
2756+
tcx: TyCtxt<'_>,
2757+
codegen_fn_attrs: &CodegenFnAttrs,
2758+
inline_span: Option<Span>,
2759+
) {
2760+
if codegen_fn_attrs.link_name.is_none() || codegen_fn_attrs.link_ordinal.is_none() {
2761+
return;
2762+
}
2763+
let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
2764+
if let Some(span) = inline_span {
2765+
tcx.sess.span_err(span, msg);
2766+
} else {
2767+
tcx.sess.err(msg);
2768+
}
2769+
}

src/libsyntax/feature_gate/active.rs

+4
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ declare_features! (
522522
/// Allows the use of or-patterns (e.g., `0 | 1`).
523523
(active, or_patterns, "1.38.0", Some(54883), None),
524524

525+
// Allows the use of raw-dylibs (RFC 2627).
526+
(active, raw_dylib, "1.39.0", Some(58713), None),
527+
525528
// -------------------------------------------------------------------------
526529
// feature-group-end: actual feature gates
527530
// -------------------------------------------------------------------------
@@ -536,4 +539,5 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
536539
sym::const_generics,
537540
sym::or_patterns,
538541
sym::let_chains,
542+
sym::raw_dylib,
539543
];

src/libsyntax/feature_gate/builtin_attrs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
276276
"the `link_args` attribute is experimental and not portable across platforms, \
277277
it is recommended to use `#[link(name = \"foo\")] instead",
278278
),
279+
gated!(
280+
link_ordinal, Whitelisted, template!(List: "ordinal"), raw_dylib,
281+
experimental!(link_ordinal)
282+
),
279283

280284
// Plugins:
281285
ungated!(plugin_registrar, Normal, template!(Word)),

src/libsyntax_pos/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ symbols! {
389389
link_cfg,
390390
link_llvm_intrinsics,
391391
link_name,
392+
link_ordinal,
392393
link_section,
393394
LintPass,
394395
lint_reasons,
@@ -531,6 +532,7 @@ symbols! {
531532
RangeInclusive,
532533
RangeTo,
533534
RangeToInclusive,
535+
raw_dylib,
534536
raw_identifiers,
535537
Ready,
536538
reason,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[link(name="foo")]
2+
extern {
3+
#[link_ordinal(42)]
4+
//~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature
5+
fn foo();
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: the `#[link_ordinal]` attribute is an experimental feature
2+
--> $DIR/feature-gate-raw-dylib-2.rs:3:5
3+
|
4+
LL | #[link_ordinal(42)]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com./rust-lang/rust/issues/58713
8+
= help: add `#![feature(raw_dylib)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[link(name="foo", kind="raw-dylib")]
2+
//~^ ERROR: kind="raw-dylib" is unstable
3+
extern {}
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: kind="raw-dylib" is unstable
2+
--> $DIR/feature-gate-raw-dylib.rs:1:1
3+
|
4+
LL | #[link(name="foo", kind="raw-dylib")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com./rust-lang/rust/issues/58713
8+
= help: add `#![feature(raw_dylib)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(raw_dylib)]
2+
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash
3+
4+
#[link(name="foo")]
5+
extern {
6+
#[link_name="foo"]
7+
#[link_ordinal(42)]
8+
//~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]`
9+
fn foo();
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
2+
--> $DIR/link-ordinal-and-name.rs:1:12
3+
|
4+
LL | #![feature(raw_dylib)]
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error: cannot use `#[link_name]` with `#[link_ordinal]`
10+
--> $DIR/link-ordinal-and-name.rs:7:5
11+
|
12+
LL | #[link_ordinal(42)]
13+
| ^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(raw_dylib)]
2+
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash
3+
4+
#[link(name="foo")]
5+
extern {
6+
#[link_ordinal("JustMonika")]
7+
//~^ ERROR illegal ordinal format in `link_ordinal`
8+
fn foo();
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
2+
--> $DIR/link-ordinal-invalid-format.rs:1:12
3+
|
4+
LL | #![feature(raw_dylib)]
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error: illegal ordinal format in `link_ordinal`
10+
--> $DIR/link-ordinal-invalid-format.rs:6:5
11+
|
12+
LL | #[link_ordinal("JustMonika")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: an unsuffixed integer value, e.g., `1`, is expected
16+
17+
error: aborting due to previous error
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(raw_dylib)]
2+
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash
3+
4+
#[link(name="foo")]
5+
extern {
6+
#[link_ordinal(18446744073709551616)]
7+
//~^ ERROR ordinal value in `link_ordinal` is too large: `18446744073709551616`
8+
fn foo();
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
2+
--> $DIR/link-ordinal-too-large.rs:1:12
3+
|
4+
LL | #![feature(raw_dylib)]
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error: ordinal value in `link_ordinal` is too large: `18446744073709551616`
10+
--> $DIR/link-ordinal-too-large.rs:6:5
11+
|
12+
LL | #[link_ordinal(18446744073709551616)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: the value may not exceed `std::usize::MAX`
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)