Skip to content

Commit

Permalink
Auto merge of #100073 - dpaoliello:externvar, r=michaelwoerister
Browse files Browse the repository at this point in the history
Add test for raw-dylib with an external variable

All existing tests of link kind `raw-dylib` only validate the ability to link against functions, but it is also possible to link against variables.

This adds tests for linking against a variable using `raw-dylib` both by-name and by-ordinal.
  • Loading branch information
bors committed Aug 5, 2022
2 parents 9bbbf60 + 0a754b3 commit d77da9d
Show file tree
Hide file tree
Showing 22 changed files with 133 additions and 9 deletions.
9 changes: 8 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,14 @@ impl CheckAttrVisitor<'_> {
}

// FIXME(@lcnr): this doesn't belong here.
if matches!(target, Target::Closure | Target::Fn | Target::Method(_) | Target::ForeignFn) {
if matches!(
target,
Target::Closure
| Target::Fn
| Target::Method(_)
| Target::ForeignFn
| Target::ForeignStatic
) {
self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/run-make/raw-dylib-c/extern_1.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdio.h>

__declspec(dllexport) int extern_variable = 0;

__declspec(dllexport) void extern_fn_1() {
printf("extern_fn_1\n");
fflush(stdout);
Expand All @@ -10,6 +12,11 @@ __declspec(dllexport) void extern_fn_2() {
fflush(stdout);
}

__declspec(dllexport) void print_extern_variable() {
printf("extern_variable value: %d\n", extern_variable);
fflush(stdout);
}

__declspec(dllexport) void extern_fn_with_long_name() {
printf("extern_fn_with_long_name; got the rename\n");
fflush(stdout);
Expand Down
10 changes: 9 additions & 1 deletion src/test/run-make/raw-dylib-c/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ extern {

pub fn library_function() {
#[link(name = "extern_1", kind = "raw-dylib")]
extern { fn extern_fn_2(); }
extern {
fn extern_fn_2();
fn print_extern_variable();
static mut extern_variable: i32;
}

unsafe {
extern_fn_1();
extern_fn_2();
extern_fn_3();
extern_variable = 42;
print_extern_variable();
extern_variable = -42;
print_extern_variable();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/run-make/raw-dylib-c/output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern_fn_1
extern_fn_2; didn't get the rename
extern_fn_3
extern_variable value: 42
extern_variable value: -42
7 changes: 7 additions & 0 deletions src/test/run-make/raw-dylib-link-ordinal/exporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
void exported_function() {
printf("exported_function\n");
}

int exported_variable = 0;

void print_exported_variable() {
printf("exported_variable value: %d\n", exported_variable);
fflush(stdout);
}
2 changes: 2 additions & 0 deletions src/test/run-make/raw-dylib-link-ordinal/exporter.def
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
LIBRARY exporter
EXPORTS
exported_function @13 NONAME
exported_variable @5 NONAME
print_exported_variable @9 NONAME
8 changes: 8 additions & 0 deletions src/test/run-make/raw-dylib-link-ordinal/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
extern {
#[link_ordinal(13)]
fn imported_function();
#[link_ordinal(5)]
static mut imported_variable: i32;
#[link_ordinal(9)]
fn print_imported_variable();
}

pub fn library_function() {
unsafe {
imported_function();
imported_variable = 42;
print_imported_variable();
imported_variable = -42;
print_imported_variable();
}
}
2 changes: 2 additions & 0 deletions src/test/run-make/raw-dylib-link-ordinal/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exported_function
exported_variable value: 42
exported_variable value: -42
3 changes: 3 additions & 0 deletions src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ extern "C" {
#[link_ordinal(42)]
//~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature
fn foo();
#[link_ordinal(5)]
//~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature
static mut imported_variable: i32;
}

fn main() {}
11 changes: 10 additions & 1 deletion src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ LL | #[link_ordinal(42)]
= note: see issue #58713 <https://github.com/rust-lang/rust/issues/58713> for more information
= help: add `#![feature(raw_dylib)]` to the crate attributes to enable

error: aborting due to previous error
error[E0658]: the `#[link_ordinal]` attribute is an experimental feature
--> $DIR/feature-gate-raw-dylib-2.rs:6:5
|
LL | #[link_ordinal(5)]
| ^^^^^^^^^^^^^^^^^^
|
= note: see issue #58713 <https://github.com/rust-lang/rust/issues/58713> for more information
= help: add `#![feature(raw_dylib)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
4 changes: 4 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ extern "C" {
#[link_ordinal(42)]
//~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]`
fn foo();
#[link_name="foo"]
#[link_ordinal(5)]
//~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]`
static mut imported_variable: i32;
}

fn main() {}
8 changes: 7 additions & 1 deletion src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ error: cannot use `#[link_name]` with `#[link_ordinal]`
LL | #[link_ordinal(42)]
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error; 1 warning emitted
error: cannot use `#[link_name]` with `#[link_ordinal]`
--> $DIR/link-ordinal-and-name.rs:11:5
|
LL | #[link_ordinal(5)]
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors; 1 warning emitted

3 changes: 3 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extern "C" {
#[link_ordinal("JustMonika")]
//~^ ERROR illegal ordinal format in `link_ordinal`
fn foo();
#[link_ordinal("JustMonika")]
//~^ ERROR illegal ordinal format in `link_ordinal`
static mut imported_variable: i32;
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ LL | #[link_ordinal("JustMonika")]
|
= note: an unsuffixed integer value, e.g., `1`, is expected

error: aborting due to previous error; 1 warning emitted
error: illegal ordinal format in `link_ordinal`
--> $DIR/link-ordinal-invalid-format.rs:9:5
|
LL | #[link_ordinal("JustMonika")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: an unsuffixed integer value, e.g., `1`, is expected

error: aborting due to 2 previous errors; 1 warning emitted

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extern "C" {
#[link_ordinal()]
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
fn foo();
#[link_ordinal()]
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
static mut imported_variable: i32;
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ LL | #[link_ordinal()]
|
= note: the attribute requires exactly one argument

error: aborting due to previous error; 1 warning emitted
error: incorrect number of arguments to `#[link_ordinal]`
--> $DIR/link-ordinal-missing-argument.rs:9:5
|
LL | #[link_ordinal()]
| ^^^^^^^^^^^^^^^^^
|
= note: the attribute requires exactly one argument

error: aborting due to 2 previous errors; 1 warning emitted

3 changes: 3 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ extern "C" {
#[link_ordinal(1)] //~ ERROR multiple `link_ordinal` attributes
#[link_ordinal(2)]
fn foo();
#[link_ordinal(1)] //~ ERROR multiple `link_ordinal` attributes
#[link_ordinal(2)]
static mut imported_variable: i32;
}

fn main() {}
14 changes: 13 additions & 1 deletion src/test/ui/rfc-2627-raw-dylib/link-ordinal-multiple.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,17 @@ note: attribute also specified here
LL | #[link_ordinal(2)]
| ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error; 1 warning emitted
error: multiple `link_ordinal` attributes
--> $DIR/link-ordinal-multiple.rs:10:5
|
LL | #[link_ordinal(1)]
| ^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
note: attribute also specified here
--> $DIR/link-ordinal-multiple.rs:11:5
|
LL | #[link_ordinal(2)]
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors; 1 warning emitted

3 changes: 3 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extern "C" {
#[link_ordinal(72436)]
//~^ ERROR ordinal value in `link_ordinal` is too large: `72436`
fn foo();
#[link_ordinal(72436)]
//~^ ERROR ordinal value in `link_ordinal` is too large: `72436`
static mut imported_variable: i32;
}

fn main() {}
10 changes: 9 additions & 1 deletion src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ LL | #[link_ordinal(72436)]
|
= note: the value may not exceed `u16::MAX`

error: aborting due to previous error; 1 warning emitted
error: ordinal value in `link_ordinal` is too large: `72436`
--> $DIR/link-ordinal-too-large.rs:9:5
|
LL | #[link_ordinal(72436)]
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: the value may not exceed `u16::MAX`

error: aborting due to 2 previous errors; 1 warning emitted

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extern "C" {
#[link_ordinal(3, 4)]
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
fn foo();
#[link_ordinal(3, 4)]
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
static mut imported_variable: i32;
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ LL | #[link_ordinal(3, 4)]
|
= note: the attribute requires exactly one argument

error: aborting due to previous error; 1 warning emitted
error: incorrect number of arguments to `#[link_ordinal]`
--> $DIR/link-ordinal-too-many-arguments.rs:9:5
|
LL | #[link_ordinal(3, 4)]
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: the attribute requires exactly one argument

error: aborting due to 2 previous errors; 1 warning emitted

0 comments on commit d77da9d

Please sign in to comment.