From c57e8ebc4da758aa5cedd0c1616da2162292fd91 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Tue, 27 Aug 2019 22:42:44 +0800 Subject: [PATCH 1/4] Add feature gate for raw_dylib. --- src/librustc/middle/cstore.rs | 2 ++ src/librustc_codegen_ssa/back/link.rs | 16 +++++++++++++--- src/librustc_metadata/cstore_impl.rs | 6 +++++- src/librustc_metadata/native_libs.rs | 9 +++++++++ src/libsyntax/feature_gate/active.rs | 3 +++ src/libsyntax/feature_gate/builtin_attrs.rs | 8 +++++++- src/libsyntax_pos/symbol.rs | 2 ++ .../ui/feature-gates/feature-gate-raw-dylib-2.rs | 8 ++++++++ .../feature-gate-raw-dylib-2.stderr | 12 ++++++++++++ .../ui/feature-gates/feature-gate-raw-dylib.rs | 5 +++++ .../feature-gates/feature-gate-raw-dylib.stderr | 12 ++++++++++++ 11 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs create mode 100644 src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr create mode 100644 src/test/ui/feature-gates/feature-gate-raw-dylib.rs create mode 100644 src/test/ui/feature-gates/feature-gate-raw-dylib.stderr diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index de84fcd7160df..fcab28c929954 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -96,6 +96,8 @@ pub enum NativeLibraryKind { NativeStaticNobundle, /// macOS-specific NativeFramework, + /// windows dynamic library without import library + NativeRawDylib, /// default way to specify a dynamic library NativeUnknown, } diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index 9b044d9b45377..622dae80f2234 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -314,6 +314,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(sess: &'a Session, NativeLibraryKind::NativeStatic => {} NativeLibraryKind::NativeStaticNobundle | NativeLibraryKind::NativeFramework | + NativeLibraryKind::NativeRawDylib | NativeLibraryKind::NativeUnknown => continue, } if let Some(name) = lib.name { @@ -874,7 +875,8 @@ pub fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary Some(format!("-framework {}", name)) }, // These are included, no need to print them - NativeLibraryKind::NativeStatic => None, + NativeLibraryKind::NativeStatic | + NativeLibraryKind::NativeRawDylib => None, } }) .collect(); @@ -1284,7 +1286,11 @@ pub fn add_local_native_libraries(cmd: &mut dyn Linker, NativeLibraryKind::NativeUnknown => cmd.link_dylib(name), NativeLibraryKind::NativeFramework => cmd.link_framework(name), NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(name), - NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(name, &search_path) + NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(name, &search_path), + NativeLibraryKind::NativeRawDylib => { + // FIXME(#58713): Proper handling for raw dylibs. + bug!("raw_dylib feature not yet implemented"); + }, } } } @@ -1661,7 +1667,11 @@ pub fn add_upstream_native_libraries(cmd: &mut dyn Linker, // ignore statically included native libraries here as we've // already included them when we included the rust library // previously - NativeLibraryKind::NativeStatic => {} + NativeLibraryKind::NativeStatic => {}, + NativeLibraryKind::NativeRawDylib => { + // FIXME(#58713): Proper handling for raw dylibs. + bug!("raw_dylib feature not yet implemented"); + }, } } } diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index a1e3bbcbf8ea9..7979353589cf8 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -248,7 +248,11 @@ pub fn provide(providers: &mut Providers<'_>) { // resolve! Does this work? Unsure! That's what the issue is about *providers = Providers { is_dllimport_foreign_item: |tcx, id| { - tcx.native_library_kind(id) == Some(NativeLibraryKind::NativeUnknown) + match tcx.native_library_kind(id) { + Some(NativeLibraryKind::NativeUnknown) | + Some(NativeLibraryKind::NativeRawDylib) => true, + _ => false, + } }, is_statically_included_foreign_item: |tcx, id| { match tcx.native_library_kind(id) { diff --git a/src/librustc_metadata/native_libs.rs b/src/librustc_metadata/native_libs.rs index ada1a8c615d44..78a954befff7f 100644 --- a/src/librustc_metadata/native_libs.rs +++ b/src/librustc_metadata/native_libs.rs @@ -73,6 +73,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> { "static-nobundle" => cstore::NativeStaticNobundle, "dylib" => cstore::NativeUnknown, "framework" => cstore::NativeFramework, + "raw-dylib" => cstore::NativeRawDylib, k => { struct_span_err!(self.tcx.sess, item.span(), E0458, "unknown kind: `{}`", k) @@ -169,6 +170,14 @@ impl Collector<'tcx> { GateIssue::Language, "kind=\"static-nobundle\" is unstable"); } + if lib.kind == cstore::NativeRawDylib && + !self.tcx.features().raw_dylib { + feature_gate::emit_feature_err(&self.tcx.sess.parse_sess, + sym::raw_dylib, + span.unwrap_or_else(|| syntax_pos::DUMMY_SP), + GateIssue::Language, + "kind=\"raw-dylib\" is feature gated"); + } self.libs.push(lib); } diff --git a/src/libsyntax/feature_gate/active.rs b/src/libsyntax/feature_gate/active.rs index dd78777b56986..9526e6e271fab 100644 --- a/src/libsyntax/feature_gate/active.rs +++ b/src/libsyntax/feature_gate/active.rs @@ -525,6 +525,9 @@ declare_features! ( /// Allows the use of or-patterns (e.g., `0 | 1`). (active, or_patterns, "1.38.0", Some(54883), None), + // Allows the use of raw-dylibs (RFC 2627). + (active, raw_dylib, "1.39.0", Some(58713), None), + // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index b6e13200f32af..67c61d417b227 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -276,7 +276,13 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ "the `link_args` attribute is experimental and not portable across platforms, \ it is recommended to use `#[link(name = \"foo\")] instead", ), - + gated!( + link_ordinal, + Whitelisted, + template!(List: "ordinal"), + raw_dylib, + experimental!(link_ordinal) + ), // Plugins: ungated!(plugin_registrar, Normal, template!(Word)), gated!( diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 597ae83572cee..eb84ef18ad6a2 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -389,6 +389,7 @@ symbols! { link_cfg, link_llvm_intrinsics, link_name, + link_ordinal, link_section, LintPass, lint_reasons, @@ -531,6 +532,7 @@ symbols! { RangeInclusive, RangeTo, RangeToInclusive, + raw_dylib, raw_identifiers, Ready, reason, diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs b/src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs new file mode 100644 index 0000000000000..d6aac45d28ffb --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs @@ -0,0 +1,8 @@ +#[link(name="foo")] +extern { +#[link_ordinal(42)] +//~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature +fn foo(); +} + +fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr b/src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr new file mode 100644 index 0000000000000..e4ff390819b54 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr @@ -0,0 +1,12 @@ +error[E0658]: the `#[link_ordinal]` attribute is an experimental feature + --> $DIR/feature-gate-raw-dylib-2.rs:3:1 + | +LL | #[link_ordinal(42)] + | ^^^^^^^^^^^^^^^^^^^ + | + = note: for more information, see https://github.com/rust-lang/rust/issues/58713 + = help: add `#![feature(raw_dylib)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib.rs b/src/test/ui/feature-gates/feature-gate-raw-dylib.rs new file mode 100644 index 0000000000000..6977f53e56f4e --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-raw-dylib.rs @@ -0,0 +1,5 @@ +#[link(name="foo", kind="raw-dylib")] +//~^ ERROR: kind="raw-dylib" is feature gated +extern {} + +fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib.stderr b/src/test/ui/feature-gates/feature-gate-raw-dylib.stderr new file mode 100644 index 0000000000000..ad5cf4615ae09 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-raw-dylib.stderr @@ -0,0 +1,12 @@ +error[E0658]: kind="raw-dylib" is feature gated + --> $DIR/feature-gate-raw-dylib.rs:1:1 + | +LL | #[link(name="foo", kind="raw-dylib")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: for more information, see https://github.com/rust-lang/rust/issues/58713 + = help: add `#![feature(raw_dylib)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. From bf4bc7d172c47eca05b97520433fb260b2479ea7 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Thu, 29 Aug 2019 01:54:25 +0800 Subject: [PATCH 2/4] Add support for parsing #[link_ordinal] attribute. --- src/librustc/hir/mod.rs | 6 ++++++ src/librustc_typeck/collect.rs | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 2c8590aa4e3fa..66ecc52a92ea5 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -2626,6 +2626,11 @@ pub struct CodegenFnAttrs { /// probably isn't set when this is set, this is for foreign items while /// `#[export_name]` is for Rust-defined functions. pub link_name: Option, + /// The `#[link_ordinal = "..."]` attribute, indicating an ordinal an + /// imported function has in the dynamic library. Note that this must not + /// be set when `link_name` is set. This is for foreign items with the + /// "raw-dylib" kind. + pub link_ordinal: Option, /// The `#[target_feature(enable = "...")]` attribute and the enabled /// features (only enabled features are supported right now). pub target_features: Vec, @@ -2685,6 +2690,7 @@ impl CodegenFnAttrs { optimize: OptimizeAttr::None, export_name: None, link_name: None, + link_ordinal: None, target_features: vec![], linkage: None, link_section: None, diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index d2e9203779cc8..0cdf4dd1ae11b 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -2603,6 +2603,35 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { } } else if attr.check_name(sym::link_name) { codegen_fn_attrs.link_name = attr.value_str(); + } else if attr.check_name(sym::link_ordinal) { + use syntax::ast::{Lit, LitIntType, LitKind}; + let meta_item_list = attr.meta_item_list(); + let sole_meta_lit = if let Some(meta_item_list) = &meta_item_list { + if meta_item_list.len() == 1 { + meta_item_list.get(0).and_then(|item| item.literal()) + } else { + None + } + } else { + None + }; + if let Some(Lit { node: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = + sole_meta_lit + { + if *ordinal <= std::usize::MAX as u128 { + codegen_fn_attrs.link_ordinal = Some(*ordinal as usize); + } else { + let msg = format!( + "too large ordinal value in link_ordinal \ + value: `{}`", + &ordinal + ); + tcx.sess.span_err(attr.span, &msg); + } + } else { + let msg = "illegal ordinal format in link_ordinal"; + tcx.sess.span_err(attr.span, &msg); + } } } @@ -2704,6 +2733,15 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { codegen_fn_attrs.export_name = Some(name); codegen_fn_attrs.link_name = Some(name); } + if codegen_fn_attrs.link_name.is_some() && codegen_fn_attrs.link_ordinal.is_some() { + if let Some(span) = inline_span { + tcx.sess.span_err( + span, + "cannot use `#[link_name]` with \ + `#[link_ordinal]`", + ); + } + } // Internal symbols to the standard library all have no_mangle semantics in // that they have defined symbol names present in the function name. This From 6e9c0b0e65c233321f3eee0923c73f82739b8dd4 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Wed, 18 Sep 2019 22:40:08 +0800 Subject: [PATCH 3/4] Address review comments. --- src/librustc/middle/cstore.rs | 2 +- src/librustc_metadata/native_libs.rs | 2 +- src/librustc_typeck/collect.rs | 81 ++++++++++--------- src/libsyntax/feature_gate/active.rs | 1 + src/libsyntax/feature_gate/builtin_attrs.rs | 6 +- .../feature-gates/feature-gate-raw-dylib-2.rs | 8 -- .../feature-gate-raw-dylib-2.rs | 8 ++ .../feature-gate-raw-dylib-2.stderr | 6 +- .../feature-gate-raw-dylib.rs | 2 +- .../feature-gate-raw-dylib.stderr | 2 +- 10 files changed, 63 insertions(+), 55 deletions(-) delete mode 100644 src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs create mode 100644 src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib-2.rs rename src/test/ui/{feature-gates => rfc-2627-raw-dylib}/feature-gate-raw-dylib-2.stderr (77%) rename src/test/ui/{feature-gates => rfc-2627-raw-dylib}/feature-gate-raw-dylib.rs (57%) rename src/test/ui/{feature-gates => rfc-2627-raw-dylib}/feature-gate-raw-dylib.stderr (89%) diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index fcab28c929954..7c8ebad68097b 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -96,7 +96,7 @@ pub enum NativeLibraryKind { NativeStaticNobundle, /// macOS-specific NativeFramework, - /// windows dynamic library without import library + /// Windows dynamic library without import library. NativeRawDylib, /// default way to specify a dynamic library NativeUnknown, diff --git a/src/librustc_metadata/native_libs.rs b/src/librustc_metadata/native_libs.rs index 78a954befff7f..4476e107475d1 100644 --- a/src/librustc_metadata/native_libs.rs +++ b/src/librustc_metadata/native_libs.rs @@ -176,7 +176,7 @@ impl Collector<'tcx> { sym::raw_dylib, span.unwrap_or_else(|| syntax_pos::DUMMY_SP), GateIssue::Language, - "kind=\"raw-dylib\" is feature gated"); + "kind=\"raw-dylib\" is unstable"); } self.libs.push(lib); } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 0cdf4dd1ae11b..0e0b4adcb11a5 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -2604,33 +2604,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { } else if attr.check_name(sym::link_name) { codegen_fn_attrs.link_name = attr.value_str(); } else if attr.check_name(sym::link_ordinal) { - use syntax::ast::{Lit, LitIntType, LitKind}; - let meta_item_list = attr.meta_item_list(); - let sole_meta_lit = if let Some(meta_item_list) = &meta_item_list { - if meta_item_list.len() == 1 { - meta_item_list.get(0).and_then(|item| item.literal()) - } else { - None - } - } else { - None - }; - if let Some(Lit { node: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = - sole_meta_lit - { - if *ordinal <= std::usize::MAX as u128 { - codegen_fn_attrs.link_ordinal = Some(*ordinal as usize); - } else { - let msg = format!( - "too large ordinal value in link_ordinal \ - value: `{}`", - &ordinal - ); - tcx.sess.span_err(attr.span, &msg); - } - } else { - let msg = "illegal ordinal format in link_ordinal"; - tcx.sess.span_err(attr.span, &msg); + if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) { + codegen_fn_attrs.link_ordinal = ordinal; } } } @@ -2709,6 +2684,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { // purpose functions as they wouldn't have the right target features // enabled. For that reason we also forbid #[inline(always)] as it can't be // respected. + if codegen_fn_attrs.target_features.len() > 0 { if codegen_fn_attrs.inline == InlineAttr::Always { if let Some(span) = inline_span { @@ -2733,15 +2709,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { codegen_fn_attrs.export_name = Some(name); codegen_fn_attrs.link_name = Some(name); } - if codegen_fn_attrs.link_name.is_some() && codegen_fn_attrs.link_ordinal.is_some() { - if let Some(span) = inline_span { - tcx.sess.span_err( - span, - "cannot use `#[link_name]` with \ - `#[link_ordinal]`", - ); - } - } + check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, inline_span); // Internal symbols to the standard library all have no_mangle semantics in // that they have defined symbol names present in the function name. This @@ -2752,3 +2720,44 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { codegen_fn_attrs } + +fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option { + use syntax::ast::{Lit, LitIntType, LitKind}; + let meta_item_list = attr.meta_item_list(); + let meta_item_list: Option<&[ast::NestedMetaItem]> = meta_item_list.as_ref().map(Vec::as_ref); + let sole_meta_list = match meta_item_list { + Some([item]) => item.literal(), + _ => None, + }; + if let Some(Lit { node: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = sole_meta_list { + if *ordinal <= std::usize::MAX as u128 { + Some(*ordinal as usize) + } else { + let msg = format!( + "too large ordinal value in link_ordinal value: `{}`", + &ordinal + ); + tcx.sess.span_err(attr.span, &msg); + None + } + } else { + tcx.sess.span_err(attr.span, "illegal ordinal format in link_ordinal"); + None + } +} + +fn check_link_name_xor_ordinal( + tcx: TyCtxt<'_>, + codegen_fn_attrs: &CodegenFnAttrs, + inline_span: Option, +) { + if codegen_fn_attrs.link_name.is_none() || codegen_fn_attrs.link_ordinal.is_none() { + return; + } + let msg = "cannot use `#[link_name]` with `#[link_ordinal]`"; + if let Some(span) = inline_span { + tcx.sess.span_err(span, msg); + } else { + tcx.sess.err(msg); + } +} diff --git a/src/libsyntax/feature_gate/active.rs b/src/libsyntax/feature_gate/active.rs index 9526e6e271fab..87e43a463d030 100644 --- a/src/libsyntax/feature_gate/active.rs +++ b/src/libsyntax/feature_gate/active.rs @@ -542,4 +542,5 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[ sym::const_generics, sym::or_patterns, sym::let_chains, + sym::raw_dylib, ]; diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index 67c61d417b227..80c04ea223723 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -277,12 +277,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ it is recommended to use `#[link(name = \"foo\")] instead", ), gated!( - link_ordinal, - Whitelisted, - template!(List: "ordinal"), - raw_dylib, + link_ordinal, Whitelisted, template!(List: "ordinal"), raw_dylib, experimental!(link_ordinal) ), + // Plugins: ungated!(plugin_registrar, Normal, template!(Word)), gated!( diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs b/src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs deleted file mode 100644 index d6aac45d28ffb..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs +++ /dev/null @@ -1,8 +0,0 @@ -#[link(name="foo")] -extern { -#[link_ordinal(42)] -//~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature -fn foo(); -} - -fn main() {} diff --git a/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib-2.rs b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib-2.rs new file mode 100644 index 0000000000000..14345bad6e5b9 --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib-2.rs @@ -0,0 +1,8 @@ +#[link(name="foo")] +extern { + #[link_ordinal(42)] + //~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature + fn foo(); +} + +fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib-2.stderr similarity index 77% rename from src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr rename to src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib-2.stderr index e4ff390819b54..0869d7ad48a8c 100644 --- a/src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr +++ b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib-2.stderr @@ -1,8 +1,8 @@ error[E0658]: the `#[link_ordinal]` attribute is an experimental feature - --> $DIR/feature-gate-raw-dylib-2.rs:3:1 + --> $DIR/feature-gate-raw-dylib-2.rs:3:5 | -LL | #[link_ordinal(42)] - | ^^^^^^^^^^^^^^^^^^^ +LL | #[link_ordinal(42)] + | ^^^^^^^^^^^^^^^^^^^ | = note: for more information, see https://github.com/rust-lang/rust/issues/58713 = help: add `#![feature(raw_dylib)]` to the crate attributes to enable diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib.rs b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib.rs similarity index 57% rename from src/test/ui/feature-gates/feature-gate-raw-dylib.rs rename to src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib.rs index 6977f53e56f4e..f0f83e0426dd6 100644 --- a/src/test/ui/feature-gates/feature-gate-raw-dylib.rs +++ b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib.rs @@ -1,5 +1,5 @@ #[link(name="foo", kind="raw-dylib")] -//~^ ERROR: kind="raw-dylib" is feature gated +//~^ ERROR: kind="raw-dylib" is unstable extern {} fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-raw-dylib.stderr b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib.stderr similarity index 89% rename from src/test/ui/feature-gates/feature-gate-raw-dylib.stderr rename to src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib.stderr index ad5cf4615ae09..0ca9de28be1ac 100644 --- a/src/test/ui/feature-gates/feature-gate-raw-dylib.stderr +++ b/src/test/ui/rfc-2627-raw-dylib/feature-gate-raw-dylib.stderr @@ -1,4 +1,4 @@ -error[E0658]: kind="raw-dylib" is feature gated +error[E0658]: kind="raw-dylib" is unstable --> $DIR/feature-gate-raw-dylib.rs:1:1 | LL | #[link(name="foo", kind="raw-dylib")] From 9846af8f4836b3874030f8e406f1259c5714c83b Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Fri, 20 Sep 2019 01:48:30 +0800 Subject: [PATCH 4/4] Add more tests, fix span issue, improve diagnostics. --- src/librustc_typeck/collect.rs | 14 ++++++++++---- .../link-ordinal-and-name.rs | 12 ++++++++++++ .../link-ordinal-and-name.stderr | 16 ++++++++++++++++ .../link-ordinal-invalid-format.rs | 11 +++++++++++ .../link-ordinal-invalid-format.stderr | 18 ++++++++++++++++++ .../link-ordinal-too-large.rs | 11 +++++++++++ .../link-ordinal-too-large.stderr | 18 ++++++++++++++++++ 7 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.rs create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.stderr create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.rs create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.stderr diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 0e0b4adcb11a5..2fcfd83f54c95 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -2522,6 +2522,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { let whitelist = tcx.target_features_whitelist(LOCAL_CRATE); let mut inline_span = None; + let mut link_ordinal_span = None; for attr in attrs.iter() { if attr.check_name(sym::cold) { codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD; @@ -2604,6 +2605,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { } else if attr.check_name(sym::link_name) { codegen_fn_attrs.link_name = attr.value_str(); } else if attr.check_name(sym::link_ordinal) { + link_ordinal_span = Some(attr.span); if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) { codegen_fn_attrs.link_ordinal = ordinal; } @@ -2709,7 +2711,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { codegen_fn_attrs.export_name = Some(name); codegen_fn_attrs.link_name = Some(name); } - check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, inline_span); + check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span); // Internal symbols to the standard library all have no_mangle semantics in // that they have defined symbol names present in the function name. This @@ -2734,14 +2736,18 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option { Some(*ordinal as usize) } else { let msg = format!( - "too large ordinal value in link_ordinal value: `{}`", + "ordinal value in `link_ordinal` is too large: `{}`", &ordinal ); - tcx.sess.span_err(attr.span, &msg); + tcx.sess.struct_span_err(attr.span, &msg) + .note("the value may not exceed `std::usize::MAX`") + .emit(); None } } else { - tcx.sess.span_err(attr.span, "illegal ordinal format in link_ordinal"); + tcx.sess.struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`") + .note("an unsuffixed integer value, e.g., `1`, is expected") + .emit(); None } } diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.rs b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.rs new file mode 100644 index 0000000000000..5769366fb45a4 --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.rs @@ -0,0 +1,12 @@ +#![feature(raw_dylib)] +//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash + +#[link(name="foo")] +extern { + #[link_name="foo"] + #[link_ordinal(42)] + //~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]` + fn foo(); +} + +fn main() {} diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.stderr b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.stderr new file mode 100644 index 0000000000000..303a1c02eb85f --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.stderr @@ -0,0 +1,16 @@ +warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash + --> $DIR/link-ordinal-and-name.rs:1:12 + | +LL | #![feature(raw_dylib)] + | ^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + +error: cannot use `#[link_name]` with `#[link_ordinal]` + --> $DIR/link-ordinal-and-name.rs:7:5 + | +LL | #[link_ordinal(42)] + | ^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs new file mode 100644 index 0000000000000..82fb1151c23df --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs @@ -0,0 +1,11 @@ +#![feature(raw_dylib)] +//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash + +#[link(name="foo")] +extern { + #[link_ordinal("JustMonika")] + //~^ ERROR illegal ordinal format in `link_ordinal` + fn foo(); +} + +fn main() {} diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr new file mode 100644 index 0000000000000..14556a7262b1c --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr @@ -0,0 +1,18 @@ +warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash + --> $DIR/link-ordinal-invalid-format.rs:1:12 + | +LL | #![feature(raw_dylib)] + | ^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + +error: illegal ordinal format in `link_ordinal` + --> $DIR/link-ordinal-invalid-format.rs:6:5 + | +LL | #[link_ordinal("JustMonika")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: an unsuffixed integer value, e.g., `1`, is expected + +error: aborting due to previous error + diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.rs b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.rs new file mode 100644 index 0000000000000..69596ad04fff3 --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.rs @@ -0,0 +1,11 @@ +#![feature(raw_dylib)] +//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash + +#[link(name="foo")] +extern { + #[link_ordinal(18446744073709551616)] + //~^ ERROR ordinal value in `link_ordinal` is too large: `18446744073709551616` + fn foo(); +} + +fn main() {} diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.stderr b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.stderr new file mode 100644 index 0000000000000..b3b22f9776df7 --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.stderr @@ -0,0 +1,18 @@ +warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash + --> $DIR/link-ordinal-too-large.rs:1:12 + | +LL | #![feature(raw_dylib)] + | ^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + +error: ordinal value in `link_ordinal` is too large: `18446744073709551616` + --> $DIR/link-ordinal-too-large.rs:6:5 + | +LL | #[link_ordinal(18446744073709551616)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: the value may not exceed `std::usize::MAX` + +error: aborting due to previous error +